Pages: 1
Print
Author Topic: Getting Cursor Position  (Read 5513 times)
S. Strube
Member

Posts: 9

-


WWW
« on: May 13, 2009, 03:14:50 AM »

Hi,

I have the need to find out the cursor position within a Selection/Range. Is there any way to do this, in a quick non-dirty way?

A feature requires to find out which sentence in the current paragraph the user has selected, to trigger a search for that sentence. I've got the block element containing the selection with several sentences, but have not found any way to say where the cursor is.

Well, there is one dirty and way too slow way:
var currentNode = Selection.ContainerNode;
while(Selection.ContainerNode == currentNode)
{
   Selection.Move(left) 
   positionCount++
}

That way I'd be able to 'count' where the cursor is, but the cursor moving function is very slow and i need to calculate the cursorposition whenever the selection is changed - so this is no solution.

Anyone can help me with this?
Logged
mag3737
XMetaL Evangelist
Administrator
Member

Posts: 117

I even use XMetaL to write my business letters.


« Reply #1 on: May 13, 2009, 11:02:19 AM »

I haven't tried this myself, but how about using the JScript string object to do a string comparison of Selection.Text and Selection.ContainerNode.xml (which is actually a property of DOMElement, not DOMNOde).  I never can remember the string-comparison functions on String, either, but I'm thinking of the one that will tell you the position where STR1 occurs within STR2.
Logged

Tom Magliery
JustSystems Canada, Inc.
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #2 on: May 15, 2009, 03:21:01 PM »

Another option (that might allow you to keep your current script with fewer changes) might be to turn of formatting before the slow part of your script (where selections are being manipulated).

Compare the speed of execution of the two following scripts and you will see that the second is far faster (with less flicker):

Script 1 (slow)
Code:
//XMetaL Script Language JScript:
for (i=0;i<100;i++) {
Selection.MoveRight();
}

Script 2 (faster)
Code:
//XMetaL Script Language JScript:
ActiveDocument.FormattingUpdating=false;
for (i=0;i<100;i++) {
Selection.MoveRight();
}
ActiveDocument.FormattingUpdating=true;

Logged
S. Strube
Member

Posts: 9

-


WWW
« Reply #3 on: July 28, 2009, 02:20:55 AM »

I haven't tried this myself, but how about using the JScript string object to do a string comparison of Selection.Text and Selection.ContainerNode.xml (which is actually a property of DOMElement, not DOMNOde).  I never can remember the string-comparison functions on String, either, but I'm thinking of the one that will tell you the position where STR1 occurs within STR2.

Thank you mag for your suggest, but comparing the selection.Text does not really work, as the pure XML (e.g. selection is in an empty <p>) may occur several times in the same XML.

I will have a look at Derek's suggestion to turn the FormattingUpdating off.

Thanks,
Stefan
Logged
Pages: 1
Print
Jump to:  

email us