Is there a way to insert content at a specified offset in tinyMCE? I need to insert an element at run time in a position in the document.
EDIT: To be more specific I need to get the cursor position from one client, send it to the server and broadcast it to other clients so that a custom cursor is painted on their screen and they know where other clients are writing. The part where I'm stuck is how can I insert the node I want which contains the caret | at an offset. I figured out how to get the caret offset and how to send it over. The only problem is that there seems to be no method for inserting a node at a position like: insertNode(element, position) or something similar.
This is the code I have right now:
hub.client.broadcastPosition = function (position) {
range = ed.selection.getRng();
var newNode = ed.getDoc().createElement("span");
newNode.id = "caret";
newNode.setAttribute("class", "all-carets");
var element = ed.getDoc().getElementById("caret");
if (element != null) {
element.parentNode.removeChild(element);
}
newNode.innerHTML = "|";
newNode.style.color = "black";
range.setStart(range.startContainer, position);
range.setEnd(range.startContainer, position);
range.insertNode(newNode, position);
};
and I am sending the position over the server by this:
var s = ed.selection.getSel();
if (s) {
if (s.anchorNode && s.anchorNode.nodeType == 3) {
caretPosInElement = s.anchorOffset;
hub.server.sendPos(caretPosInElement);
}
}
On the client side i.e in the broadcastPosition function I need to insert my span node with id caret at the position I am sending from the server. Therefore I need something else instead of that range. Any ideas?