简体   繁体   中英

CKEditor5 Get Plain Text

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM