glassbrain 0.2.0-alpha.1 → 0.2.0-alpha.3

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 CHANGED
@@ -22,24 +22,38 @@ lets you read one at a glance, and when the agent learns something, shows you ex
22
22
  ```json
23
23
  {
24
24
  "nodes": [
25
- { "id": "src", "label": "src", "kind": "folder" },
26
- { "id": "src/auth", "label": "auth", "kind": "folder", "parent": "src" },
25
+ { "id": "src", "label": "src", "kind": "folder", "parent": null },
26
+ { "id": "src/auth", "label": "auth", "kind": "folder", "parent": "src", "order": 1 },
27
27
  { "id": "src/auth/login.ts", "label": "login.ts", "kind": "file", "parent": "src/auth" },
28
- { "id": "src/db.ts", "label": "db.ts", "kind": "file", "parent": "src" }
28
+ { "id": "src/db.ts", "label": "db.ts", "kind": "file", "parent": "src", "order": 2 }
29
29
  ],
30
30
  "edges": [{ "source": "src/auth/login.ts", "target": "src/db.ts", "kind": "uses" }]
31
31
  }
32
32
  ```
33
33
 
34
- `parent` says what sits inside what; that is the whole structure. Everything else is an edge.
34
+ Next to it, one colour per kind — a kind with no colour is drawn as skeleton, so `validate` reports
35
+ it as an error (forty kinds without colours look exactly like one kind):
36
+
37
+ ```json
38
+ { "nodePalette": { "folder": "#94a3b8", "file": "#6366f1" },
39
+ "edgePalette": { "uses": "#2563eb" } }
40
+ ```
41
+
42
+ `parent` is required on every node (null for the one root) and says what sits inside what: that is
43
+ the whole structure. `order`, optional, turns the children of one node into a numbered list — the
44
+ layout reads it clockwise, and a numbered root *is* the 2.5D layer view. There is no layer field.
35
45
  A [JSON Schema](https://github.com/voidixyn/glassbrain/blob/main/schema/graph.schema.json) describes every field, and the CLI answers in a form an
36
- agent can act on:
46
+ agent can act on — every finding carries its repair:
37
47
 
38
48
  ```text
39
49
  $ npx glassbrain validate kg
40
- warn 2-filled/ Dangling edge: points to missing node src/session
50
+ error dangling-edge graph.json edge src/auth/login.ts → src/session points at the missing node src/session
51
+ an edge can only join nodes that exist. Add the node, or repoint the edge:
52
+ - { "source": "src/auth/login.ts", "target": "src/session" }
53
+ + { "source": "src/auth/login.ts", "target": "EXISTING_ID" }
41
54
 
42
- kg: 2 versions, 3 nodes, 1 edge — 0 errors, 1 warning
55
+ kg: 2 nodes, 1 edge — 1 error · 0 warnings · 0 infos
56
+ structure: 1 root · depth 2 · 1/2 parented · isomorphism 1.00
43
57
 
44
58
  $ npx glassbrain diff kg/1-skeleton kg/2-filled
45
59
  1-skeleton → 2-filled
@@ -82,7 +96,7 @@ That makes change the main event:
82
96
 
83
97
  Search ranks by how well a node matches, the detail panel renders the node's markdown, labels
84
98
  make room for each other instead of piling up, and [dark mode is computed](https://github.com/voidixyn/glassbrain/blob/main/docs/guide.md#color-hue-is-configured-everything-else-is-computed)
85
- from the light palette. Graphs with layers can be stood up in 2.5D:
99
+ from the light palette. A root whose children carry `order` opens as floors in 2.5D:
86
100
 
87
101
  ![Protocols, building blocks and products of the agent ecosystem, stacked in 2.5D](https://raw.githubusercontent.com/voidixyn/glassbrain/main/docs/media/layers.png)
88
102
 
@@ -12,6 +12,9 @@ export declare const PROFILE: {
12
12
  document: string;
13
13
  paper: string;
14
14
  image: string;
15
+ folder: string;
16
+ file: string;
17
+ group: string;
15
18
  };
16
19
  edgePalette: {
17
20
  calls: string;
@@ -28,6 +28,9 @@ export declare const PROFILE: {
28
28
  screen: string;
29
29
  component: string;
30
30
  token: string;
31
+ layer: string;
32
+ folder: string;
33
+ file: string;
31
34
  };
32
35
  edgePalette: {
33
36
  imports: string;
package/dist/adapters.js CHANGED
@@ -29,7 +29,7 @@ var clean = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !==
29
29
  // adapters/graphify.ts
30
30
  var STRUCT = /* @__PURE__ */ new Set(["contains", "method"]);
31
31
  var NOTE = "rationale_for";
32
- var looksLike = (d) => isObj(d) && Array.isArray(d.nodes) && Array.isArray(d.links ?? d.edges) && (d.nodes.length === 0 || d.nodes.some((n) => isObj(n) && ("file_type" in n || "source_file" in n))) && !("layers" in d && "project" in d);
32
+ var looksLike = (d) => isObj(d) && Array.isArray(d.nodes) && Array.isArray(d.links ?? d.edges) && (Array.isArray(d.links) || d.nodes.some((n) => isObj(n) && "file_type" in n)) && !("layers" in d && "project" in d);
33
33
  var base = (p) => p.split("/").pop() ?? p;
34
34
  function convertGraphify(d, name) {
35
35
  const raw = d.nodes.filter(isObj);
@@ -103,7 +103,18 @@ function convertGraphify(d, name) {
103
103
  var score = (l) => typeof l.confidence_score === "number" ? l.confidence_score : typeof l.weight === "number" ? Math.min(1, l.weight) : void 0;
104
104
  var groupLabel = (t) => ({ concept: "Concepts", rationale: "Rationale", document: "Documents" })[t] ?? t;
105
105
  var PROFILE = {
106
- nodePalette: { symbol: "#6366f1", concept: "#e63b7a", rationale: "#f59e0b", document: "#0891b2", paper: "#0891b2", image: "#16a34a" },
106
+ // The adapter invents these three while building the folder / file boxes, so it must colour them too.
107
+ nodePalette: {
108
+ symbol: "#6366f1",
109
+ concept: "#e63b7a",
110
+ rationale: "#f59e0b",
111
+ document: "#0891b2",
112
+ paper: "#0891b2",
113
+ image: "#16a34a",
114
+ folder: "#94a3b8",
115
+ file: "#64748b",
116
+ group: "#a855f7"
117
+ },
107
118
  edgePalette: {
108
119
  calls: "#6366f1",
109
120
  imports: "#94a3b8",
@@ -318,7 +329,8 @@ var obsidian = {
318
329
  return {
319
330
  sources,
320
331
  profile: {
321
- nodePalette: autoPalette(kinds),
332
+ // `folder` is invented by the walk, not present in frontmatter kinds: colour it here.
333
+ nodePalette: { ...autoPalette(kinds), folder: "#94a3b8" },
322
334
  edgePalette: { link: "#94a3b8" },
323
335
  kindLabels: { folder: "Folder", note: "Note", link: "link" },
324
336
  detail: { line: [{ key: "path", label: "File" }], list: [{ key: "tags", label: "Tags" }] },
@@ -409,7 +421,10 @@ var PROFILE2 = {
409
421
  source: "#64748b",
410
422
  screen: "#0891b2",
411
423
  component: "#6366f1",
412
- token: "#f59e0b"
424
+ token: "#f59e0b",
425
+ layer: "#a855f7",
426
+ folder: "#94a3b8",
427
+ file: "#64748b"
413
428
  },
414
429
  edgePalette: {
415
430
  imports: "#94a3b8",
@@ -52,17 +52,17 @@ import {
52
52
  unionSides,
53
53
  untheme,
54
54
  validateModel
55
- } from "./chunk-VCM766BZ.js";
55
+ } from "./chunk-WW23WBYG.js";
56
56
  import {
57
57
  DetailPanel,
58
58
  I,
59
59
  Icon,
60
60
  StringsContext,
61
61
  useStrings
62
- } from "./chunk-37BAJC3L.js";
62
+ } from "./chunk-PBF4KMQB.js";
63
63
  import {
64
64
  EN
65
- } from "./chunk-EJURBBFC.js";
65
+ } from "./chunk-H2WV3DTN.js";
66
66
  import {
67
67
  inlinePool
68
68
  } from "./chunk-YGRPNAXE.js";
@@ -911,6 +911,159 @@ function resolveOverlaps(boxes, o = {}) {
911
911
  }
912
912
  }
913
913
 
914
+ // src/layout/order.ts
915
+ function readOrder(elements) {
916
+ const parentOf = /* @__PURE__ */ new Map();
917
+ const kids = /* @__PURE__ */ new Map();
918
+ const nodeIds = /* @__PURE__ */ new Set();
919
+ for (const el of elements) {
920
+ const id = el.data.id;
921
+ if (!id || el.data.source) continue;
922
+ nodeIds.add(id);
923
+ const parent = el.data.parent;
924
+ if (!parent) continue;
925
+ parentOf.set(id, parent);
926
+ const raw = el.data.order;
927
+ const order = typeof raw === "number" && Number.isFinite(raw) ? raw : null;
928
+ const arr = kids.get(parent);
929
+ arr ? arr.push({ id, order }) : kids.set(parent, [{ id, order }]);
930
+ }
931
+ const orderedKids = /* @__PURE__ */ new Map();
932
+ let any = false;
933
+ for (const [parent, list] of kids) {
934
+ if (list.length < 2 || list.some((k) => k.order === null)) continue;
935
+ const sorted = [...list].sort((a, b) => a.order - b.order).map((k) => k.id);
936
+ const seen = /* @__PURE__ */ new Set();
937
+ let unique = true;
938
+ for (const k of list) {
939
+ if (seen.has(k.order)) {
940
+ unique = false;
941
+ break;
942
+ }
943
+ seen.add(k.order);
944
+ }
945
+ if (!unique) continue;
946
+ orderedKids.set(parent, sorted);
947
+ any = true;
948
+ }
949
+ return any ? { parentOf, orderedKids, nodeIds, any } : null;
950
+ }
951
+ function hash01(id) {
952
+ let h = 2166136261;
953
+ for (let i = 0; i < id.length; i++) {
954
+ h ^= id.charCodeAt(i);
955
+ h = Math.imul(h, 16777619);
956
+ }
957
+ return (h >>> 0) % 1e5 / 1e5;
958
+ }
959
+ var TAU = Math.PI * 2;
960
+ var RING = 320;
961
+ function orderSeed(elements, info) {
962
+ const children = /* @__PURE__ */ new Map();
963
+ for (const [id, parent] of info.parentOf) {
964
+ const arr = children.get(parent);
965
+ arr ? arr.push(id) : children.set(parent, [id]);
966
+ }
967
+ const roots = [...info.nodeIds].filter((id) => !info.parentOf.has(id));
968
+ const weight = /* @__PURE__ */ new Map();
969
+ const weigh = (id, seen = /* @__PURE__ */ new Set()) => {
970
+ const hit = weight.get(id);
971
+ if (hit != null) return hit;
972
+ if (seen.has(id)) return 1;
973
+ seen.add(id);
974
+ let n = 1;
975
+ for (const k of children.get(id) ?? []) n += weigh(k, seen);
976
+ weight.set(id, n);
977
+ return n;
978
+ };
979
+ for (const id of info.nodeIds) weigh(id);
980
+ const out = {};
981
+ const place = (id, cx, cy, from, to, level) => {
982
+ const r = level * RING;
983
+ const mid = (from + to) / 2;
984
+ out[id] = { x: cx + Math.cos(mid) * r, y: cy + Math.sin(mid) * r };
985
+ const kids = children.get(id) ?? [];
986
+ if (!kids.length) return;
987
+ const ordered = info.orderedKids.get(id);
988
+ const list = ordered ?? kids;
989
+ const total = list.reduce((s, k) => s + weigh(k), 0) || 1;
990
+ let at = from;
991
+ for (const k of list) {
992
+ const span = (to - from) * weigh(k) / total;
993
+ const jitter = ordered ? 0 : (hash01(k) - 0.5) * span * 0.6;
994
+ const a = at + jitter;
995
+ place(k, cx, cy, a, a + span, level + 1);
996
+ at += span;
997
+ }
998
+ };
999
+ if (roots.length === 1) place(roots[0], 0, 0, -Math.PI / 2, Math.PI * 1.5, 0);
1000
+ else {
1001
+ roots.forEach((root, i) => {
1002
+ const from = -Math.PI / 2 + TAU * i / roots.length;
1003
+ place(root, 0, 0, from, from + TAU / roots.length, 0);
1004
+ });
1005
+ }
1006
+ return out;
1007
+ }
1008
+ var angle = (p) => Math.atan2(p.y, p.x);
1009
+ function enforceOrder(elements, info, positions) {
1010
+ const children = /* @__PURE__ */ new Map();
1011
+ for (const [id, parent] of info.parentOf) {
1012
+ const arr = children.get(parent);
1013
+ arr ? arr.push(id) : children.set(parent, [id]);
1014
+ }
1015
+ const out = {};
1016
+ for (const [id, p] of Object.entries(positions)) out[id] = { ...p };
1017
+ const weight = /* @__PURE__ */ new Map();
1018
+ const weigh = (id, seen = /* @__PURE__ */ new Set()) => {
1019
+ const hit = weight.get(id);
1020
+ if (hit != null) return hit;
1021
+ if (seen.has(id)) return 1;
1022
+ seen.add(id);
1023
+ let n = 1;
1024
+ for (const k of children.get(id) ?? []) n += weigh(k, seen);
1025
+ weight.set(id, n);
1026
+ return n;
1027
+ };
1028
+ const subtree = (id, acc = []) => {
1029
+ acc.push(id);
1030
+ for (const k of children.get(id) ?? []) subtree(k, acc);
1031
+ return acc;
1032
+ };
1033
+ const rotate = (ids, pivot, delta) => {
1034
+ if (Math.abs(delta) < 1e-6) return;
1035
+ const c = Math.cos(delta), s = Math.sin(delta);
1036
+ for (const id of ids) {
1037
+ const p = out[id];
1038
+ if (!p) continue;
1039
+ const dx = p.x - pivot.x, dy = p.y - pivot.y;
1040
+ out[id] = { x: pivot.x + dx * c - dy * s, y: pivot.y + dx * s + dy * c };
1041
+ }
1042
+ };
1043
+ const settle = (parent) => {
1044
+ const list = info.orderedKids.get(parent);
1045
+ const pivot = out[parent];
1046
+ if (!list || !pivot) return;
1047
+ const present = list.filter((id) => out[id]);
1048
+ if (present.length < 2) return;
1049
+ const total = present.reduce((s, id) => s + weigh(id), 0) || 1;
1050
+ let at = angle({ x: out[present[0]].x - pivot.x, y: out[present[0]].y - pivot.y });
1051
+ for (const id of present) {
1052
+ const p = out[id];
1053
+ const cur = angle({ x: p.x - pivot.x, y: p.y - pivot.y });
1054
+ let delta = at - cur;
1055
+ while (delta > Math.PI) delta -= TAU;
1056
+ while (delta < -Math.PI) delta += TAU;
1057
+ rotate(subtree(id), pivot, delta);
1058
+ at += TAU * weigh(id) / total;
1059
+ }
1060
+ for (const id of present) settle(id);
1061
+ };
1062
+ const roots = [...info.nodeIds].filter((id) => !info.parentOf.has(id));
1063
+ for (const root of roots) settle(root);
1064
+ return out;
1065
+ }
1066
+
914
1067
  // src/layout/engine.ts
915
1068
  var CACHE_PREFIX = "gb:pos:v3:";
916
1069
  var CACHE_KEEP = 16;
@@ -921,7 +1074,7 @@ function shapeHash(elements, salt) {
921
1074
  if (el.data._force === false) continue;
922
1075
  parts.push(`E ${el.data.source}>${el.data.target}`);
923
1076
  } else {
924
- parts.push(`N ${el.data.id}|${el.data.parent ?? ""}|${el.data._struct === false ? "a" : "s"}`);
1077
+ parts.push(`N ${el.data.id}|${el.data.parent ?? ""}|${el.data.order ?? ""}|${el.data._struct === false ? "a" : "s"}`);
925
1078
  }
926
1079
  }
927
1080
  parts.sort();
@@ -1161,14 +1314,17 @@ function computeLayout(req) {
1161
1314
  const owner = req.owner ?? ns;
1162
1315
  const current = inflight.get(owner);
1163
1316
  if (current) current.superseded = true;
1317
+ const order = readOrder(req.elements);
1318
+ const job = order && !req.seed ? { ...req, seed: orderSeed(req.elements, order) } : req;
1164
1319
  const run = { hash, superseded: false, promise: null };
1165
1320
  const runner = mode === "whole" ? runWhole : runDivide;
1166
- run.promise = runner(req, () => run.superseded).then((pos) => {
1321
+ run.promise = runner(job, () => run.superseded).then((pos) => {
1322
+ const settled = order ? enforceOrder(req.elements, order, pos) : pos;
1167
1323
  if (!run.stale) {
1168
- cachePut(hash, pos);
1169
- lastByNs.set(ns, { hash, pos });
1324
+ cachePut(hash, settled);
1325
+ lastByNs.set(ns, { hash, pos: settled });
1170
1326
  }
1171
- return pos;
1327
+ return settled;
1172
1328
  }).finally(() => {
1173
1329
  if (inflight.get(owner) === run) inflight.delete(owner);
1174
1330
  });
@@ -1646,6 +1802,48 @@ function LayerPicker({ layers, view, setView, disabled }) {
1646
1802
  )
1647
1803
  ] });
1648
1804
  }
1805
+ function LayerUnavailable(p) {
1806
+ const t = useStrings();
1807
+ const pop = usePopover(MENU_PLACE);
1808
+ return /* @__PURE__ */ jsxs("div", { className: "gb-menu", ref: pop.ref, children: [
1809
+ /* @__PURE__ */ jsx(
1810
+ "button",
1811
+ {
1812
+ className: "gb-icon-btn",
1813
+ style: { opacity: 0.45 },
1814
+ "aria-label": t.layersOff,
1815
+ title: t.layersOff,
1816
+ "aria-expanded": pop.open,
1817
+ onClick: () => pop.setOpen(!pop.open),
1818
+ children: /* @__PURE__ */ jsx(Icon, { d: I.stack })
1819
+ }
1820
+ ),
1821
+ pop.open && /* @__PURE__ */ jsxs(
1822
+ "div",
1823
+ {
1824
+ ref: pop.panelRef,
1825
+ className: "gb-panel",
1826
+ role: "dialog",
1827
+ "data-x": pop.placement?.align,
1828
+ "data-y": pop.placement?.side,
1829
+ style: pop.placement ? { left: pop.placement.left, top: pop.placement.top, right: "auto", bottom: "auto" } : { visibility: "hidden" },
1830
+ children: [
1831
+ /* @__PURE__ */ jsxs("div", { className: "gb-panel-sec", children: [
1832
+ /* @__PURE__ */ jsx("div", { className: "gb-panel-h", children: t.layersOff }),
1833
+ /* @__PURE__ */ jsx("p", { style: { margin: "4px 0 0", maxWidth: 260, fontSize: 12, lineHeight: 1.45, opacity: 0.75 }, children: p.reason })
1834
+ ] }),
1835
+ /* @__PURE__ */ jsxs("div", { className: "gb-panel-sec gb-menu-list", children: [
1836
+ p.onForce && /* @__PURE__ */ jsx("button", { className: "gb-menu-item", onClick: () => {
1837
+ pop.setOpen(false);
1838
+ p.onForce();
1839
+ }, children: t.forceLayers }),
1840
+ /* @__PURE__ */ jsx("button", { className: "gb-menu-item", onClick: () => pop.setOpen(false), children: t.ok })
1841
+ ] })
1842
+ ]
1843
+ }
1844
+ )
1845
+ ] });
1846
+ }
1649
1847
  var MENU_PLACE = { side: "bottom", align: "start" };
1650
1848
  var ISSUES_PLACE = { side: "top", align: "end" };
1651
1849
  function usePopover(prefer) {
@@ -1850,13 +2048,14 @@ function Toolbar(p) {
1850
2048
  }
1851
2049
  ),
1852
2050
  multi && /* @__PURE__ */ jsx(LayerPicker, { layers: p.layers, view, setView, disabled: p.busy }),
2051
+ controls.layers && !multi && /* @__PURE__ */ jsx(LayerUnavailable, { reason: p.layerIssue ?? "", onForce: p.onForceLayers }),
1853
2052
  originPick && view.origins?.length === 1 && /* Filtering by source: the state is in plain sight, and one × returns to everything. */
1854
2053
  /* @__PURE__ */ jsxs("span", { className: "gb-chip", title: view.origins[0], children: [
1855
2054
  /* @__PURE__ */ jsx(Icon, { d: I.file, size: 13 }),
1856
2055
  /* @__PURE__ */ jsx("span", { children: shortOrigin(view.origins[0]) }),
1857
2056
  /* @__PURE__ */ jsx("button", { "aria-label": t.allOrigins, title: t.allOrigins, onClick: () => setView({ origins: null }), children: /* @__PURE__ */ jsx(Icon, { d: I.x, size: 12 }) })
1858
2057
  ] }),
1859
- (originPick || p.onAdd || p.onRelayout) && /* @__PURE__ */ jsxs(ViewMenu, { foot: t.stats(p.stats.nodes, p.stats.edges), children: [
2058
+ (originPick || p.onAdd || p.onRelayout || p.onRestructure) && /* @__PURE__ */ jsxs(ViewMenu, { foot: t.stats(p.stats.nodes, p.stats.edges), children: [
1860
2059
  originPick && /* @__PURE__ */ jsxs("div", { className: "gb-panel-sec", children: [
1861
2060
  /* @__PURE__ */ jsx("div", { className: "gb-panel-h", children: t.origins }),
1862
2061
  /* @__PURE__ */ jsxs(
@@ -1876,7 +2075,10 @@ function Toolbar(p) {
1876
2075
  /* @__PURE__ */ jsx("button", { className: "gb-menu-item", onClick: () => p.onAdd("node"), disabled: p.busy, children: t.addNode }),
1877
2076
  /* @__PURE__ */ jsx("button", { className: "gb-menu-item", onClick: () => p.onAdd("edge"), disabled: p.busy, children: t.addEdge })
1878
2077
  ] }),
1879
- p.onRelayout && /* @__PURE__ */ jsx("div", { className: "gb-panel-sec gb-menu-list", children: /* @__PURE__ */ jsx("button", { className: "gb-menu-item", onClick: p.onRelayout, disabled: p.busy, children: t.relayout }) })
2078
+ (p.onRelayout || p.onRestructure) && /* @__PURE__ */ jsxs("div", { className: "gb-panel-sec gb-menu-list", children: [
2079
+ p.onRelayout && /* @__PURE__ */ jsx("button", { className: "gb-menu-item", onClick: p.onRelayout, disabled: p.busy, children: t.relayout }),
2080
+ p.onRestructure && /* @__PURE__ */ jsx("button", { className: "gb-menu-item", onClick: p.onRestructure, disabled: p.busy, children: t.restructure })
2081
+ ] })
1880
2082
  ] })
1881
2083
  ] });
1882
2084
  }
@@ -2687,7 +2889,7 @@ function tally(model, profile) {
2687
2889
  let plain = 0;
2688
2890
  for (const n of model.nodes) {
2689
2891
  const hollow = !!n.flags?.some((f) => profile.placeholderFlags.includes(f));
2690
- if (!hollow && n.kind && n.kind in profile.nodePalette) nodes[n.kind] = (nodes[n.kind] ?? 0) + 1;
2892
+ if (!hollow && n.kind) nodes[n.kind] = (nodes[n.kind] ?? 0) + 1;
2691
2893
  else plain++;
2692
2894
  }
2693
2895
  for (const e of model.edges) if (e.kind && e.kind in profile.edgePalette) edges[e.kind] = (edges[e.kind] ?? 0) + 1;
@@ -2996,7 +3198,7 @@ function Legend(p) {
2996
3198
  picking: (on) => setPicking(on)
2997
3199
  }), [close]);
2998
3200
  const slot = (key) => ({ k: key, open: open === key, ctl });
2999
- const nodeKinds = Object.keys(profile.nodePalette).filter((k) => count.nodes[k]);
3201
+ const nodeKinds = Object.keys(count.nodes).sort((a, b) => count.nodes[b] - count.nodes[a] || a.localeCompare(b));
3000
3202
  const edgeKinds = Object.keys(profile.edgePalette).filter((k) => count.edges[k]);
3001
3203
  const markCache = useRef3({ cy: null, style: null, shapes: /* @__PURE__ */ new Map(), strokes: /* @__PURE__ */ new Map() });
3002
3204
  const marks = readMarks(markCache.current, p.cy, profile.style, nodeKinds, edgeKinds);
@@ -3010,6 +3212,7 @@ function Legend(p) {
3010
3212
  const set = (key, kind, v) => onLook({ ...look, [key]: { ...look[key], [kind]: v } });
3011
3213
  const setSkeleton = (c) => onLook({ ...look, skeleton: c });
3012
3214
  const name = (k) => profile.kindLabels[k] ?? k;
3215
+ const colorOfKind = (k) => profile.nodePalette[k] ?? theme.skeleton;
3013
3216
  const shown = (c) => themeColor(theme, c);
3014
3217
  const content = p.collapsed ? /* @__PURE__ */ jsx3(
3015
3218
  "button",
@@ -3026,7 +3229,7 @@ function Legend(p) {
3026
3229
  /* @__PURE__ */ jsxs3("div", { className: "gb-lg-body", children: [
3027
3230
  p.caption && /* @__PURE__ */ jsx3("div", { className: "gb-lg-cap", children: p.caption }),
3028
3231
  /* @__PURE__ */ jsxs3("div", { className: "gb-lg-row", children: [
3029
- /* @__PURE__ */ jsx3(
3232
+ count.plain > 0 && /* @__PURE__ */ jsx3(
3030
3233
  Item,
3031
3234
  {
3032
3235
  ...slot("n:"),
@@ -3045,15 +3248,15 @@ function Legend(p) {
3045
3248
  Item,
3046
3249
  {
3047
3250
  ...slot(`n:${k}`),
3048
- color: shown(profile.nodePalette[k]),
3049
- title: name(k),
3251
+ color: shown(colorOfKind(k)),
3252
+ title: profile.nodePalette[k] ? name(k) : `${name(k)} \u2014 ${t.noColor}`,
3050
3253
  off,
3051
3254
  onToggle: () => p.onToggle("nodes", k),
3052
3255
  onPick: (c) => set("nodePalette", k, untheme(theme, c)),
3053
3256
  scale,
3054
3257
  glyph: "dot",
3055
3258
  onScale: (s) => set("nodeSize", k, s),
3056
- mark: /* @__PURE__ */ jsx3(NodeMark, { color: shown(profile.nodePalette[k]), shape: marks.shape(k), ring: theme.canvas, off, scale }),
3259
+ mark: /* @__PURE__ */ jsx3(NodeMark, { color: shown(colorOfKind(k)), shape: marks.shape(k), ring: theme.canvas, off, scale }),
3057
3260
  label: name(k),
3058
3261
  count: count.nodes[k]
3059
3262
  },
@@ -3577,6 +3780,9 @@ function GraphCanvas(props) {
3577
3780
  onEdit,
3578
3781
  renderEditor,
3579
3782
  resolveUrl,
3783
+ onRestructure,
3784
+ layerIssue,
3785
+ onForceLayers,
3580
3786
  animate = true,
3581
3787
  matchKey,
3582
3788
  onTransition,
@@ -5518,7 +5724,10 @@ ${lt}`;
5518
5724
  onPick: pick,
5519
5725
  inputRef: searchInputRef,
5520
5726
  onAdd: editable ? startAdd : void 0,
5521
- onRelayout: controls.relayout && !diff ? relayout : void 0
5727
+ onRelayout: controls.relayout && !diff ? relayout : void 0,
5728
+ onRestructure: controls.relayout && !diff ? onRestructure : void 0,
5729
+ layerIssue,
5730
+ onForceLayers
5522
5731
  }
5523
5732
  ),
5524
5733
  (note || model.errors.length > 0 || legend && !compact) && /* Bottom left: the host's caption (if any) on top, the legend below. Top left is tools, bottom right is the camera. */
@@ -22,6 +22,9 @@ var EN = {
22
22
  stackLayer: (l, on) => `${on ? "Hide" : "Stack"} \u201C${l}\u201D`,
23
23
  stack: "Stack layers",
24
24
  stackTip: (on) => on ? "Stacked (turn off to view one layer at a time)" : "Stack layers (2.5D)",
25
+ layersOff: "Layers are off",
26
+ forceLayers: "Open the floors anyway",
27
+ ok: "OK",
25
28
  more: "More",
26
29
  origins: "Source",
27
30
  allOrigins: "All sources",
@@ -34,6 +37,7 @@ var EN = {
34
37
  fit: "Fit (0)",
35
38
  legend: "Legend",
36
39
  legendPlainName: "Default",
40
+ noColor: "no colour set",
37
41
  legendPlain: "Nodes without a color of their own: containers and placeholders, shaded by how much hangs under them",
38
42
  legendContainsName: "Contains",
39
43
  legendHint: "Hover an item: color, hide, size",
@@ -50,6 +54,7 @@ var EN = {
50
54
  addNode: "Add node\u2026",
51
55
  addEdge: "Add edge\u2026",
52
56
  relayout: "Re-layout",
57
+ restructure: "Restructure",
53
58
  edit: "Edit",
54
59
  editNode: "Edit node",
55
60
  newNode: "New node",
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  EN
3
- } from "./chunk-EJURBBFC.js";
3
+ } from "./chunk-H2WV3DTN.js";
4
4
 
5
5
  // src/react/i18n.ts
6
6
  import { createContext, useContext } from "react";
@@ -0,0 +1,68 @@
1
+ // src/layers.ts
2
+ var MAX_DERIVED_LAYERS = 8;
3
+ var EMPTY = { layers: [], layerOf: /* @__PURE__ */ new Map() };
4
+ function orderedChildren(children, id) {
5
+ const kids = children.get(id) ?? [];
6
+ if (kids.length < 2) return null;
7
+ const withOrder = kids.filter((n) => typeof n.order === "number" && Number.isFinite(n.order));
8
+ if (withOrder.length !== kids.length) return null;
9
+ const seen = /* @__PURE__ */ new Set();
10
+ for (const k of withOrder) {
11
+ if (seen.has(k.order)) return null;
12
+ seen.add(k.order);
13
+ }
14
+ return [...kids].sort((a, b) => a.order - b.order);
15
+ }
16
+ function childrenMap(model) {
17
+ const out = /* @__PURE__ */ new Map();
18
+ for (const n of model.nodes) {
19
+ if (!n.parent || !model.index.has(n.parent)) continue;
20
+ const arr = out.get(n.parent);
21
+ arr ? arr.push(n) : out.set(n.parent, [n]);
22
+ }
23
+ return out;
24
+ }
25
+ function deriveLayers(model, colors = [], force = false) {
26
+ if (!model.nodes.length) return EMPTY;
27
+ const children = childrenMap(model);
28
+ const roots = model.nodes.filter((n) => !n.parent || !model.index.has(n.parent));
29
+ let ordered = null;
30
+ let anchor;
31
+ if (roots.length === 1) {
32
+ anchor = roots[0];
33
+ ordered = orderedChildren(children, anchor.id);
34
+ if (!ordered && force) ordered = children.get(anchor.id) ?? null;
35
+ } else if (force && roots.length > 1) {
36
+ ordered = roots;
37
+ }
38
+ if (!ordered || ordered.length < 2 || ordered.length > MAX_DERIVED_LAYERS) return EMPTY;
39
+ const layers = ordered.map((n, i) => ({
40
+ id: n.id,
41
+ label: n.label || n.id,
42
+ z: i,
43
+ ...colors[i] ? { color: colors[i] } : {}
44
+ }));
45
+ const layerOf = /* @__PURE__ */ new Map();
46
+ for (const [i, layer] of ordered.entries()) {
47
+ const stack = [layer.id];
48
+ while (stack.length) {
49
+ const id = stack.pop();
50
+ layerOf.set(id, layers[i].id);
51
+ for (const k of children.get(id) ?? []) stack.push(k.id);
52
+ }
53
+ }
54
+ if (anchor) layerOf.set(anchor.id, layers[0].id);
55
+ return { layers, layerOf, anchor: anchor?.id };
56
+ }
57
+ function layerIndexOf(derived, id) {
58
+ const lid = derived.layerOf.get(id);
59
+ return lid ? derived.layers.findIndex((l) => l.id === lid) : -1;
60
+ }
61
+
62
+ export {
63
+ MAX_DERIVED_LAYERS,
64
+ orderedChildren,
65
+ childrenMap,
66
+ deriveLayers,
67
+ layerIndexOf
68
+ };
@@ -106,6 +106,7 @@ function parsePhysical(origin, text) {
106
106
  label: String(n.label ?? n.name ?? n.id),
107
107
  kind: typeof n.kind === "string" ? n.kind : void 0,
108
108
  parent: ref(n.parent),
109
+ order: typeof n.order === "number" && Number.isFinite(n.order) ? n.order : void 0,
109
110
  layer: typeof n.layer === "string" ? n.layer : void 0,
110
111
  // Marks carried by the data itself (e.g. a placeholder "with no content yet"). They are data, not derived: when the profile's
111
112
  // `placeholderFlags` recognizes one, it is drawn as skeleton.
@@ -126,7 +127,9 @@ function parsePhysical(origin, text) {
126
127
  function toPhysical(model) {
127
128
  const clean = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== void 0 && v !== ""));
128
129
  return {
129
- nodes: model.nodes.map((n) => clean({ ...n.data, id: n.id, label: n.label, kind: n.kind, parent: n.parent, layer: n.layer })),
130
+ // `layer` is derived, never written: a file that gains it would freeze a view decision into the data.
131
+ // `parent` is always written, `null` for a root: dropping it (it was `undefined`) made every export invalid in 0.2.
132
+ nodes: model.nodes.map((n) => clean({ ...n.data, id: n.id, label: n.label, kind: n.kind, parent: n.parent ?? null, order: n.order })),
130
133
  edges: model.edges.map((e) => clean({ ...e.data, source: e.source, target: e.target, kind: e.kind, label: e.label, weight: e.weight }))
131
134
  };
132
135
  }
@@ -586,6 +589,7 @@ function buildElements(model, profile, opts = {}) {
586
589
  kind: n.kind ?? "",
587
590
  layer,
588
591
  origin: n.origin ?? null,
592
+ order: n.order ?? null,
589
593
  share: shareOf(n.id),
590
594
  leaves: leavesOf(n.id),
591
595
  detail: n.data ?? null,