yabab-dev / ng2-ckeditor

Angular2 CKEditor component
MIT License
358 stars 96 forks source link

Replace or change selected text with external button click. #330

Closed shubhamp861 closed 2 years ago

shubhamp861 commented 2 years ago

How to replace selected text with different text or more on to add style . For example if i select any text from editor and click on bold button it will go and change my selected text with bold style. wanted to create a button which will change or replace my selected text in editor

kzimny commented 2 years ago

Add a custom button to your editor with click event as shown here. To get the selected text use getSelection() method as shown here

shubhamp861 commented 2 years ago

Able to get selected text and button, but still don't know how we can replace it with different text so it can reflect back same to ck editor window.

And also can we achieve Foot Note. for example refer below link

https://www.drupal.org/docs/8/modules/footnotes/adding-footnotes-to-content-via-the-ckeditor-toolbar

kzimny commented 2 years ago

Your questions are out of scope of ng2-ckeditor. For more information how ckeditor works please refere the ckeditor documentation. Yes, you can replace the text or add your own element.

const selection = this.ckeditor.instance.getSelection();
const txt = selection.getSelectedText(); // or replace the text

let element: CKEDITOR.dom.element;
element = this.ckeditor.instance.document.createElement('span');
element .setAttribute('id', 'myIdXYZ');
element.setHtml(txt);
this.ckeditor.instance.insertElement(element);
shubhamp861 commented 2 years ago

Thanks this will work for me. thanks for quick response.