Selection PIs

It's possible to set the selection by using XQuery. This can be done using the following selection processing instruction nodes, a.k.a. selection PIs. This can only be done in mutation hooks or the operation execute-update-script:

Selection PIs

XQuery

<?fontoxml-selection?> (: for collapsed selection :)
<?fontoxml-selection start?> (: for starting point of selection :)
<?fontoxml-selection end?> (: for ending point of selection :)

The editor sees selection PIs as selection markers. They are not added to your document. After setting the selection the editor immediately removes them. These can be used in an XQuery function as below:

Examples

XQuery

xquery version "3.0";


(: The namespace URI that is used in other places of the editor :)
module namespace app-hooks="http://www.example.fontoxml.com/hooks";


(: Inserts collapsed selection :)
declare %public %updating function app-hooks:on-paragraph-insert-and-put-cursor ($event-type as xs:string, $node as node(), $previous-value as item()*, $current-value as item()*) {
    if(trace($event-type) = 'add') then
        insert node ("Paragraph was added ", <?fontoxml-selection?>, "on " || current-dateTime()) into $node
		(: Inserts the text above and puts cursor just before letter "o" :)
    else ()
};


(: Inserts uncollapsed and forwards selection :)
declare %public %updating function app-hooks:on-paragraph-insert-and-select-forwards ($event-type as xs:string, $node as node(), $previous-value as item()*, $current-value as item()*) {
    if(trace($event-type) = 'add') then
        insert node (<?fontoxml-selection start?>, "Paragraph was added on " || current-dateTime(), <?fontoxml-selection end?>) into $node
		(: Inserts the text above and selects it from its beginning to its end :)
		(: If you use either a start selection PI or an end selection PI but do not use other one, the editor throws error! :)
    else ()
};


(: Inserts uncollapsed and backwards selection :)
declare %public %updating function app-hooks:on-paragraph-insert-and-select-backwards ($event-type as xs:string, $node as node(), $previous-value as item()*, $current-value as item()*) {
    if(trace($event-type) = 'add') then
        insert node (<?fontoxml-selection end?>, "Paragraph was added on " || current-dateTime(), <?fontoxml-selection start?>) into $node
		(: Inserts the text above and selects it from its end to its beginning :)
    else ()
};


(: Replaces a note with a warning note and selects whole content :)
declare %public %updating function app-hooks:replace-note-with-warning-and-select-forwards ($event-type as xs:string, $noteNode as node(), $previous-value as item()*, $current-value as item()*) {
    if(trace($event-type) = 'add') then
        replace node $noteNode with <note type=\"warning\"><?fontoxml-selection start?>{$noteNode/node()}<?fontoxml-selection end?></note>
    else ()
};

Only use the start selection PI and end selection PI once to create an expanded selection. The editor will thrown an error if multiple are found. It is not allowed to use collapsed selection PI together with any other selection PIs. Ensure to not delete a selection PI in the same function or operation that inserts it. Otherwise the editor can throw an error if one of the situations above appears.