3

I have configured the tinyMCE editor with a custom context menu. When I right-click on any word in the editor, the chosen word gets highlighted. I am able to get the selected text with editor.selection.getContent().

  1. How do I get the start and end points of the selected text within the entire text that is currently in the tinyMCE editor? I tried editor.selection.getStart() and getEnd(), but that has not yielded enough results.

My task is to take the start and end indices and get the previous word with it. I am currently using tinymce-3.5.10.

Sriram
  • 10,298
  • 21
  • 83
  • 136

1 Answers1

3

You will need to use

var range = editor.selection.getRng()

to get a range. You can get the start- and end-container using

range.startContainer
range.endContainer
Thariama
  • 50,002
  • 13
  • 138
  • 166
  • I used that like so: `var range = editor.selection.getRng(true); console.log("Range = " + range); console.log("text = " + range.startContainer.textContent);` I get the selected text, but how do I get the start and end indices? The startContainer and endContainer return an object of type Element. – Sriram Mar 19 '14 at 13:53
  • 1
    then you use range.endOffset and range.startOffset – Thariama Mar 20 '14 at 11:42
  • 2
    Thanks. That works, but only as long as the text is not within any tags. `startOffset` and `endOffset` give (I think) position between a tag. So if I choose the text "test" within the sentence: This is a good test. I will get 0 and 4 as output of start and end offset. So then, my only alternative is to strip the text of any tags and try again? – Sriram Mar 20 '14 at 12:18
  • you might be able to get the desired behaviour if you could trigger a double click on the word that has been right cicked. the default browser behaviour is to select the word autatically then. If this doesn't work for you you will have to strip the tags and look for wordboundaries which is a more complicated, then reset the range accordingly and you are done. – Thariama Mar 20 '14 at 12:37