Great, I've located the information.
To start, you'll need to initialize an instance of the tinymce.Editor class. This can be done like so:
var editor = new tinymce.Editor(); //or another method, such as tinyMCE.activeEditor
Using jQuery, retrieve the position of the TinyMCE widgets:
var tinymcePosition = $(editor.getContainer()).position();
var toolbarPosition = $(editor.getContainer()).find(".mce-toolbar").first();
Now determine the position of the HTML node you are currently editing:
var nodePosition = $(editor.selection.getNode()).position();
var textareaTop = 0;
var textareaLeft = 0;
We have the Y-axis position (via nodePosition.top
), now let's find the X-axis position:
if (editor.selection.getRng().getClientRects().length > 0) {
textareaTop = editor.selection.getRng().getClientRects()[0].top + editor.selection.getRng().getClientRects()[0].height;
textareaLeft = editor.selection.getRng().getClientRects()[0].left;
} else {
textareaTop = parseInt($($this.selection.getNode()).css("font-size")) * 1.3 + nodePosition.top;
textareaLeft = nodePosition.left;
}
The positions of the caret relative to the TinyMCE editor window (textarea) are now stored in
textareaTop && textareaLeft
. Let's find their positions relative to the entire page (browser window):
var position = $(editor.getContainer()).offset();
var caretPosition = {
top: tinymcePosition.top + toolbarPosition.innerHeight() + textareaTop,
left: tinymcePosition.left + textareaLeft + position.left
}
This solution is inspired by the autocomplete plugin for TinyMCE 3 found at here, and has been adjusted to fit my needs in TinyMCE 4.