@hattoriai/poney-layout 0.0.0-stage → 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Rui Freitas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,11 @@
1
- # Temporary Holding Version
1
+ # @hattoriai/poney-layout
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Lays out the views of a Poney document: each diagram type to a positioned scene, with ELK and custom layouts. No UI framework; the renderers in `@hattoriai/poney-react` draw its scenes.
4
+
5
+ ```ts
6
+ import { layoutView } from '@hattoriai/poney-layout'
7
+
8
+ const scene = await layoutView(doc, doc.views[0])
9
+ ```
10
+
11
+ Part of [Poney](https://github.com/hattoriai/poney): the format, the renderers, and tools to explain software. MIT licensed.
@@ -0,0 +1,533 @@
1
+ import { LayoutOptions, ElkNode } from 'elkjs/lib/elk-api';
2
+ export { ElkNode, LayoutOptions } from 'elkjs/lib/elk-api';
3
+ import { PoneyDocument, ViewOf, Message, DataFlowRole, ActivityNode, Field, Cardinality, State, Transition, Entity, Relationship, View } from '@hattoriai/poney-core';
4
+
5
+ interface Rect {
6
+ x: number;
7
+ y: number;
8
+ w: number;
9
+ h: number;
10
+ }
11
+ interface Point {
12
+ x: number;
13
+ y: number;
14
+ }
15
+ /**
16
+ * A positioned scene: the output of a layout engine and the only input of a
17
+ * renderer. Anchors map ref keys to rectangles so the canvas can frame any
18
+ * element by its semantic identity.
19
+ */
20
+ interface SceneBase {
21
+ width: number;
22
+ height: number;
23
+ anchors: Record<string, Rect>;
24
+ }
25
+
26
+ /** An SVG path through orthogonal points with rounded corners. */
27
+ declare function roundedPath(points: Point[], radius?: number): string;
28
+ /** Shortens the last segment of a polyline so an arrowhead can sit on the end. */
29
+ declare function trimEnd(points: Point[], by: number): Point[];
30
+ declare function trimStart(points: Point[], by: number): Point[];
31
+ /** Angle in radians of the last segment of a polyline. */
32
+ declare function endAngle(points: Point[]): number;
33
+ declare function startAngle(points: Point[]): number;
34
+ /** The midpoint of the longest segment — a calm place for a label. */
35
+ declare function labelPoint(points: Point[]): Point & {
36
+ horizontal: boolean;
37
+ };
38
+ /** The point at a fraction of a polyline's total length. */
39
+ declare function pointAlong(points: Point[], t: number): Point;
40
+ declare function union(rects: Rect[]): Rect;
41
+ declare const center: (r: Rect) => Point;
42
+
43
+ /**
44
+ * Text measurement for layout. Layouts size nodes from their labels, so they
45
+ * need to know how wide text is. The measurer is injectable: browsers use a
46
+ * canvas by default; Node, workers or other renderers can set their own.
47
+ */
48
+ type FontFamily = 'sans' | 'mono';
49
+ type TextMeasurer = (text: string, size: number, weight: number, family: FontFamily) => number;
50
+ /** Width estimate without any font engine. Good enough for tests and servers. */
51
+ declare const estimateText: TextMeasurer;
52
+ /** Replaces the text measurer, for example with a server-side font engine. */
53
+ declare function setTextMeasurer(fn: TextMeasurer): void;
54
+ /** Sets the font stacks the default canvas measurer uses. */
55
+ declare function setFonts(next: Partial<Record<FontFamily, string>>): void;
56
+ declare function measure(text: string, size?: number, weight?: number, family?: FontFamily): number;
57
+ /** Wraps text into lines no wider than `width`, adding an ellipsis past `maxLines`. */
58
+ declare function wrap(text: string, width: number, size?: number, weight?: number, maxLines?: number): string[];
59
+ /** Truncates a single line to `width`. */
60
+ declare function clip(text: string, width: number, size?: number, weight?: number, family?: FontFamily): string;
61
+
62
+ declare const BASE_OPTIONS: LayoutOptions;
63
+ interface FlatElkNode extends Rect {
64
+ id: string;
65
+ parent?: string;
66
+ }
67
+ interface FlatElkEdge {
68
+ id: string;
69
+ points: Point[];
70
+ labels: (Rect & {
71
+ text: string;
72
+ })[];
73
+ }
74
+ interface FlatElkGraph {
75
+ width: number;
76
+ height: number;
77
+ nodes: Map<string, FlatElkNode>;
78
+ edges: Map<string, FlatElkEdge>;
79
+ }
80
+ /**
81
+ * Runs ELK and returns every node and edge in root coordinates.
82
+ * ELK reports nodes relative to their parent and edges relative to the
83
+ * node named in `container`; this adds those offsets up once.
84
+ */
85
+ declare function runElk(graph: ElkNode): Promise<FlatElkGraph>;
86
+
87
+ interface ArchitectureSceneNode extends Rect {
88
+ id: string;
89
+ type: 'entity' | 'group';
90
+ entityId?: string;
91
+ groupId?: string;
92
+ parent?: string;
93
+ /** Has visible children drawn inside it. */
94
+ compound: boolean;
95
+ collapsed: boolean;
96
+ /** Can be opened or closed. */
97
+ expandable: boolean;
98
+ hiddenCount: number;
99
+ depth: number;
100
+ }
101
+ interface ArchitectureSceneEdge {
102
+ id: string;
103
+ source: string;
104
+ target: string;
105
+ relationships: string[];
106
+ lifted: boolean;
107
+ points: Point[];
108
+ }
109
+ interface ArchitectureScene extends SceneBase {
110
+ nodes: ArchitectureSceneNode[];
111
+ edges: ArchitectureSceneEdge[];
112
+ }
113
+ /** Sizes per node shape. Renderers draw inside these boxes. */
114
+ declare const ARCH_SIZES: {
115
+ readonly card: {
116
+ readonly w: 248;
117
+ readonly h: 104;
118
+ };
119
+ readonly cylinder: {
120
+ readonly w: 240;
121
+ readonly h: 112;
122
+ };
123
+ readonly stack: {
124
+ readonly w: 240;
125
+ readonly h: 104;
126
+ };
127
+ readonly person: {
128
+ readonly w: 176;
129
+ readonly h: 120;
130
+ };
131
+ readonly frame: {
132
+ readonly w: 330;
133
+ readonly h: 150;
134
+ };
135
+ readonly chip: {
136
+ readonly w: 220;
137
+ readonly h: 64;
138
+ };
139
+ readonly group: {
140
+ readonly w: 248;
141
+ readonly h: 104;
142
+ };
143
+ };
144
+ declare const ARCH_PADDING: {
145
+ container: number;
146
+ group: number;
147
+ };
148
+ declare function layoutArchitecture(doc: PoneyDocument, view: ViewOf<'architecture'>, options?: {
149
+ collapsed?: string[];
150
+ }): Promise<ArchitectureScene>;
151
+
152
+ interface SequenceParticipant {
153
+ entityId: string;
154
+ x: number;
155
+ header: Rect;
156
+ lifeline: {
157
+ y1: number;
158
+ y2: number;
159
+ };
160
+ }
161
+ interface SequenceMessage {
162
+ id: string;
163
+ number: number;
164
+ kind: Message['kind'];
165
+ from: string;
166
+ to: string;
167
+ label: string;
168
+ y: number;
169
+ x1: number;
170
+ x2: number;
171
+ /** Self messages loop to the right of their lifeline. */
172
+ loop?: {
173
+ x: number;
174
+ y1: number;
175
+ y2: number;
176
+ w: number;
177
+ };
178
+ /** Fragment ids around the message, outermost first. */
179
+ fragments: string[];
180
+ }
181
+ interface SequenceActivation {
182
+ entityId: string;
183
+ x: number;
184
+ y1: number;
185
+ y2: number;
186
+ }
187
+ interface SequenceFragment extends Rect {
188
+ id: string;
189
+ kind: 'alt' | 'opt' | 'loop' | 'par';
190
+ label: string;
191
+ depth: number;
192
+ branches: {
193
+ guard: string;
194
+ y: number;
195
+ }[];
196
+ }
197
+ interface SequenceScene extends SceneBase {
198
+ interactionId: string;
199
+ participants: SequenceParticipant[];
200
+ messages: SequenceMessage[];
201
+ activations: SequenceActivation[];
202
+ fragments: SequenceFragment[];
203
+ }
204
+ declare const SEQ: {
205
+ readonly headerH: 64;
206
+ readonly headerGap: 30;
207
+ readonly row: 44;
208
+ readonly selfRow: 58;
209
+ readonly fragmentHeader: 34;
210
+ readonly branchHeader: 30;
211
+ readonly fragmentEnd: 14;
212
+ readonly activationW: 10;
213
+ readonly margin: 32;
214
+ readonly labelSize: 12.5;
215
+ };
216
+ interface SequenceLayoutOptions {
217
+ /**
218
+ * Draw only these messages, the fragments around them and their
219
+ * participants. Numbers stay those of the whole interaction.
220
+ */
221
+ only?: string[];
222
+ /**
223
+ * Narrower columns for stories: labels may cross a lifeline (renderers
224
+ * draw them with a halo), so columns need only part of each label's width.
225
+ */
226
+ compact?: boolean;
227
+ }
228
+ declare function layoutSequence(doc: PoneyDocument, view: ViewOf<'sequence'>, options?: SequenceLayoutOptions): SequenceScene;
229
+
230
+ interface DataFlowScene extends SceneBase {
231
+ nodes: (Rect & {
232
+ id: string;
233
+ entityId: string;
234
+ role: DataFlowRole;
235
+ })[];
236
+ edges: {
237
+ id: string;
238
+ control: boolean;
239
+ source: string;
240
+ target: string;
241
+ points: Point[];
242
+ label?: Rect & {
243
+ text: string;
244
+ };
245
+ data: string[];
246
+ }[];
247
+ }
248
+ declare function layoutDataFlow(doc: PoneyDocument, view: ViewOf<'dataflow'>, options?: {
249
+ controlFlow?: boolean;
250
+ }): Promise<DataFlowScene>;
251
+
252
+ /**
253
+ * Activity layout: one column per performer (swimlanes), one row per step
254
+ * of the longest path from the start. Loops route back up a channel on the
255
+ * right so they never cross the forward flow.
256
+ */
257
+ interface ActivityLane {
258
+ entityId?: string;
259
+ label: string;
260
+ x: number;
261
+ w: number;
262
+ }
263
+ interface ActivitySceneNode extends Rect {
264
+ id: string;
265
+ kind: ActivityNode['kind'];
266
+ label: string;
267
+ lines: string[];
268
+ outcome?: 'success' | 'failure';
269
+ lane: number;
270
+ rank: number;
271
+ }
272
+ interface ActivitySceneEdge {
273
+ id: string;
274
+ from: string;
275
+ to: string;
276
+ points: Point[];
277
+ guard?: string;
278
+ guardAt?: Point & {
279
+ anchor: 'start' | 'end' | 'middle';
280
+ };
281
+ back: boolean;
282
+ }
283
+ interface ActivityScene extends SceneBase {
284
+ processId: string;
285
+ lanes: ActivityLane[];
286
+ laneHeader: number;
287
+ nodes: ActivitySceneNode[];
288
+ edges: ActivitySceneEdge[];
289
+ }
290
+ declare function layoutActivity(doc: PoneyDocument, view: ViewOf<'activity'>): ActivityScene;
291
+
292
+ declare const ERD: {
293
+ readonly header: 40;
294
+ readonly row: 26;
295
+ readonly more: 28;
296
+ readonly bottom: 6;
297
+ };
298
+ interface ErdSceneTable extends Rect {
299
+ dataTypeId: string;
300
+ name: string;
301
+ rows: (Field & {
302
+ y: number;
303
+ foreign: boolean;
304
+ })[];
305
+ hiddenCount: number;
306
+ expanded: boolean;
307
+ store?: string;
308
+ }
309
+ interface ErdSceneRelation {
310
+ id: string;
311
+ from: string;
312
+ to: string;
313
+ points: Point[];
314
+ fromCardinality: Cardinality;
315
+ toCardinality: Cardinality;
316
+ logical: boolean;
317
+ label: string;
318
+ via?: string;
319
+ }
320
+ interface ErdScene extends SceneBase {
321
+ stores: (Rect & {
322
+ entityId: string;
323
+ })[];
324
+ tables: ErdSceneTable[];
325
+ relations: ErdSceneRelation[];
326
+ }
327
+ declare function layoutErd(doc: PoneyDocument, view: ViewOf<'erd'>, options?: {
328
+ expanded?: string[];
329
+ }): Promise<ErdScene>;
330
+
331
+ interface StateSceneState extends Rect {
332
+ id: string;
333
+ kind: State['kind'];
334
+ name: string;
335
+ outcome: State['outcome'];
336
+ entryActions: string[];
337
+ }
338
+ interface StateSceneTransition {
339
+ id: string;
340
+ from: string;
341
+ to: string;
342
+ event: string;
343
+ guard?: string;
344
+ points: Point[];
345
+ label?: Rect;
346
+ }
347
+ interface StateScene extends SceneBase {
348
+ machineId: string;
349
+ states: StateSceneState[];
350
+ transitions: StateSceneTransition[];
351
+ }
352
+ /** Sizes of a transition label: the event, and the guard on a second line. */
353
+ declare function transitionLabelSize(event: string, guard?: string): {
354
+ width: number;
355
+ height: number;
356
+ };
357
+ declare function layoutState(doc: PoneyDocument, view: ViewOf<'state'>, options?: {
358
+ extraTransitions?: Transition[];
359
+ }): Promise<StateScene>;
360
+
361
+ /**
362
+ * Use case layout: people on the left, supporting systems on the right, the
363
+ * system boundary in the middle with capabilities grouped by area. Each actor
364
+ * sits level with the capabilities it uses, so lines stay short.
365
+ */
366
+ interface UseCaseScene extends SceneBase {
367
+ boundary: Rect & {
368
+ title: string;
369
+ };
370
+ areas: (Rect & {
371
+ label: string;
372
+ })[];
373
+ capabilities: (Rect & {
374
+ id: string;
375
+ name: string;
376
+ })[];
377
+ actors: (Rect & {
378
+ entityId: string;
379
+ side: 'left' | 'right';
380
+ })[];
381
+ associations: {
382
+ id: string;
383
+ actor: string;
384
+ capability: string;
385
+ side: 'left' | 'right';
386
+ from: Point;
387
+ to: Point;
388
+ }[];
389
+ includes: {
390
+ id: string;
391
+ from: string;
392
+ to: string;
393
+ a: Point;
394
+ b: Point;
395
+ bulge: number;
396
+ }[];
397
+ }
398
+ declare function layoutUseCase(doc: PoneyDocument, view: ViewOf<'usecase'>): UseCaseScene;
399
+
400
+ /**
401
+ * Dependency layout: one horizontal band per layer, top to bottom. Nodes in
402
+ * a band are ordered by the average position of what they connect to, which
403
+ * removes most crossings. Edges are smooth curves, so they read as
404
+ * "depends on" rather than as wires.
405
+ */
406
+ interface DependencyScene extends SceneBase {
407
+ bands: (Rect & {
408
+ id: string;
409
+ label: string;
410
+ description?: string;
411
+ })[];
412
+ nodes: (Rect & {
413
+ entityId: string;
414
+ band: number;
415
+ })[];
416
+ edges: {
417
+ id: string;
418
+ source: string;
419
+ target: string;
420
+ kind: string;
421
+ d: Point[];
422
+ direction: 'down' | 'up' | 'across';
423
+ }[];
424
+ }
425
+ declare function layoutDependency(doc: PoneyDocument, view: ViewOf<'dependency'>): DependencyScene;
426
+
427
+ /**
428
+ * Map layout: a small, composed picture of how things connect.
429
+ *
430
+ * ELK chooses the columns and the order inside each column. The edges are
431
+ * then drawn as single soft curves from side to side, and each label slides
432
+ * along its curve until it is clear of boxes and other labels. Few shapes,
433
+ * lots of air: a map is read at a glance, not explored.
434
+ *
435
+ * An entity whose parent is also in the map is drawn inside it: the parent
436
+ * becomes a group. A closed group is one box; the lines of everything inside
437
+ * it join at that box, so a large codebase can be shown a few boxes at a time.
438
+ */
439
+ interface MapSceneNode extends Rect {
440
+ entityId: string;
441
+ /** A closed group: every entity inside it, drawn as this one box. */
442
+ members?: string[];
443
+ }
444
+ /** An open group: a frame around the entities inside it. */
445
+ interface MapSceneGroup extends Rect {
446
+ entityId: string;
447
+ /** Every entity inside, at any depth. */
448
+ members: string[];
449
+ /** 0 for a group at the top, 1 inside another group, and so on. */
450
+ depth: number;
451
+ }
452
+ interface MapSceneEdge {
453
+ /** The relationship this line stands for (the first one, for a line through a pass-through entity). */
454
+ id: string;
455
+ /** Every relationship the line stands for. */
456
+ relationships: string[];
457
+ source: string;
458
+ target: string;
459
+ /** The entity the line passes through, when the map leaves it out. */
460
+ via?: string;
461
+ /** Cubic Bézier: start, control 1, control 2, end. */
462
+ curve: [Point, Point, Point, Point];
463
+ /** Where the label sits, clear of boxes and other labels. */
464
+ label: Rect & {
465
+ text: string;
466
+ sub?: string;
467
+ };
468
+ /** When the label sits beside its curve: the point on the curve its leader line goes to. */
469
+ leader?: Point;
470
+ }
471
+ interface MapScene extends SceneBase {
472
+ nodes: MapSceneNode[];
473
+ groups: MapSceneGroup[];
474
+ edges: MapSceneEdge[];
475
+ }
476
+ interface MapLayoutOptions {
477
+ /** Entities to draw that are not in the model, such as ones a change removed. */
478
+ extraEntities?: Entity[];
479
+ extraRelationships?: Relationship[];
480
+ /** Groups drawn closed. Defaults to the view's own list. */
481
+ collapsed?: string[];
482
+ }
483
+ /** The space at the top of an open group, for its name. */
484
+ declare const GROUP: {
485
+ readonly head: 52;
486
+ readonly pad: 24;
487
+ };
488
+ declare const MAP: {
489
+ readonly h: 72;
490
+ readonly gapX: 120;
491
+ readonly gapY: 44;
492
+ readonly minW: 184;
493
+ readonly maxW: 260;
494
+ };
495
+ declare const LABEL: {
496
+ readonly size: 12;
497
+ readonly h: 22;
498
+ readonly subH: 36;
499
+ };
500
+ declare function mapNodeWidth(name: string): number;
501
+ declare function mapLabelSize(text: string, sub?: string): {
502
+ w: number;
503
+ h: 36 | 22;
504
+ };
505
+ declare function layoutMap(doc: PoneyDocument, view: ViewOf<'map'>, options?: MapLayoutOptions): Promise<MapScene>;
506
+ declare function bezier([p0, p1, p2, p3]: [Point, Point, Point, Point], t: number): Point;
507
+
508
+ /** Options a reader may change without changing the document. */
509
+ interface ViewLayoutOptions {
510
+ /** Architecture: containers and groups shown closed. Defaults to the view's own list. */
511
+ collapsed?: string[];
512
+ /** ERD: tables that show every field. */
513
+ expanded?: string[];
514
+ /** Data flow: also draw relationships that carry no data. */
515
+ controlFlow?: boolean;
516
+ /** Elements that are not in the model but must be drawn, such as ones a change removed. */
517
+ extraEntities?: Entity[];
518
+ extraRelationships?: Relationship[];
519
+ extraTransitions?: Transition[];
520
+ /** Sequence: draw only these messages and their participants. */
521
+ only?: string[];
522
+ /** Sequence: narrow columns, for stories. */
523
+ compact?: boolean;
524
+ }
525
+ type ExtensionLayout = (doc: PoneyDocument, view: Extract<View, {
526
+ type: 'extension';
527
+ }>, options: ViewLayoutOptions) => SceneBase | Promise<SceneBase>;
528
+ /** Registers the layout for an extension view type, e.g. "ownership/team-map". */
529
+ declare function registerExtensionLayout(viewType: string, layout: ExtensionLayout): void;
530
+ /** Lays out any view. The result is plain data that any renderer can draw. */
531
+ declare function layoutView(doc: PoneyDocument, view: View, options?: ViewLayoutOptions): Promise<SceneBase>;
532
+
533
+ export { ARCH_PADDING, ARCH_SIZES, type ActivityLane, type ActivityScene, type ActivitySceneEdge, type ActivitySceneNode, type ArchitectureScene, type ArchitectureSceneEdge, type ArchitectureSceneNode, BASE_OPTIONS, type DataFlowScene, type DependencyScene, ERD, type ErdScene, type ErdSceneRelation, type ErdSceneTable, type ExtensionLayout, type FlatElkEdge, type FlatElkGraph, type FlatElkNode, type FontFamily, GROUP, LABEL, MAP, type MapLayoutOptions, type MapScene, type MapSceneEdge, type MapSceneGroup, type MapSceneNode, type Point, type Rect, SEQ, type SceneBase, type SequenceActivation, type SequenceFragment, type SequenceLayoutOptions, type SequenceMessage, type SequenceParticipant, type SequenceScene, type StateScene, type StateSceneState, type StateSceneTransition, type TextMeasurer, type UseCaseScene, type ViewLayoutOptions, bezier, center, clip, endAngle, estimateText, labelPoint, layoutActivity, layoutArchitecture, layoutDataFlow, layoutDependency, layoutErd, layoutMap, layoutSequence, layoutState, layoutUseCase, layoutView, mapLabelSize, mapNodeWidth, measure, pointAlong, registerExtensionLayout, roundedPath, runElk, setFonts, setTextMeasurer, startAngle, transitionLabelSize, trimEnd, trimStart, union, wrap };