Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 623
Azaz ul Haq Avatar asked Sep 07 '25 11:09

Azaz ul Haq


1 Answers

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 );
    } )
like image 143
Reinmar Avatar answered Sep 09 '25 01:09

Reinmar