iceboxqs
07-15-2004, 11:35 PM
Back again,
I've been working on a mozilla rtf editor.
I finally have found a way to place the cursor to a specific point anywhere in my editable document. Here is how I did it.
Somewhere in your program say you want to grab where the user is and head back there after you do some formating.
You use this to grab the node they are in and how many character into the node they are:
//make range and select the body
range = document.createRange();
range.selectNode(document.body);
//get the selection
sel = window.getSelection();
//this returns how many character into the current node the cursor is
// aka offset
insertOffset = sel.focusOffset;
//this selects the node the cursor is currently in.
currentNode = sel.focusNode;
The two variables currentNode and insertOffset would be passed into
setCursor like so . . . . setCursor(currentNode,insertOffset);
function setCursor(targetNode,targetPoint)
{
//Setup selection range
range = document.createRange();
//Place range around the target node
range.selectNode(targetNode);
//Get the selection
sel = window.getSelection();
//Collapse to the start of the node;
sel.collapseToStart();
//From the start of the next we extend the selection to our target character
sel.extend(targetNode,targetPoint);
//Now we collapse to that point
sel.collapseToEnd();
}
That will put the cursor in the node you grabbed at the offset you captured.
Note: If you do too much processing you will loose currentNode and it will not work. What I do to work around that is count through the body until I find the current node and then just store that nodes child # so I can always get back to it.
Obviously this is a very simple example. You have to do a lot of checking to see how many nodes deep you are and adjust your child node # if you are adding or removing nodes.
At any rate I finally have a question as well.
Has anyone done something like this before? It works about 95% of the time. However I have ran into an error like this:
[Exception... "Index or size is negative or greater than the allowed amount" code: "1" nsresult: "0x80530001 (NS_ERROR_DOM_INDEX_SIZE_ERR)"]
What I don't understand is if I look at the node I am placing the cursor in, its length is 8 and I am trying to extend the selection to 4. Which is not greater then the index and is not negative.
There is only one situation when this occurs that I have found but I can't seem to find away around it. Any suggestions? :confused:
I've been working on a mozilla rtf editor.
I finally have found a way to place the cursor to a specific point anywhere in my editable document. Here is how I did it.
Somewhere in your program say you want to grab where the user is and head back there after you do some formating.
You use this to grab the node they are in and how many character into the node they are:
//make range and select the body
range = document.createRange();
range.selectNode(document.body);
//get the selection
sel = window.getSelection();
//this returns how many character into the current node the cursor is
// aka offset
insertOffset = sel.focusOffset;
//this selects the node the cursor is currently in.
currentNode = sel.focusNode;
The two variables currentNode and insertOffset would be passed into
setCursor like so . . . . setCursor(currentNode,insertOffset);
function setCursor(targetNode,targetPoint)
{
//Setup selection range
range = document.createRange();
//Place range around the target node
range.selectNode(targetNode);
//Get the selection
sel = window.getSelection();
//Collapse to the start of the node;
sel.collapseToStart();
//From the start of the next we extend the selection to our target character
sel.extend(targetNode,targetPoint);
//Now we collapse to that point
sel.collapseToEnd();
}
That will put the cursor in the node you grabbed at the offset you captured.
Note: If you do too much processing you will loose currentNode and it will not work. What I do to work around that is count through the body until I find the current node and then just store that nodes child # so I can always get back to it.
Obviously this is a very simple example. You have to do a lot of checking to see how many nodes deep you are and adjust your child node # if you are adding or removing nodes.
At any rate I finally have a question as well.
Has anyone done something like this before? It works about 95% of the time. However I have ran into an error like this:
[Exception... "Index or size is negative or greater than the allowed amount" code: "1" nsresult: "0x80530001 (NS_ERROR_DOM_INDEX_SIZE_ERR)"]
What I don't understand is if I look at the node I am placing the cursor in, its length is 8 and I am trying to extend the selection to 4. Which is not greater then the index and is not negative.
There is only one situation when this occurs that I have found but I can't seem to find away around it. Any suggestions? :confused: