General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Getting Cursor Position

  • S. Strube

    Getting Cursor Position

    Participants 2
    Replies 3
    Last Activity 13 years, 4 months ago

    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?

    Reply

    mag3737

    Reply to: Getting Cursor Position

    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.

    Reply

    Derek Read

    Reply to: Getting Cursor Position

    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();
    }[/code]

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

    Reply

    S. Strube

    Reply to: Getting Cursor Position

    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

    ) may occur several times in the same XML.

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

    Thanks,
    Stefan

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support