Adds and removes node with skipping undo

Walter, another question.
I have many questions today, sorry and thank you.

I have this code.
I call it when I double-clicked or right-click somewhere in background using type = ‘zoomin’ and type = ‘zoomout’.

    function markZoomAt(point, type = "zoomin", color = null) {
        if (!(new Set(["zoomin", "zoomout", "pacman"]).has(type)))
            return;

        var data = {
            category:"Icon",
            geo:type,
            loc:go.Point.stringify(new go.Point(point.x - _IconNodeCircleSize / 2, point.y - _IconNodeCircleSize / 2)),
        };

        if (color != null) {
            data.color = color;
        }

        var oldskips = _d.skipsUndoManager;
        _d.skipsUndoManager = true;
        _d.startTransaction("draw focus marker");

        _d.model.addNodeData(data);

        _d.commitTransaction("draw focus marker");
        _d.skipsUndoManager = oldskips;

        setTimeout(function() {
            var oldskips = _d.skipsUndoManager;
            _d.skipsUndoManager = true;
            _d.startTransaction("remove focus marker");

            _d.model.removeNodeData(data);

            _d.commitTransaction("remove focus marker");
            _d.skipsUndoManager = oldskips;
        }, 2000);
    }

This code adds a node and removes it after 2 seconds.
And I expect this code makes the model not to be ‘modified’ status.
But it’s not working.

How should I fix it?

Setting Diagram.skipsUndoManager to true will turn off the UndoManager’s recording of ChangedEvents. But if you are modifying the model, such as by calling Model.addNodeData, you are still modifying the model and the diagram is still being notified that the model was changed (in this case that a node was added). So Diagram.isModified will still be set to true, which eventually results in a “Modified” DiagramEvent.

Basically, you have modified the model, so of course there had better be a “Modified” event. If the UndoManager has not changed does not matter.

But maybe you don’t really want to add a node via the model. Instead, could you directly add a Part to a temporary Layer? That is how Adornments are added to the diagram (but not the model) for selection or for tools without a “Modified” DiagramEvent. I suggest that you add a Part to the “Tool” layer.

I think it’s good solution.
Would you please give me some example?
I understand it in theory but I don’t know where and how I start from.
I don’t know how add a temporary layer and show it transparently with original model together.

Thanks!

Take a look at extensionsTS/DragZoomingTool.ts, how the DragZoomingTool calls Diagram.add and Diagram.remove. It preallocates a Part and reuses it, but you don’t have to do that if you don’t want to.

Note how that Part has set Part.layerName to “Tool”, so that the Part is added to the “Tool” layer, which is a pre-existing temporary layer because its Layer.isTemporary is true.

1 Like