CKEditor5 获取纯文本

编程入门 行业动态 更新时间:2024-10-26 20:25:32
本文介绍了CKEditor5 获取纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我知道如何使用 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 08:29:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1038448.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!