5

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.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
Apeiron
  • 1,881
  • 2
  • 13
  • 21

1 Answers1

1

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)

Saddam
  • 1,174
  • 7
  • 14
  • I appreciate your answer, however I would like to find a way to do it with the original slate copied objects (not just the text). In addittion InnertHTML and outerHTML contain the whole element, even copying a piece of the element – Apeiron Sep 24 '20 at 16:57
  • What I need is to get fragment object to inyect it new data, and then get that data in onPaste hook... :/ – Apeiron Sep 24 '20 at 17:26
  • I haven't gotten the copied fragment yet... You have answered me to get the text, but it is not exactly what I asked – Apeiron Sep 25 '20 at 09:10