CKEditor5 获取纯文本 [英] CKEditor5 Get Plain Text

查看:67
本文介绍了CKEditor5 获取纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 CKEditor5 API 获取数据,如文档和另一个 SO

I know how to get data using CKEditor5 API as it's mentioned in documentation and on another SO post.

但是,我怎么能得到纯文本呢?我尝试了以下操作,但没有返回任何内容.

However, how could I get the Plain Text? I tried following but it returns nothing.

alert($(editorObj.element).val());

有趣的提示:如果 TextArea 与 CKEditor 绑定,即使下面的代码也不会返回任何内容

Interesting Note: Even following code returns nothing if TextArea is bind with CKEditor

alert( $("#editor").val());

但是如果我不将 TextArea 与 CKEditor 绑定,那么它就可以正常工作.

But if I don't bind TextArea with CKEditor then it works fine.

非常感谢任何解决方案或反馈.

Any solution or feedback would be highly appreciated.

推荐答案

CKEditor 5 没有公开这样的方法,但您可以使用 @ckeditor/ckeditor5-clipboard 包 – viewToPlainText().

CKEditor 5 does not expose such a method but you can use one of the utils of the @ckeditor/ckeditor5-clipboard package – viewToPlainText().

当用户从编辑器复制一些内容时,剪贴板功能使用它来设置剪贴板中的 text/plain 风格.

It's used by the clipboard feature to set the text/plain flavour in the clipboard when the user copies some content from the editor.

要使用它,您需要使用源代码中的 CKEditor 5(因为此功能未公开).您可以在 CKEditor 5 Framework 的快速入门 指南.

To use it you'll need to use CKEditor 5 from source (because this function is not exposed publicly). You can read about that in the CKEditor 5 Framework's Quick start guide.

您可以使用此方法将整个编辑器的视图字符串化:

You can use this method to stringify the entire editor's view:

import viewToPlainText from '@ckeditor/ckeditor5-clipboard/src/utils/viewtoplaintext';
import ClassicEditorBuild from '@ckeditor/ckeditor5-build-classic/src/ckeditor';

ClassicEditorBuild
    .create( element )
    .then( editor => {
        // Will get the stringified content.
        console.log( viewToPlainText( editor.editing.view.getRoot() ) );
    } )
    .catch( error => {
        console.error( error.stack );
    } )

这篇关于CKEditor5 获取纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆