glassbrain 0.2.0-alpha.6 → 0.2.0-alpha.8
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/README.md +6 -7
- package/dist/chunks/{chunk-LMYD4QHC.js → chunk-3CNHFYHG.js} +1329 -1610
- package/dist/chunks/chunk-7PWRJEZT.js +35 -0
- package/dist/chunks/chunk-CHOCJIOD.js +1340 -0
- package/dist/chunks/chunk-DHYIMBHU.js +960 -0
- package/dist/chunks/{chunk-3SAL4526.js → chunk-NDUF3CBQ.js} +349 -989
- package/dist/cli/glassbrain.js +441 -230
- package/dist/cli/viewer.js +228 -194
- package/dist/core.d.ts +2 -0
- package/dist/core.js +54 -26
- package/dist/diff.d.ts +9 -1
- package/dist/edit.d.ts +10 -0
- package/dist/index.js +63 -35
- package/dist/internal.d.ts +1 -0
- package/dist/internal.js +103 -73
- package/dist/kinds.d.ts +23 -0
- package/dist/layers.d.ts +3 -1
- package/dist/lint-report.d.ts +70 -0
- package/dist/profile.d.ts +15 -3
- package/dist/react/DetailPanel.d.ts +31 -1
- package/dist/react/DetailPanel.js +8 -4
- package/dist/react/EdgeMark.d.ts +22 -0
- package/dist/react/EditPanel.d.ts +6 -6
- package/dist/react/GraphCanvas.d.ts +18 -5
- package/dist/react/GraphCanvas.js +4 -4
- package/dist/react/Inspector.d.ts +72 -0
- package/dist/react/ObjectRef.d.ts +12 -0
- package/dist/react/Toolbar.d.ts +10 -3
- package/dist/react/ZoomBoundary.d.ts +4 -0
- package/dist/react/camera.d.ts +7 -0
- package/dist/react/i18n.d.ts +2 -2
- package/dist/react/icons.d.ts +7 -1
- package/dist/react/input.d.ts +5 -3
- package/dist/react/useSubgraph.d.ts +15 -0
- package/dist/shell.d.ts +1 -0
- package/dist/strings.d.ts +24 -3
- package/dist/style.d.ts +2 -0
- package/dist/subgraph.d.ts +13 -0
- package/dist/tree.d.ts +1 -0
- package/dist/view.d.ts +3 -1
- package/package.json +1 -1
- package/schema/profile.schema.json +2 -2
- package/dist/chunks/chunk-3P34XHQ2.js +0 -244
- package/dist/chunks/chunk-5DEN6HXJ.js +0 -99
- package/dist/chunks/chunk-M3MAM2VD.js +0 -67
|
@@ -0,0 +1,960 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DIFF_FLAG,
|
|
3
|
+
EMPTY_MODEL,
|
|
4
|
+
STEP_FLAG,
|
|
5
|
+
VIRTUAL_ROOT,
|
|
6
|
+
buildModel,
|
|
7
|
+
indexModel,
|
|
8
|
+
mergeSlices,
|
|
9
|
+
nodeClasses,
|
|
10
|
+
originsOfNode,
|
|
11
|
+
serializeTree,
|
|
12
|
+
treeDeclarations,
|
|
13
|
+
treeNodes
|
|
14
|
+
} from "./chunk-NDUF3CBQ.js";
|
|
15
|
+
|
|
16
|
+
// src/profile.ts
|
|
17
|
+
function parsePhysical(origin, text) {
|
|
18
|
+
let raw;
|
|
19
|
+
try {
|
|
20
|
+
raw = JSON.parse(text);
|
|
21
|
+
} catch (e) {
|
|
22
|
+
return { origin, title: origin, nodes: [], edges: [], error: e instanceof Error ? e.message : String(e) };
|
|
23
|
+
}
|
|
24
|
+
if (!raw || typeof raw !== "object" || !("nodes" in raw) && !("edges" in raw)) return null;
|
|
25
|
+
const edges = (Array.isArray(raw.edges) ? raw.edges : []).filter((e) => e && typeof e === "object").map((e) => ({
|
|
26
|
+
source: String(e.source ?? e.from),
|
|
27
|
+
target: String(e.target ?? e.to),
|
|
28
|
+
kind: typeof e.kind === "string" ? e.kind : void 0,
|
|
29
|
+
label: typeof e.label === "string" ? e.label : void 0,
|
|
30
|
+
weight: typeof e.weight === "number" ? e.weight : void 0,
|
|
31
|
+
// The whole record, like nodes' `data`: fields the library does not know survive a round trip (convert, toPhysical).
|
|
32
|
+
data: e
|
|
33
|
+
}));
|
|
34
|
+
const parsed = treeDeclarations(raw.nodes, origin);
|
|
35
|
+
return { origin, title: String(raw.name ?? raw.title ?? origin), nodes: treeNodes(parsed.declarations), edges, ...parsed };
|
|
36
|
+
}
|
|
37
|
+
function toPhysical(model) {
|
|
38
|
+
return serializeTree(model);
|
|
39
|
+
}
|
|
40
|
+
function toBundle(model, profile = null) {
|
|
41
|
+
const palette = Object.assign(/* @__PURE__ */ Object.create(null), profile?.nodePalette ?? {});
|
|
42
|
+
const wheel = ["#6366f1", "#f59e0b", "#10b981", "#ef4444", "#8b5cf6", "#06b6d4", "#ec4899", "#84cc16"];
|
|
43
|
+
const kinds = [...new Set(model.nodes.map((n) => n.kind).filter((k) => !!k))];
|
|
44
|
+
for (const [i, kind] of kinds.entries()) if (!Object.hasOwn(palette, kind)) palette[kind] = wheel[i % wheel.length];
|
|
45
|
+
return { profile: { ...profile, nodePalette: palette }, ...toPhysical(model) };
|
|
46
|
+
}
|
|
47
|
+
var DEFAULT_LAYER = { id: "default", label: "Graph", z: 0, structural: true };
|
|
48
|
+
function validateStructure(model) {
|
|
49
|
+
const out = model.errors.map((e) => ({ kind: e.code ?? "schema", group: "Invalid graph", level: "error", nodeId: e.node, message: `${e.origin}: ${e.message}` }));
|
|
50
|
+
const seen = /* @__PURE__ */ new Set();
|
|
51
|
+
const dup = /* @__PURE__ */ new Set();
|
|
52
|
+
for (const n of model.nodes) {
|
|
53
|
+
if (seen.has(n.id)) dup.add(n.id);
|
|
54
|
+
else seen.add(n.id);
|
|
55
|
+
}
|
|
56
|
+
for (const id of model.duplicateIds ?? []) dup.add(id);
|
|
57
|
+
for (const id of dup) out.push({ kind: "duplicate", group: "Duplicate id", level: "warn", nodeId: id, message: `Duplicate node id: ${id} (defined more than once; the first one is kept)` });
|
|
58
|
+
const seenEdge = /* @__PURE__ */ new Set();
|
|
59
|
+
const dupEdge = /* @__PURE__ */ new Map();
|
|
60
|
+
for (const e of model.edges) {
|
|
61
|
+
const key = `${e.source}\0${e.target}\0${e.kind ?? ""}`;
|
|
62
|
+
if (seenEdge.has(key)) dupEdge.set(key, e);
|
|
63
|
+
else seenEdge.add(key);
|
|
64
|
+
}
|
|
65
|
+
for (const e of dupEdge.values()) {
|
|
66
|
+
out.push({
|
|
67
|
+
kind: "duplicate-edge",
|
|
68
|
+
group: "Duplicate edge",
|
|
69
|
+
level: "warn",
|
|
70
|
+
edge: { source: e.source, target: e.target },
|
|
71
|
+
message: `Duplicate edge: ${e.source} \u2192 ${e.target}${e.kind ? ` (${e.kind})` : ""} is defined more than once`
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
for (const e of model.edges) {
|
|
75
|
+
const miss = [!seen.has(e.source) && e.source, !seen.has(e.target) && e.target].filter(Boolean).join(" / ");
|
|
76
|
+
if (miss) out.push({ kind: "dangling", group: "Dangling edge", level: "warn", edge: { source: e.source, target: e.target }, message: `Dangling edge: points to missing node ${miss}` });
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
function kindTable(...entries) {
|
|
81
|
+
return Object.assign(/* @__PURE__ */ Object.create(null), ...entries);
|
|
82
|
+
}
|
|
83
|
+
var noDataReview = () => [];
|
|
84
|
+
function defineProfile(input) {
|
|
85
|
+
const layers = input.layers?.length ? input.layers : [DEFAULT_LAYER];
|
|
86
|
+
const fallbackLayer = layers[0].id;
|
|
87
|
+
return {
|
|
88
|
+
id: input.id,
|
|
89
|
+
parse: input.parse ?? parsePhysical,
|
|
90
|
+
merge: input.merge ?? mergeSlices,
|
|
91
|
+
layers,
|
|
92
|
+
structuralLayers: input.structuralLayers ?? !input.layers?.length,
|
|
93
|
+
layerOf: input.layerOf ?? ((n) => n.layer ?? fallbackLayer),
|
|
94
|
+
nodePalette: kindTable(input.nodePalette),
|
|
95
|
+
edgePalette: kindTable(input.edgePalette),
|
|
96
|
+
nodeSize: kindTable(input.nodeSize),
|
|
97
|
+
edgeWidth: kindTable(input.edgeWidth),
|
|
98
|
+
placeholderFlags: input.placeholderFlags ?? [],
|
|
99
|
+
style: input.style ?? [],
|
|
100
|
+
nodeClass: input.nodeClass ?? (() => ""),
|
|
101
|
+
edgeClass: input.edgeClass ?? ((e) => e.kind ?? ""),
|
|
102
|
+
kindLabels: kindTable(input.kindLabels),
|
|
103
|
+
detail: input.detail ?? {},
|
|
104
|
+
validate: input.validate ?? noDataReview,
|
|
105
|
+
layoutEdge: input.layoutEdge ?? (() => false),
|
|
106
|
+
singleRoot: input.singleRoot ?? true,
|
|
107
|
+
rootLabel: input.rootLabel ?? "Overview"
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
var defaultProfile = defineProfile({ id: "default" });
|
|
111
|
+
function parseSources(sources, profile = defaultProfile) {
|
|
112
|
+
const slices = [];
|
|
113
|
+
for (const { origin, text } of sources) {
|
|
114
|
+
const s = profile.parse(origin, text);
|
|
115
|
+
if (!s) continue;
|
|
116
|
+
slices.push({
|
|
117
|
+
...s,
|
|
118
|
+
origin,
|
|
119
|
+
nodes: s.nodes.map((n) => ({
|
|
120
|
+
...n,
|
|
121
|
+
origin: n.origin ?? origin,
|
|
122
|
+
origins: n.origins ?? [origin],
|
|
123
|
+
layer: n.layer ?? profile.layerOf(n)
|
|
124
|
+
})),
|
|
125
|
+
edges: s.edges.map((e) => ({ ...e, origin: e.origin ?? origin }))
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const model = profile.merge(slices);
|
|
129
|
+
const nodes = model.nodes.map((n) => n.layer ? n : { ...n, layer: profile.layerOf(n) });
|
|
130
|
+
const out = buildModel(nodes, model.edges, model.errors);
|
|
131
|
+
return model.duplicateIds ? { ...out, duplicateIds: model.duplicateIds } : out;
|
|
132
|
+
}
|
|
133
|
+
function validateModelBySource(model, profile, lint = validateStructure(model)) {
|
|
134
|
+
return profile.validate === noDataReview ? { lint } : { lint, review: profile.validate(model) };
|
|
135
|
+
}
|
|
136
|
+
function validateModel(model, profile) {
|
|
137
|
+
const { lint, review } = validateModelBySource(model, profile);
|
|
138
|
+
return review ? lint.concat(review) : lint;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// src/layers.ts
|
|
142
|
+
var EMPTY = { layers: [], layerOf: /* @__PURE__ */ new Map() };
|
|
143
|
+
function orderedChildren(children, id, unordered = false) {
|
|
144
|
+
if (unordered) return null;
|
|
145
|
+
const kids = children.get(id) ?? [];
|
|
146
|
+
if (kids.length < 2) return null;
|
|
147
|
+
const withOrder = kids.filter((n) => typeof n.order === "number" && Number.isFinite(n.order));
|
|
148
|
+
if (withOrder.length !== kids.length) return null;
|
|
149
|
+
const seen = /* @__PURE__ */ new Set();
|
|
150
|
+
for (const k of withOrder) {
|
|
151
|
+
if (seen.has(k.order)) return null;
|
|
152
|
+
seen.add(k.order);
|
|
153
|
+
}
|
|
154
|
+
return [...kids].sort((a, b) => a.order - b.order);
|
|
155
|
+
}
|
|
156
|
+
function childrenMap(model) {
|
|
157
|
+
const out = /* @__PURE__ */ new Map();
|
|
158
|
+
for (const n of model.nodes) {
|
|
159
|
+
if (!n.parent || !model.index.has(n.parent)) continue;
|
|
160
|
+
const arr = out.get(n.parent);
|
|
161
|
+
arr ? arr.push(n) : out.set(n.parent, [n]);
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
function deriveLayers(model, colors = [], force = false) {
|
|
166
|
+
if (!model.nodes.length) return EMPTY;
|
|
167
|
+
const children = childrenMap(model);
|
|
168
|
+
const roots = model.nodes.filter((n) => !n.parent || !model.index.has(n.parent));
|
|
169
|
+
let ordered = null;
|
|
170
|
+
let anchor;
|
|
171
|
+
if (roots.length === 1) {
|
|
172
|
+
anchor = roots[0];
|
|
173
|
+
ordered = orderedChildren(children, anchor.id, anchor.data?.unordered === true);
|
|
174
|
+
if (!ordered && force) ordered = children.get(anchor.id) ?? null;
|
|
175
|
+
} else if (roots.length > 1 && (force || roots.every((n) => n.order !== void 0))) {
|
|
176
|
+
ordered = [...roots].sort((a, b) => (a.order ?? Infinity) - (b.order ?? Infinity));
|
|
177
|
+
}
|
|
178
|
+
if (!ordered || ordered.length < 2) return EMPTY;
|
|
179
|
+
const layers = ordered.map((n, i) => ({
|
|
180
|
+
id: n.id,
|
|
181
|
+
label: n.label || n.id,
|
|
182
|
+
z: i,
|
|
183
|
+
...colors[i] ? { color: colors[i] } : {}
|
|
184
|
+
}));
|
|
185
|
+
const layerOf = /* @__PURE__ */ new Map();
|
|
186
|
+
for (const [i, layer] of ordered.entries()) {
|
|
187
|
+
const stack = [layer.id];
|
|
188
|
+
while (stack.length) {
|
|
189
|
+
const id = stack.pop();
|
|
190
|
+
layerOf.set(id, layers[i].id);
|
|
191
|
+
for (const k of children.get(id) ?? []) stack.push(k.id);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (anchor) layerOf.set(anchor.id, layers[0].id);
|
|
195
|
+
return { layers, layerOf, anchor: anchor?.id };
|
|
196
|
+
}
|
|
197
|
+
function layerIndexOf(derived, id) {
|
|
198
|
+
const lid = derived.layerOf.get(id);
|
|
199
|
+
return lid ? derived.layers.findIndex((l) => l.id === lid) : -1;
|
|
200
|
+
}
|
|
201
|
+
function layerView(model, anchor) {
|
|
202
|
+
if (!anchor || !model.index.has(anchor)) return model;
|
|
203
|
+
return buildModel(model.nodes.filter((n) => n.id !== anchor), model.edges.filter((e) => e.source !== anchor && e.target !== anchor), model.errors);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// src/subgraph.ts
|
|
207
|
+
function projectSubgraph(model, view) {
|
|
208
|
+
let ids;
|
|
209
|
+
if (view.subtree) {
|
|
210
|
+
if (!model.index.has(view.subtree)) return EMPTY_MODEL;
|
|
211
|
+
ids = indexModel(model).subtreeOf(view.subtree);
|
|
212
|
+
} else if (view.origins?.length) {
|
|
213
|
+
const sources = new Set(view.origins);
|
|
214
|
+
ids = new Set(model.nodes.filter((n) => originsOfNode(n).some((o) => sources.has(o))).map((n) => n.id));
|
|
215
|
+
for (const e of model.edges) if (e.origin && sources.has(e.origin)) {
|
|
216
|
+
if (model.index.has(e.source)) ids.add(e.source);
|
|
217
|
+
if (model.index.has(e.target)) ids.add(e.target);
|
|
218
|
+
}
|
|
219
|
+
for (const id of ids) {
|
|
220
|
+
const parent = model.index.get(id)?.parent;
|
|
221
|
+
if (parent && model.index.has(parent)) ids.add(parent);
|
|
222
|
+
}
|
|
223
|
+
} else return model;
|
|
224
|
+
const nodes = model.nodes.filter((n) => ids.has(n.id)).map((n) => n.parent && !ids.has(n.parent) ? { ...n, parent: void 0, order: void 0 } : n);
|
|
225
|
+
return buildModel(nodes, model.edges.filter((e) => ids.has(e.source) && ids.has(e.target)));
|
|
226
|
+
}
|
|
227
|
+
function subgraphFloors(model, colors, force) {
|
|
228
|
+
const floors = deriveLayers(model, colors, force);
|
|
229
|
+
const nodes = model.nodes.map((n) => ({ ...n, layer: floors.layerOf.get(n.id) }));
|
|
230
|
+
return { ...floors, model: buildModel(nodes, model.edges) };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// src/view.ts
|
|
234
|
+
var FULL_VIEW = {};
|
|
235
|
+
function planeView(v, layers) {
|
|
236
|
+
if (layers.length < 2) return v.planes || v.layers ? { ...v, planes: false, layers: null } : v;
|
|
237
|
+
const stacked = v.planes !== false;
|
|
238
|
+
const picked = layers.map((l) => l.id).filter((id) => v.layers?.includes(id));
|
|
239
|
+
const want = stacked ? picked.length && picked.length < layers.length ? picked : null : v.layers && picked.length === 1 ? picked : null;
|
|
240
|
+
const same = v.planes === stacked && (want === null ? v.layers === null : !!v.layers && want.length === v.layers.length && want.every((id, i) => id === v.layers[i]));
|
|
241
|
+
return same ? v : { ...v, planes: stacked, layers: want };
|
|
242
|
+
}
|
|
243
|
+
function isFiltering(v) {
|
|
244
|
+
return isNarrowing(v) || !!v.layers?.length;
|
|
245
|
+
}
|
|
246
|
+
function isNarrowing(v) {
|
|
247
|
+
return !!(v.subtree || v.origins?.length);
|
|
248
|
+
}
|
|
249
|
+
function originView(v, available) {
|
|
250
|
+
if (!v.origins?.length) return v;
|
|
251
|
+
const have = new Set(available);
|
|
252
|
+
const kept = v.origins.filter((o) => have.has(o));
|
|
253
|
+
if (kept.length === v.origins.length) return v;
|
|
254
|
+
return { ...v, origins: kept.length ? kept : null };
|
|
255
|
+
}
|
|
256
|
+
var textOf = (data) => {
|
|
257
|
+
if (typeof data === "string") return data;
|
|
258
|
+
if (Array.isArray(data)) return data.map(textOf).join(" ");
|
|
259
|
+
if (data && typeof data === "object") return Object.values(data).map(textOf).join(" ");
|
|
260
|
+
return "";
|
|
261
|
+
};
|
|
262
|
+
function resolveView(model, elements, view) {
|
|
263
|
+
const present = /* @__PURE__ */ new Set();
|
|
264
|
+
for (const el of elements) if (!el.data.source) present.add(el.data.id);
|
|
265
|
+
const hitsIn = (scope) => new Set(searchNodes(model, view.query ?? "", scope));
|
|
266
|
+
if (!isFiltering(view)) {
|
|
267
|
+
return { visible: present, hits: hitsIn(present) };
|
|
268
|
+
}
|
|
269
|
+
const scoped = projectSubgraph(model, view);
|
|
270
|
+
const idx = indexModel(scoped);
|
|
271
|
+
const layers = view.layers?.length ? new Set(view.layers) : null;
|
|
272
|
+
const base = /* @__PURE__ */ new Set();
|
|
273
|
+
for (const n of scoped.nodes) {
|
|
274
|
+
if (present.has(n.id) && (!layers || layers.has(n.layer ?? ""))) base.add(n.id);
|
|
275
|
+
}
|
|
276
|
+
const visible = new Set(base);
|
|
277
|
+
for (const id of base) for (const a of idx.ancestorsOf(id)) if (present.has(a)) visible.add(a);
|
|
278
|
+
if (present.has(VIRTUAL_ROOT)) {
|
|
279
|
+
let isCompoundRoot = false;
|
|
280
|
+
let topVisible = 0;
|
|
281
|
+
for (const el of elements) {
|
|
282
|
+
if (el.data.source) {
|
|
283
|
+
if (el.data.kind === "contains" && el.data.source === VIRTUAL_ROOT && visible.has(el.data.target)) topVisible++;
|
|
284
|
+
} else if (el.data.parent === VIRTUAL_ROOT) {
|
|
285
|
+
isCompoundRoot = true;
|
|
286
|
+
if (visible.has(el.data.id)) topVisible++;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (isCompoundRoot || topVisible > 1) visible.add(VIRTUAL_ROOT);
|
|
290
|
+
}
|
|
291
|
+
return { visible, hits: hitsIn(base) };
|
|
292
|
+
}
|
|
293
|
+
function parseQuery(query) {
|
|
294
|
+
const raw = query.trim();
|
|
295
|
+
const m = /^\/(.+)\/$/.exec(raw);
|
|
296
|
+
if (m) {
|
|
297
|
+
try {
|
|
298
|
+
return { re: new RegExp(m[1], "i"), phrase: "", words: [] };
|
|
299
|
+
} catch {
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const phrase = raw.toLowerCase();
|
|
303
|
+
return { re: null, phrase, words: phrase ? [...new Set(phrase.split(/\s+/))] : [] };
|
|
304
|
+
}
|
|
305
|
+
function queryMarker(query) {
|
|
306
|
+
const { re, phrase, words } = parseQuery(query);
|
|
307
|
+
if (re) return new RegExp(re.source, "gi");
|
|
308
|
+
if (!phrase) return null;
|
|
309
|
+
const esc = (w) => w.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
310
|
+
const alts = [phrase, ...words.filter((w) => w !== phrase)].sort((a, b) => b.length - a.length);
|
|
311
|
+
return new RegExp(alts.map(esc).join("|"), "gi");
|
|
312
|
+
}
|
|
313
|
+
function searchNodes(model, query, scope) {
|
|
314
|
+
const { re, phrase: q, words } = parseQuery(query);
|
|
315
|
+
if (!re && !q) return [];
|
|
316
|
+
const ids = scope ? scope : model.nodes.map((n) => n.id);
|
|
317
|
+
const ranked = [];
|
|
318
|
+
let best = 0;
|
|
319
|
+
for (const id of ids) {
|
|
320
|
+
if (id === VIRTUAL_ROOT) continue;
|
|
321
|
+
const n = model.index.get(id);
|
|
322
|
+
if (!n) continue;
|
|
323
|
+
let tier = -1;
|
|
324
|
+
let k = 1;
|
|
325
|
+
if (re) {
|
|
326
|
+
const m = re.exec(n.label);
|
|
327
|
+
tier = m ? m.index > 0 ? 2 : m[0].length === n.label.length ? 0 : 1 : re.test(n.id) ? 3 : re.test(textOf(n.data)) ? 4 : -1;
|
|
328
|
+
} else {
|
|
329
|
+
const label = n.label.toLowerCase();
|
|
330
|
+
tier = label === q ? 0 : label.startsWith(q) ? 1 : label.includes(q) ? 2 : n.id.toLowerCase().includes(q) ? 3 : -1;
|
|
331
|
+
let text = null;
|
|
332
|
+
const rest = () => text ?? (text = `${n.id} ${textOf(n.data)}`.toLowerCase());
|
|
333
|
+
if (tier < 0 && rest().includes(q)) tier = 4;
|
|
334
|
+
if (tier >= 0) k = words.length;
|
|
335
|
+
else if (words.length > 1) {
|
|
336
|
+
let inLabel = 0;
|
|
337
|
+
k = 0;
|
|
338
|
+
for (const w of words) {
|
|
339
|
+
if (label.includes(w)) {
|
|
340
|
+
k++;
|
|
341
|
+
inLabel++;
|
|
342
|
+
} else if (rest().includes(w)) k++;
|
|
343
|
+
}
|
|
344
|
+
if (k) tier = inLabel === k ? 5 : 6;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (tier < 0) continue;
|
|
348
|
+
if (k > best) best = k;
|
|
349
|
+
ranked.push([k, tier, n.label.length, id]);
|
|
350
|
+
}
|
|
351
|
+
return ranked.filter((r) => r[0] === best).sort((a, b) => a[1] - b[1] || a[2] - b[2] || (a[3] < b[3] ? -1 : a[3] > b[3] ? 1 : 0)).map((r) => r[3]);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// src/animate.ts
|
|
355
|
+
var easeOutCubic = (t) => 1 - Math.pow(1 - t, 3);
|
|
356
|
+
var easeInOutCubic = (t) => t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
|
|
357
|
+
var easeOutBack = (s) => (t) => {
|
|
358
|
+
const u = t - 1;
|
|
359
|
+
return 1 + (s + 1) * u * u * u + s * u * u;
|
|
360
|
+
};
|
|
361
|
+
var easeDrift = (t) => 0.2 * (1 - Math.pow(1 - t, 3)) + 0.8 * t;
|
|
362
|
+
var DEFAULT_ANIMATE = {
|
|
363
|
+
duration: 1800,
|
|
364
|
+
easing: easeInOutCubic,
|
|
365
|
+
drift: 0.08,
|
|
366
|
+
bounce: 0.6,
|
|
367
|
+
maxNodes: 3e3
|
|
368
|
+
};
|
|
369
|
+
function resolveSpec(a) {
|
|
370
|
+
if (a === false) return null;
|
|
371
|
+
if (a === true || a === void 0) return DEFAULT_ANIMATE;
|
|
372
|
+
return { ...DEFAULT_ANIMATE, ...a };
|
|
373
|
+
}
|
|
374
|
+
var BEATS = {
|
|
375
|
+
cut: [0, 0.1],
|
|
376
|
+
snap: [0.07, 0.2],
|
|
377
|
+
leaf: [0.18, 0.95],
|
|
378
|
+
move: [0.22, 0.74],
|
|
379
|
+
present: [0.64, 0.94],
|
|
380
|
+
sprout: [0.68, 1],
|
|
381
|
+
graft: [0.86, 1],
|
|
382
|
+
open: [0, 0.3],
|
|
383
|
+
close: [0.58, 1]
|
|
384
|
+
};
|
|
385
|
+
var TRACK_NAMES = Object.keys(BEATS);
|
|
386
|
+
var clamp01 = (v) => v < 0 ? 0 : v > 1 ? 1 : v;
|
|
387
|
+
var trackLin = (tr, t) => {
|
|
388
|
+
const span = tr.to - tr.from;
|
|
389
|
+
return span > 0 ? clamp01((t - tr.from) / span) : 0;
|
|
390
|
+
};
|
|
391
|
+
var trackAt = (tr, t) => {
|
|
392
|
+
const p = trackLin(tr, t);
|
|
393
|
+
return p <= 0 ? 0 : p >= 1 ? 1 : tr.ease(p);
|
|
394
|
+
};
|
|
395
|
+
var SNAP_DIM = 0.4;
|
|
396
|
+
var FADE_NAMES = ["cut", "out", "in", "graft"];
|
|
397
|
+
var ENVELOPES = {
|
|
398
|
+
/** Edges across the cut: **break at once**, faster than anything. */
|
|
399
|
+
cut: { base: 1, legs: [{ track: "cut", d: -1 }] },
|
|
400
|
+
/** The leaving piece: dims a notch when it springs off, then fades the whole way while drifting. */
|
|
401
|
+
out: { base: 1, legs: [{ track: "snap", d: -SNAP_DIM }, { track: "leaf", d: -(1 - SNAP_DIM) }] },
|
|
402
|
+
/** Entering nodes and the edges growing with them. */
|
|
403
|
+
in: { base: 0, legs: [{ track: "present", d: 1 }] },
|
|
404
|
+
/** New edges into the existing structure: **connected last**. */
|
|
405
|
+
graft: { base: 0, legs: [{ track: "graft", d: 1 }] }
|
|
406
|
+
};
|
|
407
|
+
function edgeFade(from, to, flag) {
|
|
408
|
+
if (flag === "removed" || from === "gone" || to === "gone") {
|
|
409
|
+
return from === "gone" && to === "gone" ? "out" : "cut";
|
|
410
|
+
}
|
|
411
|
+
if (flag === "added" || from === "new" || to === "new") {
|
|
412
|
+
return from === "new" || to === "new" ? "in" : "graft";
|
|
413
|
+
}
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
var FADE_BASE = {
|
|
417
|
+
cut: ENVELOPES.cut.base,
|
|
418
|
+
out: ENVELOPES.out.base,
|
|
419
|
+
in: ENVELOPES.in.base,
|
|
420
|
+
graft: ENVELOPES.graft.base
|
|
421
|
+
};
|
|
422
|
+
var fadeAt = (name, sc, t) => {
|
|
423
|
+
const e = ENVELOPES[name];
|
|
424
|
+
let v = e.base;
|
|
425
|
+
for (const l of e.legs) v += l.d * trackLin(sc[l.track], t);
|
|
426
|
+
return v;
|
|
427
|
+
};
|
|
428
|
+
function makeScore(spec, active = {}) {
|
|
429
|
+
const on = [];
|
|
430
|
+
if (active.leaf) on.push("cut", "snap", "leaf");
|
|
431
|
+
if (active.move) on.push("move");
|
|
432
|
+
if (active.present) on.push("present", "sprout", "graft");
|
|
433
|
+
const lead = on.length ? Math.min(...on.map((n) => BEATS[n][0])) : 0;
|
|
434
|
+
const span = Math.max(1e-6, 1 - lead);
|
|
435
|
+
const cut = (v) => clamp01((v - lead) / span);
|
|
436
|
+
const ease = {
|
|
437
|
+
cut: (t) => t,
|
|
438
|
+
// a cut is an instant, not a stretch
|
|
439
|
+
snap: easeOutCubic,
|
|
440
|
+
// spring off: fastest at first, stops on arrival
|
|
441
|
+
leaf: easeDrift,
|
|
442
|
+
// then drift steadily to the end
|
|
443
|
+
move: spec.easing,
|
|
444
|
+
// sigmoid
|
|
445
|
+
present: (t) => t,
|
|
446
|
+
// envelopes take no curve
|
|
447
|
+
sprout: easeOutBack(spec.bounce),
|
|
448
|
+
graft: (t) => t,
|
|
449
|
+
open: easeInOutCubic,
|
|
450
|
+
close: easeInOutCubic
|
|
451
|
+
};
|
|
452
|
+
const out = { span };
|
|
453
|
+
for (const n of TRACK_NAMES) {
|
|
454
|
+
out[n] = { from: cut(BEATS[n][0]), to: cut(BEATS[n][1]), ease: ease[n] };
|
|
455
|
+
}
|
|
456
|
+
return out;
|
|
457
|
+
}
|
|
458
|
+
function edgeTraffic(delta) {
|
|
459
|
+
let i = 0, o = 0;
|
|
460
|
+
for (const e of delta.model.edges) {
|
|
461
|
+
const f = e.flags;
|
|
462
|
+
if (!f) continue;
|
|
463
|
+
if (f.includes(DIFF_FLAG.added) || f.includes(STEP_FLAG.added)) i++;
|
|
464
|
+
else if (f.includes(DIFF_FLAG.removed) || f.includes(STEP_FLAG.removed)) o++;
|
|
465
|
+
}
|
|
466
|
+
return { in: i, out: o };
|
|
467
|
+
}
|
|
468
|
+
var NOT_OURS = /^(display|visibility|opacity|z-|transition-|events|text-events|min-zoomed)/;
|
|
469
|
+
var GEOMETRY = /^(padding|shape|font-|text-|label|source-arrow-shape|target-arrow-shape)/;
|
|
470
|
+
var SKIN_SIZE_PROPS = ["width", "height"];
|
|
471
|
+
var RGB = /^rgba?\(\s*([\d.]+)[,\s]+([\d.]+)[,\s]+([\d.]+)(?:[,/\s]+([\d.]+))?\s*\)$/;
|
|
472
|
+
function asColor(v) {
|
|
473
|
+
const m = RGB.exec(String(v));
|
|
474
|
+
return m ? [+m[1], +m[2], +m[3], m[4] === void 0 ? 1 : +m[4]] : null;
|
|
475
|
+
}
|
|
476
|
+
function asNumber(v) {
|
|
477
|
+
if (typeof v === "number") return Number.isFinite(v) ? v : null;
|
|
478
|
+
const m = /^(-?[\d.]+)(px)?$/.exec(String(v).trim());
|
|
479
|
+
if (!m) return null;
|
|
480
|
+
const n = Number(m[1]);
|
|
481
|
+
return Number.isFinite(n) ? n : null;
|
|
482
|
+
}
|
|
483
|
+
function diffSkinStyle(past, now) {
|
|
484
|
+
const tween = [];
|
|
485
|
+
const steps = {};
|
|
486
|
+
for (const prop of Object.keys(now)) {
|
|
487
|
+
if (/[A-Z]/.test(prop) || NOT_OURS.test(prop) || GEOMETRY.test(prop)) continue;
|
|
488
|
+
const a = past[prop], b = now[prop];
|
|
489
|
+
if (a === void 0 || a === b || String(a) === String(b)) continue;
|
|
490
|
+
const [ca, cb] = [asColor(a), asColor(b)];
|
|
491
|
+
if (ca && cb) {
|
|
492
|
+
tween.push({ prop, from: ca, to: cb, color: true });
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
const [na, nb] = [asNumber(a), asNumber(b)];
|
|
496
|
+
if (na !== null && nb !== null) {
|
|
497
|
+
tween.push({ prop, from: [na], to: [nb], color: false });
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
steps[prop] = a;
|
|
501
|
+
}
|
|
502
|
+
return { tween, steps };
|
|
503
|
+
}
|
|
504
|
+
function sampleSkinStyle(tween, p) {
|
|
505
|
+
const out = {};
|
|
506
|
+
for (const t of tween) {
|
|
507
|
+
const v = t.from.map((f, i) => f + (t.to[i] - f) * p);
|
|
508
|
+
out[t.prop] = t.color ? `rgba(${Math.round(v[0])},${Math.round(v[1])},${Math.round(v[2])},${v[3]})` : v[0];
|
|
509
|
+
}
|
|
510
|
+
return out;
|
|
511
|
+
}
|
|
512
|
+
var MOVED_EPS = 1.5;
|
|
513
|
+
var SNAP_KICK = 0.3;
|
|
514
|
+
function anchorOf(id, seen, parentOf) {
|
|
515
|
+
const walked = /* @__PURE__ */ new Set([id]);
|
|
516
|
+
let cur = parentOf(id);
|
|
517
|
+
while (cur && !walked.has(cur)) {
|
|
518
|
+
if (seen(cur)) return cur;
|
|
519
|
+
walked.add(cur);
|
|
520
|
+
cur = parentOf(cur);
|
|
521
|
+
}
|
|
522
|
+
return void 0;
|
|
523
|
+
}
|
|
524
|
+
function planTransition(delta, from, union, opts = {}) {
|
|
525
|
+
const skins = opts.skins ?? [];
|
|
526
|
+
const spec = opts.spec ?? DEFAULT_ANIMATE;
|
|
527
|
+
const drift = spec.drift;
|
|
528
|
+
const items = [];
|
|
529
|
+
const leaving = /* @__PURE__ */ new Set();
|
|
530
|
+
const report = {
|
|
531
|
+
total: 0,
|
|
532
|
+
moved: 0,
|
|
533
|
+
movedMedian: 0,
|
|
534
|
+
entering: 0,
|
|
535
|
+
leaving: 0,
|
|
536
|
+
relocated: 0,
|
|
537
|
+
converging: 0,
|
|
538
|
+
restyled: skins.length,
|
|
539
|
+
pieces: 0,
|
|
540
|
+
edgesIn: 0,
|
|
541
|
+
edgesOut: 0
|
|
542
|
+
};
|
|
543
|
+
const dists = [];
|
|
544
|
+
if (delta) ({ in: report.edgesIn, out: report.edgesOut } = edgeTraffic(delta));
|
|
545
|
+
const at = (id) => from[id] ?? union[id];
|
|
546
|
+
const ids = Object.keys(union);
|
|
547
|
+
const kindOf = (id) => {
|
|
548
|
+
const k = delta?.byId.get(id)?.kind;
|
|
549
|
+
if (k) return k;
|
|
550
|
+
const p = opts.presence?.get(id);
|
|
551
|
+
return p === "gone" ? "removed" : p === "new" ? "added" : void 0;
|
|
552
|
+
};
|
|
553
|
+
const parentOf = opts.parentOf ?? (() => {
|
|
554
|
+
const model = /* @__PURE__ */ new Map();
|
|
555
|
+
for (const n of delta?.model.nodes ?? []) if (n.parent) model.set(n.id, n.parent);
|
|
556
|
+
const root = union[VIRTUAL_ROOT] ? VIRTUAL_ROOT : void 0;
|
|
557
|
+
return (id) => model.get(id) ?? (id === root ? void 0 : root);
|
|
558
|
+
})();
|
|
559
|
+
const hub = { x: 0, y: 0 };
|
|
560
|
+
for (const id of ids) {
|
|
561
|
+
hub.x += union[id].x / ids.length;
|
|
562
|
+
hub.y += union[id].y / ids.length;
|
|
563
|
+
}
|
|
564
|
+
const jitter = (seed) => {
|
|
565
|
+
let h = 2166136261;
|
|
566
|
+
for (let i = 0; i < seed.length; i++) {
|
|
567
|
+
h ^= seed.charCodeAt(i);
|
|
568
|
+
h = Math.imul(h, 16777619);
|
|
569
|
+
}
|
|
570
|
+
return (h >>> 0) % 2048 / 2048;
|
|
571
|
+
};
|
|
572
|
+
const bb = { x1: Infinity, y1: Infinity, x2: -Infinity, y2: -Infinity };
|
|
573
|
+
for (const id of ids) {
|
|
574
|
+
const q = union[id];
|
|
575
|
+
bb.x1 = Math.min(bb.x1, q.x);
|
|
576
|
+
bb.x2 = Math.max(bb.x2, q.x);
|
|
577
|
+
bb.y1 = Math.min(bb.y1, q.y);
|
|
578
|
+
bb.y2 = Math.max(bb.y2, q.y);
|
|
579
|
+
}
|
|
580
|
+
const span = Math.max(bb.x2 - bb.x1, bb.y2 - bb.y1) || 0;
|
|
581
|
+
const cutIds = /* @__PURE__ */ new Set();
|
|
582
|
+
for (const id of ids) if (kindOf(id) === "removed") cutIds.add(id);
|
|
583
|
+
const topOf = /* @__PURE__ */ new Map();
|
|
584
|
+
const climb = (id) => {
|
|
585
|
+
const path = [];
|
|
586
|
+
let cur = id;
|
|
587
|
+
for (; ; ) {
|
|
588
|
+
const hit = topOf.get(cur);
|
|
589
|
+
if (hit) {
|
|
590
|
+
for (const q of path) topOf.set(q, hit);
|
|
591
|
+
return hit;
|
|
592
|
+
}
|
|
593
|
+
const pa = parentOf(cur);
|
|
594
|
+
if (!pa || !cutIds.has(pa) || path.includes(cur)) break;
|
|
595
|
+
path.push(cur);
|
|
596
|
+
cur = pa;
|
|
597
|
+
}
|
|
598
|
+
for (const q of path) topOf.set(q, cur);
|
|
599
|
+
topOf.set(cur, cur);
|
|
600
|
+
return cur;
|
|
601
|
+
};
|
|
602
|
+
const crew = /* @__PURE__ */ new Map();
|
|
603
|
+
for (const id of cutIds) {
|
|
604
|
+
const top = climb(id);
|
|
605
|
+
const list = crew.get(top);
|
|
606
|
+
if (list) list.push(id);
|
|
607
|
+
else crew.set(top, [id]);
|
|
608
|
+
}
|
|
609
|
+
const piece = /* @__PURE__ */ new Map();
|
|
610
|
+
for (const [top, mates] of crew) {
|
|
611
|
+
const pa = parentOf(top);
|
|
612
|
+
const anchor = pa && union[pa] ? at(pa) : hub;
|
|
613
|
+
const legs = [];
|
|
614
|
+
let cx = 0, cy = 0;
|
|
615
|
+
for (const m of mates) {
|
|
616
|
+
cx += at(m).x / mates.length;
|
|
617
|
+
cy += at(m).y / mates.length;
|
|
618
|
+
}
|
|
619
|
+
const r = Math.hypot(cx - anchor.x, cy - anchor.y);
|
|
620
|
+
const ang = jitter(`${top}@`) * Math.PI * 2;
|
|
621
|
+
const ux = r > 1e-6 ? (cx - anchor.x) / r : Math.cos(ang);
|
|
622
|
+
const uy = r > 1e-6 ? (cy - anchor.y) / r : Math.sin(ang);
|
|
623
|
+
const len = drift * span * (0.7 + 0.6 * jitter(top));
|
|
624
|
+
const side = 0.32 * (jitter(`${top}~`) * 2 - 1);
|
|
625
|
+
const [dx, dy] = [(ux - uy * side) * len, (uy + ux * side) * len];
|
|
626
|
+
if (len > 0) {
|
|
627
|
+
legs.push({ track: "snap", dx: dx * SNAP_KICK, dy: dy * SNAP_KICK });
|
|
628
|
+
legs.push({ track: "leaf", dx: dx * (1 - SNAP_KICK), dy: dy * (1 - SNAP_KICK) });
|
|
629
|
+
}
|
|
630
|
+
piece.set(top, legs);
|
|
631
|
+
}
|
|
632
|
+
report.pieces = crew.size;
|
|
633
|
+
for (const id of ids) {
|
|
634
|
+
const to = union[id];
|
|
635
|
+
const c = delta?.byId.get(id);
|
|
636
|
+
const kind = kindOf(id);
|
|
637
|
+
report.total++;
|
|
638
|
+
if (kind === "removed" || kind === "merged") {
|
|
639
|
+
leaving.add(id);
|
|
640
|
+
report.leaving++;
|
|
641
|
+
const base = at(id);
|
|
642
|
+
const pair = kind === "merged" && c.pairId ? c.pairId : void 0;
|
|
643
|
+
if (pair && union[pair]) {
|
|
644
|
+
report.converging++;
|
|
645
|
+
const pf = at(pair);
|
|
646
|
+
items.push({
|
|
647
|
+
id,
|
|
648
|
+
x: base.x,
|
|
649
|
+
y: base.y,
|
|
650
|
+
legs: [
|
|
651
|
+
{ track: "move", dx: union[pair].x - pf.x, dy: union[pair].y - pf.y },
|
|
652
|
+
{ track: "leaf", dx: pf.x - base.x, dy: pf.y - base.y }
|
|
653
|
+
]
|
|
654
|
+
});
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
items.push({ id, x: base.x, y: base.y, legs: piece.get(topOf.get(id) ?? id) ?? [] });
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
if (kind === "added") {
|
|
661
|
+
report.entering++;
|
|
662
|
+
const anc = anchorOf(id, (q) => !!from[q], parentOf);
|
|
663
|
+
if (anc && union[anc]) {
|
|
664
|
+
const pf = from[anc];
|
|
665
|
+
items.push({
|
|
666
|
+
id,
|
|
667
|
+
x: pf.x,
|
|
668
|
+
y: pf.y,
|
|
669
|
+
legs: [
|
|
670
|
+
{ track: "move", dx: union[anc].x - pf.x, dy: union[anc].y - pf.y },
|
|
671
|
+
{ track: "sprout", dx: to.x - union[anc].x, dy: to.y - union[anc].y }
|
|
672
|
+
]
|
|
673
|
+
});
|
|
674
|
+
} else {
|
|
675
|
+
items.push({ id, x: to.x, y: to.y, legs: [] });
|
|
676
|
+
}
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
if (kind === "moved") {
|
|
680
|
+
report.relocated++;
|
|
681
|
+
const a0 = at(id);
|
|
682
|
+
items.push({ id, x: a0.x, y: a0.y, legs: [{ track: "move", dx: to.x - a0.x, dy: to.y - a0.y }] });
|
|
683
|
+
continue;
|
|
684
|
+
}
|
|
685
|
+
const a = at(id);
|
|
686
|
+
const d = Math.hypot(a.x - to.x, a.y - to.y);
|
|
687
|
+
if (d > MOVED_EPS) {
|
|
688
|
+
report.moved++;
|
|
689
|
+
dists.push(d);
|
|
690
|
+
}
|
|
691
|
+
items.push({ id, x: a.x, y: a.y, legs: [{ track: "move", dx: to.x - a.x, dy: to.y - a.y }] });
|
|
692
|
+
}
|
|
693
|
+
if (dists.length) {
|
|
694
|
+
dists.sort((x, y) => x - y);
|
|
695
|
+
report.movedMedian = Math.round(dists[dists.length >> 1]);
|
|
696
|
+
}
|
|
697
|
+
const worth = report.moved + report.entering + report.leaving + report.relocated + report.restyled + report.edgesIn + report.edgesOut > 0;
|
|
698
|
+
const score = makeScore(spec, {
|
|
699
|
+
// A branch move counts on both sides: it is **one containment edge gone + one containment edge come**, so both cut and graft
|
|
700
|
+
// have content. Counting only node additions and removals would judge the step "motion only", then empty-beat trimming
|
|
701
|
+
// squeezes cut to zero width, and the old link would not "break" but "vanish within a frame".
|
|
702
|
+
leaf: report.leaving + report.edgesOut + report.relocated > 0,
|
|
703
|
+
move: report.moved + report.relocated > 0,
|
|
704
|
+
present: report.entering + report.edgesIn + report.restyled + report.relocated > 0
|
|
705
|
+
});
|
|
706
|
+
return { items, skins, score, leaving, worth, report };
|
|
707
|
+
}
|
|
708
|
+
function planSkins(delta, prev, next, profile) {
|
|
709
|
+
if (!delta || !prev) return [];
|
|
710
|
+
const kids = (m) => {
|
|
711
|
+
const s = /* @__PURE__ */ new Set();
|
|
712
|
+
for (const n of m.nodes) if (n.parent) s.add(n.parent);
|
|
713
|
+
return s;
|
|
714
|
+
};
|
|
715
|
+
const [pKids, nKids] = [kids(prev), kids(next)];
|
|
716
|
+
const skin = (n) => n.flags?.some((f) => f.startsWith("gb-")) ? { ...n, flags: n.flags.filter((f) => !f.startsWith("gb-")) } : n;
|
|
717
|
+
const out = [];
|
|
718
|
+
for (const [oldId, unionId] of delta.resolved) {
|
|
719
|
+
const a = prev.index.get(oldId), b = next.index.get(unionId);
|
|
720
|
+
if (!a || !b) continue;
|
|
721
|
+
const past = nodeClasses(skin(a), profile, { container: pKids.has(oldId) });
|
|
722
|
+
const now = nodeClasses(skin(b), profile, { container: nKids.has(unionId) });
|
|
723
|
+
if (past !== now) out.push({ id: unionId, past, now });
|
|
724
|
+
}
|
|
725
|
+
return out;
|
|
726
|
+
}
|
|
727
|
+
function leavingOf(delta) {
|
|
728
|
+
const out = /* @__PURE__ */ new Set();
|
|
729
|
+
for (const c of delta?.changes ?? []) if (c.kind === "removed" || c.kind === "merged") out.add(c.id);
|
|
730
|
+
return out;
|
|
731
|
+
}
|
|
732
|
+
function sampleTransition(t, time) {
|
|
733
|
+
const sc = t.score;
|
|
734
|
+
const s = {};
|
|
735
|
+
for (const n of TRACK_NAMES) s[n] = trackAt(sc[n], time);
|
|
736
|
+
const pos = {};
|
|
737
|
+
for (const it of t.items) {
|
|
738
|
+
let { x, y } = it;
|
|
739
|
+
for (const l of it.legs) {
|
|
740
|
+
const k = s[l.track];
|
|
741
|
+
x += l.dx * k;
|
|
742
|
+
y += l.dy * k;
|
|
743
|
+
}
|
|
744
|
+
pos[it.id] = { x, y };
|
|
745
|
+
}
|
|
746
|
+
const fade = {};
|
|
747
|
+
for (const n of FADE_NAMES) fade[n] = fadeAt(n, sc, time);
|
|
748
|
+
return { pos, fade, open: s.open, close: s.close };
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
// src/layout/diff-layout.ts
|
|
752
|
+
function repOf(model, pos) {
|
|
753
|
+
const idx = indexModel(model);
|
|
754
|
+
const memo = /* @__PURE__ */ new Map();
|
|
755
|
+
const walk = (id) => {
|
|
756
|
+
const hit = memo.get(id);
|
|
757
|
+
if (hit !== void 0) return hit;
|
|
758
|
+
memo.set(id, null);
|
|
759
|
+
const own = pos[id];
|
|
760
|
+
let out = own ? { x: own.x, y: own.y } : null;
|
|
761
|
+
if (!out) {
|
|
762
|
+
let sx = 0, sy = 0, n = 0;
|
|
763
|
+
for (const c of idx.childrenOf.get(id) ?? []) {
|
|
764
|
+
const p = walk(c);
|
|
765
|
+
if (p) {
|
|
766
|
+
sx += p.x;
|
|
767
|
+
sy += p.y;
|
|
768
|
+
n++;
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
out = n ? { x: sx / n, y: sy / n } : null;
|
|
772
|
+
}
|
|
773
|
+
memo.set(id, out);
|
|
774
|
+
return out;
|
|
775
|
+
};
|
|
776
|
+
return walk;
|
|
777
|
+
}
|
|
778
|
+
function adjacency(m) {
|
|
779
|
+
const adj = /* @__PURE__ */ new Map();
|
|
780
|
+
const link = (a, b) => {
|
|
781
|
+
let s = adj.get(a);
|
|
782
|
+
if (!s) adj.set(a, s = /* @__PURE__ */ new Set());
|
|
783
|
+
s.add(b);
|
|
784
|
+
};
|
|
785
|
+
for (const n of m.nodes) if (n.parent) {
|
|
786
|
+
link(n.id, n.parent);
|
|
787
|
+
link(n.parent, n.id);
|
|
788
|
+
}
|
|
789
|
+
for (const e of m.edges) {
|
|
790
|
+
link(e.source, e.target);
|
|
791
|
+
link(e.target, e.source);
|
|
792
|
+
}
|
|
793
|
+
return adj;
|
|
794
|
+
}
|
|
795
|
+
function bboxOf(ids, pos) {
|
|
796
|
+
let x1 = Infinity, y1 = Infinity, x2 = -Infinity, y2 = -Infinity, n = 0;
|
|
797
|
+
for (const id of ids) {
|
|
798
|
+
const p = pos[id];
|
|
799
|
+
if (!p) continue;
|
|
800
|
+
n++;
|
|
801
|
+
x1 = Math.min(x1, p.x);
|
|
802
|
+
y1 = Math.min(y1, p.y);
|
|
803
|
+
x2 = Math.max(x2, p.x);
|
|
804
|
+
y2 = Math.max(y2, p.y);
|
|
805
|
+
}
|
|
806
|
+
return n ? { x1, y1, x2, y2, w: x2 - x1, h: y2 - y1 } : null;
|
|
807
|
+
}
|
|
808
|
+
function diffPositions(sides, posBefore, posAfter) {
|
|
809
|
+
const out = { ...posAfter };
|
|
810
|
+
const alive = sides.after.index;
|
|
811
|
+
const ghostSet = /* @__PURE__ */ new Set();
|
|
812
|
+
for (const n of sides.before.nodes) if (!alive.has(n.id)) ghostSet.add(n.id);
|
|
813
|
+
if (!ghostSet.size) return out;
|
|
814
|
+
const adj = adjacency(sides.before);
|
|
815
|
+
const repB = repOf(sides.before, posBefore);
|
|
816
|
+
const repA = repOf(sides.after, posAfter);
|
|
817
|
+
const seen = /* @__PURE__ */ new Set();
|
|
818
|
+
const parked = [];
|
|
819
|
+
for (const n of sides.before.nodes) {
|
|
820
|
+
const start = n.id;
|
|
821
|
+
if (!ghostSet.has(start) || seen.has(start)) continue;
|
|
822
|
+
const comp = [];
|
|
823
|
+
const anchors = /* @__PURE__ */ new Set();
|
|
824
|
+
const stack = [start];
|
|
825
|
+
seen.add(start);
|
|
826
|
+
while (stack.length) {
|
|
827
|
+
const id = stack.pop();
|
|
828
|
+
comp.push(id);
|
|
829
|
+
for (const nb of adj.get(id) ?? []) {
|
|
830
|
+
if (ghostSet.has(nb)) {
|
|
831
|
+
if (!seen.has(nb)) {
|
|
832
|
+
seen.add(nb);
|
|
833
|
+
stack.push(nb);
|
|
834
|
+
}
|
|
835
|
+
} else if (alive.has(nb)) anchors.add(nb);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
let dx = 0, dy = 0, k = 0;
|
|
839
|
+
for (const a of anchors) {
|
|
840
|
+
const b = repB(a), f = repA(a);
|
|
841
|
+
if (b && f) {
|
|
842
|
+
dx += f.x - b.x;
|
|
843
|
+
dy += f.y - b.y;
|
|
844
|
+
k++;
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
if (!k) {
|
|
848
|
+
parked.push(comp);
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
dx /= k;
|
|
852
|
+
dy /= k;
|
|
853
|
+
for (const g of comp) {
|
|
854
|
+
const p = posBefore[g];
|
|
855
|
+
if (p) out[g] = { x: p.x + dx, y: p.y + dy };
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
if (parked.length) {
|
|
859
|
+
const main = bboxOf(Object.keys(posAfter), posAfter);
|
|
860
|
+
const ids = parked.flat();
|
|
861
|
+
const block = bboxOf(ids, posBefore);
|
|
862
|
+
const dx = block && main ? (main.x1 + main.x2) / 2 - (block.x1 + block.x2) / 2 : 0;
|
|
863
|
+
const dy = block && main ? (main.y1 + main.y2) / 2 - (block.y1 + block.y2) / 2 : 0;
|
|
864
|
+
for (const id of ids) {
|
|
865
|
+
const p = posBefore[id];
|
|
866
|
+
if (p) out[id] = { x: p.x + dx, y: p.y + dy };
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
return out;
|
|
870
|
+
}
|
|
871
|
+
var DEFAULT_DIFF_LAYOUT = { rounds: 1, iterations: 12, snap: true };
|
|
872
|
+
var EMPTY2 = /* @__PURE__ */ new Set();
|
|
873
|
+
function split(pos, shared) {
|
|
874
|
+
const a = {}, b = {};
|
|
875
|
+
for (const [id, p] of Object.entries(pos)) (shared.has(id) ? a : b)[id] = p;
|
|
876
|
+
return { shared: a, own: b };
|
|
877
|
+
}
|
|
878
|
+
async function solveDiffLayout(sides, base, solve, spec = {}) {
|
|
879
|
+
const rounds = Math.max(0, Math.floor(spec.rounds ?? DEFAULT_DIFF_LAYOUT.rounds));
|
|
880
|
+
const snap = spec.snap !== false;
|
|
881
|
+
const shared = /* @__PURE__ */ new Set();
|
|
882
|
+
for (const n of sides.before.nodes) if (sides.after.index.has(n.id)) shared.add(n.id);
|
|
883
|
+
const seeded = diffPositions(sides, base.before, base.after);
|
|
884
|
+
let x = split(seeded, shared).shared;
|
|
885
|
+
let ownA = split(seeded, shared).own;
|
|
886
|
+
let ownB = split(base.after, shared).own;
|
|
887
|
+
for (const id of Object.keys(ownA)) if (sides.after.index.has(id)) delete ownA[id];
|
|
888
|
+
for (const id of Object.keys(ownB)) if (sides.before.index.has(id)) delete ownB[id];
|
|
889
|
+
const steps = [];
|
|
890
|
+
let iter = Math.max(0, Math.floor(spec.iterations ?? DEFAULT_DIFF_LAYOUT.iterations));
|
|
891
|
+
for (let k = 0; k < rounds && iter > 0; k++) {
|
|
892
|
+
const side = k % 2 === 0 ? "before" : "after";
|
|
893
|
+
const seed = { ...x, ...side === "before" ? ownA : ownB };
|
|
894
|
+
const got = await solve(side, seed, iter, snap ? shared : EMPTY2);
|
|
895
|
+
const parts = split(got, shared);
|
|
896
|
+
if (side === "before") ownA = parts.own;
|
|
897
|
+
else ownB = parts.own;
|
|
898
|
+
if (!snap) x = parts.shared;
|
|
899
|
+
steps.push({ side, iterations: iter });
|
|
900
|
+
iter = Math.floor(iter / 2);
|
|
901
|
+
}
|
|
902
|
+
return { past: { ...x, ...ownA }, now: { ...x, ...ownB }, steps };
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
export {
|
|
906
|
+
parsePhysical,
|
|
907
|
+
toPhysical,
|
|
908
|
+
toBundle,
|
|
909
|
+
validateStructure,
|
|
910
|
+
kindTable,
|
|
911
|
+
defineProfile,
|
|
912
|
+
defaultProfile,
|
|
913
|
+
parseSources,
|
|
914
|
+
validateModelBySource,
|
|
915
|
+
validateModel,
|
|
916
|
+
orderedChildren,
|
|
917
|
+
childrenMap,
|
|
918
|
+
deriveLayers,
|
|
919
|
+
layerIndexOf,
|
|
920
|
+
layerView,
|
|
921
|
+
projectSubgraph,
|
|
922
|
+
subgraphFloors,
|
|
923
|
+
FULL_VIEW,
|
|
924
|
+
planeView,
|
|
925
|
+
isFiltering,
|
|
926
|
+
isNarrowing,
|
|
927
|
+
originView,
|
|
928
|
+
resolveView,
|
|
929
|
+
parseQuery,
|
|
930
|
+
queryMarker,
|
|
931
|
+
searchNodes,
|
|
932
|
+
easeOutCubic,
|
|
933
|
+
easeInOutCubic,
|
|
934
|
+
easeOutBack,
|
|
935
|
+
easeDrift,
|
|
936
|
+
DEFAULT_ANIMATE,
|
|
937
|
+
resolveSpec,
|
|
938
|
+
TRACK_NAMES,
|
|
939
|
+
trackLin,
|
|
940
|
+
trackAt,
|
|
941
|
+
SNAP_DIM,
|
|
942
|
+
FADE_NAMES,
|
|
943
|
+
ENVELOPES,
|
|
944
|
+
edgeFade,
|
|
945
|
+
FADE_BASE,
|
|
946
|
+
fadeAt,
|
|
947
|
+
makeScore,
|
|
948
|
+
edgeTraffic,
|
|
949
|
+
SKIN_SIZE_PROPS,
|
|
950
|
+
diffSkinStyle,
|
|
951
|
+
sampleSkinStyle,
|
|
952
|
+
anchorOf,
|
|
953
|
+
planTransition,
|
|
954
|
+
planSkins,
|
|
955
|
+
leavingOf,
|
|
956
|
+
sampleTransition,
|
|
957
|
+
diffPositions,
|
|
958
|
+
DEFAULT_DIFF_LAYOUT,
|
|
959
|
+
solveDiffLayout
|
|
960
|
+
};
|