"ExternalCopy" transaction is the last transaction in undoManager.history

Hi!

I’m doing some changes to nodeDataArray in some special use case when handling “ExternalObjectsDropped” event.
Those changes are: adding newNode to the model, adding one extra link, and updating some props of existing node in model. All of that happens within my transaction.

Now I’m having issues with scenario if I use undo right after this event happens.
I’m overriding diagram.commandHandler.undo and
I expect to see my transaction as diagram.undoManager.transactionToUndo or at diagram.undoManager.history as last one, but actually it is missing form history and last transaction in the history list is “ExternalCopy”.

Please assist me with handling my transaction correctly for undo action.

Thanks
Vlad.

Here’s an example that adds an extra link and updates the “color” property of some existing node data when dropping a new node from the Palette. https://codepen.io/jhardy/pen/RwYQdYe

You’ll note the undo works as expected. Is there a reason you’re overriding undo? Could you show us your override?

Thanks a lot for a code example! It really helps me to figure out what was wrong with my code.
Long story short - I’ve set a custom prop to node.data just by assigning value
like this: node.data.customProp = value;
Even though it happens inside of commit transaction, apparently that wasn’t enough to perform undo correctly (with all necessary reversal).
That lead me to wrong idea, to handle what I need in undo override. Now everything works perfectly as expected. Now I’m using model.set() to assign value, and that solves all the issue so far.
Thanks @jhardy for assist!

Glad that works for you. Yes, make sure if you’re modifying data already in the model that you call the appropriate Model methods.

https://gojs.net/latest/intro/usingModels.html#ModifyingModels

1 Like