setNodes Batching
Add many nodes as one change rather than many: AddNodes takes the whole batch in one call, and EditAsync folds any number of calls into one edit. Either way the canvas raises NodesChanged once and records one undo step for the lot, which the counters here make visible.
Press Tab to move between the nodes and edges of the diagram.
Press Enter or Space to select it, Shift with Enter to add it to the selection, the arrow keys to move the selected nodes, Delete to remove them and Escape to clear the selection. Press C to start a connection from it.
Press Enter or Space to select it, Delete to remove it and Escape to clear the selection. Press C to move its end to another node.
bit BlowTry it
- Pick how many Nodes to add, then press Add in one call: the nodes appear together, NodesChanged goes up by one and there is one more undo step.
- Press Add one by one: the same nodes, but the counter and the undo steps go up by one per node, and Undo takes back only the last one.
- Press Add in one edit: the same calls one at a time again, wrapped in one edit - one event, one step, and Undo takes the whole batch back.
- Add 500 a few times, then scroll to zoom in and drag the background to pan: only the nodes in view are drawn, while the minimap shows them all.
- Clear removes everything in one undoable step.
How it works
AddNodes(params BlowNode[] nodes)appends the whole array toNodesand treats it as one edit: oneNodesChanged, oneOnGraphChanged, one undo step labelledadd, and one measuring pass for the lot after the next render.- Called once per node, each call is an edit of its own - an event and an undo step each - though Blazor still renders the canvas once, after the handler.
EditAsync(action, label)runs the action as a single edit: any number ofAddNodescalls, or direct changes to the lists, inside it raiseNodesChangedonce at the end, are recorded as one undo step under the label you give (add batchhere), and render once.HistoryEnabledkeeps the undo stack, here with aHistoryCapacityof 1,000 so that adding 500 one by one does not push the earlier steps out;OnHistoryChangedreports the depth and the label of the stepUndoAsync()would take back.OnlyRenderVisibleElementsdraws only the nodes and edges inside the view, which keeps a pan over hundreds of nodes smooth. The nodes have an explicitWidthandHeight, so their size is known before they are first drawn.- Clear empties both lists inside
EditAsync(..., label: "clear"), so the canvas records it; changed directly in a handler of the page, the lists would be redrawn but the history would not know.