How to listen to focus event in CKEditor 5

老子叫甜甜 提交于 2021-02-08 04:39:21

问题


I'd like to listen to the focus event in CKEditor 5.

I thought something like this would work but the callback is never called:

document.querySelector("#editable");
ClassicEditor.create(el).then(editor => {
    editor.on('focus', () => {
        console.log("Focused");
    });
});

The editor is successfully created but the callback is not called.

Any ideas?


回答1:


The editor comes with a FocusTracker (and the observable #isFocused property) for that purpose:

editor.ui.focusTracker.on( 'change:isFocused', ( evt, name, value ) => {
    console.log( 'isFocused = ', value );
} );

Note that editor.ui.focusTracker.isFocused is true as long as any UI has focus, which includes the editable but also the toolbar, floating panels, etc.

To determine the focus of the editable, i.e. when the caret is blinking and typing is possible, use this listener instead:

editor.editing.view.document.on( 'change:isFocused', ( evt, name, value ) => {
    console.log( 'editable isFocused =', value );
} );

Place one listener next to the other and play with the editor and the UI to see the difference.



来源:https://stackoverflow.com/questions/49663625/how-to-listen-to-focus-event-in-ckeditor-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!