Modifying the Document

To make changes to an XML document, you first have to obtain a reference to the sync.api.Editor instance where the document is loaded. You can obtain such a reference by listening on the workspace object for the EDITOR_LOADED event:

workspace.listen(sync.api.Workspace.EventType.EDITOR_LOADED, function(e) {
  var editor = e.editor;
});

To do this, you should use the sync.api.ActionsManager#invokeOperation API to invoke a Java operation (subclass of ro.sync.ecss.extensions.api.AuthorOperation) on the server-side model of the document. The client-side model will be updated automatically.

The code should look like this:

  if (editor.getEditingSupport() === sync.api.Editor.EditorTypes.AUTHOR) {
    editor.getActionsManager().invokeOperation(
      'ro.sync.ecss.extensions.commons.operations.InsertOrReplaceTextOperation', 
      {fragment: 'Oxygen XML Web Author'}, 
      function() {
        console.log('Done!');
    }); 
  }

The operation is the name of a Java class that implements the desired behavior. It can be one of the operations listed here as sub-classes of ro.sync.ecss.extensions.api.AuthorOperation.

Note: If none of the the built-in operations is suitable for your use-case, you need to implement a custom Java sub-class of ro.sync.ecss.extensions.api.AuthorOperation. In some cases, chaining two built-in actions would solve your problem, for example, inserting a fragment and setting a pseudo-class. Even in such a case it is advisable to implement a Java operation for the following reasons:

  • Performance - each AuthorOperation requires a server round-trip which adds to the latency.
  • Undo behavior - each AuthorOperation generates an undo-able edit, so the user will have to undo twice to revert what they percieve as a single edit.

The operation can be a custom one implemented in a framework or a plugin. In this case, it should be annotated with ro.sync.ecss.extensions.api.webapp.WebappRestSafe for Web Author to allow invoking it from client-side JS code. It is also useful to annotate it with ro.sync.ecss.extensions.api.WebappCompatible.

If the operation is implemented in a plugin, make sure that the class attribute of the plugin element in the plugin.xml descriptor file is a not the ro.sync.exml.plugin.Plugin class itself, but a subclass of it.

If the operation extends ro.sync.ecss.extensions.api.webapp.AuthorOperationWithResult, besides modifying the document, it can also return a result to the client-side JS code. This result will be passed as the first result to the callback function.

You can find details about the arguments of default operations in the user's manual