@adecore/diagram 0.0.0-stage → 0.17.0-beta.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,110 @@
1
+ # Functional Source License, Version 1.1, MIT Future License
2
+
3
+ ## Abbreviation
4
+
5
+ FSL-1.1-MIT
6
+
7
+ ## Notice
8
+
9
+ Copyright 2026 Bas Milius
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to
27
+ use, copy, modify, create derivative works, publicly perform, publicly display
28
+ and redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or
34
+ service that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software
39
+ that exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee
52
+ using the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to
59
+ the infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software,
68
+ you must include a copy of or a link to these Terms and Conditions and not
69
+ remove any copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
74
+ IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
75
+ PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
79
+ EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of
84
+ the Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the MIT license that is effective on the second anniversary of the date we make
91
+ the Software available. On or after that date, you may use the Software under
92
+ the MIT license, in which case the following will apply:
93
+
94
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
95
+ this software and associated documentation files (the "Software"), to deal in
96
+ the Software without restriction, including without limitation the rights to
97
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
98
+ of the Software, and to permit persons to whom the Software is furnished to do
99
+ so, subject to the following conditions:
100
+
101
+ The above copyright notice and this permission notice shall be included in all
102
+ copies or substantial portions of the Software.
103
+
104
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
105
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
106
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
107
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
108
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
109
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
110
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,35 @@
1
- # Temporary Holding Version
1
+ # @adecore/diagram
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
+ Directed graph schemas, deterministic layered layout, orthogonal routes, SVG export and reading order. The core has no DOM or canvas state. It depends on Zod and `@adecore/drawing` for shared schemas, geometry and palette defaults.
4
+
5
+ The package is private at `0.0.0` pending initial publication. Use local source exports or build drawing before diagram for default compiled JavaScript/declarations. The transferred code retains FSL-1.1-MIT in [LICENSE](./LICENSE).
6
+
7
+ ```ts
8
+ import { layoutOf, readingOrder, toSvg } from '@adecore/diagram';
9
+ import { DiagramDocumentSchema, diagramProblemIn } from '@adecore/diagram/protocol';
10
+
11
+ const document = DiagramDocumentSchema.parse({
12
+ version: 1,
13
+ rev: 0,
14
+ meta: { title: 'Delivery', direction: 'right' },
15
+ nodes: [
16
+ { id: 'draft', label: 'Draft' },
17
+ { id: 'review', label: 'Review', shape: 'diamond' }
18
+ ],
19
+ groups: [],
20
+ edges: [{ from: 'draft', to: 'review' }]
21
+ });
22
+ const problem = diagramProblemIn(document);
23
+ if (problem !== null) {
24
+ throw new Error(problem);
25
+ }
26
+ const layout = layoutOf(document);
27
+ const svg = toSvg(document, { layout });
28
+ const lines = readingOrder(document);
29
+ ```
30
+
31
+ Graph validation is separate from parsing. It caps documents at 100 nodes/120 edges and checks ids and references. Cycles, loops and parallel edges are supported. Pins keep manual positions but do not reserve automatic-layout space. The host owns dragging, collision policy, fonts, persistence and revision checks.
32
+
33
+ Read the [overview](https://adecore.dev/diagram/), [getting started](https://adecore.dev/diagram/getting-started), [layout guide](https://adecore.dev/diagram/layout), [painting/output guide](https://adecore.dev/diagram/rendering), [API reference](https://adecore.dev/diagram/api), [protocol](https://adecore.dev/diagram/protocol) and [integration/testing guide](https://adecore.dev/diagram/migration).
34
+
35
+ From the repository root, run `bun run --cwd packages/diagram test`, `typecheck` or `build`. A standalone source-relative example is in [examples/export-graph.ts](./examples/export-graph.ts).
@@ -0,0 +1,2 @@
1
+ import { type Layering } from './graph.ts';
2
+ export declare function placeAcross(layering: Layering): number[];
package/dist/across.js ADDED
@@ -0,0 +1,202 @@
1
+ import { BAND_MARGIN, bandOfCode } from './graph.js';
2
+ /* Rounds that pull every unit towards the units it is joined to, alternating the direction of the sweep. */
3
+ const ROUNDS = 8;
4
+ function clamp(value, low, high) {
5
+ return Math.min(high, Math.max(low, value));
6
+ }
7
+ /*
8
+ * Where every unit sits across the flow, as the top of its box, with the order of `orderLayers` kept.
9
+ * A group's block has one top in every layer it spans and its members sit inside it. It starts from
10
+ * the middle of the tightest packing towards either end, which is always a valid place, and then
11
+ * moves every unit towards the median of its neighbors as far as its own neighbors in the layer let
12
+ * it, so a chain lines up straight. Every step is clamped to whole numbers.
13
+ */
14
+ export function placeAcross(layering) {
15
+ const { units, bands, lines, items } = layering;
16
+ const unitCount = units.length;
17
+ const variableCount = unitCount + bands.length;
18
+ // A free unit is its own variable; a band is one variable for all its layers, its members sit at an offset from it.
19
+ const top = new Array(variableCount).fill(0);
20
+ const local = new Array(unitCount).fill(0);
21
+ const variableOf = (code) => (code >= 0 ? code : unitCount + bandOfCode(code));
22
+ const codeOf = (variable) => (variable < unitCount ? variable : -(variable - unitCount + 1));
23
+ const sizeOf = (code) => (code >= 0 ? units[code].h : bands[bandOfCode(code)].height);
24
+ const marginOf = (code) => (code >= 0 ? units[code].margin : BAND_MARGIN);
25
+ const topOf = (code) => top[variableOf(code)];
26
+ const absoluteTop = (unit) => {
27
+ const band = units[unit].band;
28
+ return band >= 0 ? top[unitCount + band] + local[unit] : top[unit];
29
+ };
30
+ const successors = Array.from({ length: variableCount }, () => []);
31
+ const incoming = new Array(variableCount).fill(0);
32
+ const present = new Array(variableCount).fill(false);
33
+ for (const row of items) {
34
+ row.forEach((code, index) => {
35
+ present[variableOf(code)] = true;
36
+ if (index > 0) {
37
+ successors[variableOf(row[index - 1])].push(code);
38
+ incoming[variableOf(code)] += 1;
39
+ }
40
+ });
41
+ }
42
+ // The band order is the same in every layer, so these constraints never form a cycle.
43
+ const lowest = new Array(variableCount).fill(0);
44
+ const queue = [];
45
+ for (let variable = 0; variable < variableCount; variable++) {
46
+ if (present[variable] && incoming[variable] === 0) {
47
+ queue.push(variable);
48
+ }
49
+ }
50
+ for (let i = 0; i < queue.length; i++) {
51
+ const variable = queue[i];
52
+ const code = codeOf(variable);
53
+ for (const next of successors[variable]) {
54
+ const target = variableOf(next);
55
+ lowest[target] = Math.max(lowest[target], lowest[variable] + sizeOf(code) + marginOf(code) + marginOf(next));
56
+ incoming[target] -= 1;
57
+ if (incoming[target] === 0) {
58
+ queue.push(target);
59
+ }
60
+ }
61
+ }
62
+ const tail = new Array(variableCount).fill(0);
63
+ let total = 0;
64
+ for (let i = queue.length - 1; i >= 0; i--) {
65
+ const variable = queue[i];
66
+ const code = codeOf(variable);
67
+ let length = sizeOf(code);
68
+ for (const next of successors[variable]) {
69
+ length = Math.max(length, sizeOf(code) + marginOf(code) + marginOf(next) + tail[variableOf(next)]);
70
+ }
71
+ tail[variable] = length;
72
+ total = Math.max(total, lowest[variable] + length);
73
+ }
74
+ for (const variable of queue) {
75
+ top[variable] = Math.floor((lowest[variable] + total - tail[variable]) / 2);
76
+ }
77
+ for (const band of bands) {
78
+ for (const slot of band.members) {
79
+ const low = [];
80
+ let at = band.before;
81
+ slot.forEach((unit, index) => {
82
+ if (index > 0) {
83
+ const previous = units[slot[index - 1]];
84
+ at += previous.h + previous.margin + units[unit].margin;
85
+ }
86
+ low.push(at);
87
+ });
88
+ let high = band.height - band.after;
89
+ for (let index = slot.length - 1; index >= 0; index--) {
90
+ const unit = units[slot[index]];
91
+ if (index < slot.length - 1) {
92
+ high -= unit.margin + units[slot[index + 1]].margin;
93
+ }
94
+ high -= unit.h;
95
+ local[slot[index]] = Math.floor((low[index] + high) / 2);
96
+ }
97
+ }
98
+ }
99
+ const medianOf = (values) => {
100
+ values.sort((left, right) => left - right);
101
+ const middle = values.length >> 1;
102
+ return values.length % 2 === 1 ? values[middle] : (values[middle - 1] + values[middle]) / 2;
103
+ };
104
+ const desiredTop = (unit) => {
105
+ const self = units[unit];
106
+ if (self.line >= 0) {
107
+ // The points of one long edge pull towards each other rather than their neighbors, so the edge runs straight
108
+ // instead of stepping a little in every layer it passes.
109
+ const line = lines[self.line];
110
+ const ends = [units[line[0]].up[0], units[line.at(-1)].down[0]].map((end) => absoluteTop(end) + units[end].h / 2);
111
+ if (line.length === 1) {
112
+ const here = absoluteTop(unit);
113
+ return Math.round(Math.abs(ends[1] - here) < Math.abs(ends[0] - here) ? ends[1] : ends[0]);
114
+ }
115
+ return Math.round(medianOf([...line.filter((other) => other !== unit).map(absoluteTop), ...ends]));
116
+ }
117
+ const neighbors = [...self.up, ...self.down];
118
+ if (neighbors.length === 0) {
119
+ return null;
120
+ }
121
+ return Math.round(medianOf(neighbors.map((neighbor) => absoluteTop(neighbor) + units[neighbor].h / 2)) - self.h / 2);
122
+ };
123
+ for (let round = 0; round < ROUNDS; round++) {
124
+ for (let step = 0; step < items.length; step++) {
125
+ const layer = round % 2 === 0 ? step : items.length - 1 - step;
126
+ const row = items[layer];
127
+ row.forEach((code, index) => {
128
+ if (code >= 0) {
129
+ const want = desiredTop(code);
130
+ if (want === null) {
131
+ return;
132
+ }
133
+ const previous = row[index - 1];
134
+ const next = row[index + 1];
135
+ const low = previous === undefined ? -Infinity : topOf(previous) + sizeOf(previous) + marginOf(previous) + marginOf(code);
136
+ const high = next === undefined ? Infinity : topOf(next) - marginOf(next) - marginOf(code) - sizeOf(code);
137
+ top[code] = clamp(want, low, high);
138
+ return;
139
+ }
140
+ const bandIndex = bandOfCode(code);
141
+ const band = bands[bandIndex];
142
+ const slot = band.members[layer - band.first];
143
+ slot.forEach((unit, position) => {
144
+ const want = desiredTop(unit);
145
+ if (want === null) {
146
+ return;
147
+ }
148
+ const self = units[unit];
149
+ const previous = slot[position - 1];
150
+ const next = slot[position + 1];
151
+ const low = previous === undefined ? band.before : local[previous] + units[previous].h + units[previous].margin + self.margin;
152
+ const high = next === undefined ? band.height - band.after - self.h : local[next] - units[next].margin - self.margin - self.h;
153
+ local[unit] = clamp(want - top[unitCount + bandIndex], low, high);
154
+ });
155
+ });
156
+ }
157
+ bands.forEach((band, bandIndex) => {
158
+ const shifts = [];
159
+ for (const unit of band.members.flat()) {
160
+ const want = desiredTop(unit);
161
+ if (want !== null) {
162
+ shifts.push(want - absoluteTop(unit));
163
+ }
164
+ }
165
+ if (shifts.length === 0) {
166
+ return;
167
+ }
168
+ const code = -(bandIndex + 1);
169
+ let low = -Infinity;
170
+ let high = Infinity;
171
+ for (let layer = band.first; layer <= band.last; layer++) {
172
+ const row = items[layer];
173
+ const index = row.indexOf(code);
174
+ const previous = row[index - 1];
175
+ const next = row[index + 1];
176
+ if (previous !== undefined) {
177
+ low = Math.max(low, topOf(previous) + sizeOf(previous) + marginOf(previous) + BAND_MARGIN);
178
+ }
179
+ if (next !== undefined) {
180
+ high = Math.min(high, topOf(next) - marginOf(next) - BAND_MARGIN - band.height);
181
+ }
182
+ }
183
+ const shift = Math.round(shifts.reduce((sum, value) => sum + value, 0) / shifts.length);
184
+ top[unitCount + bandIndex] = clamp(top[unitCount + bandIndex] + shift, low, high);
185
+ });
186
+ }
187
+ let least = Infinity;
188
+ for (const row of items) {
189
+ for (const code of row) {
190
+ least = Math.min(least, topOf(code));
191
+ }
192
+ }
193
+ if (Number.isFinite(least)) {
194
+ for (let variable = 0; variable < variableCount; variable++) {
195
+ top[variable] = top[variable] - least || 0;
196
+ }
197
+ }
198
+ bands.forEach((band, index) => {
199
+ band.top = top[unitCount + index];
200
+ });
201
+ return units.map((_, unit) => absoluteTop(unit));
202
+ }
@@ -0,0 +1,40 @@
1
+ export interface Unit {
2
+ layer: number;
3
+ node: number;
4
+ band: number;
5
+ w: number;
6
+ h: number;
7
+ margin: number;
8
+ line: number;
9
+ up: number[];
10
+ down: number[];
11
+ }
12
+ export interface Hop {
13
+ edge: number;
14
+ from: number;
15
+ to: number;
16
+ }
17
+ export interface Band {
18
+ group: number;
19
+ first: number;
20
+ last: number;
21
+ members: number[][];
22
+ top: number;
23
+ height: number;
24
+ before: number;
25
+ after: number;
26
+ rank: number;
27
+ labelWidth: number;
28
+ }
29
+ export interface Layering {
30
+ units: Unit[];
31
+ hops: Hop[];
32
+ bands: Band[];
33
+ lines: number[][];
34
+ items: number[][];
35
+ }
36
+ export declare function bandCode(band: number): number;
37
+ export declare function bandOfCode(code: number): number;
38
+ export declare const NODE_MARGIN = 16;
39
+ export declare const DUMMY_MARGIN = 8;
40
+ export declare const BAND_MARGIN = 16;
package/dist/graph.js ADDED
@@ -0,0 +1,14 @@
1
+ /*
2
+ * The graph the layout works on once every edge that skips a layer has a point per layer it passes
3
+ * through. Internal to the package: the coordinates it ends in are what `layoutOf` hands out.
4
+ */
5
+ export function bandCode(band) {
6
+ return -(band + 1);
7
+ }
8
+ export function bandOfCode(code) {
9
+ return -code - 1;
10
+ }
11
+ export const NODE_MARGIN = 16;
12
+ export const DUMMY_MARGIN = 8;
13
+ /* A node beside a group is this plus its own margin away from the border. */
14
+ export const BAND_MARGIN = 16;
@@ -0,0 +1,4 @@
1
+ export { type NodeBox, type GroupBox, type EdgeLabelBox, type EdgeRoute, type DiagramLayout, NODE_MAX_WIDTH, CYLINDER_LID, EDGE_LABEL_MAX_WIDTH, EDGE_LABEL_PADDING, GROUP_PADDING, GROUP_LABEL_BAND, GROUP_LABEL_INSET, type NodeSize, sizeOfNode, sizeOfEdgeLabel, layersOf, layoutOf, LABEL_LINE, LABEL_SIZE, SUB_LINE, SUB_SIZE, estimateTextWidth, wrapText, type Point, type Rect } from './layout.ts';
2
+ export { readingOrder } from './reading-order.ts';
3
+ export { type ShapePaths, shapePaths, type TextLine, textLinesOf, edgeLabelLinesOf, edgePath, arrowHeadPath, dashOf } from './shapes.ts';
4
+ export { type DiagramSvgOptions, DEFAULT_DIAGRAM_MARGIN, DEFAULT_NODE_TONE, DEFAULT_GROUP_TONE, DEFAULT_EDGE_TONE, toSvg } from './svg.ts';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { NODE_MAX_WIDTH, CYLINDER_LID, EDGE_LABEL_MAX_WIDTH, EDGE_LABEL_PADDING, GROUP_PADDING, GROUP_LABEL_BAND, GROUP_LABEL_INSET, sizeOfNode, sizeOfEdgeLabel, layersOf, layoutOf, LABEL_LINE, LABEL_SIZE, SUB_LINE, SUB_SIZE, estimateTextWidth, wrapText } from './layout.js';
2
+ export { readingOrder } from './reading-order.js';
3
+ export { shapePaths, textLinesOf, edgeLabelLinesOf, edgePath, arrowHeadPath, dashOf } from './shapes.js';
4
+ export { DEFAULT_DIAGRAM_MARGIN, DEFAULT_NODE_TONE, DEFAULT_GROUP_TONE, DEFAULT_EDGE_TONE, toSvg } from './svg.js';
@@ -0,0 +1,55 @@
1
+ import type { DiagramDocument, DiagramEdge, DiagramNode } from './protocol.ts';
2
+ import { type Point, type Rect } from '@adecore/drawing';
3
+ export { LABEL_LINE, LABEL_SIZE, SUB_LINE, SUB_SIZE, estimateTextWidth, wrapText } from './text.ts';
4
+ export type { Point, Rect } from '@adecore/drawing';
5
+ export interface NodeBox extends Rect {
6
+ id: string;
7
+ layer: number;
8
+ pinned: boolean;
9
+ label: string[];
10
+ sub: string[];
11
+ }
12
+ export interface GroupBox extends Rect {
13
+ id: string;
14
+ labelBox: Rect;
15
+ }
16
+ export interface EdgeLabelBox extends Rect {
17
+ lines: string[];
18
+ }
19
+ export interface EdgeRoute {
20
+ index: number;
21
+ from: string;
22
+ to: string;
23
+ points: Point[];
24
+ label: EdgeLabelBox | null;
25
+ }
26
+ export interface DiagramLayout {
27
+ nodes: NodeBox[];
28
+ groups: GroupBox[];
29
+ edges: EdgeRoute[];
30
+ bounds: Rect;
31
+ }
32
+ export declare const NODE_MAX_WIDTH = 280;
33
+ export declare const CYLINDER_LID = 10;
34
+ export declare const EDGE_LABEL_MAX_WIDTH = 160;
35
+ export declare const EDGE_LABEL_PADDING: {
36
+ x: number;
37
+ y: number;
38
+ };
39
+ export declare const GROUP_PADDING = 20;
40
+ export declare const GROUP_LABEL_BAND = 24;
41
+ export declare const GROUP_LABEL_INSET = 12;
42
+ export interface NodeSize {
43
+ w: number;
44
+ h: number;
45
+ label: string[];
46
+ sub: string[];
47
+ }
48
+ export declare function sizeOfNode(node: Pick<DiagramNode, 'label' | 'sub' | 'shape'>): NodeSize;
49
+ export declare function sizeOfEdgeLabel(text: string): {
50
+ w: number;
51
+ h: number;
52
+ lines: string[];
53
+ };
54
+ export declare function layersOf(nodes: readonly Pick<DiagramNode, 'id'>[], edges: readonly Pick<DiagramEdge, 'from' | 'to'>[]): Map<string, number>;
55
+ export declare function layoutOf(document: Pick<DiagramDocument, 'meta' | 'nodes' | 'groups' | 'edges'>): DiagramLayout;