onpaste event
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.
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.
Thanks, @Reinmar
I want an afterPaste event, It looks like ckeditor5-clipboard can't be implemented.
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
I want to afterPaste event when to do something.
such as localize remote images, then change the img Dom node of editing area.
So, generally you have two options:
- Listen on the
editor.data#insertContentevent and process the content before it gets inserted into the editor. This event is fired wheneditor.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#changeevent 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 in the1.0.0-beta.1 version, The above method doesn't working. Can you help me? Thank you.
Do you mean insertContent()? It was moved to editor.model.insertContent().
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?
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 Thanks. So, I want to achieve the above purpose(Automatically upload paste’s images to the server.), what other methods?
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).
Resolved, reference: https://github.com/ckeditor/ckeditor5-image/blob/master/src/imageupload/imageuploadediting.js#L78