CKEditor 5 纯文本粘贴

10

有没有一种选项可以始终将剪贴板中的内容作为纯文本粘贴?

我尝试了这种方法,但并没有起作用:

$(document).ready(function () {

    ClassicEditor.create(document.querySelector('#text'), {
        toolbar: [
            'heading',
            'bold',
            'italic',
            'link',
            'bulletedList',
            'numberedList',
            'blockQuote',
            'undo',
            'redo'
        ]
    }).then(function (editor) {

        this.listenTo(editor.editing.view, 'clipboardInput', function (event, data) {
            // No log.
            console.log('hello');
        });

    }).catch(function (error) {

    });

});

https://docs.ckeditor.com/ckeditor5/latest/api/module_clipboard_clipboard-Clipboard.html

https://docs.ckeditor.com/ckeditor5/latest/api/clipboard.html

https://docs.ckeditor.com/ckeditor5/latest/api/module_engine_view_document-Document.html#event-event:paste

2个回答

8
clipboardInput事件是在Document上触发的,而不是View。因此,第一件事情是要监听正确的对象。
第二件事是确保插入编辑器的内容是纯文本。这可以通过两种方式来实现:
  • 从剪贴板中获取的HTML可以被“文本化”。但这很难。
  • 我们可以从剪贴板中获取纯文本,并将其插入到编辑器中。然而,编辑器期望粘贴HTML,所以你需要将这个纯文本“HTML化”。CKEditor 5提供了一个函数来实现这个功能 - plainTextToHtml()
为了覆盖编辑器的默认行为,我们需要重写这个回调函数:https://github.com/ckeditor/ckeditor5-clipboard/blob/a7819b9e6e2bfd64cc27f65d8e56b0d26423d156/src/clipboard.js#L137-L158 为此,我们将监听相同的事件(具有更高的优先级),执行所有相同的操作,但忽略剪贴板数据的text/html类型。最后,我们将调用evt.stop()来阻止默认的监听器被执行并破坏我们的工作:
import plainTextToHtml from '@ckeditor/ckeditor5-clipboard/src/utils/plaintexttohtml';

// ...

const clipboardPlugin = editor.plugins.get( 'Clipboard' );
const editingView = editor.editing.view;

editingView.document.on( 'clipboardInput', ( evt, data ) => {
    if ( editor.isReadOnly ) {
        return;
    }

    const dataTransfer = data.dataTransfer;

    let content = plainTextToHtml( dataTransfer.getData( 'text/plain' ) );

    content = clipboardPlugin._htmlDataProcessor.toView( content );

    clipboardPlugin.fire( 'inputTransformation', { content, dataTransfer } );

    editingView.scrollToTheSelection();

    evt.stop();
} );

编辑:

从 CKEditor 27.0.0 开始,代码已经发生了变化(您可以在此处阅读更多相关信息:https://ckeditor.com/docs/ckeditor5/latest/builds/guides/migration/migration-to-27.html#clipboard-input-pipeline-integration)。

import plainTextToHtml from '@ckeditor/ckeditor5-clipboard/src/utils/plaintexttohtml';
//...
const clipboardPlugin = editor.plugins.get( 'ClipboardPipeline' );
const editingView = editor.editing.view;

editingView.document.on( 'clipboardInput', ( evt, data ) => {
    if ( editor.isReadOnly ) {
        return;
    }
    const dataTransfer = data.dataTransfer;
    let content = plainTextToHtml( dataTransfer.getData( 'text/plain' ) );
    data.content = editor.data.htmlProcessor.toView( content );
                
    editingView.scrollToTheSelection();
}, { priority: 'high' } );

5

没有任何导入:

.then(editor => {
                editor.editing.view.document.on('clipboardInput', (evt, data) => {
                    data.content = editor.data.htmlProcessor.toView(data.dataTransfer.getData('text/plain'));
                });
            })

您可以在 ckeditor 剪贴板事件文档中找到完整的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接