selection

The selection namespace controls which nodes and edges are currently selected or highlighted in the graph. Selection is the primary handle the host UI uses to act on a subset of the graph — context menus, info panels, batch operations, and visual emphasis all read from here.

Selection is intentionally outside the undo/redo stack. A user clicking around the canvas should not flood history.undo with selection noise. Mutations emit "selection:changed" on the shared event bus instead.

Selection mode

Every mutating method accepts a SelectionMode that controls how the new ids combine with the current selection:

Mode Behaviour
"replace" (default) Clear the existing selection, install the new ids
"add" Union — add the new ids to whatever is selected
"subtract" Remove the listed ids from the current selection
"toggle" Flip membership for each id individually
api.selection.selectNodes(["s1", "s2"]);                  // replaces
api.selection.selectNodes(["s3"], "add");                 // now { s1, s2, s3 }
api.selection.selectNodes(["s2"], "subtract");            // now { s1, s3 }
api.selection.toggleNodes(["s1", "s4"]);                  // now { s3, s4 }

Reading the current selection

getSelectedNodeIds() and getSelectedEdgeIds() return synchronous snapshots. For "is anything selected?" checks, hasSelection() is faster than computing array lengths. isSelected(id) / isSelectedEdge(id) probe individual ids.

To subscribe to changes, listen on the engine's event bus:

api.events.on("selection:changed", ({ nodes, edges }) => {
  panel.refresh(nodes, edges);
});

Convenience selectors

Beyond direct id lists, the namespace exposes a family of structural selectors that compute their target set on the fly:

Method Selects
selectAll() / selectAllEdges() Every node or edge in the graph
selectVisibleNodes() Only nodes currently passing visibility filters
selectNeighbors() Nodes adjacent to the current selection
selectConnectionNodes() Nodes that connect any two currently-selected nodes
selectNeighborEdges() Edges incident on the current selection
selectChildren() / selectParents() Members of selected groups, or groups of selected members
selectFloating() Nodes with no incident edges
invertSelection() / invertEdgeSelection() Everything currently NOT selected

These all participate in the same mode system — passing "add" lets you grow a selection by repeated structural queries.

Highlight vs selection

highlight(ids) and clearHighlight() are a parallel emphasis channel: nodes are visually outlined without entering the "selected" set. Use highlight for hover previews, search results, and other transient affordances that should not change what the next batch operation acts on.

Methods