删除元素时的 CKEditor 事件?

发布于 2024-12-05 00:09:54 字数 114 浏览 2 评论 0原文

JavaScript(或 CKEditor)中有没有办法检测图像何时从 CKEditor 中删除。 我需要它作为与图像一起插入的标题元素,但是一旦删除图像,标题也应该被删除。

任何帮助将不胜感激。

Is there a way in JavaScript (or CKEditor) to detect when an image is removed from CKEditor.
I need it for a caption element which is inserted together with the image, but once I delete the image, the caption should be deleted aswell.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦忆晨望 2024-12-12 00:09:54

没有像 onDeleteonImageRemovedFromContent 这样的特殊事件。但很少有活动可以帮助您。

editor.on('afterUndoImage', function( e ){ ... } )

但是 afterUndoImage 仅在 Undo 命令上触发,在手动删除元素时不会触发。

editor.on('afterCommandExec', function( e ){ ... } )

CKEditor 使用 execCommand (大多数情况下)更改内容,这样会触发许多内容的更改,因此您可以使用正则表达式检查差异。

您还可以使用插件 onchange 来检测内容,它结合了onUndoonRedoafterCommandExec等。

There are no special event like a onDelete or onImageRemovedFromContent. But there are few events that can help you.

editor.on('afterUndoImage', function( e ){ ... } )

But afterUndoImage fires only on Undo command, does not fires on manual deleting of elements.

editor.on('afterCommandExec', function( e ){ ... } )

CKEditor changes content with execCommand (mostly), so that fires on many content's change, so you can check the diff with regex for example.

Also you can use plugin onchange to detect the changes of contents, it combines onUndo, onRedo, afterCommandExec, etc.

莳間冲淡了誓言ζ 2024-12-12 00:09:54

使用插件--> https://github.com/shibbirweb/ckeditor5-image-remove- event-callback-plugin

该插件使您能够传递自己的回调函数,当您从编辑器中删除图像时将调用该函数。我在 VueJs 中实现了这个,花了几个小时后,我意识到这很容易做到。如果你问我的话,它是一个很酷的插件。下面的代码片段演示了实现此目的的非 Vuejs 方式。如果您需要了解 VueJs 实现,可以给我回信。

谢谢

​:此代码片段来自包文档,这就是其中的所有信息。

import ImageRemoveEventCallbackPlugin from 'ckeditor5-image-remove-event-callback-plugin'; // Add this
// ...

ClassicEditor.builtinPlugins = [
    // ...
    ImageRemoveEventCallbackPlugin
    // ...

]
ClassicEditor.create(document.querySelector('#editor'), {
    //... 
    imageRemoveEvent: {
        callback: (imagesSrc, nodeObjects) => {
            // note: imagesSrc is array of src & nodeObjects is array of nodeObject
            // node object api: https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_model_node-Node.html

            console.log('callback called', imagesSrc, nodeObjects)
        }
    },
    // ...
});

Use the plugin--> https://github.com/shibbirweb/ckeditor5-image-remove-event-callback-plugin

This plugin enables you to pass your own callback function which will be called when you will remove the image from your editor. I implemented this in VueJs and after having spent a couple of hours, I realized it was quite easy to do so. Its a cool plugin if you ask me. The below code snippet demonstrate the non-Vuejs way to implement this. You can write back to me if you need to know the VueJs implementation.

Thanks

P.S. : This code snippet is from the package documentation and that's all the info there is.

import ImageRemoveEventCallbackPlugin from 'ckeditor5-image-remove-event-callback-plugin'; // Add this
// ...

ClassicEditor.builtinPlugins = [
    // ...
    ImageRemoveEventCallbackPlugin
    // ...

]
ClassicEditor.create(document.querySelector('#editor'), {
    //... 
    imageRemoveEvent: {
        callback: (imagesSrc, nodeObjects) => {
            // note: imagesSrc is array of src & nodeObjects is array of nodeObject
            // node object api: https://ckeditor.com/docs/ckeditor5/latest/api/module_engine_model_node-Node.html

            console.log('callback called', imagesSrc, nodeObjects)
        }
    },
    // ...
});
硪扪都還晓 2024-12-12 00:09:54

我知道这已经相当老了,但我在寻找解决方案时最终来到这里,所以我认为值得发布另一种方法。

我不想监视所有可能的更改,因为我在小部件中预见的大多数活动都是正常编辑或从外部源创建小部件,所以我最终只是监视会导致删除的事件:

    editor.widgets.on( 'instanceCreated', function(evt) {
        let widget = evt.data ;
        if (widget.name === 'mywidget') {
            widget.on('select', function() {
                widget.on('key', function(evt) {
                    if ( evt.data.keyCode == 46                       // Delete
                         || evt.data.keyCode == 8                     // Backspace
                         || evt.data.keyCode == CKEDITOR.CTRL + 88    // Ctrl-X
                    )
                        jQuery(widget.element.$).myCallback() ;    // callback defined as a jQuery function for the sake of simplicity
                }) ;
            }) ;
            widget.on('deselect', function() {
                widget.on('key', function(evt) {
                }) ;
            }) ;
        }
    }) ;

当然回调有假设调用小部件尚未被删除,但这是一个优点,因为人们通常需要其数据。

I know this is rather old, but I ended up here while searching a solution, so I think it's worth to post another approach.

I didn't want to monitor all possible changes, since most of the activity I forsee in my widget is normal editing or widget creation from external sources, so I ended up simply monitoring the events that would cause the deletion:

    editor.widgets.on( 'instanceCreated', function(evt) {
        let widget = evt.data ;
        if (widget.name === 'mywidget') {
            widget.on('select', function() {
                widget.on('key', function(evt) {
                    if ( evt.data.keyCode == 46                       // Delete
                         || evt.data.keyCode == 8                     // Backspace
                         || evt.data.keyCode == CKEDITOR.CTRL + 88    // Ctrl-X
                    )
                        jQuery(widget.element.$).myCallback() ;    // callback defined as a jQuery function for the sake of simplicity
                }) ;
            }) ;
            widget.on('deselect', function() {
                widget.on('key', function(evt) {
                }) ;
            }) ;
        }
    }) ;

Of course the callback has to assume that the calling widget has not been deleted yet, but that's an advantage, since one typically needs its data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文