Add selection event listener

Post here questions and problems related to oXygen frameworks/document types.
nstensland
Posts: 21
Joined: Wed Jan 16, 2019 5:16 am

Add selection event listener

Post 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
mihaela
Posts: 489
Joined: Wed May 20, 2009 2:40 pm

Re: Add selection event listener

Post 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
Mihaela Calotescu
http://www.oxygenxml.com
nstensland
Posts: 21
Joined: Wed Jan 16, 2019 5:16 am

Re: Add selection event listener

Post 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?
nstensland
Posts: 21
Joined: Wed Jan 16, 2019 5:16 am

Re: Add selection event listener

Post by nstensland »

Is there a way to scroll to a selection? scrollManager.scrollTo(SelectionFromGetSelection)?
mihaela
Posts: 489
Joined: Wed May 20, 2009 2:40 pm

Re: Add selection event listener

Post 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
Mihaela Calotescu
http://www.oxygenxml.com
cristi_talau
Posts: 493
Joined: Thu Sep 04, 2014 4:22 pm

Re: Add selection event listener

Post 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
Post Reply