Page 1 of 1

Add selection event listener

Posted: Tue Apr 23, 2019 5:17 am
by nstensland
Can you provide example code ( or link to documentation of how to) add a custom Selection event listener on the javascript client side (framework.js)? We would like to capture the selection range and re-selection the range at a later time.

Thanks,
Nat

Re: Add selection event listener

Posted: Tue Apr 23, 2019 5:21 pm
by mihaela
Hi,

You can listen for SELECTION_CHANGED event [0] like in this example:

Code: Select all

goog.events.listen(workspace, sync.api.Workspace.EventType.EDITOR_LOADED, function(e) {
 var editor = e.editor;
  var selectionModel = editor.getSelectionManager();
  var eventHandler = new goog.events.EventHandler(this)
  eventHandler.listen(selectionModel, sync.api.SelectionModel.EventType.SELECTION_CHANGED, function() {
    var currentSelection = selectionModel.getSelection();
     console.log("Selection changed ", currentSelection);
  });
});
But if you plan to restore selection at a later time I suggest to do that by implementing a custom Java operation.
The selection available from JavaScript is not updated as the document changes, so the selection may no longer be valid at a later time.
The Java code that restores a seletion from a operation may look like this:

Code: Select all

int start = authorAccess.getEditorAccess().getSelectionStart();
int end = authorAccess.getEditorAccess().getSelectionEnd();
Position startPos = authorAccess.getDocumentController().createPositionInContent(start);
Position endPos = authorAccess.getDocumentController().createPositionInContent(end);
authorAccess.getEditorAccess().select(startPos.getOffset(), endPos.getOffset());
You an find more details about customizing Web Author with plugins here:
https://www.oxygenxml.com/doc/ug-waCust ... ugins.html

[0] https://www.oxygenxml.com/maven/com/oxy ... Model.html


Best Regards,
Mihaela

Re: Add selection event listener

Posted: Wed Apr 24, 2019 5:32 pm
by nstensland
In this simple example

var currentSelection = selectionModel.getSelection();
selectionModel.setSelection(currentSelection);

It seems to work but results in this error in console:

nIndex is not defined
at <anonymous>:577:74

Should this work without an error?

Re: Add selection event listener

Posted: Wed Apr 24, 2019 5:41 pm
by nstensland
Is there a way to scroll to a selection? scrollManager.scrollTo(SelectionFromGetSelection)?

Re: Add selection event listener

Posted: Thu Apr 25, 2019 2:13 pm
by mihaela
Hi,

1. To scroll to a selection you can find the element at selection:

Code: Select all

var nodeAtSelection = selection.getNodeAtSelection();
and then use the "scrollIntoView" method on this element:

Code: Select all

nodeAtSelection.scrollIntoView()
2.
Should this work without an error?
That snippet shouldn't casuse any errors. On our demo deployment [1] the following code works as expected:

Code: Select all

var selectionModel = workspace.currentEditor.getSelectionManager();
var currentSelection = selectionModel.getSelection();
selectionModel.setSelection(currentSelection);
Can you check that the error is thrown by a custom plugin?
This can be done by looking for the JavaScript file that throws that exception, by clicking the link presented in browser's console next to the error.
If the JavaScript file is not "workspace*.js" then is thrown by a custom plugin.

[1] https://staging-webapp.sync.ro/oxygen-x ... xygen.html

Bes Regards,
Mihaela

Re: Add selection event listener

Posted: Mon Oct 28, 2019 11:23 am
by cristi_talau
Hello,

I am updating this thread to let you know that version 21.1.1 of Web Author was released and now there is a new API sync.api.SelectionManager.scrollSelectionIntoView that can be used to scroll the selection into view.

You can find a list of other features and improvements that were added in this version on our website: https://www.oxygenxml.com/xml_web_author/whats_new.html .

Best,
Cristian