ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

onpaste event

Open devgogo opened this issue 9 years ago • 12 comments

Does anyone know how I can attach an onpaste event in CKEditor5?

I basically want to grab CTRL + V data and add few text to it and then add it to the editor.

Thanks.

devgogo avatar Nov 18 '17 15:11 devgogo

Hey! Great question – pity that you haven't asked it on StackOverflow :)

Anyway, quick answer – see the https://github.com/ckeditor/ckeditor5-clipboard's code. This plugin (which is available by default in all builds) expose additional events using which you can processed the content which was pasted before that content is inserted into the model.

You can read about so-called "input/output pipelines" in https://docs.ckeditor.com/ckeditor5/latest/api/module_clipboard_clipboard-Clipboard.html.

Reinmar avatar Nov 19 '17 16:11 Reinmar

Thanks, @Reinmar

I want an afterPaste event, It looks like ckeditor5-clipboard can't be implemented.

devgogo avatar Nov 20 '17 00:11 devgogo

Why do you need the afterPaste event? Could you explain your use cases for it?

There's no afterPaste in CKEditor 5. CKEditor 5 has a bit different architecture so some things need to be achieved differently with it. However, in general, this architecture is far more powerful so it's just a matter of understanding what you want to achieve and I'm sure that we'll find a way to do that.

Reinmar avatar Nov 22 '17 13:11 Reinmar

@Reinmar I want to afterPaste event when to do something.

such as localize remote images, then change the img Dom node of editing area.

devgogo avatar Nov 23 '17 02:11 devgogo

So, generally you have two options:

  • Listen on the editor.data#insertContent event and process the content before it gets inserted into the editor. This event is fired when editor.data.insertContent() is used (and it's, of course, used by the clipboard plugin). Processing the content which gets through this event will ensure that you support also other features which may use it, not only pasting.
  • Listen on the editor.document#change event and make changes to the model after the content was inserted there. This event is fired with granular information about changes applied to the model (through operations). E.g. the image upload plugin uses this method to upload images which were inserted into the content.

change the img Dom node of editing are

Please note that you can't change nodes in the DOM directly. You need to change their properties in the model. Othwerise, the editor will revert your changes.

Reinmar avatar Nov 23 '17 10:11 Reinmar

@Reinmar in the1.0.0-beta.1 version, The above method doesn't working. Can you help me? Thank you.

devgogo avatar Apr 03 '18 14:04 devgogo

Do you mean insertContent()? It was moved to editor.model.insertContent().

Reinmar avatar Apr 03 '18 16:04 Reinmar

In the 1.0.0-alpha.2 version, My code is as follows:

ClassicEditor
    .create(document.querySelector('#article_body'), {
        // configure
    })
    .then(editor => {

        editor.document.on('change', (evt, type, data) => {
            console.log(evt, type, data)

            if (type === 'insert' || type === 'reinsert') {
                for (const value of data.range) {
                    if (value.type === 'elementStart' && value.item.name === 'image') {
                        const imageElement = value.item;
                        const uploadId = imageElement.getAttribute('uploadId');
                        const uploadStatus = imageElement.getAttribute('uploadStatus');

                        if (!uploadId && !uploadStatus) {
                            tryUploadRemoteImage(editor, imageElement); // Automatically upload paste’s images to the server.
                        }
                    }
                }
            }
        });
    })
    .catch(error => {
        console.log(error)
    });

But in the 1.0.0-beta.1 version,

the method editor.document.on('change', (evt, type, data) is not working.

Are there any other similar methods?

devgogo avatar Apr 04 '18 01:04 devgogo

editor.document is now available under editor.model.document.

However, in 1.0.0-beta.1 we have refactored the API a lot. Actually, this was the point and one of the main goals for that version (so the API is more-or-less stable after when we will approach final release). Just changing editor.document to editor.document.model won't work. For example, the change event changed (no pun intended) a lot. Now it is fired after all changes on the document are done (so it is equivalent to the old changesDone event, which got removed now). This means that the change event is not fired after every applied operation and does not contain data as it used to.

Please refer to API docs and guides for the up-to-date documentation. I encourage you to re-read API docs of every class you use to be sure what works how now.

scofalik avatar Apr 04 '18 07:04 scofalik

@scofalik Thanks. So, I want to achieve the above purpose(Automatically upload paste’s images to the server.), what other methods?

devgogo avatar Apr 04 '18 08:04 devgogo

Please check https://docs.ckeditor.com/ckeditor5/latest/features/image-upload.html.

PS. This discussion is beyond this ticket's original purpose so if you have more specific questions, please ask them in new tickets on StackOverflow (SO is preferred in case of questions).

Reinmar avatar Apr 04 '18 09:04 Reinmar

Resolved, reference: https://github.com/ckeditor/ckeditor5-image/blob/master/src/imageupload/imageuploadediting.js#L78

devgogo avatar Apr 13 '18 14:04 devgogo