General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Highlighting current XML element with CSS

  • Progresso

    Highlighting current XML element with CSS

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

    Hi,

    This is a question concerning XMetaL Author version 4.6 (we use XMetaL Developer for adaptations).

    Could you please help me figure out how the [u]current[/u] XML element can be highlighted preferably with CSS (or alternatively with JScript although this seems inefficient).  This is for “Normal” and “Tags On” view.

    For instance, the XML could be the following (shown as plain text here):

    This is a paragraph with an embedded element inside.

    Only when the text cursor is placed between and is the highlight desired.  This is because some other functionality finds a relevant element and positions the text cursor inside it.  The document will be scrolled automatically so that the element is in view, but it is too hard for the user to locate the element on the screen without highlighting the element.

    I guess that it is possible somehow, because the program is aware of which element is the current one, as can be seen in the Attribute Inspector.

    Thanks
    Bo Krantz Simonsen, Denmark

    Reply

    Derek Read

    Reply to: Highlighting current XML element with CSS

    I can think of two ways to do this.

    1. The first (assuming you have script that already positions the selection inside the element) would be to use the Selection.ContainerStyle property.

    Getting rid of the highlighting will be the tricky part however, as an appropriate event will need to be used that fires when you want to reset the CSS to remove it. You will also need to remember which element you changed ContainerStyle for and reset the value so that it matches the original (before the highlighting change). It will very likely be necessary to store the original Selection object (the one you are navigating to in your script) as a variable using the NameVariantProperties API so that this value can be retrieved from another event macro, in order to reset the CSS for that element (to remove the highlighting).

    The alternative might be to switch views, however, ContainerStyle is remembered unless you switch to PlainText view and back, or reload the document.

    2.The second would be to rewrite the CSS file and then reload it using ActiveDocument.RefreshCSSStyle() method.

    One simple way to do that would be to add an import statement to your existing CSS that imports a second CSS file (path should be relative to the first one) and then use Windows FSO to write that second CSS file out using script just before calling RefreshCSSStyle(). The CSS “@import” statement is used for this.

    The file you write out would contain only the CSS with the proper selector to affect the element in question. You seem to be lucky enough to have an id attribute on the element in question, which I assume must be a unique id value. So, the CSS you put into your file might be something as simple as the following:

    emb[id="3"] {
      background-color: yellow;
    }

    Your script would need to be smart enough to alter the value of the id attribute every time it writes out the imported CSS file so that only that particular element is affected. This might be a better solution than the first one above because there will be no need to reset the style for the element because ContainerStyle is not being used and normal CSS cascading rules apply (meaning that once a new CSS file is loaded that does not contain the selector emb[id="3"] that element will revert to whatever styling is given to it by the main CSS file.

    Reply

    mag3737

    Reply to: Highlighting current XML element with CSS

    Here's a JScript function that will “flash” the current element in XMetaL by repeatedly selecting the parent element and then reselecting the original selection.  It's a bit of a silly hack, but it may be appropriate for certain kinds of “where am I?” situations.

    [code]
    //XMetaL Script Language JScript:

    function flash_element(num_blinks) {
    var obj=new ActiveXObject(“SQExtras.Methods”);
    for (var i=1; i<=num_blinks; i++) {
    var remember_position = ActiveDocument.Range;
    Selection.SelectElement();
    obj.GoToSleep(250);  // 250 milliseconds = quarter second
    remember_position.Select();
    }
    obj = null;
    }

    flash_element(3);
    [/code]

    Reply

    Progresso

    Reply to: Highlighting current XML element with CSS

    Thank you Derek Read and Tom Magliery.

    On our system I not sure that I cannot always rely on write access for creating a CSS file.  Furthermore, the id attribute is not always present, and the attributes that are always present are not always unique.

    I have therefore decided to flash the selection on-off as Tom suggests.

    Regards
    Bo

    Reply

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

Lost Your Password?

Products
Downloads
Support