Ckeditor5-angular: Retrieve cursor position / selection with events

Created on 29 May 2019  路  2Comments  路  Source: ckeditor/ckeditor5-angular

Hi!
According to the following implementation:

<ckeditor [editor]="Editor"
                 (change)="onChange($event)"
                 (blur)="blurEvent($event)"
                 placeholder="Some stuff"
                 formControlName="message"></ckeditor>

I would like to retrieve the cursor position or the selection when blur event occurs, but I can't find how to get these values.
According to the documentation I can retrieve the editor instance which is given by the Output() event. And in some topics on the web or from the ckeditor support, I read that it was possible to get this values with the getSelection() method (but that was for CKE4, not 5).

public blurEvent(e: BlurEvent) {
  const selection = el.editor.model.getSelectedContent();
  console.log('ckedit', selection);
}

So with that, I got the following error:

ERROR TypeError: Cannot read property 'getFirstRange' of undefined

Is there any way to get these values with this actual implementation?

EDIT: I guess I can do something with DocumentSelection but I'm not sure.

Regards! 馃枛

solved question

Most helpful comment

Hello, you're right - you can get the current selection from editor.model.document.selection. It exposes API which you can use to obtain the current selection.

All 2 comments

Hello, you're right - you can get the current selection from editor.model.document.selection. It exposes API which you can use to obtain the current selection.

Awesome @Mgsy, it works perfectly with

editor.model.change(writer => {
  editor.model.insertContent(
    writer.createText(newText), editor.model.document.selection, 'in');
});

Thank you for your fast answer! Regards! 馃枛

Was this page helpful?
0 / 5 - 0 ratings