@weasel-js/diagram 1.9.1 → 1.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import * as _weasel_js_geom from '@weasel-js/geom';
2
- import { Path, Vec2, PoseDescriptor, SimulationNode, createSimulation } from '@weasel-js/core/math';
2
+ import { Path, PoseDescriptor, Vec2, SimulationNode, createSimulation } from '@weasel-js/core/math';
3
3
 
4
4
  /**
5
5
  * The shapes a built body can wear, as paths in the node's bounds.
@@ -53,6 +53,324 @@ declare function boxForContent(outline: Outline, content: {
53
53
  height: number;
54
54
  };
55
55
 
56
+ /**
57
+ * Reading the `DiagramNode` trait off a scene node.
58
+ *
59
+ * Two ways to attach it, both of which this module's default reader answers:
60
+ *
61
+ * - **On the node.** `data.diagram` holds the trait. One node, one answer;
62
+ * what a connect gesture writes when the author drags a new edge.
63
+ * - **By kind.** A consumer declares once which data shapes take part, and
64
+ * every matching node gets the trait without being stamped. This mirrors
65
+ * `createNodeRouting` in the kit — a predicate over `data`, first match
66
+ * wins — and is the shape to reach for when the participants are a class
67
+ * of node rather than a hand-picked set.
68
+ *
69
+ * A reader is a plain function, so a consumer whose data lives somewhere else
70
+ * entirely passes their own and never touches either form.
71
+ */
72
+
73
+ /** The key on a node's `data` the default reader looks under. */
74
+ declare const DIAGRAM_TRAIT_KEY = "diagram";
75
+ /** All a reader looks at. Deliberately not `SceneNode`: naming `TPose` there
76
+ * would put it in a contravariant position for no gain, since nothing here
77
+ * reads a pose. */
78
+ interface DiagramNodeLike {
79
+ id: string;
80
+ kind: 'leaf' | 'container';
81
+ data: unknown;
82
+ }
83
+ /** Answers "is this node a diagram participant, and on what terms" — `null`
84
+ * for a node that takes no part. */
85
+ type DiagramNodeReader = (node: DiagramNodeLike) => DiagramNode | null;
86
+ /** One class of participant, declared once for every node whose data matches.
87
+ * Mirrors `NodeRoutingEntry`. */
88
+ interface DiagramNodeEntry {
89
+ /** Unique within a reader; the same vocabulary as `NodeRoutingEntry.name`. */
90
+ name: string;
91
+ matches: (data: unknown) => boolean;
92
+ /** The trait every matching node carries. A function when it varies with the
93
+ * node's own data — a pipeline stage whose ports come from its rows. */
94
+ trait: DiagramNode | ((data: unknown) => DiagramNode);
95
+ }
96
+ /** The trait on the node's own `data.diagram`, or `null`.
97
+ *
98
+ * An edge's trait lives under the same key, so `from` and `to` are what tells
99
+ * the two apart. Without that test an edge reads as a participant declaring
100
+ * no ports, and collects the four defaults on the degenerate pose an edge
101
+ * carries — four grabbable ports in the middle of nowhere. An edge label's
102
+ * `label` trait and an anchored node's `anchor` are the same story, and would
103
+ * also take a slot in every layout. */
104
+ declare const dataKeyReader: DiagramNodeReader;
105
+ /**
106
+ * A reader over declared kinds. A trait on the node's own data still wins, so
107
+ * a single node can opt out of, or override, whatever its kind says.
108
+ *
109
+ * Throws on a duplicate `name`, for the reason `mergeContributions` does: a
110
+ * class of node silently losing its ports is what this registry exists to make
111
+ * loud.
112
+ */
113
+ declare function createDiagramNodes(entries: readonly DiagramNodeEntry[]): DiagramNodeReader;
114
+ /** The trait `node` carries, through `read` (default: its own `data.diagram`). */
115
+ declare function diagramNodeOf(node: DiagramNodeLike, read?: DiagramNodeReader): DiagramNode | null;
116
+ /** True when layout must leave this node where it is. */
117
+ declare function isPinned(node: DiagramNodeLike, read?: DiagramNodeReader): boolean;
118
+
119
+ /**
120
+ * `Graph` — the adjacency index a layout runs over.
121
+ *
122
+ * The scene is the document, so there is no second graph kept in sync with it:
123
+ * this one is built from the scene on demand, used, and thrown away. Rebuilding
124
+ * per layout invocation is deliberate — a maintained index is a cache to
125
+ * invalidate on every add, delete, reparent and `dependsOn` edit, and layout is
126
+ * not on the hot path. Measure before changing that.
127
+ *
128
+ * A node's *identity* is its id and its *size* is its bounds; where it currently
129
+ * sits is here too, because a re-layout that ignores where things already are
130
+ * scrambles a diagram the author has arranged.
131
+ */
132
+
133
+ /** A scene node as the graph reads it. `dependsOn` is what an edge's two
134
+ * endpoints are: the edge trait names *ports*, and the nodes those ports are
135
+ * on are the dependencies the derivation already runs on. */
136
+ interface GraphNodeLike extends DiagramNodeLike {
137
+ dependsOn?: readonly string[] | 'children';
138
+ }
139
+ /** Where the graph gets its nodes. The same thunk shape the port affordance
140
+ * takes, so one source answers both. */
141
+ type GraphSource<TPose> = () => Iterable<{
142
+ node: GraphNodeLike;
143
+ pose: TPose;
144
+ }>;
145
+ /** One participant. */
146
+ interface GraphNode {
147
+ id: string;
148
+ /** Where it is now, in world coordinates. */
149
+ bounds: Bounds;
150
+ /** Layout must not move it. */
151
+ pinned: boolean;
152
+ /** The group it belongs to, when it belongs to one. */
153
+ group?: string;
154
+ }
155
+ /** A box drawn around some participants, which a layout keeps together and
156
+ * leaves room for. */
157
+ interface GraphGroup {
158
+ id: string;
159
+ /** In source order. */
160
+ members: readonly string[];
161
+ /** Room the box keeps around its members, in world units. */
162
+ inset: GroupInset;
163
+ }
164
+ /** One connection. `id` is the edge node's own id, so a caller can go back to
165
+ * the scene for its trait. */
166
+ interface GraphEdge {
167
+ id: string;
168
+ from: string;
169
+ to: string;
170
+ }
171
+ /**
172
+ * Nodes, edges, and the two adjacency reads a layout needs.
173
+ *
174
+ * Every list is in source order — render order, when the source is a scene —
175
+ * which is what a layout's tiebreaks resolve against. There is no RNG anywhere
176
+ * downstream of this, and this is where that starts.
177
+ */
178
+ interface Graph {
179
+ readonly nodes: readonly GraphNode[];
180
+ readonly edges: readonly GraphEdge[];
181
+ /** Absent or empty for a graph with no groups. */
182
+ readonly groups?: readonly GraphGroup[];
183
+ node(id: string): GraphNode | undefined;
184
+ /** Edges leaving `id`, in source order. */
185
+ outgoing(id: string): readonly GraphEdge[];
186
+ /** Edges arriving at `id`, in source order. */
187
+ incoming(id: string): readonly GraphEdge[];
188
+ }
189
+ /** Options for {@link buildGraph}: how the trait is read, and how a pose
190
+ * yields bounds. Both default to the kit's own. */
191
+ interface BuildGraphOptions<TPose> {
192
+ read?: DiagramNodeReader;
193
+ geometry?: PoseDescriptor<TPose>;
194
+ }
195
+ /**
196
+ * Read a graph out of a source of scene nodes.
197
+ *
198
+ * A node is an edge if it carries the edge trait and names exactly two
199
+ * dependencies; a group if its trait carries `group`, with its dependencies as
200
+ * its members; a participant if the reader hands back any other
201
+ * `DiagramNode`; and otherwise not in the diagram at all. An edge whose
202
+ * endpoints are not both participants or groups is dropped — a dangling edge
203
+ * should not be ranking anything — and an edge to a group ranks against its
204
+ * members (see `expandGroupEdges`).
205
+ */
206
+ declare function buildGraph<TPose>(source: GraphSource<TPose>, opts?: BuildGraphOptions<TPose>): Graph;
207
+
208
+ /**
209
+ * Layout: where the participants go.
210
+ *
211
+ * A layout is a pure function of the graph — no scene, no ops, no history. It
212
+ * hands back the new top-left for every node that **moves**, and a node already
213
+ * standing where the layout wants it is simply absent from the answer. That is
214
+ * what makes re-running a layout free rather than destructive, and it is the
215
+ * property the idempotence test asserts.
216
+ *
217
+ * Three rules keep re-layout non-destructive, and all three live here rather
218
+ * than in each algorithm:
219
+ *
220
+ * - **No RNG anywhere.** Every tiebreak falls back to the graph's own node
221
+ * order, which is the source's order — render order, for a scene.
222
+ * - **Within-rank order is seeded from where the nodes already are**, read
223
+ * off the cross axis, rather than from crossing-minimization. An author who
224
+ * dragged two boxes into an order gets that order back.
225
+ * - **A pin set nothing moves.** Pinned nodes are never in the result, and
226
+ * the rest of the layout is translated to sit around them.
227
+ *
228
+ * `layered` and `tree` are idempotent by construction: the second run reads the
229
+ * order the first run produced and computes the same slots. `force` is an
230
+ * iterative relaxation and makes no such claim — re-running it keeps relaxing.
231
+ */
232
+
233
+ /** Which way the ranks stack. `force` ignores it. */
234
+ type LayoutDirection = 'down' | 'up' | 'right' | 'left';
235
+ /** Options every layout takes. */
236
+ interface LayoutOptions {
237
+ /** Default `'down'`. */
238
+ direction?: LayoutDirection;
239
+ /** Between neighbors within a rank, in world units. Default 48. */
240
+ nodeGap?: number;
241
+ /** Between one rank and the next. Default 96. */
242
+ rankGap?: number;
243
+ /** How `layered` orders nodes within a rank. `'seeded'` (default) keeps
244
+ * the order they already sit in; `'barycenter'` reorders to reduce edge
245
+ * crossings, for a generated diagram with no arranged order to keep.
246
+ * Other layouts ignore it. */
247
+ order?: 'seeded' | 'barycenter';
248
+ /** Ids layout must not move, on top of whatever carries `pinned: true`. */
249
+ pin?: Iterable<string>;
250
+ /** How far a node has to move to be worth moving, in world units. Default
251
+ * `1e-6` — a float-noise floor, not a snapping quantum. Raise it to let a
252
+ * `force` pass that has stopped doing anything useful commit nothing. */
253
+ tolerance?: number;
254
+ }
255
+ /** The new top-left for each node that moves. A node that is already there is
256
+ * absent, so an empty result means "nothing to do". */
257
+ type LayoutResult = ReadonlyMap<string, Vec2>;
258
+ /** Every layout in this package has this shape, and so does a consumer's. */
259
+ type LayoutFn = (graph: Graph, opts?: LayoutOptions) => LayoutResult;
260
+ /** `LayoutOptions.nodeGap`'s default, in world units. */
261
+ declare const DEFAULT_NODE_GAP = 48;
262
+ /** `LayoutOptions.rankGap`'s default, in world units. */
263
+ declare const DEFAULT_RANK_GAP = 96;
264
+ /** Which world axis carries ranks, which carries within-rank order, and
265
+ * whether ranks grow toward positive world coordinates. */
266
+ interface LayoutAxes {
267
+ rank: 'x' | 'y';
268
+ cross: 'x' | 'y';
269
+ /** `-1` for `'up'` and `'left'`, where the canonical frame is mirrored. */
270
+ sign: 1 | -1;
271
+ }
272
+ /** The axes a direction lays ranks along. Default `'down'`. */
273
+ declare function axesFor(direction?: LayoutDirection): LayoutAxes;
274
+ /** A node's extent along one axis. */
275
+ declare function extent(bounds: Bounds, axis: 'x' | 'y'): number;
276
+ /** Every id layout must leave alone. */
277
+ declare function pinnedSet(graph: Graph, opts?: LayoutOptions): Set<string>;
278
+ /**
279
+ * A placement in the canonical `(cross, rank)` frame, before it is turned into
280
+ * world coordinates. `cross` and `rank` are the node's low corner along each.
281
+ */
282
+ interface Slot {
283
+ cross: number;
284
+ rank: number;
285
+ }
286
+ /**
287
+ * Turn canonical slots into the positions to write.
288
+ *
289
+ * Three steps, in order: map the canonical frame onto the world axes the
290
+ * direction names; translate the whole layout so it lands where the graph
291
+ * already is; drop everything that is pinned or that would not move.
292
+ *
293
+ * The translation is what makes the layout non-destructive, and it is chosen so
294
+ * that a second run lands on the same answer. With a pin, it is whatever keeps
295
+ * the first pinned node exactly where it is. Without one, it is whatever puts
296
+ * the layout's own bounding box where the graph's bounding box already starts —
297
+ * which the first run then makes true, so the second run computes the same
298
+ * translation and writes nothing.
299
+ */
300
+ declare function settle(graph: Graph, slots: ReadonlyMap<string, Slot>, opts?: LayoutOptions): LayoutResult;
301
+ /**
302
+ * {@link settle}'s second half, for a layout that already works in world
303
+ * coordinates — `force` does, since the integrator moves points around rather
304
+ * than filling slots.
305
+ */
306
+ declare function translated(graph: Graph, placed: ReadonlyMap<string, Vec2>, pinned: ReadonlySet<string>, tolerance?: number): LayoutResult;
307
+ /**
308
+ * Lay a list of nodes out along the cross axis, in the order given, and hand
309
+ * back each one's low corner plus the total span.
310
+ *
311
+ * Shared by `layered`'s ranks and `tree`'s sibling rows, which pack a row the
312
+ * same way and differ only in what decides the order.
313
+ */
314
+ declare function packAcross(nodes: readonly GraphNode[], axis: 'x' | 'y', gap: number): {
315
+ at: Map<string, number>;
316
+ span: number;
317
+ };
318
+ /** Order within a rank: where the nodes already sit on the cross axis, with
319
+ * graph order breaking a tie. Never a comparison that could reverse between
320
+ * two runs on the same input. */
321
+ declare function seededOrder(nodes: readonly GraphNode[], axis: 'x' | 'y', indexOf: (id: string) => number): GraphNode[];
322
+ /** Each node's position in `graph.nodes`, for tiebreaks. */
323
+ declare function graphOrder(graph: Graph): (id: string) => number;
324
+
325
+ /**
326
+ * Clusters: keeping a group's members together through a ranked layout.
327
+ *
328
+ * A group's box is the union of its members plus an inset, so the layout's job
329
+ * is to leave a rectangle for it that nothing else enters. Each group gets a
330
+ * **column** on the cross axis, reserved in every rank from its first member's
331
+ * to its last — including a rank in between that holds none of its members, or
332
+ * an outsider would land inside the box there. Groups keep one cross position
333
+ * across every rank, so two groups' columns never cross and their boxes never
334
+ * overlap. Ungrouped nodes pack around the columns rank by rank, as before.
335
+ *
336
+ * Groups are flat: a group is not a member of another group.
337
+ */
338
+
339
+ /** Room a group's box keeps around its members, in world units. */
340
+ interface GroupInset {
341
+ top: number;
342
+ right: number;
343
+ bottom: number;
344
+ left: number;
345
+ }
346
+ /** A `DiagramGroup.inset` as all four sides. Default 12 on every side. */
347
+ declare function insetOf(inset: number | Partial<GroupInset> | undefined): GroupInset;
348
+ /** `DiagramGroup.inset`'s default on each side, in world units. */
349
+ declare const DEFAULT_GROUP_INSET = 12;
350
+ /**
351
+ * The gap between each rank and the next, widened so a group's box fits: the
352
+ * widest inset of a group ending at rank `i` and of one starting at `i + 1`
353
+ * both come on top of `rankGap`, since those two boxes can face each other
354
+ * across it. `rows` is the nodes of each rank.
355
+ */
356
+ declare function rankGaps(graph: Graph, rows: readonly (readonly GraphNode[])[], axes: LayoutAxes, rankGap: number): number[];
357
+ /**
358
+ * Cross positions for every node, with each group's members kept in a column
359
+ * of their own. `preferred` is where the layout would put each node's low
360
+ * corner with no groups at all; nodes and columns keep that order and are only
361
+ * ever pushed toward the high end, by as much as it takes to stop overlapping.
362
+ */
363
+ declare function packClusters(graph: Graph, rows: readonly (readonly GraphNode[])[], preferred: ReadonlyMap<string, number>, axes: LayoutAxes, nodeGap: number): Map<string, number>;
364
+ /**
365
+ * Edges as a layout should rank by: one into a group arrives at its entry
366
+ * members — those no other member points at — and one out of it leaves from its
367
+ * exit members. A group with no such member, a pure cycle, is all entries and
368
+ * all exits. An edge between a group and one of its own members, or two
369
+ * members of one group, is a group's internal business and ranks nothing
370
+ * beyond what its member-to-member edges already do.
371
+ */
372
+ declare function expandGroupEdges(edges: readonly GraphEdge[], groups: readonly GraphGroup[]): GraphEdge[];
373
+
56
374
  /**
57
375
  * Where a port sits on its node, in **normalized bounds coordinates**:
58
376
  * `(0, 0)` is the top-left corner of the node's AABB and `(1, 1)` the
@@ -95,6 +413,17 @@ interface DiagramNode {
95
413
  * participant that already had a look of its own — a text block, an image,
96
414
  * a path — which keeps whatever painter it already matched. */
97
415
  outline?: Outline;
416
+ /** Present on a group's box: a node drawn around the participants it
417
+ * depends on, which layout keeps together and leaves room for. The box
418
+ * itself is never laid out — it derives its pose from its members — but an
419
+ * edge can attach to it like any participant. */
420
+ group?: DiagramGroup;
421
+ }
422
+ /** The group half of a {@link DiagramNode}. */
423
+ interface DiagramGroup {
424
+ /** Room the box keeps around its members, in world units: one number for
425
+ * every side, or per side. Default 12 on each. */
426
+ inset?: number | Partial<GroupInset>;
98
427
  }
99
428
  /** A port resolved against a node's current pose: where it is in world
100
429
  * coordinates, and which way an edge leaves it. */
@@ -165,6 +494,11 @@ interface BodySpec {
165
494
  padding?: number;
166
495
  /** Space between adjacent rows. Default 4. */
167
496
  gap?: number;
497
+ /** Where the rows sit as a block when the body is taller than they need. Default `'center'`. */
498
+ verticalAlign?: 'top' | 'center' | 'bottom';
499
+ /** The perimeter ports, replacing the four compass midpoints. A `ports`
500
+ * row's own ports are added either way. */
501
+ ports?: readonly PortSpec[];
168
502
  }
169
503
  /** What {@link buildBody} emits: the `AddNodeSpec` fields it fills in. Named
170
504
  * structurally rather than imported so a consumer whose scene is typed
@@ -221,8 +555,8 @@ declare function measureBody(spec: BodySpec, measure?: MeasureRowText): BodyFloo
221
555
  */
222
556
  declare function sizeToBody<TPose extends Bounds>(pose: TPose, floor: BodyFloor): TPose;
223
557
  /** Where each row sits inside `bounds`, top to bottom. Rows keep their
224
- * measured heights; the leftover goes unclaimed at the bottom rather than
225
- * being distributed, so a row does not move when a sibling grows. */
558
+ * measured heights rather than sharing out the leftover; `verticalAlign`
559
+ * places the block of them within it. */
226
560
  declare function layoutBody(spec: BodySpec, bounds: Bounds, measure?: MeasureRowText): RowBox[];
227
561
  /**
228
562
  * Where each of a `ports` row's ports goes inside `box`, on the same terms
@@ -298,67 +632,6 @@ declare function buildBody<TData, TLayer extends string, TPose extends Bounds>(s
298
632
  specs: BodyNodeSpec<TData, TLayer, TPose>[];
299
633
  };
300
634
 
301
- /**
302
- * Reading the `DiagramNode` trait off a scene node.
303
- *
304
- * Two ways to attach it, both of which this module's default reader answers:
305
- *
306
- * - **On the node.** `data.diagram` holds the trait. One node, one answer;
307
- * what a connect gesture writes when the author drags a new edge.
308
- * - **By kind.** A consumer declares once which data shapes take part, and
309
- * every matching node gets the trait without being stamped. This mirrors
310
- * `createNodeRouting` in the kit — a predicate over `data`, first match
311
- * wins — and is the shape to reach for when the participants are a class
312
- * of node rather than a hand-picked set.
313
- *
314
- * A reader is a plain function, so a consumer whose data lives somewhere else
315
- * entirely passes their own and never touches either form.
316
- */
317
-
318
- /** The key on a node's `data` the default reader looks under. */
319
- declare const DIAGRAM_TRAIT_KEY = "diagram";
320
- /** All a reader looks at. Deliberately not `SceneNode`: naming `TPose` there
321
- * would put it in a contravariant position for no gain, since nothing here
322
- * reads a pose. */
323
- interface DiagramNodeLike {
324
- id: string;
325
- kind: 'leaf' | 'container';
326
- data: unknown;
327
- }
328
- /** Answers "is this node a diagram participant, and on what terms" — `null`
329
- * for a node that takes no part. */
330
- type DiagramNodeReader = (node: DiagramNodeLike) => DiagramNode | null;
331
- /** One class of participant, declared once for every node whose data matches.
332
- * Mirrors `NodeRoutingEntry`. */
333
- interface DiagramNodeEntry {
334
- /** Unique within a reader; the same vocabulary as `NodeRoutingEntry.name`. */
335
- name: string;
336
- matches: (data: unknown) => boolean;
337
- /** The trait every matching node carries. A function when it varies with the
338
- * node's own data — a pipeline stage whose ports come from its rows. */
339
- trait: DiagramNode | ((data: unknown) => DiagramNode);
340
- }
341
- /** The trait on the node's own `data.diagram`, or `null`.
342
- *
343
- * An edge's trait lives under the same key, so `from` and `to` are what tells
344
- * the two apart. Without that test an edge reads as a participant declaring
345
- * no ports, and collects the four defaults on the degenerate pose an edge
346
- * carries — four grabbable ports in the middle of nowhere. */
347
- declare const dataKeyReader: DiagramNodeReader;
348
- /**
349
- * A reader over declared kinds. A trait on the node's own data still wins, so
350
- * a single node can opt out of, or override, whatever its kind says.
351
- *
352
- * Throws on a duplicate `name`, for the reason `mergeContributions` does: a
353
- * class of node silently losing its ports is what this registry exists to make
354
- * loud.
355
- */
356
- declare function createDiagramNodes(entries: readonly DiagramNodeEntry[]): DiagramNodeReader;
357
- /** The trait `node` carries, through `read` (default: its own `data.diagram`). */
358
- declare function diagramNodeOf(node: DiagramNodeLike, read?: DiagramNodeReader): DiagramNode | null;
359
- /** True when layout must leave this node where it is. */
360
- declare function isPinned(node: DiagramNodeLike, read?: DiagramNodeReader): boolean;
361
-
362
635
  /**
363
636
  * Where a node's ports are, in world coordinates.
364
637
  *
@@ -429,197 +702,6 @@ declare function portsOf<TPose>(node: DiagramNodeLike, pose: TPose, opts?: Ports
429
702
  /** One named port, or `undefined` when the node has no such port. */
430
703
  declare function portOf<TPose>(node: DiagramNodeLike, pose: TPose, portId: string, opts?: PortsOptions<TPose>): Port | undefined;
431
704
 
432
- /**
433
- * `Graph` — the adjacency index a layout runs over.
434
- *
435
- * The scene is the document, so there is no second graph kept in sync with it:
436
- * this one is built from the scene on demand, used, and thrown away. Rebuilding
437
- * per layout invocation is deliberate — a maintained index is a cache to
438
- * invalidate on every add, delete, reparent and `dependsOn` edit, and layout is
439
- * not on the hot path. Measure before changing that.
440
- *
441
- * A node's *identity* is its id and its *size* is its bounds; where it currently
442
- * sits is here too, because a re-layout that ignores where things already are
443
- * scrambles a diagram the author has arranged.
444
- */
445
-
446
- /** A scene node as the graph reads it. `dependsOn` is what an edge's two
447
- * endpoints are: the edge trait names *ports*, and the nodes those ports are
448
- * on are the dependencies the derivation already runs on. */
449
- interface GraphNodeLike extends DiagramNodeLike {
450
- dependsOn?: readonly string[] | 'children';
451
- }
452
- /** Where the graph gets its nodes. The same thunk shape the port affordance
453
- * takes, so one source answers both. */
454
- type GraphSource<TPose> = () => Iterable<{
455
- node: GraphNodeLike;
456
- pose: TPose;
457
- }>;
458
- /** One participant. */
459
- interface GraphNode {
460
- id: string;
461
- /** Where it is now, in world coordinates. */
462
- bounds: Bounds;
463
- /** Layout must not move it. */
464
- pinned: boolean;
465
- }
466
- /** One connection. `id` is the edge node's own id, so a caller can go back to
467
- * the scene for its trait. */
468
- interface GraphEdge {
469
- id: string;
470
- from: string;
471
- to: string;
472
- }
473
- /**
474
- * Nodes, edges, and the two adjacency reads a layout needs.
475
- *
476
- * Every list is in source order — render order, when the source is a scene —
477
- * which is what a layout's tiebreaks resolve against. There is no RNG anywhere
478
- * downstream of this, and this is where that starts.
479
- */
480
- interface Graph {
481
- readonly nodes: readonly GraphNode[];
482
- readonly edges: readonly GraphEdge[];
483
- node(id: string): GraphNode | undefined;
484
- /** Edges leaving `id`, in source order. */
485
- outgoing(id: string): readonly GraphEdge[];
486
- /** Edges arriving at `id`, in source order. */
487
- incoming(id: string): readonly GraphEdge[];
488
- }
489
- /** Options for {@link buildGraph}: how the trait is read, and how a pose
490
- * yields bounds. Both default to the kit's own. */
491
- interface BuildGraphOptions<TPose> {
492
- read?: DiagramNodeReader;
493
- geometry?: PoseDescriptor<TPose>;
494
- }
495
- /**
496
- * Read a graph out of a source of scene nodes.
497
- *
498
- * A node is an edge if it carries the edge trait and names exactly two
499
- * dependencies; a participant if the reader hands back a `DiagramNode`;
500
- * neither, and it is not in the diagram at all. An edge whose endpoints are not
501
- * both participants is dropped — a dangling edge should not be ranking
502
- * anything.
503
- */
504
- declare function buildGraph<TPose>(source: GraphSource<TPose>, opts?: BuildGraphOptions<TPose>): Graph;
505
-
506
- /**
507
- * Layout: where the participants go.
508
- *
509
- * A layout is a pure function of the graph — no scene, no ops, no history. It
510
- * hands back the new top-left for every node that **moves**, and a node already
511
- * standing where the layout wants it is simply absent from the answer. That is
512
- * what makes re-running a layout free rather than destructive, and it is the
513
- * property the idempotence test asserts.
514
- *
515
- * Three rules keep re-layout non-destructive, and all three live here rather
516
- * than in each algorithm:
517
- *
518
- * - **No RNG anywhere.** Every tiebreak falls back to the graph's own node
519
- * order, which is the source's order — render order, for a scene.
520
- * - **Within-rank order is seeded from where the nodes already are**, read
521
- * off the cross axis, rather than from crossing-minimization. An author who
522
- * dragged two boxes into an order gets that order back.
523
- * - **A pin set nothing moves.** Pinned nodes are never in the result, and
524
- * the rest of the layout is translated to sit around them.
525
- *
526
- * `layered` and `tree` are idempotent by construction: the second run reads the
527
- * order the first run produced and computes the same slots. `force` is an
528
- * iterative relaxation and makes no such claim — re-running it keeps relaxing.
529
- */
530
-
531
- /** Which way the ranks stack. `force` ignores it. */
532
- type LayoutDirection = 'down' | 'up' | 'right' | 'left';
533
- /** Options every layout takes. */
534
- interface LayoutOptions {
535
- /** Default `'down'`. */
536
- direction?: LayoutDirection;
537
- /** Between neighbors within a rank, in world units. Default 48. */
538
- nodeGap?: number;
539
- /** Between one rank and the next. Default 96. */
540
- rankGap?: number;
541
- /** How `layered` orders nodes within a rank. `'seeded'` (default) keeps
542
- * the order they already sit in; `'barycenter'` reorders to reduce edge
543
- * crossings, for a generated diagram with no arranged order to keep.
544
- * Other layouts ignore it. */
545
- order?: 'seeded' | 'barycenter';
546
- /** Ids layout must not move, on top of whatever carries `pinned: true`. */
547
- pin?: Iterable<string>;
548
- /** How far a node has to move to be worth moving, in world units. Default
549
- * `1e-6` — a float-noise floor, not a snapping quantum. Raise it to let a
550
- * `force` pass that has stopped doing anything useful commit nothing. */
551
- tolerance?: number;
552
- }
553
- /** The new top-left for each node that moves. A node that is already there is
554
- * absent, so an empty result means "nothing to do". */
555
- type LayoutResult = ReadonlyMap<string, Vec2>;
556
- /** Every layout in this package has this shape, and so does a consumer's. */
557
- type LayoutFn = (graph: Graph, opts?: LayoutOptions) => LayoutResult;
558
- /** `LayoutOptions.nodeGap`'s default, in world units. */
559
- declare const DEFAULT_NODE_GAP = 48;
560
- /** `LayoutOptions.rankGap`'s default, in world units. */
561
- declare const DEFAULT_RANK_GAP = 96;
562
- /** Which world axis carries ranks, which carries within-rank order, and
563
- * whether ranks grow toward positive world coordinates. */
564
- interface LayoutAxes {
565
- rank: 'x' | 'y';
566
- cross: 'x' | 'y';
567
- /** `-1` for `'up'` and `'left'`, where the canonical frame is mirrored. */
568
- sign: 1 | -1;
569
- }
570
- /** The axes a direction lays ranks along. Default `'down'`. */
571
- declare function axesFor(direction?: LayoutDirection): LayoutAxes;
572
- /** A node's extent along one axis. */
573
- declare function extent(bounds: Bounds, axis: 'x' | 'y'): number;
574
- /** Every id layout must leave alone. */
575
- declare function pinnedSet(graph: Graph, opts?: LayoutOptions): Set<string>;
576
- /**
577
- * A placement in the canonical `(cross, rank)` frame, before it is turned into
578
- * world coordinates. `cross` and `rank` are the node's low corner along each.
579
- */
580
- interface Slot {
581
- cross: number;
582
- rank: number;
583
- }
584
- /**
585
- * Turn canonical slots into the positions to write.
586
- *
587
- * Three steps, in order: map the canonical frame onto the world axes the
588
- * direction names; translate the whole layout so it lands where the graph
589
- * already is; drop everything that is pinned or that would not move.
590
- *
591
- * The translation is what makes the layout non-destructive, and it is chosen so
592
- * that a second run lands on the same answer. With a pin, it is whatever keeps
593
- * the first pinned node exactly where it is. Without one, it is whatever puts
594
- * the layout's own bounding box where the graph's bounding box already starts —
595
- * which the first run then makes true, so the second run computes the same
596
- * translation and writes nothing.
597
- */
598
- declare function settle(graph: Graph, slots: ReadonlyMap<string, Slot>, opts?: LayoutOptions): LayoutResult;
599
- /**
600
- * {@link settle}'s second half, for a layout that already works in world
601
- * coordinates — `force` does, since the integrator moves points around rather
602
- * than filling slots.
603
- */
604
- declare function translated(graph: Graph, placed: ReadonlyMap<string, Vec2>, pinned: ReadonlySet<string>, tolerance?: number): LayoutResult;
605
- /**
606
- * Lay a list of nodes out along the cross axis, in the order given, and hand
607
- * back each one's low corner plus the total span.
608
- *
609
- * Shared by `layered`'s ranks and `tree`'s sibling rows, which pack a row the
610
- * same way and differ only in what decides the order.
611
- */
612
- declare function packAcross(nodes: readonly GraphNode[], axis: 'x' | 'y', gap: number): {
613
- at: Map<string, number>;
614
- span: number;
615
- };
616
- /** Order within a rank: where the nodes already sit on the cross axis, with
617
- * graph order breaking a tie. Never a comparison that could reverse between
618
- * two runs on the same input. */
619
- declare function seededOrder(nodes: readonly GraphNode[], axis: 'x' | 'y', indexOf: (id: string) => number): GraphNode[];
620
- /** Each node's position in `graph.nodes`, for tiebreaks. */
621
- declare function graphOrder(graph: Graph): (id: string) => number;
622
-
623
705
  /**
624
706
  * `force` — relaxation, for a graph with no direction to read it in.
625
707
  *
@@ -704,25 +786,6 @@ declare function ranksOf(graph: Graph, back: ReadonlySet<string>): Map<string, n
704
786
  * moves nothing — except under `order: 'barycenter'`, which reorders them. */
705
787
  declare const layered: LayoutFn;
706
788
 
707
- /**
708
- * `tree` — a parent centered over the block its children occupy.
709
- *
710
- * The hierarchy layout: org charts, file trees, decision trees. Depth is the
711
- * rank axis, so every child sits one band past its parent, and a subtree is
712
- * packed as a solid block so two siblings' descendants never interleave.
713
- *
714
- * A graph that is not a tree still lays out. Roots are the nodes nothing points
715
- * at, in graph order; a node reachable from two parents belongs to whichever
716
- * the depth-first walk reaches first; and anything the walk never reaches — a
717
- * disconnected part, or a pure cycle — becomes a root of its own. So the answer
718
- * is always a forest, and which forest depends only on the node order.
719
- *
720
- * The block packing is deliberately not Reingold–Tilford: it never slides a
721
- * deep subtree under a shallow neighbor's overhang. That costs horizontal
722
- * space and buys an invariant an author can rely on — a subtree's extent is a
723
- * rectangle, and dragging one never lands it inside another.
724
- */
725
-
726
789
  interface Forest {
727
790
  roots: GraphNode[];
728
791
  children: Map<string, GraphNode[]>;
@@ -762,4 +825,4 @@ declare function rayHit(poly: readonly number[], center: {
762
825
  */
763
826
  declare function portsOnOutline(ports: readonly Port[], outline: Outline, bounds: Bounds): Port[];
764
827
 
765
- export { force as $, type RowNodeData as A, type BodyFloor as B, COMPASS as C, type DiagramNodeLike as D, type RowPort as E, type ForceOptions as F, type GraphSource as G, type RowPortBox as H, type RowTextStyle as I, axesFor as J, backEdges as K, type LayoutFn as L, type MeasureRowText as M, bodyOutline as N, type Outline as O, type Port as P, bodyTrait as Q, type Row as R, type Slot as S, boxForContent as T, buildBody as U, buildGraph as V, canvasMeasure as W, contentBox as X, createDiagramNodes as Y, dataKeyReader as Z, diagramNodeOf as _, type PortsOptions as a, forceRelaxation as a0, forestOf as a1, graphOrder as a2, isPinned as a3, layered as a4, layoutBody as a5, layoutRowPorts as a6, measureBody as a7, outlinePath as a8, outlinePolyline as a9, packAcross as aa, pinnedSet as ab, portOf as ac, portsOf as ad, portsOnOutline as ae, ranksOf as af, rayHit as ag, seededOrder as ah, settle as ai, sizeToBody as aj, translated as ak, tree as al, extent as am, type DiagramNodeReader as b, type DiagramNode as c, type LayoutOptions as d, type LayoutResult as e, type Graph as f, type BodyNodeSpec as g, type BodySpec as h, type Bounds as i, type BuildBodyOptions as j, type BuildGraphOptions as k, DEFAULT_NODE_GAP as l, DEFAULT_PORTS as m, DEFAULT_RANK_GAP as n, DIAGRAM_TRAIT_KEY as o, type DiagramNodeEntry as p, type ForceBody as q, type ForceRelaxation as r, type GraphEdge as s, type GraphNode as t, type GraphNodeLike as u, type LayoutAxes as v, type LayoutDirection as w, type PortAnchor as x, type PortSpec as y, type RowBox as z };
828
+ export { contentBox as $, type GroupInset as A, type BodySpec as B, COMPASS as C, type DiagramNodeLike as D, type LayoutAxes as E, type ForceOptions as F, type GraphSource as G, type LayoutDirection as H, type RowBox as I, type RowNodeData as J, type RowPort as K, type LayoutFn as L, type MeasureRowText as M, type RowPortBox as N, type Outline as O, type Port as P, type RowTextStyle as Q, type Row as R, type Slot as S, axesFor as T, backEdges as U, bodyOutline as V, bodyTrait as W, boxForContent as X, buildBody as Y, buildGraph as Z, canvasMeasure as _, type PortsOptions as a, createDiagramNodes as a0, dataKeyReader as a1, diagramNodeOf as a2, expandGroupEdges as a3, force as a4, forceRelaxation as a5, forestOf as a6, graphOrder as a7, insetOf as a8, isPinned as a9, layered as aa, layoutBody as ab, layoutRowPorts as ac, measureBody as ad, outlinePath as ae, outlinePolyline as af, packAcross as ag, packClusters as ah, pinnedSet as ai, portOf as aj, portsOf as ak, portsOnOutline as al, rankGaps as am, ranksOf as an, rayHit as ao, seededOrder as ap, settle as aq, sizeToBody as ar, translated as as, tree as at, extent as au, type DiagramNodeReader as b, type PortAnchor as c, type DiagramGroup as d, type PortSpec as e, type DiagramNode as f, type LayoutOptions as g, type LayoutResult as h, type Graph as i, type BodyFloor as j, type BodyNodeSpec as k, type Bounds as l, type BuildBodyOptions as m, type BuildGraphOptions as n, DEFAULT_GROUP_INSET as o, DEFAULT_NODE_GAP as p, DEFAULT_PORTS as q, DEFAULT_RANK_GAP as r, DIAGRAM_TRAIT_KEY as s, type DiagramNodeEntry as t, type ForceBody as u, type ForceRelaxation as v, type GraphEdge as w, type GraphGroup as x, type GraphNode as y, type GraphNodeLike as z };