General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Getting Cursor Position
-
S. Strube May 13, 2009 at 9:14 am
Getting Cursor Position
May 13, 2009 at 9:14 amParticipants 2Replies 3Last Activity 13 years, 8 months agoHi,
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?
mag3737 May 13, 2009 at 5:02 pm
Reply to: Getting Cursor Position
May 13, 2009 at 5:02 pmI 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.
Derek Read May 15, 2009 at 9:21 pm
Reply to: Getting Cursor Position
May 15, 2009 at 9:21 pmAnother 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();
}[/code]Script 2 (faster)
[code]
//XMetaL Script Language JScript:
ActiveDocument.FormattingUpdating=false;
for (i=0;i<100;i++) {
Selection.MoveRight();
}
ActiveDocument.FormattingUpdating=true;[/code]S. Strube July 28, 2009 at 8:20 am
Reply to: Getting Cursor Position
July 28, 2009 at 8:20 amI 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
) may occur several times in the same XML.
I will have a look at Derek's suggestion to turn the FormattingUpdating off.
Thanks,
Stefan -
AuthorPosts
- You must be logged in to reply to this topic.