Page 1 of 1

Call js function from backend

Posted: Tue Feb 11, 2025 4:57 pm
by Isabelle
Hello,

With Oxygen 27, is it possible to call javascript function from backend ?
We have extended WSEditorListener to implement our own editorSaved(int var1) method.
But we need to show a modal to the user and for that we need to send to frontend a json element and call a javascript function.
Is it possible from java to throw event or call javascript method with parameters ?
Thanks.

Regards,
Isabelle

Re: Call js function from backend

Posted: Wed Feb 12, 2025 7:14 pm
by cosminef
Hello,

You should use the JavaScript API [1] to override the method with the ID "Author/Save".
Do you also want to modify the autosave functionality?

[1] https://www.oxygenxml.com/maven/com/oxy ... 0.0/jsdoc/

Best,
Cosmin

Re: Call js function from backend

Posted: Fri Feb 14, 2025 1:20 pm
by Isabelle
Hello,
Do you also want to modify the autosave functionality?
No thanks, only save functionality.

The link you have mentioned does not explain how to override the method with the ID "Author/Save".
But I have found this on your github => web-author-block-save-if-invalid.
I have tried to implement it like this to launch our own validation after save:

Code: Select all

function applicationStarted(pluginWorkspaceAccess) {
  pluginWorkspaceAccess.addEditingSessionLifecycleListener(
    new JavaAdapter(Packages.ro.sync.ecss.extensions.api.webapp.access.WebappEditingSessionLifecycleListener, {
      editingSessionStarted: function (docId, authorDocumentModel) {
        authorDocumentModel.getWSEditor().addEditorListener(new JavaAdapter(Packages.ro.sync.exml.workspace.api.listeners.WSEditorListener, {
          editorSaved: function(var1) {
              this.editor.getActionsManager().invokeOperation('SetReadOnlyStatusOperation', {'read-only': true});
              this.editor.getActionsManager().invokeOperation('com.plugin.operations.ValidationOperation', {},
                ((error, result) => {
                  this.editor.getActionsManager().invokeOperation('SetReadOnlyStatusOperation', {'read-only': false});
                  if (error == null) {
                    if (result) {
                      currentValidationResults = JSON.parse(result);
                      createValidationModal(callback);
                    }
                  }
                }));
            return true;
          }
        }))
      }
    }));
}
But it fails on application start with the error "missing ;"
Any idea why ?
Thanks,

Regards,
Isabelle

Re: Call js function from backend

Posted: Mon Feb 17, 2025 5:26 pm
by mihaela
Hello,

The plugin that you mentioned is a JavaScript-Based Workspace Access Plugin Extension:
https://www.oxygenxml.com/doc/versions/ ... in-js.html
This type of extension can use the same API as the JAVA Workspace Access plugin extension, but the implementation is JavaScript-based and uses the bundled Rhino library to create and work with Java API from the JavaScript code.

So all the code executes on the server side actually so you cannot mix it with the javascript API that is called on the client:
https://www.oxygenxml.com/maven/com/oxy ... 0.0/jsdoc/
This is why you obtain the error above.

Best Regards,
Mihaela

Re: Call js function from backend

Posted: Mon Feb 17, 2025 6:00 pm
by Isabelle
Hello,

Ok, I understand the problem.
But then can you tell me, how, in javascript, can I add our custom validation method after save action (only save and not auto save) ?
Thanks,

Regards,
Isabelle

Re: Call js function from backend

Posted: Mon Feb 17, 2025 6:36 pm
by mihaela
Hello,

Here is a possibility: there is an event called "DIRTY_STATUS_CHANGED" that is triggered on the editor, that you can listen to:
https://www.oxygenxml.com/maven/com/oxy ... es__anchor
When the "isDirty" status is set to false you can trigger your validation.

Check the documentation for a sample code on how to listen on this event:
https://www.oxygenxml.com/maven/com/oxy ... Event.html

Best Regards,
Mihaela

Re: Call js function from backend

Posted: Mon Feb 17, 2025 6:38 pm
by Isabelle
Thanks a lot.
I will try that.

Regards,
Isabelle

Re: Call js function from backend

Posted: Thu Feb 20, 2025 8:05 pm
by Isabelle
Hello,

It works fine.
Thank you.

Regards,
Isabelle