@standarx/graph 0.0.0-stage → 1.0.0
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 +21 -0
- package/README.fr.md +436 -0
- package/README.md +435 -2
- package/THIRD_PARTY_NOTICES.md +22 -0
- package/assets/fonts/Inter-Regular.ttf +0 -0
- package/assets/fonts/Inter-SemiBold.ttf +0 -0
- package/assets/fonts/OFL.txt +92 -0
- package/dist/adapter-0bggwh8x.js +393 -0
- package/dist/cli-4m00785g.js +20 -0
- package/dist/cli-5v9761s9.js +330 -0
- package/dist/cli-9f1pdw5q.js +462 -0
- package/dist/cli-a4ax1vkc.js +172 -0
- package/dist/cli-b4n941t8.js +1439 -0
- package/dist/cli-eqq7a3wz.js +1144 -0
- package/dist/cli-gh5166k6.js +1565 -0
- package/dist/cli-kzp16pt5.js +207 -0
- package/dist/cli-mzmjkk5v.js +11 -0
- package/dist/cli-njgyf690.js +1060 -0
- package/dist/cli-nkm2j980.js +141 -0
- package/dist/cli-nyckgf1t.js +4 -0
- package/dist/cli-sfdqkbd1.js +433 -0
- package/dist/cli-txrzzr2q.js +0 -0
- package/dist/cli-vecew2na.js +709 -0
- package/dist/cli-wt9dmbhm.js +560 -0
- package/dist/cli-xhdxp3cj.js +387 -0
- package/dist/cli-ywgtv1cg.js +39 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +415 -0
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.js +93 -0
- package/dist/core/spatial.d.ts +16 -0
- package/dist/core/threshold.d.ts +1 -0
- package/dist/core/view.d.ts +80 -0
- package/dist/export/index.d.ts +18 -0
- package/dist/export/index.js +172 -0
- package/dist/export/pdf.d.ts +5 -0
- package/dist/export/png.d.ts +4 -0
- package/dist/format/index.d.ts +4 -0
- package/dist/format/index.js +71 -0
- package/dist/format/json-schema.d.ts +2 -0
- package/dist/format/schema.d.ts +787 -0
- package/dist/format/semantic/er.d.ts +3 -0
- package/dist/format/semantic/graph.d.ts +3 -0
- package/dist/format/semantic/sequence.d.ts +3 -0
- package/dist/format/semantic/shared.d.ts +14 -0
- package/dist/format/semantic/state.d.ts +3 -0
- package/dist/format/semantic/tree.d.ts +3 -0
- package/dist/format/transition-label.d.ts +2 -0
- package/dist/format/validate.d.ts +11 -0
- package/dist/html/index.d.ts +3 -0
- package/dist/html/index.js +127 -0
- package/dist/html/layout-worker.d.ts +1 -0
- package/dist/html/layout-worker.generated.d.ts +1 -0
- package/dist/html/mount.d.ts +12 -0
- package/dist/html/page.d.ts +14 -0
- package/dist/html/render.d.ts +7 -0
- package/dist/html/runtime.d.ts +1 -0
- package/dist/html/runtime.generated.d.ts +1 -0
- package/dist/html/shell.d.ts +10 -0
- package/dist/html/svg-adapter.d.ts +8 -0
- package/dist/html/ui.d.ts +34 -0
- package/dist/language/analysis.d.ts +38 -0
- package/dist/language/complete.d.ts +3 -0
- package/dist/language/docs.d.ts +11 -0
- package/dist/language/format.d.ts +17 -0
- package/dist/language/hover.d.ts +3 -0
- package/dist/language/index.d.ts +6 -0
- package/dist/language/index.js +1385 -0
- package/dist/language/navigation.d.ts +9 -0
- package/dist/language/positions.d.ts +7 -0
- package/dist/language/symbols.d.ts +3 -0
- package/dist/language/types.d.ts +34 -0
- package/dist/layout/elk.d.ts +35 -0
- package/dist/layout/er.d.ts +3 -0
- package/dist/layout/font-metrics.d.ts +9 -0
- package/dist/layout/graph.d.ts +3 -0
- package/dist/layout/index.d.ts +5 -0
- package/dist/layout/index.js +17 -0
- package/dist/layout/measure.d.ts +10 -0
- package/dist/layout/sequence.d.ts +3 -0
- package/dist/layout/shapes.d.ts +16 -0
- package/dist/layout/shared.d.ts +31 -0
- package/dist/layout/state.d.ts +3 -0
- package/dist/layout/tree.d.ts +3 -0
- package/dist/layout/types.d.ts +198 -0
- package/dist/layout-worker.generated-emmvgfb1.js +34 -0
- package/dist/mermaid/common.d.ts +36 -0
- package/dist/mermaid/er.d.ts +3 -0
- package/dist/mermaid/graph.d.ts +4 -0
- package/dist/mermaid/index.d.ts +6 -0
- package/dist/mermaid/index.js +6 -0
- package/dist/mermaid/mindmap.d.ts +3 -0
- package/dist/mermaid/sequence.d.ts +3 -0
- package/dist/mermaid/state.d.ts +3 -0
- package/dist/remark/index.d.ts +9 -0
- package/dist/remark/index.js +104 -0
- package/dist/shared/errors.d.ts +6 -0
- package/dist/shared/package-root.d.ts +5 -0
- package/dist/svg/escape.d.ts +1 -0
- package/dist/svg/index.d.ts +3 -0
- package/dist/svg/index.js +25 -0
- package/dist/svg/render.d.ts +9 -0
- package/dist/svg/theme.d.ts +62 -0
- package/dist/svg-adapter-gq5vmp6q.js +227 -0
- package/dist/text/build.d.ts +10 -0
- package/dist/text/canonical.d.ts +10 -0
- package/dist/text/errors.d.ts +33 -0
- package/dist/text/highlight.d.ts +13 -0
- package/dist/text/index.d.ts +33 -0
- package/dist/text/index.js +27 -0
- package/dist/text/lexer.d.ts +29 -0
- package/dist/text/parser.d.ts +160 -0
- package/dist/text/print.d.ts +6 -0
- package/dist/text/slug.d.ts +1 -0
- package/dist/text/syntax.d.ts +8 -0
- package/dist/version.d.ts +1 -0
- package/dist/webgl/adapter.d.ts +4 -0
- package/dist/webgl/index.d.ts +7 -0
- package/dist/webgl/index.js +15 -0
- package/dist/webgl/runtime.d.ts +1 -0
- package/dist/webgl/runtime.generated.d.ts +1 -0
- package/grammars/standargraph.tmLanguage.json +599 -0
- package/language-configuration.json +35 -0
- package/licenses/EPL-2.0.txt +277 -0
- package/licenses/MPL-2.0.txt +373 -0
- package/package.json +133 -4
- package/schema/graph-document.v1.json +1737 -0
|
@@ -0,0 +1,709 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SELF_LOOP_WIDTH,
|
|
3
|
+
SELF_LOOP_HEIGHT
|
|
4
|
+
} from "./cli-gh5166k6.js";
|
|
5
|
+
|
|
6
|
+
// src/core/spatial.ts
|
|
7
|
+
var MAX_CELLS = 262144;
|
|
8
|
+
var MIN_CELL = 32;
|
|
9
|
+
var LARGE_CELLS = 64;
|
|
10
|
+
function intersects(a, b) {
|
|
11
|
+
return a.x <= b.x + b.width && b.x <= a.x + a.width && a.y <= b.y + b.height && b.y <= a.y + a.height;
|
|
12
|
+
}
|
|
13
|
+
function createSpatialIndex2(entries, cellSize) {
|
|
14
|
+
const count = entries.length;
|
|
15
|
+
let minX = Infinity;
|
|
16
|
+
let minY = Infinity;
|
|
17
|
+
let maxX = -Infinity;
|
|
18
|
+
let maxY = -Infinity;
|
|
19
|
+
for (const { box } of entries) {
|
|
20
|
+
minX = Math.min(minX, box.x);
|
|
21
|
+
minY = Math.min(minY, box.y);
|
|
22
|
+
maxX = Math.max(maxX, box.x + box.width);
|
|
23
|
+
maxY = Math.max(maxY, box.y + box.height);
|
|
24
|
+
}
|
|
25
|
+
if (count === 0) {
|
|
26
|
+
minX = minY = 0;
|
|
27
|
+
maxX = maxY = 1;
|
|
28
|
+
}
|
|
29
|
+
const spanX = Math.max(1, maxX - minX);
|
|
30
|
+
const spanY = Math.max(1, maxY - minY);
|
|
31
|
+
let size = cellSize ?? Math.max(MIN_CELL, Math.sqrt(spanX * spanY / Math.max(1, count)) * 2);
|
|
32
|
+
while ((Math.floor(spanX / size) + 1) * (Math.floor(spanY / size) + 1) > MAX_CELLS) {
|
|
33
|
+
size *= 1.5;
|
|
34
|
+
}
|
|
35
|
+
const cols = Math.floor(spanX / size) + 1;
|
|
36
|
+
const rows = Math.floor(spanY / size) + 1;
|
|
37
|
+
const cells = new Map;
|
|
38
|
+
const large = [];
|
|
39
|
+
const column = (x) => Math.min(cols - 1, Math.max(0, Math.floor((x - minX) / size)));
|
|
40
|
+
const row = (y) => Math.min(rows - 1, Math.max(0, Math.floor((y - minY) / size)));
|
|
41
|
+
entries.forEach(({ box }, index) => {
|
|
42
|
+
const c0 = column(box.x);
|
|
43
|
+
const c1 = column(box.x + box.width);
|
|
44
|
+
const r0 = row(box.y);
|
|
45
|
+
const r1 = row(box.y + box.height);
|
|
46
|
+
if ((c1 - c0 + 1) * (r1 - r0 + 1) > LARGE_CELLS) {
|
|
47
|
+
large.push(index);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
for (let r = r0;r <= r1; r++) {
|
|
51
|
+
for (let c = c0;c <= c1; c++) {
|
|
52
|
+
const key = r * cols + c;
|
|
53
|
+
const cell = cells.get(key);
|
|
54
|
+
if (cell === undefined)
|
|
55
|
+
cells.set(key, [index]);
|
|
56
|
+
else
|
|
57
|
+
cell.push(index);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
const marks = new Int32Array(count);
|
|
62
|
+
let stamp = 0;
|
|
63
|
+
function query(rect) {
|
|
64
|
+
stamp += 1;
|
|
65
|
+
const found = [];
|
|
66
|
+
const visit = (index) => {
|
|
67
|
+
if (marks[index] === stamp)
|
|
68
|
+
return;
|
|
69
|
+
marks[index] = stamp;
|
|
70
|
+
const entry = entries[index];
|
|
71
|
+
if (intersects(entry.box, rect))
|
|
72
|
+
found.push(entry.value);
|
|
73
|
+
};
|
|
74
|
+
if (rect.x <= maxX && rect.x + rect.width >= minX && rect.y <= maxY && rect.y + rect.height >= minY) {
|
|
75
|
+
const c0 = column(rect.x);
|
|
76
|
+
const c1 = column(rect.x + rect.width);
|
|
77
|
+
const r0 = row(rect.y);
|
|
78
|
+
const r1 = row(rect.y + rect.height);
|
|
79
|
+
for (let r = r0;r <= r1; r++) {
|
|
80
|
+
for (let c = c0;c <= c1; c++) {
|
|
81
|
+
const cell = cells.get(r * cols + c);
|
|
82
|
+
if (cell === undefined)
|
|
83
|
+
continue;
|
|
84
|
+
for (const index of cell)
|
|
85
|
+
visit(index);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const index of large)
|
|
90
|
+
visit(index);
|
|
91
|
+
return found;
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
size: count,
|
|
95
|
+
query,
|
|
96
|
+
at: (x, y) => query({ x, y, width: 0, height: 0 })
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/core/view.ts
|
|
101
|
+
var HOVERABLE2 = new Set([
|
|
102
|
+
"node",
|
|
103
|
+
"participant",
|
|
104
|
+
"state",
|
|
105
|
+
"entity"
|
|
106
|
+
]);
|
|
107
|
+
function sceneItemCount2(scene) {
|
|
108
|
+
switch (scene.kind) {
|
|
109
|
+
case "graph":
|
|
110
|
+
return scene.groups.length + scene.nodes.length + scene.edges.length;
|
|
111
|
+
case "sequence":
|
|
112
|
+
return scene.participants.length + scene.messages.length;
|
|
113
|
+
case "state":
|
|
114
|
+
return scene.states.length + scene.transitions.length;
|
|
115
|
+
case "er":
|
|
116
|
+
return scene.entities.length + scene.relations.length;
|
|
117
|
+
case "tree":
|
|
118
|
+
return scene.nodes.length + scene.edges.length;
|
|
119
|
+
default:
|
|
120
|
+
return unreachable(scene);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function unreachable(value) {
|
|
124
|
+
throw new Error(`Unsupported scene: ${JSON.stringify(value)}`);
|
|
125
|
+
}
|
|
126
|
+
var DEFAULT_MIN_SCALE = 0.05;
|
|
127
|
+
var DEFAULT_MAX_SCALE = 8;
|
|
128
|
+
var DEFAULT_LABEL_MIN_PX = 7;
|
|
129
|
+
var LABEL_FONT_SIZE = 14;
|
|
130
|
+
var CAPTION_FONT_SIZE = 11;
|
|
131
|
+
var EDGE_LABEL_FONT_SIZE = 12;
|
|
132
|
+
var HIT_PX = 4;
|
|
133
|
+
var CULL_MARGIN_PX = 100;
|
|
134
|
+
var BEZIER_SAMPLES = 16;
|
|
135
|
+
var EMPTY = new Set;
|
|
136
|
+
var RECT = 0;
|
|
137
|
+
var SEGMENT = 1;
|
|
138
|
+
var LABEL = 2;
|
|
139
|
+
var EXTENT = 3;
|
|
140
|
+
function keyOf(kind, id) {
|
|
141
|
+
return `${kind}:${id}`;
|
|
142
|
+
}
|
|
143
|
+
function distanceToSegment(px, py, x1, y1, x2, y2) {
|
|
144
|
+
const dx = x2 - x1;
|
|
145
|
+
const dy = y2 - y1;
|
|
146
|
+
const lengthSquared = dx * dx + dy * dy;
|
|
147
|
+
const t = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, ((px - x1) * dx + (py - y1) * dy) / lengthSquared));
|
|
148
|
+
return Math.hypot(px - (x1 + t * dx), py - (y1 + t * dy));
|
|
149
|
+
}
|
|
150
|
+
function sampleBezier(points) {
|
|
151
|
+
const [p0, c1, c2, p3] = points;
|
|
152
|
+
if (p0 === undefined || c1 === undefined || c2 === undefined || p3 === undefined) {
|
|
153
|
+
return [...points];
|
|
154
|
+
}
|
|
155
|
+
const samples = [];
|
|
156
|
+
for (let step = 0;step < BEZIER_SAMPLES; step++) {
|
|
157
|
+
const t = step / (BEZIER_SAMPLES - 1);
|
|
158
|
+
const u = 1 - t;
|
|
159
|
+
const a = u * u * u;
|
|
160
|
+
const b = 3 * u * u * t;
|
|
161
|
+
const c = 3 * u * t * t;
|
|
162
|
+
const d = t * t * t;
|
|
163
|
+
samples.push({
|
|
164
|
+
x: a * p0.x + b * c1.x + c * c2.x + d * p3.x,
|
|
165
|
+
y: a * p0.y + b * c1.y + c * c2.y + d * p3.y
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return samples;
|
|
169
|
+
}
|
|
170
|
+
function contains(box, x, y) {
|
|
171
|
+
return x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.y + box.height;
|
|
172
|
+
}
|
|
173
|
+
function sameItems(a, b) {
|
|
174
|
+
if (a.length !== b.length)
|
|
175
|
+
return false;
|
|
176
|
+
for (let index = 0;index < a.length; index++) {
|
|
177
|
+
if (a[index] !== b[index])
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
function createView2(scene, options = {}) {
|
|
183
|
+
const minScale = options.minScale ?? DEFAULT_MIN_SCALE;
|
|
184
|
+
const maxScale = options.maxScale ?? DEFAULT_MAX_SCALE;
|
|
185
|
+
const padding = options.padding ?? 0;
|
|
186
|
+
const labelMinPx = options.labelMinPx ?? DEFAULT_LABEL_MIN_PX;
|
|
187
|
+
const sceneWidth = Math.max(1, scene.width);
|
|
188
|
+
const sceneHeight = Math.max(1, scene.height);
|
|
189
|
+
const entries = [];
|
|
190
|
+
const byKey = new Map;
|
|
191
|
+
const shapes = [];
|
|
192
|
+
const neighbors = new Map;
|
|
193
|
+
const searchable = [];
|
|
194
|
+
function addEntry(kind, id, item, depth = 0, container = false) {
|
|
195
|
+
const index = entries.length;
|
|
196
|
+
const key = keyOf(kind, id);
|
|
197
|
+
entries.push({ ref: { kind, id }, key, depth, container, item });
|
|
198
|
+
byKey.set(key, index);
|
|
199
|
+
return index;
|
|
200
|
+
}
|
|
201
|
+
function addShape(entry, role, x1, y1, x2, y2) {
|
|
202
|
+
const box = {
|
|
203
|
+
x: Math.min(x1, x2),
|
|
204
|
+
y: Math.min(y1, y2),
|
|
205
|
+
width: Math.abs(x2 - x1),
|
|
206
|
+
height: Math.abs(y2 - y1)
|
|
207
|
+
};
|
|
208
|
+
shapes.push({ box, value: { entry, role, box, x1, y1, x2, y2 } });
|
|
209
|
+
}
|
|
210
|
+
function addRect(entry, x, y, width, height) {
|
|
211
|
+
addShape(entry, RECT, x, y, x + width, y + height);
|
|
212
|
+
}
|
|
213
|
+
function addPath(entry, points) {
|
|
214
|
+
const first = points[0];
|
|
215
|
+
if (first === undefined)
|
|
216
|
+
return;
|
|
217
|
+
if (points.length === 1)
|
|
218
|
+
addShape(entry, SEGMENT, first.x, first.y, first.x, first.y);
|
|
219
|
+
for (let index = 1;index < points.length; index++) {
|
|
220
|
+
const a = points[index - 1];
|
|
221
|
+
const b = points[index];
|
|
222
|
+
addShape(entry, SEGMENT, a.x, a.y, b.x, b.y);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function link(a, b) {
|
|
226
|
+
let set = neighbors.get(a);
|
|
227
|
+
if (set === undefined)
|
|
228
|
+
neighbors.set(a, set = new Set);
|
|
229
|
+
set.add(b);
|
|
230
|
+
}
|
|
231
|
+
switch (scene.kind) {
|
|
232
|
+
case "graph": {
|
|
233
|
+
for (const group of scene.groups) {
|
|
234
|
+
const index = addEntry("group", group.id, group, group.depth, true);
|
|
235
|
+
addRect(index, group.x, group.y, group.width, group.height);
|
|
236
|
+
}
|
|
237
|
+
const edgeIndexes = [];
|
|
238
|
+
for (const edge of scene.edges) {
|
|
239
|
+
const index = addEntry("edge", edge.id, edge);
|
|
240
|
+
edgeIndexes.push(index);
|
|
241
|
+
addPath(index, edge.points);
|
|
242
|
+
if (edge.labelBox !== undefined) {
|
|
243
|
+
const box = edge.labelBox;
|
|
244
|
+
addShape(index, LABEL, box.x, box.y, box.x + box.width, box.y + box.height);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
for (const node of scene.nodes) {
|
|
248
|
+
const index = addEntry("node", node.id, node);
|
|
249
|
+
addRect(index, node.x, node.y, node.width, node.height);
|
|
250
|
+
searchable.push({ index, label: node.label.toLowerCase(), id: node.id.toLowerCase() });
|
|
251
|
+
}
|
|
252
|
+
scene.edges.forEach((edge, position) => {
|
|
253
|
+
const edgeKey = entries[edgeIndexes[position]].key;
|
|
254
|
+
const from = keyOf("node", edge.from);
|
|
255
|
+
const to = keyOf("node", edge.to);
|
|
256
|
+
for (const [self, other] of [[from, to], [to, from]]) {
|
|
257
|
+
if (!byKey.has(self))
|
|
258
|
+
continue;
|
|
259
|
+
link(self, edgeKey);
|
|
260
|
+
if (byKey.has(other))
|
|
261
|
+
link(self, other);
|
|
262
|
+
}
|
|
263
|
+
if (byKey.has(from))
|
|
264
|
+
link(edgeKey, from);
|
|
265
|
+
if (byKey.has(to))
|
|
266
|
+
link(edgeKey, to);
|
|
267
|
+
});
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
case "sequence": {
|
|
271
|
+
for (const participant of scene.participants) {
|
|
272
|
+
const index = addEntry("participant", participant.id, participant);
|
|
273
|
+
addRect(index, participant.x, participant.y, participant.width, participant.height);
|
|
274
|
+
addShape(index, EXTENT, participant.lifelineX, participant.y + participant.height, participant.lifelineX, scene.lifelineBottom);
|
|
275
|
+
searchable.push({
|
|
276
|
+
index,
|
|
277
|
+
label: participant.label.toLowerCase(),
|
|
278
|
+
id: participant.id.toLowerCase()
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
for (const message of scene.messages) {
|
|
282
|
+
const id = String(message.index);
|
|
283
|
+
const index = addEntry("message", id, message);
|
|
284
|
+
if (message.self) {
|
|
285
|
+
const right = message.x1 + SELF_LOOP_WIDTH;
|
|
286
|
+
const bottom = message.y + SELF_LOOP_HEIGHT;
|
|
287
|
+
addPath(index, [
|
|
288
|
+
{ x: message.x1, y: message.y },
|
|
289
|
+
{ x: right, y: message.y },
|
|
290
|
+
{ x: right, y: bottom },
|
|
291
|
+
{ x: message.x1, y: bottom }
|
|
292
|
+
]);
|
|
293
|
+
} else {
|
|
294
|
+
addPath(index, [
|
|
295
|
+
{ x: message.x1, y: message.y },
|
|
296
|
+
{ x: message.x2, y: message.y }
|
|
297
|
+
]);
|
|
298
|
+
}
|
|
299
|
+
if (message.labelBox !== undefined) {
|
|
300
|
+
const box = message.labelBox;
|
|
301
|
+
addShape(index, LABEL, box.x, box.y, box.x + box.width, box.y + box.height);
|
|
302
|
+
}
|
|
303
|
+
const messageKey = entries[index].key;
|
|
304
|
+
const from = keyOf("participant", message.from);
|
|
305
|
+
const to = keyOf("participant", message.to);
|
|
306
|
+
for (const [self, other] of [[from, to], [to, from]]) {
|
|
307
|
+
if (!byKey.has(self))
|
|
308
|
+
continue;
|
|
309
|
+
link(self, messageKey);
|
|
310
|
+
if (byKey.has(other))
|
|
311
|
+
link(self, other);
|
|
312
|
+
}
|
|
313
|
+
if (byKey.has(from))
|
|
314
|
+
link(messageKey, from);
|
|
315
|
+
if (byKey.has(to))
|
|
316
|
+
link(messageKey, to);
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
case "state": {
|
|
321
|
+
const addState = (state) => {
|
|
322
|
+
const index = addEntry("state", state.id, state, state.depth, state.composite);
|
|
323
|
+
addRect(index, state.x, state.y, state.width, state.height);
|
|
324
|
+
searchable.push({ index, label: state.label.toLowerCase(), id: state.id.toLowerCase() });
|
|
325
|
+
};
|
|
326
|
+
scene.states.filter((state) => state.composite).sort((a, b) => a.depth - b.depth).forEach(addState);
|
|
327
|
+
const transitionIndexes = [];
|
|
328
|
+
for (const transition of scene.transitions) {
|
|
329
|
+
const index = addEntry("transition", transition.id, transition);
|
|
330
|
+
transitionIndexes.push(index);
|
|
331
|
+
addPath(index, transition.points);
|
|
332
|
+
if (transition.labelBox !== undefined) {
|
|
333
|
+
const box = transition.labelBox;
|
|
334
|
+
addShape(index, LABEL, box.x, box.y, box.x + box.width, box.y + box.height);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
scene.states.filter((state) => !state.composite).forEach(addState);
|
|
338
|
+
scene.transitions.forEach((transition, position) => {
|
|
339
|
+
const transitionKey = entries[transitionIndexes[position]].key;
|
|
340
|
+
const from = keyOf("state", transition.from);
|
|
341
|
+
const to = keyOf("state", transition.to);
|
|
342
|
+
for (const [self, other] of [[from, to], [to, from]]) {
|
|
343
|
+
if (!byKey.has(self))
|
|
344
|
+
continue;
|
|
345
|
+
link(self, transitionKey);
|
|
346
|
+
if (byKey.has(other))
|
|
347
|
+
link(self, other);
|
|
348
|
+
}
|
|
349
|
+
if (byKey.has(from))
|
|
350
|
+
link(transitionKey, from);
|
|
351
|
+
if (byKey.has(to))
|
|
352
|
+
link(transitionKey, to);
|
|
353
|
+
});
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
case "er": {
|
|
357
|
+
const relationIndexes = [];
|
|
358
|
+
for (const relation of scene.relations) {
|
|
359
|
+
const index = addEntry("relation", relation.id, relation);
|
|
360
|
+
relationIndexes.push(index);
|
|
361
|
+
addPath(index, relation.points);
|
|
362
|
+
if (relation.labelBox !== undefined) {
|
|
363
|
+
const box = relation.labelBox;
|
|
364
|
+
addShape(index, LABEL, box.x, box.y, box.x + box.width, box.y + box.height);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
for (const entity of scene.entities) {
|
|
368
|
+
const index = addEntry("entity", entity.id, entity);
|
|
369
|
+
addRect(index, entity.x, entity.y, entity.width, entity.height);
|
|
370
|
+
searchable.push({
|
|
371
|
+
index,
|
|
372
|
+
label: [entity.label, ...entity.attributes.map((attribute) => attribute.name)].join(" ").toLowerCase(),
|
|
373
|
+
id: entity.id.toLowerCase()
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
scene.relations.forEach((relation, position) => {
|
|
377
|
+
const relationKey = entries[relationIndexes[position]].key;
|
|
378
|
+
const from = keyOf("entity", relation.from);
|
|
379
|
+
const to = keyOf("entity", relation.to);
|
|
380
|
+
for (const [self, other] of [[from, to], [to, from]]) {
|
|
381
|
+
if (!byKey.has(self))
|
|
382
|
+
continue;
|
|
383
|
+
link(self, relationKey);
|
|
384
|
+
if (byKey.has(other))
|
|
385
|
+
link(self, other);
|
|
386
|
+
}
|
|
387
|
+
if (byKey.has(from))
|
|
388
|
+
link(relationKey, from);
|
|
389
|
+
if (byKey.has(to))
|
|
390
|
+
link(relationKey, to);
|
|
391
|
+
});
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
case "tree": {
|
|
395
|
+
const edgeIndexes = [];
|
|
396
|
+
for (const edge of scene.edges) {
|
|
397
|
+
const index = addEntry("edge", edge.id, edge);
|
|
398
|
+
edgeIndexes.push(index);
|
|
399
|
+
addPath(index, edge.curve === "bezier" ? sampleBezier(edge.points) : edge.points);
|
|
400
|
+
}
|
|
401
|
+
for (const node of scene.nodes) {
|
|
402
|
+
const index = addEntry("node", node.id, node);
|
|
403
|
+
addRect(index, node.x, node.y, node.width, node.height);
|
|
404
|
+
searchable.push({ index, label: node.label.toLowerCase(), id: node.id.toLowerCase() });
|
|
405
|
+
}
|
|
406
|
+
scene.edges.forEach((edge, position) => {
|
|
407
|
+
const edgeKey = entries[edgeIndexes[position]].key;
|
|
408
|
+
const from = keyOf("node", edge.from);
|
|
409
|
+
const to = keyOf("node", edge.to);
|
|
410
|
+
for (const [self, other] of [[from, to], [to, from]]) {
|
|
411
|
+
if (!byKey.has(self))
|
|
412
|
+
continue;
|
|
413
|
+
link(self, edgeKey);
|
|
414
|
+
if (byKey.has(other))
|
|
415
|
+
link(self, other);
|
|
416
|
+
}
|
|
417
|
+
if (byKey.has(from))
|
|
418
|
+
link(edgeKey, from);
|
|
419
|
+
if (byKey.has(to))
|
|
420
|
+
link(edgeKey, to);
|
|
421
|
+
});
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
default:
|
|
425
|
+
unreachable(scene);
|
|
426
|
+
}
|
|
427
|
+
const grid = createSpatialIndex2(shapes);
|
|
428
|
+
const itemCount = entries.length;
|
|
429
|
+
const mark = new Int32Array(itemCount);
|
|
430
|
+
let markStamp = 0;
|
|
431
|
+
let camera = { x: 0, y: 0, scale: 1 };
|
|
432
|
+
let viewportSize = { width: 0, height: 0 };
|
|
433
|
+
let hoveredIndex = -1;
|
|
434
|
+
let selectedIndex = -1;
|
|
435
|
+
let query = "";
|
|
436
|
+
let matchKeys = EMPTY;
|
|
437
|
+
let highlightState = { focused: false, active: EMPTY };
|
|
438
|
+
let visibleIndexes = computeVisible();
|
|
439
|
+
let visibleItems = null;
|
|
440
|
+
let lodState = computeLod(1);
|
|
441
|
+
let depth = 0;
|
|
442
|
+
let pending = new Set;
|
|
443
|
+
let stale = false;
|
|
444
|
+
const listeners = new Set;
|
|
445
|
+
function computeLod(scale) {
|
|
446
|
+
return {
|
|
447
|
+
labels: LABEL_FONT_SIZE * scale >= labelMinPx,
|
|
448
|
+
captions: CAPTION_FONT_SIZE * scale >= labelMinPx,
|
|
449
|
+
edgeLabels: EDGE_LABEL_FONT_SIZE * scale >= labelMinPx
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
function lowerBound() {
|
|
453
|
+
const { width, height } = viewportSize;
|
|
454
|
+
if (width <= 0 || height <= 0)
|
|
455
|
+
return minScale;
|
|
456
|
+
const fitScale = Math.min(Math.max(1, width - 2 * padding) / sceneWidth, Math.max(1, height - 2 * padding) / sceneHeight);
|
|
457
|
+
return Math.min(minScale, fitScale);
|
|
458
|
+
}
|
|
459
|
+
function clampScale(scale) {
|
|
460
|
+
return Math.min(maxScale, Math.max(lowerBound(), scale));
|
|
461
|
+
}
|
|
462
|
+
function assignCamera(next) {
|
|
463
|
+
if (next.x === camera.x && next.y === camera.y && next.scale === camera.scale)
|
|
464
|
+
return;
|
|
465
|
+
camera = next;
|
|
466
|
+
pending.add("camera");
|
|
467
|
+
stale = true;
|
|
468
|
+
}
|
|
469
|
+
function computeVisible() {
|
|
470
|
+
const { width, height } = viewportSize;
|
|
471
|
+
if (width <= 0 || height <= 0) {
|
|
472
|
+
const all = new Int32Array(itemCount);
|
|
473
|
+
for (let position = 0;position < itemCount; position++)
|
|
474
|
+
all[position] = position;
|
|
475
|
+
return all;
|
|
476
|
+
}
|
|
477
|
+
const margin = CULL_MARGIN_PX / camera.scale;
|
|
478
|
+
const found = grid.query({
|
|
479
|
+
x: -camera.x / camera.scale - margin,
|
|
480
|
+
y: -camera.y / camera.scale - margin,
|
|
481
|
+
width: width / camera.scale + 2 * margin,
|
|
482
|
+
height: height / camera.scale + 2 * margin
|
|
483
|
+
});
|
|
484
|
+
markStamp += 1;
|
|
485
|
+
const list = [];
|
|
486
|
+
for (const shape of found) {
|
|
487
|
+
if (mark[shape.entry] === markStamp)
|
|
488
|
+
continue;
|
|
489
|
+
mark[shape.entry] = markStamp;
|
|
490
|
+
list.push(shape.entry);
|
|
491
|
+
}
|
|
492
|
+
return Int32Array.from(list).sort();
|
|
493
|
+
}
|
|
494
|
+
function refresh() {
|
|
495
|
+
if (!stale)
|
|
496
|
+
return;
|
|
497
|
+
stale = false;
|
|
498
|
+
const nextLod = computeLod(camera.scale);
|
|
499
|
+
if (nextLod.labels !== lodState.labels || nextLod.captions !== lodState.captions || nextLod.edgeLabels !== lodState.edgeLabels) {
|
|
500
|
+
lodState = nextLod;
|
|
501
|
+
pending.add("lod");
|
|
502
|
+
}
|
|
503
|
+
const nextVisible = computeVisible();
|
|
504
|
+
if (!sameItems(nextVisible, visibleIndexes)) {
|
|
505
|
+
visibleIndexes = nextVisible;
|
|
506
|
+
visibleItems = null;
|
|
507
|
+
pending.add("visible");
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
function flush() {
|
|
511
|
+
refresh();
|
|
512
|
+
if (pending.size === 0)
|
|
513
|
+
return;
|
|
514
|
+
const changes = pending;
|
|
515
|
+
pending = new Set;
|
|
516
|
+
for (const listener of [...listeners])
|
|
517
|
+
listener(changes);
|
|
518
|
+
}
|
|
519
|
+
function run(fn) {
|
|
520
|
+
depth += 1;
|
|
521
|
+
try {
|
|
522
|
+
fn();
|
|
523
|
+
} finally {
|
|
524
|
+
depth -= 1;
|
|
525
|
+
if (depth === 0)
|
|
526
|
+
flush();
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
function recomputeHighlight() {
|
|
530
|
+
if (hoveredIndex >= 0) {
|
|
531
|
+
const entry = entries[hoveredIndex];
|
|
532
|
+
const active = new Set([entry.key]);
|
|
533
|
+
for (const key of neighbors.get(entry.key) ?? [])
|
|
534
|
+
active.add(key);
|
|
535
|
+
highlightState = { focused: true, active };
|
|
536
|
+
} else if (query !== "") {
|
|
537
|
+
highlightState = { focused: true, active: matchKeys };
|
|
538
|
+
} else {
|
|
539
|
+
highlightState = { focused: false, active: EMPTY };
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
function lookup(ref) {
|
|
543
|
+
if (ref === null)
|
|
544
|
+
return -1;
|
|
545
|
+
return byKey.get(keyOf(ref.kind, ref.id)) ?? -1;
|
|
546
|
+
}
|
|
547
|
+
const view = {
|
|
548
|
+
scene,
|
|
549
|
+
camera: () => ({ ...camera }),
|
|
550
|
+
viewport: () => ({ ...viewportSize }),
|
|
551
|
+
setViewport(width, height) {
|
|
552
|
+
if (width === viewportSize.width && height === viewportSize.height)
|
|
553
|
+
return;
|
|
554
|
+
run(() => {
|
|
555
|
+
viewportSize = { width, height };
|
|
556
|
+
pending.add("viewport");
|
|
557
|
+
stale = true;
|
|
558
|
+
});
|
|
559
|
+
},
|
|
560
|
+
setCamera(next) {
|
|
561
|
+
if (![next.x, next.y, next.scale].every(Number.isFinite) || next.scale <= 0)
|
|
562
|
+
return;
|
|
563
|
+
run(() => assignCamera({ x: next.x, y: next.y, scale: clampScale(next.scale) }));
|
|
564
|
+
},
|
|
565
|
+
fit() {
|
|
566
|
+
const { width, height } = viewportSize;
|
|
567
|
+
if (width <= 0 || height <= 0)
|
|
568
|
+
return;
|
|
569
|
+
run(() => {
|
|
570
|
+
const available = Math.min(Math.max(1, width - 2 * padding) / sceneWidth, Math.max(1, height - 2 * padding) / sceneHeight);
|
|
571
|
+
const scale = clampScale(Math.min(available, 1));
|
|
572
|
+
assignCamera({
|
|
573
|
+
scale,
|
|
574
|
+
x: (width - scene.width * scale) / 2,
|
|
575
|
+
y: (height - scene.height * scale) / 2
|
|
576
|
+
});
|
|
577
|
+
});
|
|
578
|
+
},
|
|
579
|
+
panBy(dx, dy) {
|
|
580
|
+
if (dx === 0 && dy === 0)
|
|
581
|
+
return;
|
|
582
|
+
run(() => assignCamera({ x: camera.x + dx, y: camera.y + dy, scale: camera.scale }));
|
|
583
|
+
},
|
|
584
|
+
zoomAt(factor, screenX, screenY) {
|
|
585
|
+
if (!Number.isFinite(factor) || factor <= 0)
|
|
586
|
+
return;
|
|
587
|
+
run(() => {
|
|
588
|
+
const next = clampScale(camera.scale * factor);
|
|
589
|
+
const ratio = next / camera.scale;
|
|
590
|
+
assignCamera({
|
|
591
|
+
scale: next,
|
|
592
|
+
x: screenX - (screenX - camera.x) * ratio,
|
|
593
|
+
y: screenY - (screenY - camera.y) * ratio
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
},
|
|
597
|
+
screenToWorld: (x, y) => ({
|
|
598
|
+
x: (x - camera.x) / camera.scale,
|
|
599
|
+
y: (y - camera.y) / camera.scale
|
|
600
|
+
}),
|
|
601
|
+
worldToScreen: (x, y) => ({
|
|
602
|
+
x: x * camera.scale + camera.x,
|
|
603
|
+
y: y * camera.scale + camera.y
|
|
604
|
+
}),
|
|
605
|
+
hitTest(screenX, screenY) {
|
|
606
|
+
const px = (screenX - camera.x) / camera.scale;
|
|
607
|
+
const py = (screenY - camera.y) / camera.scale;
|
|
608
|
+
const tolerance = HIT_PX / camera.scale;
|
|
609
|
+
const near = grid.query({
|
|
610
|
+
x: px - tolerance,
|
|
611
|
+
y: py - tolerance,
|
|
612
|
+
width: 2 * tolerance,
|
|
613
|
+
height: 2 * tolerance
|
|
614
|
+
});
|
|
615
|
+
let top = -1;
|
|
616
|
+
let line = -1;
|
|
617
|
+
let lineDistance = Infinity;
|
|
618
|
+
let group = -1;
|
|
619
|
+
let groupDepth = -1;
|
|
620
|
+
for (const shape of near) {
|
|
621
|
+
const entry = entries[shape.entry];
|
|
622
|
+
if (shape.role === RECT) {
|
|
623
|
+
if (!contains(shape.box, px, py))
|
|
624
|
+
continue;
|
|
625
|
+
if (entry.container) {
|
|
626
|
+
if (entry.depth > groupDepth || entry.depth === groupDepth && shape.entry > group) {
|
|
627
|
+
group = shape.entry;
|
|
628
|
+
groupDepth = entry.depth;
|
|
629
|
+
}
|
|
630
|
+
} else if (shape.entry > top) {
|
|
631
|
+
top = shape.entry;
|
|
632
|
+
}
|
|
633
|
+
} else if (shape.role === SEGMENT || shape.role === LABEL) {
|
|
634
|
+
const distance = shape.role === LABEL ? contains(shape.box, px, py) ? 0 : Infinity : distanceToSegment(px, py, shape.x1, shape.y1, shape.x2, shape.y2);
|
|
635
|
+
if (distance <= tolerance && (distance < lineDistance || distance === lineDistance && shape.entry > line)) {
|
|
636
|
+
line = shape.entry;
|
|
637
|
+
lineDistance = distance;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
const found = top >= 0 ? top : line >= 0 ? line : group;
|
|
642
|
+
return found >= 0 ? entries[found].ref : null;
|
|
643
|
+
},
|
|
644
|
+
visible() {
|
|
645
|
+
if (visibleItems === null) {
|
|
646
|
+
visibleItems = Array.from(visibleIndexes, (position) => entries[position].ref);
|
|
647
|
+
}
|
|
648
|
+
return { items: visibleItems, count: visibleIndexes.length };
|
|
649
|
+
},
|
|
650
|
+
lod: () => lodState,
|
|
651
|
+
hover(ref) {
|
|
652
|
+
const next = lookup(ref);
|
|
653
|
+
if (next === hoveredIndex)
|
|
654
|
+
return;
|
|
655
|
+
run(() => {
|
|
656
|
+
hoveredIndex = next;
|
|
657
|
+
recomputeHighlight();
|
|
658
|
+
pending.add("hover");
|
|
659
|
+
});
|
|
660
|
+
},
|
|
661
|
+
select(ref) {
|
|
662
|
+
const next = lookup(ref);
|
|
663
|
+
if (next === selectedIndex)
|
|
664
|
+
return;
|
|
665
|
+
run(() => {
|
|
666
|
+
selectedIndex = next;
|
|
667
|
+
pending.add("selection");
|
|
668
|
+
});
|
|
669
|
+
},
|
|
670
|
+
search(text) {
|
|
671
|
+
const next = text.trim().toLowerCase();
|
|
672
|
+
if (next === query)
|
|
673
|
+
return;
|
|
674
|
+
run(() => {
|
|
675
|
+
query = next;
|
|
676
|
+
if (next === "") {
|
|
677
|
+
matchKeys = EMPTY;
|
|
678
|
+
} else {
|
|
679
|
+
const keys = new Set;
|
|
680
|
+
for (const entry of searchable) {
|
|
681
|
+
if (entry.label.includes(next) || entry.id.includes(next)) {
|
|
682
|
+
keys.add(entries[entry.index].key);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
matchKeys = keys;
|
|
686
|
+
}
|
|
687
|
+
recomputeHighlight();
|
|
688
|
+
pending.add("search");
|
|
689
|
+
});
|
|
690
|
+
},
|
|
691
|
+
hovered: () => hoveredIndex >= 0 ? entries[hoveredIndex].ref : null,
|
|
692
|
+
selected: () => selectedIndex >= 0 ? entries[selectedIndex].ref : null,
|
|
693
|
+
highlight: () => highlightState,
|
|
694
|
+
item(ref) {
|
|
695
|
+
const found = lookup(ref);
|
|
696
|
+
return found >= 0 ? entries[found].item : undefined;
|
|
697
|
+
},
|
|
698
|
+
subscribe(listener) {
|
|
699
|
+
listeners.add(listener);
|
|
700
|
+
return () => {
|
|
701
|
+
listeners.delete(listener);
|
|
702
|
+
};
|
|
703
|
+
},
|
|
704
|
+
batch: run
|
|
705
|
+
};
|
|
706
|
+
return view;
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
export { createSpatialIndex2, HOVERABLE2, sceneItemCount2, createView2 };
|