Can I listen for a "save event"?

For my Modeler plugin I have the following need:

When the user saves the diagram, I need to start a separate function, that saves some kind of comment to a separate datastore. As far as I can see, there is no eventBus listener for this or am I missing something?

Is there any way to know, when a user saves the diagram while my program is in the context of the canvas?

Sorry, I’m a bit sick for some time now and missed a couple of things:
Actions like ‘save’ are not fired on the eventBus, but triggered as an ‘EditorAction’. I should have known that. And as I understand it, I could register an additional listener for an existing action like this:

function PQCParser(editorActions, eventBus, elementRegistry, databaseUtil){
  editorActions.register({
    save: function(){
      console.log("An additional listener for the save action");
    }
  });
};

But this does not work for the ‘save’ action, because ‘editorActions’ does not have access a lot of the camunda modeler listeners.

Is there another way to do this?

But this does not work for the ‘save’ action, because ‘editorActions’ does not have access a lot of the camunda modeler listeners.

The Camunda Modeler handles the save action itself. The action never actually reaches the bpmn-js instance. This leaves you with no way to hook into save.

Just to clarify: Exporting XML in bpmn-js and saving the tab (persisting the XML to disk) are two different things. When exactly would you like to save additional data? On every XML export or on every persist operation?

Thanks for this very good question. I thought, the straight forward thing would be, to save my ‘comments’ every time a user saves his work, so the user experience is not broken. And I thought, the best way to achieve this would be to hook into the save action itself.

Am I understanding this correctly that for every save action a bpmn-js export is triggered? It would be totally fine to hook into the XML export then. If this was possible, could you point me at how to do this?

Right now there is no way to hook into saving the XML. It doesn’t fire any events you could listen for. The only way I see right now is wrapping the saveXML method and doing you magic inbetween. This is very hacky, though.

I’ve created an issue for that since I see no reason why you shouldn’t be able to hook into export.

Thank you. That would really help me.

I’m not quite sure that would help you. What you need is to hook into the tab saving mechanism of the Camunda Modeler. We’re going to implement it anyway, as it is a valuable addition to the toolkit.

Oh. I thought the saving mechanism the Camunda Modeler would simply trigger the Viewer.prototype.saveXML function of bpmn-js. So this seems not to be the case. (I tried it just now with your new patch, to better understand it.)

So do you see any way to promote the save action to the bpmn-js instance, or is there a reason why these things must be separated?

The Camunda Modeler retrieves the XML representation via BpmnViewer#saveXML for a number of reasons, including:

  • to show the correct XML in the XML tab
  • saving the XML to disk because the user triggered save

We’d need a way to expose Camunda Modeler events to plug-in authors to really satisfy your use case.

Is this something that could happen if I open a feature request and ask very kindly (or implement it myself sometime) or are there strong reasons for not exposing Camunda Modeler events?

To clarify: My prototype can work without this and it’s not something totally urgent. But I would need to know if this was possible.
Maybe you could even say how much work you think this would be.

You could implement this feature and send it to us as a PR. This would surely help us to better understand what we need. We cannot promise that we ship this as an official feature any time soon, a PR may speed up things though.

Thanks for opening the issue. I understand, that this is not a priority, but I’m glad, that it is possible to do. I try to make a PR as soon as I can. (Might be a month though.)