Get the highlighted/selected text in CKEDITOR 5

不羁岁月 提交于 2021-02-07 19:53:16

问题


I have my Inline CKeditor

let globalEditor;

InlineEditor.create(document.querySelector("#textarea"), {
        toolbar: {
            items: ['heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote', 'insertTable', 'undo', 'redo']
        }
}).then(editor => {
        globalEditor = editor;
}).catch(err => {
    console.error(err.stack);
});

I also have a button that supposed to be getting the highlighted/selected text inside the ckeditor

 $("#btnAddTag").click(function (e) {
        e.preventDefault();
        var editor = globalEditor;

        var getText = editor.getSelection().getNative(); //I tried this but the *getSelection* is undefined
 });

Any suggestions?


回答1:


Already fixed the problem

const editor = globalEditor;
const selection = editor.model.document.selection;
const range = selection.getFirstRange();

for (const item of range.getItems()) {
    console.log(item.data) //return the selected text
}  


来源:https://stackoverflow.com/questions/57196777/get-the-highlighted-selected-text-in-ckeditor-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!