javascript - 替换ckeditor内容并添加undo history entry

标签 javascript ckeditor ckeditor5

我有一个我需要实现的基本功能,但找不到有关如何实现的信息:我只是想用自定义标记替换 CKEditor 的所有内容并添加一个撤消历史记录条目,以便您可以返回到替换前的版本。

  1. 所以替换内容的基本命令是 editor.setData 但它不会添加撤消历史记录,在 editor.model.change 中包装调用的条目赢了' 也改变行为。
  2. 然后在文档的某个深处有关于如何向编辑器添加(但不是替换)自定义 html 的信息,该编辑器正在添加撤消历史记录条目:

    this.editor.model.change((writer) => {
          const html = '<p>aaa</p><ul><li>huu</li><li>zzzz</li></ul><p>ssss</p>'
          const viewFragment = this.editor.data.processor.toView(html);
          const modelFragment = this.editor.data.toModel(viewFragment);
          this.editor.model.insertContent(modelFragment);
          this.editor.model.insertContent(modelFragment);
          this.editor.editing.view.scrollToTheSelection();
    });
    

那么如何替换编辑器的内容添加撤消历史条目呢?我只能满足其中一个要求,但不能同时满足这两个要求。

最佳答案

这是一个例子 selects all先是正文,然后是 inserts自定义文本:

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';

class Replace extends Plugin {
    init() {
        const editor = this.editor;

        editor.ui.componentFactory.add( 'Replace', locale => {
            const replaceButtonView = new ButtonView( locale );

            replaceButtonView.set( {
                label: 'Replace',
                withText: true,
            });

            replaceButtonView.on( 'execute', () => {
                editor.execute('selectAll');
                const html = '<p>aaa</p><ul><li>huu</li><li>zzzz</li></ul><p>ssss</p>';
                const viewFragment = editor.data.processor.toView(html);
                const modelFragment = editor.data.toModel(viewFragment);
                editor.model.insertContent(modelFragment);
                editor.editing.view.scrollToTheSelection();
            });

            return replaceButtonView;
        } );
    }
}

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        plugins: [ Essentials, Paragraph, Undo, Replace ],
        toolbar: [ 'undo', 'Replace' ]
    } )
    .then( editor => {
        console.log( 'Editor was initialized', editor );
    } )
    .catch( error => {
        console.error( error.stack );
    } );

直播:

CKEditor5-Replace-Undo-Demo

关于javascript - 替换ckeditor内容并添加undo history entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61728192/

相关文章:

javascript - 我如何验证来自 CKEditor 5 编辑器的内容?

css - 前端的 CKEDitor 5 CSS 未应用

javascript - d3 : confusion about selectAll() when creating new elements

php - 使用ckeditor在页面上编辑多个div,然后将HTML保存到文件

angularjs - ckeditor angularjs 指令 : how to upload multiple images?

javascript数组只显示第一个字符

javascript - 动态调整 CKEditor 5 中粘性工具栏的位置

javascript - 查看单击按钮时执行的脚本

JavaScript 作为服务器端语言

vue.js - 将 CKEditor 5 与 nuxtjs 结合使用