Undo & Redo is not working when focusing on Node/Link.
When we drag the nodes/links the default focus is on the node we dragged. When we try command+z undo/redo is not working until we explicitly focus on canvas. Is this expected behavior? If not, is anything to be handled?
Yes, by default only the Diagram’s Div element handles keyboard input events, so the Diagram must have focus in order for it to receive keyboard events if you want it to execute keyboard commands.
You could define a keyboard input event handler on an HTML element that encloses the Diagram’s Div. Or you could call Diagram.focus when focus is elsewhere.
Perhaps when the users selects a Part you change focus to be in some other HTML element? Either don’t change focus to there, or automatically change focus back to the diagram after the user is finished there, or handle some or all keyboard events and redirect them to the Diagram.
By the way, 10 seems a rather short undo history length.
We tried adding a breakpoint in the diagram initialization code, but it’s been calling 3 times. It’s needed as per the requirement is what I got to know from our team.
I am attaching a screen recording @walter please have a look.
Hi @walter I debugged & got to know that in selectionchanged, we have a code to update diagram model data (diagram.model.commit(m => {});), because of which we are unable to undo/redo/delete while the selection is on node.
As documented, the “ChangedSelection” DiagramEvent listener must not modify the diagram, including its model.
“ChangedSelection” , an operation has just changed the Diagram.selection collection,
which is also the value of the DiagramEvent.subject;
do not make any changes to the selection or the diagram in the event listener; note that just setting Part.isSelected will not raise this event, but tools and commands will.
In both cases, that means not executing any transactions that record any ChangedEvents.
So, why are you executing that transaction? Is it because users have updated some fields that reflect state in the model? It’s certainly reasonable for some HTML DOM event handlers to execute a transaction. For example, the GoJS Data Inspector does that. But such transactions are necessarily asynchronous, not synchronous in a DiagramEvent listener or selectionChanged event handler.
Hi @walter, As per your comment, we tried to avoid **model commit **- related changes. But we are still facing a similar issue. We debugged through it and found we introduced an accessibility feature. To do that, we used invisible buttons, and using those buttons, we navigated. To achieve that, we explicitly focused on the invisible buttons.
I think that’s changing the diagram focus. Without that code, it’s working. Any thoughts here? Any alternatives?
Yes, if your code is giving focus to an HTML button, the Diagram/Div is losing focus.
Either pass unhandled keyboard events to the Diagram or handle what you need in the diagram by overriding CommandHandler.doKeyDown and calling the super method for all keyboard events that your code doesn’t handle.
Have you seen the AriaCommandHandler? Its implementation is at either extensionsJSM/AriaCommandHandler.ts or extensions/AriaCommandHandler.js. That can give you an example of implementing such behavior.
I did not suggest that you should override CommandHandler.undo. In fact, that it exactly how it is defined by default, except that there is no argument to the method.
I did suggest handling keyboard events on your HTML button and passing most of the keys on to the Diagram.
I also suggested an alternative: not passing focus to that HTML button but instead handling the additional events on the Diagram, and perhaps passing some of the events onto elsewhere.