Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get copied content inside onCopy hook (plugin)

I am trying to get copied fragment into onCopy hook vainly.

I've been trying with the event.clipboardData...

const { clipboardData } = event;
const encoded = clipboardData.getData("application/x-slate-fragment");

But it seems as empty. I have also tried with the getEventTransfer utils. But it returns {type: 'unknow'}

Here is the CodeSandBox that I have been testing.

like image 285
Apeiron Avatar asked Nov 06 '22 05:11

Apeiron


1 Answers

i think its not possible with the saltejs to get the fragment, if your will see the doucmentation as well https://docs.slatejs.org/v/v0.47/slate-react/utils#functions here as well they are asking to make the fragment first and then copy the data to it. So i think you could use JavaScript for it, if it helps, just a suggestion.

if you just want to get the copied text then try plain javascript inside onCopy function

const copied_text = window.getSelection().toString();

if your want to get the fragment of copied text use

event.target.outerHTML or event.target.innerHTML in onCopy function

if you want to create another copied fragment you can do

document.createRange().createContextualFragment(event.target.outerHTML)

like image 74
Saddam Avatar answered Nov 15 '22 11:11

Saddam