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 » Tue Apr 23, 2019 5:17 am

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: 201
Joined: Wed May 20, 2009 2:40 pm

Re: Add selection event listener

Post by mihaela » Tue Apr 23, 2019 5:21 pm

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 » Wed Apr 24, 2019 5:32 pm

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 » Wed Apr 24, 2019 5:41 pm

Is there a way to scroll to a selection? scrollManager.scrollTo(SelectionFromGetSelection)?

mihaela
Posts: 201
Joined: Wed May 20, 2009 2:40 pm

Re: Add selection event listener

Post by mihaela » Thu Apr 25, 2019 2:13 pm

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

Post Reply