5

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....

AmintaCode
  • 364
  • 5
  • 15

1 Answers1

5

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

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265