CKEditor5,我该如何在JavaScript函数中编写Blur事件处理程序

问题描述

我以前使用过CKEditor4并设法为其编写了模糊事件处理程序。但是我不明白如何为ckeditor5做它

这是我为ckeditor5编写的用于捕获模糊的代码,但我没有收到任何警告消息。

HTML

<div id='editor'></textarea>

javascript代码-

ClassicEditor
    .create( document.querySelector( '#editor' ),{} )
    .then( editor => {
        editor.on("blur",function(){alert("hello world");});
    } )
    .catch( err => {
        console.error( err.stack );
    } );

在使用CKEditor4之前,我的代码如下,并且在CKEditor4上正常工作

HTML

<textarea id="editor"></textarea>

javascript

CKEDITOR.replace('editor',{});
CKEDITOR.instances.editor.on('blur',function () {alert("Hello World");});

在上述每种情况下,我的CKEditor都可以在我的页面上正常加载。

我阅读了他们的文档,但不知道如何实现。

解决方法

我在链接下面找到了关于stackover流的解决方案-

Detect when text has changed AND editor has lost focus in CKEditor 5

这正是我想要做的

这里是解决方案

editor.ui.focusTracker.on( 'change:isFocused',( evt,name,isFocused ) => {
    if ( !isFocused ) {
        // Do whatever you want with current editor data:
        console.log( editor.getData() );
    }
} );