Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TinyMCE custom plugin, how to make a word automatically selected based on current cursor position?

Tags:

Making a custom plugin for TinyMCE, I wonder how to make a word automatically selected based on current cursor position, like in the the Wordpress "Add link" plugin.

I've searched the Wordpress TinyMCe Wplink code and TinyMCE Docs but I've got no reference....

like image 370
AmintaCode Avatar asked Apr 18 '18 18:04

AmintaCode


1 Answers

You can do it like below

if (editor.selection.isCollapsed()) {
    var selRng = editor.selection.getRng();
    selRng.expand("word"); //expands the DOM range to the current word
    editor.selection.setRng(selRng);
}

Below is a JS Fiddle for the same

https://jsfiddle.net/t9qhmguo/

Output

like image 106
Tarun Lalwani Avatar answered Sep 28 '22 18:09

Tarun Lalwani