@weasel-js/diagram 1.4.4 → 1.5.1

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.
package/dist/index.d.ts CHANGED
@@ -1,425 +1,7 @@
1
- import * as _weasel_js_core from '@weasel-js/core';
2
- import { Path, Vec2, PoseProjection, DerivedDep, SceneRegistry, ChromeState, FillStyle, Stroke, CursorSpec, Affordance, InvocationCtx, GestureBinding, Action, Contribution, RenderLayer, SimulationNode, createSimulation, EasingFn, NodeId, Scene, NodeShapeEntry } from '@weasel-js/core';
3
-
4
- /**
5
- * The shapes a built body can wear, as paths in the node's bounds.
6
- *
7
- * These are the flowchart vocabulary — a decision is a diamond, an I/O step a
8
- * parallelogram, a terminator a stadium — plus `'rect'` for everything else and
9
- * a `Path` escape hatch for a shape the vocabulary does not have. The path is
10
- * built in world coordinates from the bounds it is given, the same way the
11
- * kit's own `clipFromPose` factories are.
12
- */
13
-
14
- /** An axis-aligned box, in the shape `PoseProjection.getBounds` returns. */
15
- interface Bounds {
16
- x: number;
17
- y: number;
18
- width: number;
19
- height: number;
20
- }
21
- /** A named flowchart shape, or a path a consumer built themselves. */
22
- type Outline = 'rect' | 'diamond' | 'stadium' | 'parallelogram' | Path;
23
- /** The path `outline` describes inside `bounds`. A zero-area box gives a
24
- * zero-area rect rather than a degenerate polygon. */
25
- declare function outlinePath(outline: Outline, bounds: Bounds): Path;
26
- /**
27
- * The largest axis-aligned box a shape's content can occupy without leaving
28
- * the shape. Rows lay out in here, not in the bounds — a diamond's corners and
29
- * a parallelogram's lean are outside its own box, and a label placed against
30
- * the bounding box lands there and gets clipped away by the silhouette.
31
- *
32
- * Conservative rather than exact: the inscribed rect of a rhombus is exact,
33
- * and a stadium reports the flat span between its rounded ends, which gives up
34
- * a little height near them.
35
- */
36
- declare function contentBox(outline: Outline, bounds: Bounds): Bounds;
37
- /**
38
- * The box whose {@link contentBox} is at least `content` — the inverse, for
39
- * sizing a node to what its rows measured.
40
- *
41
- * The stadium case is the only inexact one: its inset depends on the height it
42
- * is solving for, so it assumes the wide orientation and adds one height's
43
- * worth of end caps.
44
- */
45
- declare function boxForContent(outline: Outline, content: {
46
- width: number;
47
- height: number;
48
- }): {
49
- width: number;
50
- height: number;
51
- };
52
-
53
- /**
54
- * Where a port sits on its node, in **normalized bounds coordinates**:
55
- * `(0, 0)` is the top-left corner of the node's AABB and `(1, 1)` the
56
- * bottom-right. `COMPASS` names the eight positions almost everything wants.
57
- *
58
- * Normalized rather than absolute because a node's size is authored data that
59
- * resize, align and distribute all write — a port fixed at an offset would
60
- * drift off the shape the first time one of them ran.
61
- */
62
- interface PortAnchor {
63
- u: number;
64
- v: number;
65
- }
66
- /** One attachment point declared on a node. */
67
- interface PortSpec {
68
- /** Unique within the node. Edges name this, so it outlives a re-layout. */
69
- id: string;
70
- at: PortAnchor;
71
- /** Consulted by a connect gesture's validity predicate. Uninterpreted here. */
72
- type?: string;
73
- /** The direction an edge leaves along, overriding the one the anchor
74
- * implies. `null` means "no preferred direction" — a router goes straight
75
- * at it. */
76
- normal?: Vec2 | null;
77
- }
78
- /**
79
- * The trait that makes an existing scene node a diagram participant.
80
- *
81
- * Attached to a node's `data`, not a node kind the plugin mints: a text block,
82
- * an image, a path, a group or a plain rect all take part by carrying this,
83
- * and nothing has to be authored through this package to be connectable.
84
- */
85
- interface DiagramNode {
86
- /** Defaults to {@link DEFAULT_PORTS} — the four edge midpoints of the
87
- * node's own bounds, which every node has. */
88
- ports?: readonly PortSpec[];
89
- /** Layout must not move this node. */
90
- pinned?: boolean;
91
- /** What the node is *drawn* as. Present on a built body; absent on a
92
- * participant that already had a look of its own — a text block, an image,
93
- * a path — which keeps whatever painter it already matched. */
94
- outline?: Outline;
95
- }
96
- /** A port resolved against a node's current pose: where it is in world
97
- * coordinates, and which way an edge leaves it. */
98
- interface Port {
99
- id: string;
100
- /** The node this port belongs to. */
101
- nodeId: string;
102
- /** World point an edge attaches to. */
103
- point: Vec2;
104
- /** Unit vector pointing away from the node, or `null` when the port
105
- * declared no direction. Rotated with the node. */
106
- normal: Vec2 | null;
107
- type?: string;
108
- }
109
-
110
- /** One line of body content. */
111
- type Row =
112
- /** Text. `label` is the node's name; `field` is a named value. */
113
- {
114
- kind: 'label';
115
- text: string;
116
- style?: RowTextStyle;
117
- } | {
118
- kind: 'field';
119
- label: string;
120
- value: string;
121
- style?: RowTextStyle;
122
- }
123
- /** A row that carries ports on its own left and right edges — the
124
- * visual-programming shape, where an operator's inputs line up with the
125
- * rows they feed. */
126
- | {
127
- kind: 'ports';
128
- left?: readonly RowPort[];
129
- right?: readonly RowPort[];
130
- height?: number;
131
- }
132
- /** Reserved space a consumer paints into. */
133
- | {
134
- kind: 'slot';
135
- height: number;
136
- id?: string;
137
- };
138
- /** A port declared on a row rather than on the node's perimeter. */
139
- interface RowPort {
140
- id: string;
141
- /** Label shown beside it, and what the row measures against. */
142
- label?: string;
143
- type?: string;
144
- }
145
- /** The subset of a text style the body measures against. Deliberately small:
146
- * a full `ResolvedTextStyle` belongs to whatever paints the row. */
147
- interface RowTextStyle {
148
- fontSize?: number;
149
- fontFamily?: string;
150
- bold?: boolean;
151
- }
152
- /** Measures one run of text. Returns its advance width and its line height. */
153
- type MeasureRowText = (text: string, style: RowTextStyle) => {
154
- width: number;
155
- height: number;
156
- };
157
- interface BodySpec {
158
- outline: Outline;
159
- rows: readonly Row[];
160
- /** Space between the outline and its rows. Default 8. */
161
- padding?: number;
162
- /** Space between adjacent rows. Default 4. */
163
- gap?: number;
164
- }
165
- /** What {@link buildBody} emits: the `AddNodeSpec` fields it fills in. Named
166
- * structurally rather than imported so a consumer whose scene is typed
167
- * differently can still spread one. */
168
- interface BodyNodeSpec<TData, TLayer extends string, TPose> {
169
- kind: 'container' | 'leaf';
170
- layer: TLayer;
171
- pose: TPose;
172
- data: TData;
173
- id?: string;
174
- parent?: string;
175
- /** Set `false` on every leaf the builder emits: the innermost hit wins, so a
176
- * row that answers a press is a body that cannot be dragged. */
177
- pickable?: boolean;
178
- }
179
- /** The floor a body's content puts under its node's size. */
180
- interface BodyFloor {
181
- minWidth: number;
182
- minHeight: number;
183
- }
184
- /** Where one row landed, in the same frame as the bounds it was laid out in.
185
- * A `ports` row's own ports anchor to this box. */
186
- interface RowBox extends Bounds {
187
- row: Row;
188
- index: number;
189
- }
190
- /** Where one row port's label sits, in the same frame as the row box it was
191
- * laid out in. Its vertical center is also where the port itself attaches, so
192
- * a label and the port it names cannot drift apart. */
193
- interface RowPortBox extends Bounds {
194
- port: RowPort;
195
- side: 'left' | 'right';
196
- /** Position within its own side, top to bottom. */
197
- index: number;
198
- }
199
- /** Adapts the kit's `measureText` for a caller holding a 2D context. Pass the
200
- * same context the renderer measures with, or the floor will not match what
201
- * is painted. */
202
- declare function canvasMeasure(ctx: {
203
- measureText(text: string): {
204
- width: number;
205
- };
206
- font: string;
207
- }): MeasureRowText;
208
- /**
209
- * The smallest box `spec`'s rows fit in. A node's pose is maxed against this,
210
- * never set to it.
211
- */
212
- declare function measureBody(spec: BodySpec, measure?: MeasureRowText): BodyFloor;
213
- /**
214
- * The authored size, grown to fit the body. Never shrinks: a row removed
215
- * leaves the node the size the author last saw it at, which is the only
216
- * behavior that survives a resize the author did on purpose.
217
- */
218
- declare function sizeToBody<TPose extends Bounds>(pose: TPose, floor: BodyFloor): TPose;
219
- /** Where each row sits inside `bounds`, top to bottom. Rows keep their
220
- * measured heights; the leftover goes unclaimed at the bottom rather than
221
- * being distributed, so a row does not move when a sibling grows. */
222
- declare function layoutBody(spec: BodySpec, bounds: Bounds, measure?: MeasureRowText): RowBox[];
223
- /**
224
- * Where each of a `ports` row's ports goes inside `box`, on the same terms
225
- * `layoutBody` gives one box per row.
226
- *
227
- * Both sides are cut into the same number of slots — the longer side's count —
228
- * so the nth input faces the nth output whichever side has more. A label box
229
- * is as wide as its own text and pinned to its own edge of the row; an
230
- * unlabeled port gets a zero-width box there, which is still where it attaches.
231
- */
232
- declare function layoutRowPorts(row: Extract<Row, {
233
- kind: 'ports';
234
- }>, box: Bounds, measure?: MeasureRowText): RowPortBox[];
235
- /**
236
- * The `DiagramNode` trait a built body implies: perimeter ports for the
237
- * outline, plus one port per `ports`-row entry, anchored to that row's own
238
- * left or right edge.
239
- *
240
- * Anchors are normalized against `bounds`, so they survive the node being
241
- * resized — which is the whole reason `PortAnchor` is normalized.
242
- *
243
- * **A row port on a side takes that side's compass default with it.** Both sit
244
- * on the same edge, and a row near the vertical middle puts one exactly on top
245
- * of `w` or `e` — where the later region wins the hit and the other is
246
- * grabbable nowhere. A body that declares where its inputs attach has said what
247
- * that side is for.
248
- */
249
- declare function bodyTrait(spec: BodySpec, bounds: Bounds, measure?: MeasureRowText): DiagramNode;
250
- /** The outline path for a built body at `bounds` — what paints it, and what a
251
- * perimeter-hugging port will eventually be placed on. */
252
- declare function bodyOutline(spec: BodySpec, bounds: Bounds): _weasel_js_core.Path;
253
- /** What a built body's rows are handed to, one call per row.
254
- *
255
- * `text` is the row's content already formatted — a `label`'s own text, or a
256
- * `field`'s `"label: value"` — so the common case needs no discrimination.
257
- * Return `null` for a row the consumer paints itself; `ports` and `slot` rows
258
- * arrive with `text` empty and are the usual ones to decline. */
259
- type RowNodeData<TData> = (text: string, box: RowBox) => TData | null;
260
- interface BuildBodyOptions<TData, TLayer extends string, TPose extends Bounds> {
261
- layer: TLayer;
262
- /** The container's id. Edges name it, so pass one for anything an edge or a
263
- * layout `pin` will refer to. */
264
- id?: string;
265
- measure?: MeasureRowText;
266
- /** The container's own data, given the trait the builder computed and the
267
- * pose it was computed against. */
268
- body: (trait: DiagramNode, pose: TPose) => TData;
269
- /** One row's data, or `null` to leave that row undrawn. */
270
- row: RowNodeData<TData>;
271
- /** One row port's label, or `null` to leave it undrawn. Ports with no
272
- * `label` are never offered. Omit to draw none — a body whose ports are
273
- * named only for the width they claim needs nothing here. */
274
- portLabel?: (text: string, box: RowPortBox) => TData | null;
275
- }
276
- /**
277
- * The scene nodes a built body is: one container carrying the trait, and one
278
- * leaf per row that wants drawing.
279
- *
280
- * Rows are ordinary scene nodes rather than something this package paints, so
281
- * the kit's own text painter draws them and text editing, styling and
282
- * selection work on them unchanged. The walk that places them is
283
- * `layoutBody` — the same one `bodyTrait` anchors its row ports against, which
284
- * is why a label and its ports cannot drift apart.
285
- *
286
- * The pose is `sizeToBody(at, measureBody(spec))`: the body measures a floor
287
- * and the authored size is grown to clear it, never shrunk.
288
- */
289
- declare function buildBody<TData, TLayer extends string, TPose extends Bounds>(spec: BodySpec, at: TPose, opts: BuildBodyOptions<TData, TLayer, TPose>): {
290
- pose: TPose;
291
- specs: BodyNodeSpec<TData, TLayer, TPose>[];
292
- };
293
-
294
- /**
295
- * Reading the `DiagramNode` trait off a scene node.
296
- *
297
- * Two ways to attach it, both of which this module's default reader answers:
298
- *
299
- * - **On the node.** `data.diagram` holds the trait. One node, one answer;
300
- * what a connect gesture writes when the author drags a new edge.
301
- * - **By kind.** A consumer declares once which data shapes take part, and
302
- * every matching node gets the trait without being stamped. This mirrors
303
- * `createNodeRouting` in the kit — a predicate over `data`, first match
304
- * wins — and is the shape to reach for when the participants are a class
305
- * of node rather than a hand-picked set.
306
- *
307
- * A reader is a plain function, so a consumer whose data lives somewhere else
308
- * entirely passes their own and never touches either form.
309
- */
310
-
311
- /** The key on a node's `data` the default reader looks under. */
312
- declare const DIAGRAM_TRAIT_KEY = "diagram";
313
- /** All a reader looks at. Deliberately not `SceneNode`: naming `TPose` there
314
- * would put it in a contravariant position for no gain, since nothing here
315
- * reads a pose. */
316
- interface DiagramNodeLike {
317
- id: string;
318
- kind: 'leaf' | 'container';
319
- data: unknown;
320
- }
321
- /** Answers "is this node a diagram participant, and on what terms" — `null`
322
- * for a node that takes no part. */
323
- type DiagramNodeReader = (node: DiagramNodeLike) => DiagramNode | null;
324
- /** One class of participant, declared once for every node whose data matches.
325
- * Mirrors `NodeRoutingEntry`. */
326
- interface DiagramNodeEntry {
327
- /** Unique within a reader; the same vocabulary as `NodeRoutingEntry.name`. */
328
- name: string;
329
- matches: (data: unknown) => boolean;
330
- /** The trait every matching node carries. A function when it varies with the
331
- * node's own data — a pipeline stage whose ports come from its rows. */
332
- trait: DiagramNode | ((data: unknown) => DiagramNode);
333
- }
334
- /** The trait on the node's own `data.diagram`, or `null`.
335
- *
336
- * An edge's trait lives under the same key, so `from` and `to` are what tells
337
- * the two apart. Without that test an edge reads as a participant declaring
338
- * no ports, and collects the four defaults on the degenerate pose an edge
339
- * carries — four grabbable ports in the middle of nowhere. */
340
- declare const dataKeyReader: DiagramNodeReader;
341
- /**
342
- * A reader over declared kinds. A trait on the node's own data still wins, so
343
- * a single node can opt out of, or override, whatever its kind says.
344
- *
345
- * Throws on a duplicate `name`, for the reason `mergeContributions` does: a
346
- * class of node silently losing its ports is what this registry exists to make
347
- * loud.
348
- */
349
- declare function createDiagramNodes(entries: readonly DiagramNodeEntry[]): DiagramNodeReader;
350
- /** The trait `node` carries, through `read` (default: its own `data.diagram`). */
351
- declare function diagramNodeOf(node: DiagramNodeLike, read?: DiagramNodeReader): DiagramNode | null;
352
- /** True when layout must leave this node where it is. */
353
- declare function isPinned(node: DiagramNodeLike, read?: DiagramNodeReader): boolean;
354
-
355
- /**
356
- * Where a node's ports are, in world coordinates.
357
- *
358
- * A port is placed in the node's **unrotated** bounds, then carried through
359
- * the pose's rotation — the same pivot every other kit reader uses, the AABB
360
- * center. Bounds come from the pose descriptor rather than from `pose.width`
361
- * and friends, so a node whose pose is a path, an ellipse or anything else a
362
- * consumer wired is connectable on the same terms as a rect.
363
- */
364
-
365
- /** The eight anchors almost everything wants, named the way a compass is. */
366
- declare const COMPASS: {
367
- readonly nw: {
368
- readonly u: 0;
369
- readonly v: 0;
370
- };
371
- readonly n: {
372
- readonly u: 0.5;
373
- readonly v: 0;
374
- };
375
- readonly ne: {
376
- readonly u: 1;
377
- readonly v: 0;
378
- };
379
- readonly e: {
380
- readonly u: 1;
381
- readonly v: 0.5;
382
- };
383
- readonly se: {
384
- readonly u: 1;
385
- readonly v: 1;
386
- };
387
- readonly s: {
388
- readonly u: 0.5;
389
- readonly v: 1;
390
- };
391
- readonly sw: {
392
- readonly u: 0;
393
- readonly v: 1;
394
- };
395
- readonly w: {
396
- readonly u: 0;
397
- readonly v: 0.5;
398
- };
399
- };
400
- /**
401
- * What a node carrying no `ports` gets: the four edge midpoints of its own
402
- * bounds. Every node has bounds, so every node is connectable without saying
403
- * anything.
404
- */
405
- declare const DEFAULT_PORTS: readonly PortSpec[];
406
- interface PortsOptions<TPose> {
407
- /** Reads the AABB a port is placed in, and the node's rotation. Defaults to
408
- * the kit's `AUTO_POSE_DESCRIPTOR`, which handles rect and path poses. */
409
- geometry?: PoseProjection<TPose>;
410
- /** How the `DiagramNode` trait is read off a node. */
411
- read?: DiagramNodeReader;
412
- }
413
- /**
414
- * Every port on `node`, resolved against `pose`.
415
- *
416
- * Pass the pose the node is **painted** at — `effectivePose(scene, node)` —
417
- * not `node.pose`. A port that answers from the document pose while the node
418
- * is mid-drag puts an edge somewhere the user can see the node is not.
419
- */
420
- declare function portsOf<TPose>(node: DiagramNodeLike, pose: TPose, opts?: PortsOptions<TPose>): Port[];
421
- /** One named port, or `undefined` when the node has no such port. */
422
- declare function portOf<TPose>(node: DiagramNodeLike, pose: TPose, portId: string, opts?: PortsOptions<TPose>): Port | undefined;
1
+ import { P as Port, a as PortsOptions, D as DiagramNodeLike, b as DiagramNodeReader, L as LayoutFn, c as LayoutOptions, F as ForceOptions, d as LayoutResult, G as GraphSource, e as Graph } from './layout.entry-BO7d72yq.js';
2
+ export { B as BodyFloor, f as BodyNodeSpec, g as BodySpec, h as Bounds, i as BuildBodyOptions, j as BuildGraphOptions, C as COMPASS, k as DEFAULT_NODE_GAP, l as DEFAULT_PORTS, m as DEFAULT_RANK_GAP, n as DIAGRAM_TRAIT_KEY, o as DiagramNode, p as DiagramNodeEntry, q as ForceRelaxation, r as GraphEdge, s as GraphNode, t as GraphNodeLike, u as LayoutAxes, v as LayoutDirection, M as MeasureRowText, O as Outline, w as PortAnchor, x as PortSpec, R as Row, y as RowBox, z as RowNodeData, A as RowPort, E as RowPortBox, H as RowTextStyle, S as Slot, I as axesFor, J as backEdges, K as bodyOutline, N as bodyTrait, Q as boxForContent, T as buildBody, U as buildGraph, V as canvasMeasure, W as contentBox, X as createDiagramNodes, Y as dataKeyReader, Z as diagramNodeOf, _ as force, $ as forceRelaxation, a0 as forestOf, a1 as graphOrder, a2 as isPinned, a3 as layered, a4 as layoutBody, a5 as layoutRowPorts, a6 as measureBody, a7 as outlinePath, a8 as outlinePolyline, a9 as packAcross, aa as pinnedSet, ab as portOf, ac as portsOf, ad as portsOnOutline, ae as ranksOf, af as rayHit, ag as seededOrder, ah as settle, ai as sizeToBody, aj as translated, ak as tree } from './layout.entry-BO7d72yq.js';
3
+ import { Vec2, DerivedDep, Path, SceneRegistry, PoseDescriptor, ChromeState, FillStyle, Stroke, CursorSpec, Affordance, InvocationCtx, GestureBinding, Action, Contribution, RenderLayer, EasingFn, NodeId, Scene, NodeShapeEntry } from '@weasel-js/core';
4
+ import '@weasel-js/core/math';
423
5
 
424
6
  /**
425
7
  * `DiagramEdge` — a connection between two participants.
@@ -589,7 +171,7 @@ interface PortAffordanceOptions<TPose> {
589
171
  /** Also the chrome-caps visibility id. Default {@link PORT_LAYER_ID}. */
590
172
  id?: string;
591
173
  read?: DiagramNodeReader;
592
- geometry?: PoseProjection<TPose>;
174
+ geometry?: PoseDescriptor<TPose>;
593
175
  /** Which participants show their ports. Default: all of them. Gate on
594
176
  * selection or hover by reading `state`. */
595
177
  shows?: (node: DiagramNodeLike, state: ChromeState) => boolean;
@@ -673,7 +255,7 @@ interface ConnectActionOptions<TPose> {
673
255
  /** Default {@link CONNECT_ACTION_ID}. */
674
256
  id?: string;
675
257
  read?: DiagramNodeReader;
676
- geometry?: PoseProjection<TPose>;
258
+ geometry?: PoseDescriptor<TPose>;
677
259
  /** Default {@link defaultCanConnect}. */
678
260
  canConnect?: CanConnect;
679
261
  /** How near the pointer must come to a port, in world units, before the
@@ -683,8 +265,6 @@ interface ConnectActionOptions<TPose> {
683
265
  * Default `'straight'`. */
684
266
  router?: string;
685
267
  routers?: Readonly<Record<string, Router>>;
686
- /** How the in-flight edge is painted. */
687
- stroke?: Stroke;
688
268
  /**
689
269
  * Mints and commits the edge. Called once, on release over a valid port.
690
270
  *
@@ -778,78 +358,6 @@ declare function diagramPorts<TPose>(opts: DiagramContributionOptions<TPose>): {
778
358
  contribution: Contribution;
779
359
  };
780
360
 
781
- /**
782
- * `Graph` — the adjacency index a layout runs over.
783
- *
784
- * The scene is the document, so there is no second graph kept in sync with it:
785
- * this one is built from the scene on demand, used, and thrown away. Rebuilding
786
- * per layout invocation is deliberate — a maintained index is a cache to
787
- * invalidate on every add, delete, reparent and `dependsOn` edit, and layout is
788
- * not on the hot path. Measure before changing that.
789
- *
790
- * A node's *identity* is its id and its *size* is its bounds; where it currently
791
- * sits is here too, because a re-layout that ignores where things already are
792
- * scrambles a diagram the author has arranged.
793
- */
794
-
795
- /** A scene node as the graph reads it. `dependsOn` is what an edge's two
796
- * endpoints are: the edge trait names *ports*, and the nodes those ports are
797
- * on are the dependencies the derivation already runs on. */
798
- interface GraphNodeLike extends DiagramNodeLike {
799
- dependsOn?: readonly string[] | 'children';
800
- }
801
- /** Where the graph gets its nodes. The same thunk shape the port affordance
802
- * takes, so one source answers both. */
803
- type GraphSource<TPose> = () => Iterable<{
804
- node: GraphNodeLike;
805
- pose: TPose;
806
- }>;
807
- /** One participant. */
808
- interface GraphNode {
809
- id: string;
810
- /** Where it is now, in world coordinates. */
811
- bounds: Bounds;
812
- /** Layout must not move it. */
813
- pinned: boolean;
814
- }
815
- /** One connection. `id` is the edge node's own id, so a caller can go back to
816
- * the scene for its trait. */
817
- interface GraphEdge {
818
- id: string;
819
- from: string;
820
- to: string;
821
- }
822
- /**
823
- * Nodes, edges, and the two adjacency reads a layout needs.
824
- *
825
- * Every list is in source order — render order, when the source is a scene —
826
- * which is what a layout's tiebreaks resolve against. There is no RNG anywhere
827
- * downstream of this, and this is where that starts.
828
- */
829
- interface Graph {
830
- readonly nodes: readonly GraphNode[];
831
- readonly edges: readonly GraphEdge[];
832
- node(id: string): GraphNode | undefined;
833
- /** Edges leaving `id`, in source order. */
834
- outgoing(id: string): readonly GraphEdge[];
835
- /** Edges arriving at `id`, in source order. */
836
- incoming(id: string): readonly GraphEdge[];
837
- }
838
- interface BuildGraphOptions<TPose> {
839
- read?: DiagramNodeReader;
840
- geometry?: PoseProjection<TPose>;
841
- }
842
- /**
843
- * Read a graph out of a source of scene nodes.
844
- *
845
- * A node is an edge if it carries the edge trait and names exactly two
846
- * dependencies; a participant if the reader hands back a `DiagramNode`;
847
- * neither, and it is not in the diagram at all. An edge whose endpoints are not
848
- * both participants is dropped — a dangling edge should not be ranking
849
- * anything.
850
- */
851
- declare function buildGraph<TPose>(source: GraphSource<TPose>, opts?: BuildGraphOptions<TPose>): Graph;
852
-
853
361
  /**
854
362
  * A label on an edge: an ordinary leaf node that derives its *pose* from the
855
363
  * route it sits on.
@@ -875,7 +383,7 @@ declare function diagramLabelOf(node: {
875
383
  data: unknown;
876
384
  }): DiagramLabel | null;
877
385
  interface LabelPoseOptions<TPose> {
878
- geometry?: PoseProjection<TPose>;
386
+ geometry?: PoseDescriptor<TPose>;
879
387
  }
880
388
  /**
881
389
  * A `derivePose` that stations a label along its dependency's path.
@@ -898,177 +406,6 @@ declare const LABEL_DERIVE_POSE: (node: {
898
406
  data: unknown;
899
407
  }, deps: readonly (DerivedDep<unknown> | undefined)[]) => unknown;
900
408
 
901
- /**
902
- * Layout: where the participants go.
903
- *
904
- * A layout is a pure function of the graph — no scene, no ops, no history. It
905
- * hands back the new top-left for every node that **moves**, and a node already
906
- * standing where the layout wants it is simply absent from the answer. That is
907
- * what makes re-running a layout free rather than destructive, and it is the
908
- * property the idempotence test asserts.
909
- *
910
- * Three rules keep re-layout non-destructive, and all three live here rather
911
- * than in each algorithm:
912
- *
913
- * - **No RNG anywhere.** Every tiebreak falls back to the graph's own node
914
- * order, which is the source's order — render order, for a scene.
915
- * - **Within-rank order is seeded from where the nodes already are**, read
916
- * off the cross axis, rather than from crossing-minimization. An author who
917
- * dragged two boxes into an order gets that order back.
918
- * - **A pin set nothing moves.** Pinned nodes are never in the result, and
919
- * the rest of the layout is translated to sit around them.
920
- *
921
- * `layered` and `tree` are idempotent by construction: the second run reads the
922
- * order the first run produced and computes the same slots. `force` is an
923
- * iterative relaxation and makes no such claim — re-running it keeps relaxing.
924
- */
925
-
926
- /** Which way the ranks stack. `force` ignores it. */
927
- type LayoutDirection = 'down' | 'up' | 'right' | 'left';
928
- interface LayoutOptions {
929
- /** Default `'down'`. */
930
- direction?: LayoutDirection;
931
- /** Between neighbors within a rank, in world units. Default 48. */
932
- nodeGap?: number;
933
- /** Between one rank and the next. Default 96. */
934
- rankGap?: number;
935
- /** Ids layout must not move, on top of whatever carries `pinned: true`. */
936
- pin?: Iterable<string>;
937
- /** How far a node has to move to be worth moving, in world units. Default
938
- * `1e-6` — a float-noise floor, not a snapping quantum. Raise it to let a
939
- * `force` pass that has stopped doing anything useful commit nothing. */
940
- tolerance?: number;
941
- }
942
- /** The new top-left for each node that moves. A node that is already there is
943
- * absent, so an empty result means "nothing to do". */
944
- type LayoutResult = ReadonlyMap<string, Vec2>;
945
- /** Every layout in this package has this shape, and so does a consumer's. */
946
- type LayoutFn = (graph: Graph, opts?: LayoutOptions) => LayoutResult;
947
- declare const DEFAULT_NODE_GAP = 48;
948
- declare const DEFAULT_RANK_GAP = 96;
949
- /** Which world axis carries ranks, which carries within-rank order, and
950
- * whether ranks grow toward positive world coordinates. */
951
- interface LayoutAxes {
952
- rank: 'x' | 'y';
953
- cross: 'x' | 'y';
954
- /** `-1` for `'up'` and `'left'`, where the canonical frame is mirrored. */
955
- sign: 1 | -1;
956
- }
957
- declare function axesFor(direction?: LayoutDirection): LayoutAxes;
958
- /** Every id layout must leave alone. */
959
- declare function pinnedSet(graph: Graph, opts?: LayoutOptions): Set<string>;
960
- /**
961
- * A placement in the canonical `(cross, rank)` frame, before it is turned into
962
- * world coordinates. `cross` and `rank` are the node's low corner along each.
963
- */
964
- interface Slot {
965
- cross: number;
966
- rank: number;
967
- }
968
- /**
969
- * Turn canonical slots into the positions to write.
970
- *
971
- * Three steps, in order: map the canonical frame onto the world axes the
972
- * direction names; translate the whole layout so it lands where the graph
973
- * already is; drop everything that is pinned or that would not move.
974
- *
975
- * The translation is what makes the layout non-destructive, and it is chosen so
976
- * that a second run lands on the same answer. With a pin, it is whatever keeps
977
- * the first pinned node exactly where it is. Without one, it is whatever puts
978
- * the layout's own bounding box where the graph's bounding box already starts —
979
- * which the first run then makes true, so the second run computes the same
980
- * translation and writes nothing.
981
- */
982
- declare function settle(graph: Graph, slots: ReadonlyMap<string, Slot>, opts?: LayoutOptions): LayoutResult;
983
- /**
984
- * {@link settle}'s second half, for a layout that already works in world
985
- * coordinates — `force` does, since the integrator moves points around rather
986
- * than filling slots.
987
- */
988
- declare function translated(graph: Graph, placed: ReadonlyMap<string, Vec2>, pinned: ReadonlySet<string>, tolerance?: number): LayoutResult;
989
- /**
990
- * Lay a list of nodes out along the cross axis, in the order given, and hand
991
- * back each one's low corner plus the total span.
992
- *
993
- * Shared by `layered`'s ranks and `tree`'s sibling rows, which pack a row the
994
- * same way and differ only in what decides the order.
995
- */
996
- declare function packAcross(nodes: readonly GraphNode[], axis: 'x' | 'y', gap: number): {
997
- at: Map<string, number>;
998
- span: number;
999
- };
1000
- /** Order within a rank: where the nodes already sit on the cross axis, with
1001
- * graph order breaking a tie. Never a comparison that could reverse between
1002
- * two runs on the same input. */
1003
- declare function seededOrder(nodes: readonly GraphNode[], axis: 'x' | 'y', indexOf: (id: string) => number): GraphNode[];
1004
- /** Each node's position in `graph.nodes`, for tiebreaks. */
1005
- declare function graphOrder(graph: Graph): (id: string) => number;
1006
-
1007
- /**
1008
- * `force` — relaxation, for a graph with no direction to read it in.
1009
- *
1010
- * Seeded from where the nodes already are rather than from a fresh random
1011
- * scatter: an author who has arranged half a diagram gets that arrangement
1012
- * relaxed, not replaced. There is no RNG in it at all — two coincident nodes
1013
- * are separated along a golden-angle spiral keyed to their index, which is what
1014
- * a random jiggle is for elsewhere.
1015
- *
1016
- * The integrator is the kit's own `createSimulation`, the same one
1017
- * `useSimulation` runs a frame at a time. Only the forces are local, and they
1018
- * are the naive O(n²) kind — right for the tens-to-hundreds of nodes an
1019
- * edge-per-scene-node diagram is already sized for, wrong for a 10k-node graph,
1020
- * which wants d3-force's Barnes–Hut through `useSimulation` and its own render
1021
- * layer.
1022
- *
1023
- * **This is the one layout that is not idempotent.** A relaxation re-run from
1024
- * its own output keeps relaxing; `direction` means nothing to it. Pins and the
1025
- * `tolerance` option are what keep a second pass from scrambling a settled
1026
- * diagram.
1027
- */
1028
-
1029
- interface ForceOptions extends LayoutOptions {
1030
- /** Ticks to run. Default 300 — one full cooling schedule. */
1031
- iterations?: number;
1032
- /** Rest length of an edge, in world units. Default 160. */
1033
- linkDistance?: number;
1034
- /** How hard an edge pulls, 0..1. Default 0.35. */
1035
- linkStrength?: number;
1036
- /** Pairwise repulsion. Negative repels; default -3000. */
1037
- charge?: number;
1038
- /** How hard the graph is held around its own centroid. Default 0.02. */
1039
- gravity?: number;
1040
- /** Clear space kept between two boxes, in world units. Default 24. */
1041
- padding?: number;
1042
- }
1043
- interface Body extends SimulationNode {
1044
- id: string;
1045
- /** Half the node's box, so separation can work on the shape the author sees
1046
- * rather than on a point. */
1047
- hw: number;
1048
- hh: number;
1049
- }
1050
- /**
1051
- * The bodies and the force list, wound up and ready to tick.
1052
- *
1053
- * Shared by the one-shot `force` below and the live relaxation in `live.ts`,
1054
- * which ticks it a frame at a time. Two copies of a force list drift, and the
1055
- * only thing that would say so is the arrangement they produce.
1056
- */
1057
- interface ForceRelaxation {
1058
- bodies: Body[];
1059
- byId: Map<string, Body>;
1060
- /** Ids the caller must not move: declared pins, held as `fx`/`fy`. */
1061
- pinned: ReadonlySet<string>;
1062
- sim: ReturnType<typeof createSimulation<Body>>;
1063
- /** Where a body's node's top-left sits, given the body's center. */
1064
- placed(): Map<string, {
1065
- x: number;
1066
- y: number;
1067
- }>;
1068
- }
1069
- declare function forceRelaxation(graph: Graph, opts?: ForceOptions): ForceRelaxation;
1070
- declare const force: (graph: Graph, opts?: ForceOptions) => LayoutResult;
1071
-
1072
409
  /** What a live producer is told, and what it answers with. */
1073
410
  interface LiveLayoutCtx<TPose> {
1074
411
  /** Nodes another gesture is moving, and where each is drawn. */
@@ -1084,7 +421,7 @@ interface LiveLayoutFrame {
1084
421
  }
1085
422
  type LiveLayoutProducer<TPose> = (ctx: LiveLayoutCtx<TPose>) => LiveLayoutFrame;
1086
423
  interface ForceProducerOptions<TPose> extends ForceOptions {
1087
- geometry?: PoseProjection<TPose>;
424
+ geometry?: PoseDescriptor<TPose>;
1088
425
  /** Alpha held while something is pinned, so the graph keeps answering a drag
1089
426
  * instead of freezing under it. Default 0.3. */
1090
427
  dragAlpha?: number;
@@ -1131,7 +468,7 @@ interface UseLiveLayoutOptions<TPose> {
1131
468
  frames?: number;
1132
469
  easing?: EasingFn;
1133
470
  read?: DiagramNodeReader;
1134
- geometry?: PoseProjection<TPose>;
471
+ geometry?: PoseDescriptor<TPose>;
1135
472
  /** The undo entry's name. Default `'Layout'`. */
1136
473
  label?: string;
1137
474
  /** Fires once the run has settled or been stopped and its poses are in the
@@ -1166,31 +503,6 @@ declare function useLiveLayout<TPose>(opts: UseLiveLayoutOptions<TPose>): LiveLa
1166
503
  /** The layouts that ease rather than relax, for a consumer choosing one. */
1167
504
  declare const EASED_LAYOUTS: Readonly<Record<string, LayoutFn>>;
1168
505
 
1169
- /**
1170
- * `layered` — ranks running one way, nodes running across.
1171
- *
1172
- * The flowchart / pipeline layout: an edge points from one rank to the next, so
1173
- * reading down the page is reading the order things happen in. Ranks come from
1174
- * longest-path layering, which puts every node as far along as its deepest
1175
- * predecessor allows.
1176
- *
1177
- * Cycles do not disqualify a graph. A depth-first walk in graph order names the
1178
- * edges that close a cycle, layering ignores those, and they draw as edges
1179
- * running back up the page — which is what a reader expects a loop to look
1180
- * like. Which edges get named depends only on the node order, so it is the same
1181
- * every run.
1182
- *
1183
- * This is not crossing-minimization: within a rank, nodes keep the order they
1184
- * already have on the cross axis. An author who dragged two branches into an
1185
- * order gets that order back, and re-running the layout is free.
1186
- */
1187
-
1188
- /** Edge ids that close a cycle, found by a depth-first walk in graph order. */
1189
- declare function backEdges(graph: Graph): Set<string>;
1190
- /** Longest-path rank per node: one past its deepest predecessor. */
1191
- declare function ranksOf(graph: Graph, back: ReadonlySet<string>): Map<string, number>;
1192
- declare const layered: LayoutFn;
1193
-
1194
506
  /**
1195
507
  * Running a layout against a scene: `LAYOUTS`, one batch of pose writes, and
1196
508
  * the action that reaches both.
@@ -1204,7 +516,7 @@ declare const layered: LayoutFn;
1204
516
  /** The layouts this package ships. A consumer's own go in the same shape. */
1205
517
  declare const LAYOUTS: Readonly<Record<string, LayoutFn>>;
1206
518
  interface ApplyLayoutOptions<TPose> {
1207
- geometry?: PoseProjection<TPose>;
519
+ geometry?: PoseDescriptor<TPose>;
1208
520
  /** The undo entry's name. Default `'Layout'`. */
1209
521
  label?: string;
1210
522
  }
@@ -1232,7 +544,7 @@ declare function applyLayout<TPose>(scene: Scene<unknown, string, TPose>, result
1232
544
  * translation is measured from the node's document pose, which a run in flight
1233
545
  * never changes, so a frame's answer does not depend on the frame before it.
1234
546
  */
1235
- declare function layoutPoses<TPose>(scene: Scene<unknown, string, TPose>, result: LayoutResult, geometry?: PoseProjection<TPose>): Map<string, TPose>;
547
+ declare function layoutPoses<TPose>(scene: Scene<unknown, string, TPose>, result: LayoutResult, geometry?: PoseDescriptor<TPose>): Map<string, TPose>;
1236
548
  /** The default action id. */
1237
549
  declare const LAYOUT_ACTION_ID = "diagram.layout";
1238
550
  interface LayoutActionOptions<TPose> {
@@ -1249,7 +561,7 @@ interface LayoutActionOptions<TPose> {
1249
561
  algorithm?: string | LayoutFn;
1250
562
  layout?: LayoutOptions;
1251
563
  read?: DiagramNodeReader;
1252
- geometry?: PoseProjection<TPose>;
564
+ geometry?: PoseDescriptor<TPose>;
1253
565
  }
1254
566
  /**
1255
567
  * `diagram.layout` — rebuild the graph, run a layout, write the result.
@@ -1260,61 +572,6 @@ interface LayoutActionOptions<TPose> {
1260
572
  */
1261
573
  declare function createLayoutAction<TPose>(opts: LayoutActionOptions<TPose>): Action;
1262
574
 
1263
- /**
1264
- * `tree` — a parent centered over the block its children occupy.
1265
- *
1266
- * The hierarchy layout: org charts, file trees, decision trees. Depth is the
1267
- * rank axis, so every child sits one band past its parent, and a subtree is
1268
- * packed as a solid block so two siblings' descendants never interleave.
1269
- *
1270
- * A graph that is not a tree still lays out. Roots are the nodes nothing points
1271
- * at, in graph order; a node reachable from two parents belongs to whichever
1272
- * the depth-first walk reaches first; and anything the walk never reaches — a
1273
- * disconnected part, or a pure cycle — becomes a root of its own. So the answer
1274
- * is always a forest, and which forest depends only on the node order.
1275
- *
1276
- * The block packing is deliberately not Reingold–Tilford: it never slides a
1277
- * deep subtree under a shallow neighbor's overhang. That costs horizontal
1278
- * space and buys an invariant an author can rely on — a subtree's extent is a
1279
- * rectangle, and dragging one never lands it inside another.
1280
- */
1281
-
1282
- interface Forest {
1283
- roots: GraphNode[];
1284
- children: Map<string, GraphNode[]>;
1285
- depth: Map<string, number>;
1286
- }
1287
- /** The forest a depth-first walk in graph order finds. Exported because "which
1288
- * node ended up whose child" is the question anyone debugging a tree layout
1289
- * asks first. */
1290
- declare function forestOf(graph: Graph, crossAxis: 'x' | 'y'): Forest;
1291
- declare const tree: LayoutFn;
1292
-
1293
- /** The outline as a closed polyline: `[x0, y0, x1, y1, …]`. */
1294
- declare function outlinePolyline(outline: Outline, bounds: Bounds): number[];
1295
- /**
1296
- * Where the ray from `center` through `point` last crosses `poly`, or `null`
1297
- * when it never does.
1298
- *
1299
- * The *last* crossing rather than the first: a concave outline can be crossed
1300
- * more than once, and the far side is the boundary an edge should attach to.
1301
- */
1302
- declare function rayHit(poly: readonly number[], center: {
1303
- x: number;
1304
- y: number;
1305
- }, point: {
1306
- x: number;
1307
- y: number;
1308
- }): {
1309
- x: number;
1310
- y: number;
1311
- } | null;
1312
- /**
1313
- * Every port moved onto `outline`. A port the ray misses — one at the node's
1314
- * dead center, with no direction to cast along — is left where it was.
1315
- */
1316
- declare function portsOnOutline(ports: readonly Port[], outline: Outline, bounds: Bounds): Port[];
1317
-
1318
575
  /**
1319
576
  * The port affordance, composed into a layer and pointed at a scene.
1320
577
  *
@@ -1388,7 +645,7 @@ interface DiagramShapeOptions<TPose> {
1388
645
  /** How the trait is read. Default: the node's own `data.diagram`. */
1389
646
  read?: DiagramNodeReader;
1390
647
  /** Reads the box the outline is built in. Default `RECT_POSE_DESCRIPTOR`. */
1391
- geometry?: PoseProjection<TPose>;
648
+ geometry?: PoseDescriptor<TPose>;
1392
649
  /** Painted when the node's data declares neither. */
1393
650
  defaultFill?: FillStyle | null;
1394
651
  defaultStroke?: Stroke | null;
@@ -1402,4 +659,4 @@ declare function diagramShape<TPose>(opts?: DiagramShapeOptions<TPose>): NodeSha
1402
659
  */
1403
660
  declare function registerDiagramShape<TPose>(opts?: DiagramShapeOptions<TPose>): () => void;
1404
661
 
1405
- export { type ApplyLayoutOptions, type BodyFloor, type BodyNodeSpec, type BodySpec, type Bounds, type BuildBodyOptions, type BuildGraphOptions, COMPASS, CONNECT_ACTION_ID, type CanConnect, type ConnectActionOptions, DEFAULT_EDGE_STROKE, DEFAULT_NODE_GAP, DEFAULT_PORTS, DEFAULT_RANK_GAP, DIAGRAM_EDGE, DIAGRAM_LABEL, DIAGRAM_TRAIT_KEY, type DiagramContributionOptions, type DiagramEdge, type DiagramLabel, type DiagramNode, type DiagramNodeEntry, type DiagramNodeLike, type DiagramNodeReader, type DiagramShapeOptions, EASED_LAYOUTS, EDGE_DERIVE_PATH, type EasedProducerOptions, type EdgeEnd, type EdgeRouteOptions, type ForceOptions, type ForceProducerOptions, type ForceRelaxation, GRAB_PORT_ACTION_ID, type Graph, type GraphEdge, type GraphNode, type GraphNodeLike, type GraphSource, LABEL_DERIVE_POSE, LAYOUTS, LAYOUT_ACTION_ID, type LabelPoseOptions, type LayoutActionOptions, type LayoutAxes, type LayoutDirection, type LayoutFn, type LayoutOptions, type LayoutResult, type LiveLayout, type LiveLayoutCtx, type LiveLayoutFrame, type LiveLayoutProducer, type MeasureRowText, type Outline, PORT_AFFORDANCE_KIND, PORT_LAYER_ID, type ParticipantPose, type ParticipantScene, type ParticipantSource, type PendingEdge, type Port, type PortAffordanceOptions, type PortAnchor, type PortScratch, type PortSpec, type PortsOptions, ROUTERS, type RouteRequest, type Router, type Row, type RowBox, type RowNodeData, type RowPort, type RowPortBox, type RowTextStyle, type Slot, type UseLiveLayoutOptions, applyLayout, axesFor, backEdges, bezier, bodyOutline, bodyTrait, boxForContent, buildBody, buildGraph, canvasMeasure, commitEdgeToScene, connectBinding, contentBox, createConnectAction, createDiagramContribution, createDiagramNodes, createLayoutAction, createPortAffordance, dataKeyReader, defaultCanConnect, diagramEdgeOf, diagramLabelOf, diagramNodeOf, diagramPorts, diagramShape, easedProducer, edgeDerivePath, force, forceProducer, forceRelaxation, forestOf, grabPortAction, grabPortBindings, graphOrder, isPinned, labelDerivePose, layered, layoutBody, layoutPoses, layoutRowPorts, measureBody, orthogonal, outlinePath, outlinePolyline, packAcross, pinnedSet, portLayer, portOf, portScratchOf, portsOf, portsOnOutline, ranksOf, rayHit, registerDiagramShape, resolveEnd, sceneParticipants, seededOrder, settle, sizeToBody, straight, translated, tree, useLiveLayout, withDiagramRegistry };
662
+ export { type ApplyLayoutOptions, CONNECT_ACTION_ID, type CanConnect, type ConnectActionOptions, DEFAULT_EDGE_STROKE, DIAGRAM_EDGE, DIAGRAM_LABEL, type DiagramContributionOptions, type DiagramEdge, type DiagramLabel, DiagramNodeLike, DiagramNodeReader, type DiagramShapeOptions, EASED_LAYOUTS, EDGE_DERIVE_PATH, type EasedProducerOptions, type EdgeEnd, type EdgeRouteOptions, ForceOptions, type ForceProducerOptions, GRAB_PORT_ACTION_ID, Graph, GraphSource, LABEL_DERIVE_POSE, LAYOUTS, LAYOUT_ACTION_ID, type LabelPoseOptions, type LayoutActionOptions, LayoutFn, LayoutOptions, LayoutResult, type LiveLayout, type LiveLayoutCtx, type LiveLayoutFrame, type LiveLayoutProducer, PORT_AFFORDANCE_KIND, PORT_LAYER_ID, type ParticipantPose, type ParticipantScene, type ParticipantSource, type PendingEdge, Port, type PortAffordanceOptions, type PortScratch, PortsOptions, ROUTERS, type RouteRequest, type Router, type UseLiveLayoutOptions, applyLayout, bezier, commitEdgeToScene, connectBinding, createConnectAction, createDiagramContribution, createLayoutAction, createPortAffordance, defaultCanConnect, diagramEdgeOf, diagramLabelOf, diagramPorts, diagramShape, easedProducer, edgeDerivePath, forceProducer, grabPortAction, grabPortBindings, labelDerivePose, layoutPoses, orthogonal, portLayer, portScratchOf, registerDiagramShape, resolveEnd, sceneParticipants, straight, useLiveLayout, withDiagramRegistry };