groups
The groups namespace organises nodes into named collections that can be collapsed onto a designated anchor member, then expanded back when the user wants detail. Groups are how the engine handles hierarchical or aggregated views without duplicating the underlying graph data.
A group has three states:
- Created, expanded — members are visible as individual nodes; the group is a logical container.
- Collapsed — non-anchor members are hidden. One real member (the anchor, by default the centroid; configurable via
spec.anchorNodeId) stays visible and displays the 4-corner-ball collection visual. Cross-group edges to other collapsed groups or free nodes are aggregated into single virtual edges (width = base × (1 + ln(weight))). - Dissolved — the group is destroyed and members return to being plain nodes. No data is lost.
Crucially, collapse does not add or remove any nodes from the graph (no synthetic proxy nodes) and does not mark structure dirty — the anchor stays where the layout placed it.
Creating a group
The base constructor takes an explicit member id list:
const gid = await api.groups.create(["s1", "s2", "s3"], {
name: "Cluster A",
});
Three convenience factories build the member set for you:
| Method | Builds members from… |
|---|---|
createBy |
A predicate over node fields |
createByAttribute |
All distinct values of a single property (one group per value) |
createFromClusters |
A Map<groupId, nodeIds[]> produced by an algorithm (e.g. community detection) |
Lifecycle
await api.groups.collapse(gid); // hide members, show proxy
await api.groups.expand(gid); // reverse
await api.groups.toggle(gid); // flip whichever side it's on
await api.groups.dissolve(gid); // destroy group, restore plain nodes
collapseAll(), expandAll(), and dissolveAll() operate on every group at once and are individually undoable steps.
Membership
await api.groups.addNodes(gid, ["s4", "s5"]);
await api.groups.removeNodes(gid, ["s1"]);
const members = api.groups.getMembers(gid);
A node may belong to at most one group at a time — adding it to a second group moves it.
Reads
list() returns every group's state record. get(id) fetches one by id. getByNode(nodeId) finds the group a given node belongs to (or null).
for (const g of api.groups.list()) {
console.log(g.name, g.members.length, g.collapsed ? "collapsed" : "expanded");
}
Drag behaviour
Groups have a per-group drag mode that controls what happens when the user drags one of the members on canvas:
| Mode | Effect |
|---|---|
"node" (default) |
Drag the individual node — group stays put around it |
"group" |
Drag the entire group rigidly — all members translate together |
api.groups.setDragMode(gid, "group");
const mode = api.groups.getDragMode(gid); // "group"
Updating metadata
update(id, patch) merges new fields (e.g. name, color) onto a group spec without touching membership.
Events
All mutating methods emit typed events on the shared event bus: "group:created", "group:collapsed", "group:expanded", "group:dissolved", "group:members-changed", "group:anchor-changed". Subscribe when host UI needs to mirror group state in a sidebar.
Relationship to Collections
CollectionManager (accessed via store.getCollectionManager() — there is no engineApi.collect namespace) is a thin facade over GroupManager:
cm.collect(hubId, leafIds)
≡ api.groups.create([hubId, ...leafIds], { anchorNodeId: hubId, kind: "collect" })
+ api.groups.collapse(...)
Collect-style groups always pin the hub as the anchor. The existing engineApi.events.on("group:collapsed", ...) covers both manual groups.collapse() calls and CollectionManager.collect() calls.