4

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.

Ludwika
  • 15
  • 2
Azaz ul Haq
  • 1,635
  • 2
  • 18
  • 46
  • Use the dev console to take a look at the actual dom and see which element contains your actual data. – blueren Nov 29 '17 at 11:49
  • It doesn't update editor's element data until we call updateEditorElement function. But that too updates the formatted text not the plain text. – Azaz ul Haq Nov 29 '17 at 11:56

2 Answers2

6

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 );
    } )
Reinmar
  • 21,729
  • 4
  • 67
  • 78
4

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.

Azaz ul Haq
  • 1,635
  • 2
  • 18
  • 46