Question


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());

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

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

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

Any solution or feedback would be highly appreciated.


Answer 1:


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

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.

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 );
    } )



Answer 2:


Ok, I found a workaround:

var plainText = $(editorObj.getData()).text();

Until we get a proper solution or a method exposed by library, I hope this workaround will work.



来源:https://stackoverflow.com/questions/47552095/ckeditor5-get-plain-text


码神部落- 版权声明 1、本主题所有言论和图片纯属会员个人意见,与码神部落立场无关。
2、本站所有主题由该帖子作者发表,该帖子作者这爷绝蝂码神部落享有帖子相关版权。
3、码神部落管理员和版主有权不事先通知发贴者而删除本文。
4、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者这爷绝蝂码神部落的同意。
5、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任。
6、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责。
7、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。

最新回复 (0)
    • 码神部落
      2
        立即登录 立即注册 GitHub登录
返回
发新帖
作者最近主题: