CKEditor 5 如何在 React 中获取编辑器的纯文本内容?

Good“素玲 阅读 2

我在 React 项目里集成了 CKEditor 5,现在想获取用户输入的纯文本(不带 HTML 标签),但官方文档只提到了 editor.getData() 返回的是 HTML 字符串。

试过用 textContent 去解析返回的 HTML,但感觉很麻烦,而且可能有兼容问题。有没有更直接的方法?比如 CKEditor 自带的 API 能直接输出纯文本吗?

我现在的代码是这样的:

import { CKEditor } from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';

function MyEditor() {
  const handleReady = (editor) => {
    editor.model.document.on('change:data', () => {
      const htmlContent = editor.getData(); // 这个是带标签的
      // 怎么拿到纯文本?
    });
  };

  return (
    <CKEditor
      editor={ClassicEditor}
      onReady={handleReady}
    />
  );
}
我来解答 赞 2 收藏
二维码
手机扫码查看
暂无解答

暂无解答