简体   繁体   English

检测文本何时更改并且编辑器在 CKEditor 5 中失去焦点

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

I'm trying to implement an autosave feature in CKEditor 5, where a save only occurs if changes were made and after the editor has lost focus.我正在尝试在 CKEditor 5 中实现自动保存功能,只有在进行了更改并且编辑器失去焦点后才会进行保存。

How could I do this?我怎么能这样做? The documentation has been very confusing to me.文档对我来说非常混乱。

This is the closest I've gotten:这是我得到的最接近的:

function onChange(el,editor) {
    editor.document.once('change',function(evt,data) {
        $(el).one('blur',function() {
            saveFunction();
            onChange(el,editor);
        });
    });
}

onChange(el,editor);

The problem with my solution is the blur event fires whenever CKEditor brings up a modal.我的解决方案的问题是每当 CKEditor 调出模态时都会触发模糊事件。

To track the focused element of the editor ui you can use the focusTracker (available on editor.ui.focusTracker ).要跟踪编辑器 ui的焦点元素,您可以使用focusTracker (在editor.ui.focusTrackereditor.ui.focusTracker )。 It tracks various parts of the editor that are currently focused.它跟踪当前关注的编辑器的各个部分。

The focusTracker.isFocused is true whenever any of the editor's registered components are focused.每当编辑器的任何注册组件获得焦点时, focusTracker.isFocusedtrue For the classic editor build the focused elements might be one of:对于经典编辑器构建,焦点元素可能是以下元素之一:

  • editing area,编辑区,
  • toolbar,工具栏,
  • contextual toolbar.上下文工具栏。
editor.ui.focusTracker.on( 'change:isFocused', ( evt, name, isFocused ) => {
    if ( !isFocused ) {
        // Do whatever you want with current editor data:
        console.log( editor.getData() );
    }
} );

Based on jodators answer fullfilling Adams 2nd critieria (data has changed):基于 jodators 回答满足 Adams 2nd 标准(数据已更改):

var editor_changed = false;

editor.model.document.on('change:data', () => { editor_changed = true; });

editor.ui.focusTracker.on('change:isFocused', (evt, name, isFocused) => {
  if(!isFocused && editor_changed) {
    editor_changed = false;
    console.log(editor.getData());
  }
} );

Here a complete example based on both the answers of @Peter and @jodator.这是一个基于@Peter 和@jodator 的答案的完整示例。 It uses the CKEditor 5 Inline Editor.它使用 CKEditor 5 内联编辑器。 As Adam Cook mentioned in his question, the documentation is not very clear.正如亚当库克在他的问题中提到的,文档不是很清楚。

<div id="textblock1" class="editor">
    <p>
        Lorem ipsum dolor sit amet.
    </p>
</div>

<div id="textblock2" class="editor">
    <p>
        Sed ut perspiciatis unde omnis iste natus.
    </p>
</div>

<script src="https://cdn.ckeditor.com/ckeditor5/17.0.0/inline/ckeditor.js"></script>

<script>
    var text_changed = false;

    InlineEditor
        .create(document.querySelector('.editor'), {
        })
        .then(editor => {
            window.editor = editor;

            detectTextChanges(editor);
            detectFocusOut(editor);
        })
        .catch(error => {
            console.error(error);
        });

    function detectFocusOut(editor) {
        editor.ui.focusTracker.on('change:isFocused', (evt, name, isFocused) => {
            if (!isFocused && text_changed) {
                text_changed = false;
                console.log(editor.getData());
            }
        });
    }

    function detectTextChanges(editor) {
        editor.model.document.on('change:data', () => {
            text_changed = true;
        });
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM