ajaxorg / ace

Ace (Ajax.org Cloud9 Editor)
https://ace.c9.io
Other
26.61k stars 5.27k forks source link

Paste event for data other than text? #3109

Open sinedied opened 7 years ago

sinedied commented 7 years ago

From what I have read in the docs and tested, it is currently not possible to catch the "paste" event anything other than text, am I correct?

If so, how can I register event for catching for example pasted images on editor, to perform special processing on it? Is there a way or the onPaste function of the editor must be updated to support this?

Thanks for your help!

hengkx commented 6 years ago

I have the same problem, How did you solve it?

sinedied commented 6 years ago

I did not solve the issue unfortunately 😭

hengkx commented 6 years ago

I solved it this way

431824 commented 2 years ago

It is still open! I solved like this:


navigator.clipboard.read().then((data) => {
  for (let i = 0; i < data.length; i++) {
    if (data[i].types.includes("image/png")) {
      data[i].getType("image/png").then((blob) => {
        var img = document.createElement('img');
        img.src = URL.createObjectURL(blob);
        document.getElementById('body').appendChild(img);
      });
    }
  }
});```

the only problem i have ace_editor.on('paster' ... runs 2 times paste function
whazor commented 1 year ago

This indeed seems like a useful addition, where based on the paste event you could upload the image data, and for example return a markdown syntax.

I see that currently image paste events are ignored in the onPaste code. So the above workaround would not work. I do wonder whether changing the behavior might break people's implementation, especially if they are using a the 'text' value from paste but are receiving null.

Compared to other feature requests/bugs, I label this as a p2 request, as it would not be essential for a code editor and be more of a nice addition.

ruicaramalho commented 1 year ago

Why not allow a function to override. For example if function onPasteOverride exists run the function otherwise runs default code

the new onPaste function would be:

    var onPaste = function(e) {
        var data = handleClipboardData(e);
        if (clipboard.pasteCancelled())
            return;

        if (typeof onPasteOverride == "function") {
            onPasteOverride(data);
            return;
        }

        if (typeof data == "string") {
            if (data)
                host.onPaste(data, e);
            if (useragent.isIE)
                setTimeout(resetSelection);
            event.preventDefault(e);
        }
        else {
            text.value = "";
            pasted = true;
        }
    };

or maybe to have this as an option to set like:

editor.setOnPasteOverride('functionname');
dimovpetar commented 1 year ago

Hello @whazor, is there any progress on this topic?

github-actions[bot] commented 2 months ago

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

dimovpetar commented 2 months ago

Hello,

This issue is still relevant. Please reopen it.

Best regards, Petar

nightwing commented 2 months ago

Hi, what would be the advantage of using an such event dispatched from editor, instead of directly using

editor.container.addEventListener("paste", function(e) {

})
dimovpetar commented 1 month ago

@nightwing adding the event listener to the container seems sufficient for now. I always find it hard to find out which is the correct element to attach listeners to. You can close this issue then.