Remove just last transaction from UndoManager history?

When users turn the grid on and off we don’t want that to be part of the undo history. For example when they add nodes and links, then turn on the grid, ctrl-z will turn off the grid as part of the undo history. Our code creates the grid as part of the transaction. UndoManager.clear() removes all the undo history - is there a way to remove just the last history after we create the grid?

Changes to GraphObjects that are in temporary Layers are not recorded in the UndoManager. (The Diagram.grid is in the “Grid” Layer, for which Layer.isTemporary is true.) So you are probably getting empty Transactions in your UndoManager.history. Is that right?

If so, then it should be safe to make those changes outside of a transaction. In other words, don’t start and commit a transaction just for changing some properties of the Diagram.grid.

Here is my code to create the grid.

if (prefs.get('showGrid') && (Ext.isDefined(me.diagramOptions['grid.visible']) ? me.diagramOptions['grid.visible'] : true)) {
    lines.push(
        make('Shape', 'LineH', {
            stroke: '#D1D1D1',
            strokeWidth: 1
        }),
        make('Shape', 'LineV', {
            stroke: '#D1D1D1',
            strokeWidth: 1
        })
    );
}
grid = make('Panel', 'Grid', {
        gridCellSize: new go.Size(prefs.get('gridSize'), prefs.get('gridSize')),
        gridOrigin: new go.Point(0.5, 0.5) // Need to keep canvas from anti-aliasing 1px lines
    },
    lines
);

me.goDiagram.startTransaction('updateGrid');
me.goDiagram.grid = grid;
me.goDiagram.toolManager.draggingTool.isGridSnapEnabled = prefs.get('snapToGridMove');
me.goDiagram.toolManager.draggingTool.gridSnapCellSize = new go.Size(prefs.get('gridSnapSize'), prefs.get('gridSnapSize'));
me.goDiagram.toolManager.resizingTool.isGridSnapEnabled = prefs.get('snapToGridResize');
me.goDiagram.commitTransaction('updateGrid');

If I remove the transaction above then I get this error on the console:

Change not within a transaction: !d grid: Diagram “wp-modeler-process-diagram-1541” old: GRID new: GRID

Whether I used a transaction or not, ctrl-z will remove the grid.

Replace the calls to startTransaction/commitTransaction with settings of Diagram.skipsUndoManager.

      myDiagram.skipsUndoManager = true;
      myDiagram.grid = grid;
      . . .
      myDiagram.skipsUndoManager = false;

This will allow people to turn on and off the grid without anything being recorded in the UndoManager. Then after they undo, if they toggle the grid, no UndoManager state will be lost.