suneditor icon indicating copy to clipboard operation
suneditor copied to clipboard

How to stop user from pasting image directly inside editor?

Open roker15 opened this issue 2 years ago • 5 comments

I want user to provide image only through link. So i want to prevent from direct copy and pasting image inside editor. How to do this. Otherwise user may mistakenly paste a lots of image directly , which will slow down netowrk query or sometimes it may stop network query.

roker15 avatar May 27 '22 10:05 roker15

There are no related options. You can prevent it in the following way:

editorInstance.core._setClipboardComponent = function (e) {
  e.preventDefault();
  e.stopPropagation();
  return;
}

JiHong88 avatar May 28 '22 13:05 JiHong88

But this doesn't make much sense because you can copy with range selection.

JiHong88 avatar May 28 '22 14:05 JiHong88

There are no related options. You can prevent it in the following way:

editorInstance.core._setClipboardComponent = function (e) {
  e.preventDefault();
  e.stopPropagation();
  return;
}

How to do this in react with typescript. This is important thing because user could mess up by pasting lots of images in a document directly herby increasing document size.

roker15 avatar May 30 '22 05:05 roker15

editorInstance.onPaste =  (e, cleanData, maxCharCount, core) => {
    const dom = core._d.createRange().createContextualFragment(cleanData);
    const chilren = dom.childNodes;
    let html = '';
    chilren.forEach(v=> {
        html += core.util.isComponent(v) ? '' : (v.outerHTML || v.textContent);
    })
    return html;
}

JiHong88 avatar May 30 '22 09:05 JiHong88

editorInstance.onPaste =  (e, cleanData, maxCharCount, core) => {
    const dom = core._d.createRange().createContextualFragment(cleanData);
    const chilren = dom.childNodes;
    let html = '';
    chilren.forEach(v=> {
        html += core.util.isComponent(v) ? '' : (v.outerHTML || v.textContent);
    })
    return html;
}

Does this stops pasting everything or only image?

roker15 avatar Jun 03 '22 19:06 roker15