glassbrain 0.2.0-alpha.3 → 0.2.0-alpha.4

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
@@ -23,15 +23,15 @@ lets you read one at a glance, and when the agent learns something, shows you ex
23
23
  {
24
24
  "nodes": [
25
25
  { "id": "src", "label": "src", "kind": "folder", "parent": null },
26
- { "id": "src/auth", "label": "auth", "kind": "folder", "parent": "src", "order": 1 },
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", "order": 2 }
26
+ { "id": "src/auth", "label": "auth", "kind": "folder", "parent": "src@1" },
27
+ { "id": "src/auth/login.ts", "label": "login.ts", "kind": "file", "parent": "src/auth@-1" },
28
+ { "id": "src/db.ts", "label": "db.ts", "kind": "file", "parent": "src@2" }
29
29
  ],
30
30
  "edges": [{ "source": "src/auth/login.ts", "target": "src/db.ts", "kind": "uses" }]
31
31
  }
32
32
  ```
33
33
 
34
- Next to it, one colour per kind — a kind with no colour is drawn as skeleton, so `validate` reports
34
+ Next to it, one colour per kind — a kind with no colour is drawn as skeleton, so `lint` reports
35
35
  it as an error (forty kinds without colours look exactly like one kind):
36
36
 
37
37
  ```json
@@ -39,21 +39,21 @@ it as an error (forty kinds without colours look exactly like one kind):
39
39
  "edgePalette": { "uses": "#2563eb" } }
40
40
  ```
41
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.
42
+ `parent` is required on every node (null for the one root) and says what sits inside what **and where
43
+ it sits**: `id@order`, or `id@-1` for an explicitly unordered set. The layout reads the numbers
44
+ clockwise, and a root whose children are numbered *is* the 2.5D layer view. There is no layer field.
45
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
46
- agent can act on — every finding carries its repair:
46
+ agent can act on — findings identify the problem and structural advice suggests a next step:
47
47
 
48
48
  ```text
49
- $ npx glassbrain validate kg
49
+ $ npx glassbrain lint kg
50
50
  error dangling-edge graph.json edge src/auth/login.ts → src/session points at the missing node src/session
51
51
  an edge can only join nodes that exist. Add the node, or repoint the edge:
52
52
  - { "source": "src/auth/login.ts", "target": "src/session" }
53
53
  + { "source": "src/auth/login.ts", "target": "EXISTING_ID" }
54
54
 
55
55
  kg: 2 nodes, 1 edge — 1 error · 0 warnings · 0 infos
56
- structure: 1 root · depth 2 · 1/2 parented · isomorphism 1.00
56
+ structure: 1 root · depth 2 · 1/2 parented · structure pressure 0.000 · 0 assessed / 1 unassessed
57
57
 
58
58
  $ npx glassbrain diff kg/1-skeleton kg/2-filled
59
59
  1-skeleton → 2-filled
@@ -65,7 +65,7 @@ changed (1)
65
65
  ```
66
66
 
67
67
  Errors exit 1 (`--strict`: warnings too), `diff --exit-code` exits 1 when anything changed, and
68
- `--json` gives the same as structured output. So an agent can build a graph in steps, validate
68
+ `--json` gives the same as structured output. So an agent can build a graph in steps, lint
69
69
  each one, and check that a step did only its job.
70
70
 
71
71
  ## 2. Read it like a brain scan
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  EN
3
- } from "./chunk-H2WV3DTN.js";
3
+ } from "./chunk-ISWKAU5G.js";
4
4
 
5
5
  // src/react/i18n.ts
6
6
  import { createContext, useContext } from "react";
@@ -174,6 +174,7 @@ function DetailPanel({ node, spec, kindLabels, onClose, renderText, onEdit, colo
174
174
  "id",
175
175
  "label",
176
176
  "parent",
177
+ "order",
177
178
  "layer",
178
179
  "flags"
179
180
  ]);
@@ -195,6 +196,14 @@ function DetailPanel({ node, spec, kindLabels, onClose, renderText, onEdit, colo
195
196
  ] }),
196
197
  /* @__PURE__ */ jsxs("div", { className: "gb-db", children: [
197
198
  /* @__PURE__ */ jsx3("div", { className: "gb-dt", children: node.label }),
199
+ /* @__PURE__ */ jsxs("div", { className: "gb-line", children: [
200
+ /* @__PURE__ */ jsx3("span", { className: "gb-k", children: t.parent }),
201
+ /* @__PURE__ */ jsx3("span", { children: node.parent ?? "Root" })
202
+ ] }),
203
+ node.parent && /* @__PURE__ */ jsxs("div", { className: "gb-line", children: [
204
+ /* @__PURE__ */ jsx3("span", { className: "gb-k", children: "Order" }),
205
+ /* @__PURE__ */ jsx3("span", { children: node.order ?? "Unordered" })
206
+ ] }),
198
207
  node.flags.map((f) => {
199
208
  const note = spec.flagNotes?.[f];
200
209
  return note ? /* @__PURE__ */ jsx3("div", { className: "gb-flag", children: note }, f) : null;
@@ -1,5 +1,4 @@
1
1
  // src/layers.ts
2
- var MAX_DERIVED_LAYERS = 8;
3
2
  var EMPTY = { layers: [], layerOf: /* @__PURE__ */ new Map() };
4
3
  function orderedChildren(children, id) {
5
4
  const kids = children.get(id) ?? [];
@@ -35,7 +34,7 @@ function deriveLayers(model, colors = [], force = false) {
35
34
  } else if (force && roots.length > 1) {
36
35
  ordered = roots;
37
36
  }
38
- if (!ordered || ordered.length < 2 || ordered.length > MAX_DERIVED_LAYERS) return EMPTY;
37
+ if (!ordered || ordered.length < 2) return EMPTY;
39
38
  const layers = ordered.map((n, i) => ({
40
39
  id: n.id,
41
40
  label: n.label || n.id,
@@ -60,7 +59,6 @@ function layerIndexOf(derived, id) {
60
59
  }
61
60
 
62
61
  export {
63
- MAX_DERIVED_LAYERS,
64
62
  orderedChildren,
65
63
  childrenMap,
66
64
  deriveLayers,
@@ -54,7 +54,7 @@ var EN = {
54
54
  addNode: "Add node\u2026",
55
55
  addEdge: "Add edge\u2026",
56
56
  relayout: "Re-layout",
57
- restructure: "Restructure",
57
+ restructure: "Re-structure",
58
58
  edit: "Edit",
59
59
  editNode: "Edit node",
60
60
  newNode: "New node",
@@ -84,6 +84,10 @@ var EN = {
84
84
  "no-node": "Node not found",
85
85
  "no-parent": "Parent not found",
86
86
  cycle: "Cannot be placed inside itself",
87
+ "id-format": "Use 1\u201380 characters without spaces or @",
88
+ "multiple-roots": "Keep one root: choose an existing parent",
89
+ "order-mixed": "Siblings must all be numbered or all unordered",
90
+ "order-invalid": "Order must be positive and unique among siblings (blank = unordered)",
87
91
  "has-children": "Has children \u2014 delete or move them first",
88
92
  "no-endpoint": "Pick both ends",
89
93
  "self-loop": "Both ends are the same node",
@@ -52,17 +52,17 @@ import {
52
52
  unionSides,
53
53
  untheme,
54
54
  validateModel
55
- } from "./chunk-WW23WBYG.js";
55
+ } from "./chunk-SEOAVI3E.js";
56
56
  import {
57
57
  DetailPanel,
58
58
  I,
59
59
  Icon,
60
60
  StringsContext,
61
61
  useStrings
62
- } from "./chunk-PBF4KMQB.js";
62
+ } from "./chunk-3JGZ27DC.js";
63
63
  import {
64
64
  EN
65
- } from "./chunk-H2WV3DTN.js";
65
+ } from "./chunk-ISWKAU5G.js";
66
66
  import {
67
67
  inlinePool
68
68
  } from "./chunk-YGRPNAXE.js";
@@ -912,19 +912,26 @@ function resolveOverlaps(boxes, o = {}) {
912
912
  }
913
913
 
914
914
  // src/layout/order.ts
915
+ var VIRTUAL_PARENT = "";
915
916
  function readOrder(elements) {
916
- const parentOf = /* @__PURE__ */ new Map();
917
- const kids = /* @__PURE__ */ new Map();
918
917
  const nodeIds = /* @__PURE__ */ new Set();
918
+ const rawParent = /* @__PURE__ */ new Map();
919
+ const rawOrder = /* @__PURE__ */ new Map();
919
920
  for (const el of elements) {
920
921
  const id = el.data.id;
921
922
  if (!id || el.data.source) continue;
922
923
  nodeIds.add(id);
923
- const parent = el.data.parent;
924
- if (!parent) continue;
925
- parentOf.set(id, parent);
924
+ const p = el.data.parentId ?? el.data.parent;
925
+ if (p) rawParent.set(id, p);
926
926
  const raw = el.data.order;
927
- const order = typeof raw === "number" && Number.isFinite(raw) ? raw : null;
927
+ rawOrder.set(id, typeof raw === "number" && Number.isFinite(raw) ? raw : null);
928
+ }
929
+ const parentOf = /* @__PURE__ */ new Map();
930
+ const kids = /* @__PURE__ */ new Map();
931
+ for (const [id, p] of rawParent) {
932
+ const parent = nodeIds.has(p) ? p : VIRTUAL_PARENT;
933
+ parentOf.set(id, parent);
934
+ const order = rawOrder.get(id) ?? null;
928
935
  const arr = kids.get(parent);
929
936
  arr ? arr.push({ id, order }) : kids.set(parent, [{ id, order }]);
930
937
  }
@@ -997,12 +1004,16 @@ function orderSeed(elements, info) {
997
1004
  }
998
1005
  };
999
1006
  if (roots.length === 1) place(roots[0], 0, 0, -Math.PI / 2, Math.PI * 1.5, 0);
1000
- else {
1007
+ else if (roots.length > 1) {
1001
1008
  roots.forEach((root, i) => {
1002
1009
  const from = -Math.PI / 2 + TAU * i / roots.length;
1003
1010
  place(root, 0, 0, from, from + TAU / roots.length, 0);
1004
1011
  });
1005
1012
  }
1013
+ if ((children.get(VIRTUAL_PARENT) ?? []).length) {
1014
+ place(VIRTUAL_PARENT, 0, 0, -Math.PI / 2, Math.PI * 1.5, 0);
1015
+ delete out[VIRTUAL_PARENT];
1016
+ }
1006
1017
  return out;
1007
1018
  }
1008
1019
  var angle = (p) => Math.atan2(p.y, p.x);
@@ -1042,10 +1053,16 @@ function enforceOrder(elements, info, positions) {
1042
1053
  };
1043
1054
  const settle = (parent) => {
1044
1055
  const list = info.orderedKids.get(parent);
1045
- const pivot = out[parent];
1046
- if (!list || !pivot) return;
1056
+ if (!list) {
1057
+ for (const id of children.get(parent) ?? []) settle(id);
1058
+ return;
1059
+ }
1047
1060
  const present = list.filter((id) => out[id]);
1048
1061
  if (present.length < 2) return;
1062
+ const pivot = out[parent] ?? {
1063
+ x: present.reduce((s, id) => s + out[id].x, 0) / present.length,
1064
+ y: present.reduce((s, id) => s + out[id].y, 0) / present.length
1065
+ };
1049
1066
  const total = present.reduce((s, id) => s + weigh(id), 0) || 1;
1050
1067
  let at = angle({ x: out[present[0]].x - pivot.x, y: out[present[0]].y - pivot.y });
1051
1068
  for (const id of present) {
@@ -1061,6 +1078,7 @@ function enforceOrder(elements, info, positions) {
1061
1078
  };
1062
1079
  const roots = [...info.nodeIds].filter((id) => !info.parentOf.has(id));
1063
1080
  for (const root of roots) settle(root);
1081
+ if (children.has(VIRTUAL_PARENT)) settle(VIRTUAL_PARENT);
1064
1082
  return out;
1065
1083
  }
1066
1084
 
@@ -1190,6 +1208,18 @@ async function runDivide(req, aborted) {
1190
1208
  }
1191
1209
  const rootDims = req.elements.some((e) => e.data.id === VIRTUAL_ROOT && !e.data.source) && req.dims[VIRTUAL_ROOT] ? req.dims[VIRTUAL_ROOT] : void 0;
1192
1210
  const coarseEls = buildCoarseElements(comps, compOf, req.elements, (k) => byKey.get(k).bbox, rootDims);
1211
+ if (req.seed) {
1212
+ const seed2 = req.seed;
1213
+ const compByKey = new Map(comps.map((c) => [c.key, c]));
1214
+ const mid = (pts) => pts.length ? { x: pts.reduce((s, q2) => s + q2.x, 0) / pts.length, y: pts.reduce((s, q2) => s + q2.y, 0) / pts.length } : void 0;
1215
+ for (const el of coarseEls) {
1216
+ if (el.data.source) continue;
1217
+ const key = el.data.id;
1218
+ const pts = key === VIRTUAL_ROOT ? Object.values(seed2) : (compByKey.get(key)?.nodes ?? []).map((n) => seed2[n.data.id]).filter(Boolean);
1219
+ const p = mid(pts);
1220
+ if (p) el.position = p;
1221
+ }
1222
+ }
1193
1223
  const avgSize = comps.reduce((s, c) => {
1194
1224
  const b = byKey.get(c.key).bbox;
1195
1225
  return s + (b.w + b.h) / 2;
@@ -1717,7 +1747,7 @@ function Hl({ text, re }) {
1717
1747
  out.push(text.slice(last));
1718
1748
  return /* @__PURE__ */ jsx(Fragment, { children: out });
1719
1749
  }
1720
- function LayerPicker({ layers, view, setView, disabled }) {
1750
+ function LayerPicker({ layers, view, setView, disabled, collapsed }) {
1721
1751
  const t = useStrings();
1722
1752
  const box = useRef(null);
1723
1753
  const [thumb, setThumb] = useState2(null);
@@ -1727,7 +1757,7 @@ function LayerPicker({ layers, view, setView, disabled }) {
1727
1757
  const single = stacked ? null : view.layers?.[0] ?? layers[0].id;
1728
1758
  useLayoutEffect2(() => {
1729
1759
  const root = box.current;
1730
- if (!root || single == null) {
1760
+ if (collapsed || !root || single == null) {
1731
1761
  setThumb(null);
1732
1762
  return;
1733
1763
  }
@@ -1767,8 +1797,8 @@ function LayerPicker({ layers, view, setView, disabled }) {
1767
1797
  const next = layers.map((l) => l.id).filter((l) => l === id ? !on.has(l) : on.has(l));
1768
1798
  setView({ layers: next });
1769
1799
  };
1770
- return /* @__PURE__ */ jsxs("div", { className: `gb-segm gb-layers${stacked ? " is-multi" : ""}`, role: stacked ? "group" : "radiogroup", children: [
1771
- /* @__PURE__ */ jsxs("div", { className: `gb-layer-track${edges.left ? " can-left" : ""}${edges.right ? " can-right" : ""}`, ref: box, children: [
1800
+ return /* @__PURE__ */ jsxs("div", { className: `gb-segm gb-layers${stacked ? " is-multi" : ""}${collapsed ? " is-collapsed" : ""}`, role: stacked ? "group" : "radiogroup", children: [
1801
+ /* @__PURE__ */ jsxs("div", { className: `gb-layer-track${edges.left ? " can-left" : ""}${edges.right ? " can-right" : ""}`, ref: box, "aria-hidden": collapsed, children: [
1772
1802
  thumb && /* @__PURE__ */ jsx("i", { className: "gb-thumb", style: { transform: `translateX(${thumb.x}px)`, width: thumb.w } }),
1773
1803
  layers.map((l) => {
1774
1804
  const lit = stacked ? on.has(l.id) : l.id === single;
@@ -1796,7 +1826,7 @@ function LayerPicker({ layers, view, setView, disabled }) {
1796
1826
  disabled,
1797
1827
  "aria-label": t.stack,
1798
1828
  title: t.stackTip(stacked),
1799
- onClick: () => setView(stacked ? { planes: false } : { planes: true, layers: null }),
1829
+ onClick: () => setView(stacked ? { planes: false, layers: null } : { planes: true, layers: null }),
1800
1830
  children: /* @__PURE__ */ jsx(Icon, { d: I.stack })
1801
1831
  }
1802
1832
  )
@@ -2047,7 +2077,7 @@ function Toolbar(p) {
2047
2077
  inputRef: p.inputRef
2048
2078
  }
2049
2079
  ),
2050
- multi && /* @__PURE__ */ jsx(LayerPicker, { layers: p.layers, view, setView, disabled: p.busy }),
2080
+ multi && /* @__PURE__ */ jsx(LayerPicker, { layers: p.layers, view, setView, disabled: p.busy, collapsed: !view.planes }),
2051
2081
  controls.layers && !multi && /* @__PURE__ */ jsx(LayerUnavailable, { reason: p.layerIssue ?? "", onForce: p.onForceLayers }),
2052
2082
  originPick && view.origins?.length === 1 && /* Filtering by source: the state is in plain sight, and one × returns to everything. */
2053
2083
  /* @__PURE__ */ jsxs("span", { className: "gb-chip", title: view.origins[0], children: [
@@ -2642,6 +2672,7 @@ function NodeForm({ target, model, onSubmit, onCancel, onRetarget, renderEditor,
2642
2672
  [node]
2643
2673
  );
2644
2674
  const [text, setText] = useState3(initial);
2675
+ const [orderText, setOrderText] = useState3(node?.order?.toString() ?? "");
2645
2676
  const [confirming, setConfirming] = useState3(false);
2646
2677
  const idErr = creating && id ? checkEdit(model, { op: "addNode", id, set: {} }) : null;
2647
2678
  const below = useMemo2(() => {
@@ -2665,8 +2696,9 @@ function NodeForm({ target, model, onSubmit, onCancel, onRetarget, renderEditor,
2665
2696
  setError(t.badJson);
2666
2697
  return;
2667
2698
  }
2699
+ const order = parent && orderText.trim() ? Number(orderText) : null;
2668
2700
  if (creating) {
2669
- if (submit({ op: "addNode", id: id.trim(), set: { ...content, parent } })) onCancel();
2701
+ if (submit({ op: "addNode", id: id.trim(), set: { ...content, parent, order } })) onCancel();
2670
2702
  return;
2671
2703
  }
2672
2704
  const set = {};
@@ -2674,6 +2706,7 @@ function NodeForm({ target, model, onSubmit, onCancel, onRetarget, renderEditor,
2674
2706
  if ((content.kind ?? void 0) !== node.kind) set.kind = content.kind;
2675
2707
  if (pretty(content.data ?? {}) !== pretty(node.data ?? {})) set.data = content.data ?? {};
2676
2708
  if (parent !== (node.parent ?? null)) set.parent = parent;
2709
+ if (order !== (node.order ?? null)) set.order = order;
2677
2710
  if (!Object.keys(set).length) {
2678
2711
  onCancel();
2679
2712
  return;
@@ -2739,6 +2772,23 @@ function NodeForm({ target, model, onSubmit, onCancel, onRetarget, renderEditor,
2739
2772
  }
2740
2773
  )
2741
2774
  ] }),
2775
+ /* @__PURE__ */ jsxs2("div", { className: "gb-fld", children: [
2776
+ /* @__PURE__ */ jsx2("label", { className: "gb-k", htmlFor: `${uid}-order`, children: "Order" }),
2777
+ /* @__PURE__ */ jsx2(
2778
+ "input",
2779
+ {
2780
+ id: `${uid}-order`,
2781
+ className: "gb-input",
2782
+ type: "number",
2783
+ min: "0",
2784
+ step: "any",
2785
+ disabled: !parent,
2786
+ placeholder: "Unordered",
2787
+ value: orderText,
2788
+ onChange: (e) => setOrderText(e.target.value)
2789
+ }
2790
+ )
2791
+ ] }),
2742
2792
  /* @__PURE__ */ jsxs2("div", { className: "gb-fld gb-grow", children: [
2743
2793
  /* @__PURE__ */ jsxs2("label", { className: "gb-k", htmlFor: `${uid}-json`, children: [
2744
2794
  t.content,
@@ -3889,6 +3939,9 @@ function GraphCanvas(props) {
3889
3939
  const searchInputRef = useRef4(null);
3890
3940
  const [selected, setSelected] = useState5(null);
3891
3941
  const [editing, setEditing] = useState5(null);
3942
+ const editingModel = props.editModel ?? model;
3943
+ const modelRef = useRef4(model);
3944
+ modelRef.current = model;
3892
3945
  const editable = !!onEdit && !diff;
3893
3946
  const onEditRef = useRef4(onEdit);
3894
3947
  onEditRef.current = onEdit;
@@ -4631,7 +4684,9 @@ function GraphCanvas(props) {
4631
4684
  origin: d.origin,
4632
4685
  container: builtRef.current.containers.has(d.id),
4633
4686
  flags: el.classes().filter((c) => !["gb-node", "flat", "container", "gb-hit", "gb-preview", "gb-preview-hop", d.kind].includes(c)),
4634
- detail: d.detail ?? null
4687
+ detail: d.detail ?? null,
4688
+ parent: modelRef.current.index.get(d.id)?.parent,
4689
+ order: modelRef.current.index.get(d.id)?.order
4635
4690
  });
4636
4691
  }, []);
4637
4692
  const selectTouch = useCallback2((target) => {
@@ -4907,7 +4962,7 @@ function GraphCanvas(props) {
4907
4962
  return { positions, source: "preset" };
4908
4963
  }
4909
4964
  let known;
4910
- if (!force && layoutModeRef.current === "continue") {
4965
+ if (force || layoutModeRef.current === "continue") {
4911
4966
  const all = { ...preset, ...rawRef.current };
4912
4967
  known = {};
4913
4968
  for (const id of ids) if (all[id]) known[id] = all[id];
@@ -5548,6 +5603,8 @@ ${lt}`;
5548
5603
  if (!s || !n) return null;
5549
5604
  return {
5550
5605
  ...s,
5606
+ parent: n.parent,
5607
+ order: n.order,
5551
5608
  label: n.label,
5552
5609
  kind: n.kind ?? "",
5553
5610
  layer: n.layer,
@@ -5564,14 +5621,14 @@ ${lt}`;
5564
5621
  const submitEdit = useCallback2((input) => {
5565
5622
  const only = view.origins?.length === 1 ? view.origins[0] : void 0;
5566
5623
  const op = input.op === "addNode" && only && !input.origin ? { ...input, origin: only } : input;
5567
- const err = checkEdit(model, op);
5624
+ const err = checkEdit(editingModel, op) ?? props.validateEdit?.(op);
5568
5625
  if (err) return err;
5569
5626
  if (op.op === "addNode") pendingPickRef.current = op.id;
5570
5627
  else if (op.op === "removeNode") setSelected(null);
5571
5628
  else keepSelRef.current = selected?.id ?? null;
5572
5629
  onEditRef.current?.(op);
5573
5630
  return null;
5574
- }, [model, selected, view.origins]);
5631
+ }, [editingModel, selected, view.origins, props.validateEdit]);
5575
5632
  const startAdd = useCallback2((what) => {
5576
5633
  setEditing(what === "node" ? { what, parent: selected?.id ?? null } : { what, source: selected?.id });
5577
5634
  }, [selected]);
@@ -5579,7 +5636,7 @@ ${lt}`;
5579
5636
  EditPanel,
5580
5637
  {
5581
5638
  target: editing,
5582
- model,
5639
+ model: editingModel,
5583
5640
  onSubmit: submitEdit,
5584
5641
  renderEditor,
5585
5642
  colorOf,
@@ -90,6 +90,19 @@ function originsOf(model) {
90
90
  for (const n of model.nodes) for (const o of originsOfNode(n)) seen.add(o);
91
91
  return [...seen].sort();
92
92
  }
93
+ function encodeParent(parent, order) {
94
+ if (!parent) return null;
95
+ if (order === void 0) return `${parent}@-1`;
96
+ if (!Number.isFinite(order) || order <= 0) throw new Error("order must be finite and positive");
97
+ const text = String(order);
98
+ if (!/[eE]/.test(text)) return `${parent}@${text}`;
99
+ const [mantissa, exponent] = text.toLowerCase().split("e");
100
+ const [whole, fraction = ""] = mantissa.split(".");
101
+ const digits = whole + fraction;
102
+ const at = whole.length + Number(exponent);
103
+ const decimal = at <= 0 ? `0.${"0".repeat(-at)}${digits}` : at >= digits.length ? digits + "0".repeat(at - digits.length) : `${digits.slice(0, at)}.${digits.slice(at)}`;
104
+ return `${parent}@${decimal}`;
105
+ }
93
106
 
94
107
  // src/profile.ts
95
108
  function parsePhysical(origin, text) {
@@ -101,18 +114,29 @@ function parsePhysical(origin, text) {
101
114
  }
102
115
  if (!raw || typeof raw !== "object" || !("nodes" in raw) && !("edges" in raw)) return null;
103
116
  const ref = (v) => typeof v === "string" || typeof v === "number" ? String(v) : void 0;
104
- const nodes = (Array.isArray(raw.nodes) ? raw.nodes : []).filter((n) => n && typeof n === "object" && ref(n.id) !== void 0).map((n) => ({
105
- id: String(n.id),
106
- label: String(n.label ?? n.name ?? n.id),
107
- kind: typeof n.kind === "string" ? n.kind : void 0,
108
- parent: ref(n.parent),
109
- order: typeof n.order === "number" && Number.isFinite(n.order) ? n.order : void 0,
110
- layer: typeof n.layer === "string" ? n.layer : void 0,
111
- // Marks carried by the data itself (e.g. a placeholder "with no content yet"). They are data, not derived: when the profile's
112
- // `placeholderFlags` recognizes one, it is drawn as skeleton.
113
- ...Array.isArray(n.flags) ? { flags: n.flags.filter((f) => typeof f === "string") } : {},
114
- data: n
115
- }));
117
+ const parentOf = (v) => {
118
+ const rawParent = ref(v);
119
+ const at = rawParent?.lastIndexOf("@") ?? -1;
120
+ const tail = at > 0 ? rawParent.slice(at + 1) : "";
121
+ if (!/^(-1|\d+(\.\d+)?)$/.test(tail)) return { parent: rawParent, order: void 0 };
122
+ const order = tail === "-1" ? void 0 : Number(tail);
123
+ return { parent: rawParent.slice(0, at), order: order !== void 0 && order > 0 ? order : void 0 };
124
+ };
125
+ const nodes = (Array.isArray(raw.nodes) ? raw.nodes : []).filter((n) => n && typeof n === "object" && ref(n.id) !== void 0).map((n) => {
126
+ const { parent, order } = parentOf(n.parent);
127
+ return {
128
+ id: String(n.id),
129
+ label: String(n.label ?? n.name ?? n.id),
130
+ kind: typeof n.kind === "string" ? n.kind : void 0,
131
+ parent,
132
+ order,
133
+ layer: typeof n.layer === "string" ? n.layer : void 0,
134
+ // Marks carried by the data itself (e.g. a placeholder "with no content yet"). They are data, not derived: when the profile's
135
+ // `placeholderFlags` recognizes one, it is drawn as skeleton.
136
+ ...Array.isArray(n.flags) ? { flags: n.flags.filter((f) => typeof f === "string") } : {},
137
+ data: n
138
+ };
139
+ });
116
140
  const edges = (Array.isArray(raw.edges) ? raw.edges : []).filter((e) => e && typeof e === "object").map((e) => ({
117
141
  source: String(e.source ?? e.from),
118
142
  target: String(e.target ?? e.to),
@@ -128,8 +152,11 @@ function toPhysical(model) {
128
152
  const clean = (o) => Object.fromEntries(Object.entries(o).filter(([, v]) => v !== void 0 && v !== ""));
129
153
  return {
130
154
  // `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 })),
155
+ // `parent` is always written, `null` for a root, and carries the order: `"<id>@<order|-1>"`. `order` is never a field of its own.
156
+ nodes: model.nodes.map((n) => {
157
+ const { order: _order, layer: _layer, ...data } = n.data ?? {};
158
+ return clean({ ...data, id: n.id, label: n.label, kind: n.kind, parent: encodeParent(n.parent, n.order) });
159
+ }),
133
160
  edges: model.edges.map((e) => clean({ ...e.data, source: e.source, target: e.target, kind: e.kind, label: e.label, weight: e.weight }))
134
161
  };
135
162
  }
@@ -590,6 +617,8 @@ function buildElements(model, profile, opts = {}) {
590
617
  layer,
591
618
  origin: n.origin ?? null,
592
619
  order: n.order ?? null,
620
+ // Parentage for the layout's order pass. A separate key, not `parent`: cytoscape treats `data.parent` as a compound.
621
+ parentId: effParent ?? null,
593
622
  share: shareOf(n.id),
594
623
  leaves: leavesOf(n.id),
595
624
  detail: n.data ?? null,
@@ -681,8 +710,8 @@ function planeView(v, layers) {
681
710
  if (layers.length < 2) return v.planes || v.layers ? { ...v, planes: false, layers: null } : v;
682
711
  const stacked = v.planes !== false;
683
712
  const picked = layers.map((l) => l.id).filter((id) => v.layers?.includes(id));
684
- const want = stacked ? picked.length && picked.length < layers.length ? picked : null : [picked[0] ?? (layers.find((l) => l.structural !== false) ?? layers[0]).id];
685
- 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]));
713
+ const want = stacked ? picked.length && picked.length < layers.length ? picked : null : v.layers && picked.length === 1 ? picked : null;
714
+ 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]));
686
715
  return same ? v : { ...v, planes: stacked, layers: want };
687
716
  }
688
717
  function isFiltering(v) {
@@ -1243,7 +1272,10 @@ var GK_CSS = `
1243
1272
  min-width:0; max-width:100%; flex:0 1 auto; }
1244
1273
  .gb-layer-track { position:relative; display:flex; flex:1 1 auto; min-width:0; height:28px;
1245
1274
  overflow-x:auto; overflow-y:hidden; overscroll-behavior-x:contain; touch-action:pan-x;
1246
- scrollbar-width:none; }
1275
+ scrollbar-width:none; max-width:1000px; transition:max-width .26s ease, opacity .18s ease; }
1276
+ /* Floors off: the list folds away; the switch beside it does not move (same element, same place, as the legend). */
1277
+ .gb-layers.is-collapsed .gb-layer-track { max-width:0; opacity:0; pointer-events:none; }
1278
+ .gb-layers.is-collapsed .gb-sep { opacity:0; margin:0; }
1247
1279
  .gb-layer-track::-webkit-scrollbar { display:none; }
1248
1280
  /* Only the edge that actually has more layers is faded; a row that fits stays crisp. */
1249
1281
  .gb-layer-track.can-right { -webkit-mask-image:linear-gradient(to right, #000 calc(100% - 18px), transparent); mask-image:linear-gradient(to right, #000 calc(100% - 18px), transparent); }
@@ -1260,7 +1292,7 @@ var GK_CSS = `
1260
1292
  box-shadow: 0 1px 2px rgba(15,23,42,.08); transition: transform .3s cubic-bezier(.3,.7,.2,1), width .3s cubic-bezier(.3,.7,.2,1); }
1261
1293
  /* Stacked layers on: layer names are multi-select, each lit on its own; no slider. */
1262
1294
  .gb-layers.is-multi .gb-layer-track > button[data-v].on { background: var(--gb-active,var(--gb-default-active,#e0f2fe)); box-shadow: 0 1px 2px rgba(15,23,42,.08); }
1263
- .gb-sep { align-self:center; width:1px; height:18px; margin:0 3px; background: var(--gb-border,var(--gb-default-border,#e2e8f0)); flex:none; }
1295
+ .gb-sep { align-self:center; width:1px; height:18px; margin:0 3px; background: var(--gb-border,var(--gb-default-border,#e2e8f0)); flex:none; transition:opacity .18s ease, margin .26s ease; }
1264
1296
  .gb-layer-track > button:focus-visible { outline-offset:-2px; }
1265
1297
  .gb-segm > button.gb-stack { display:grid; place-items:center; width:30px; padding:0; color: var(--gb-muted2,var(--gb-default-muted2,#94a3b8)); }
1266
1298
  .gb-segm > button.gb-stack:hover:not(:disabled) { color: var(--gb-text,var(--gb-default-text,#1f2937)); background: var(--gb-tint,var(--gb-default-tint,#f1f5f9)); }
@@ -1618,6 +1650,14 @@ function checkEdit(model, op) {
1618
1650
  }
1619
1651
  return null;
1620
1652
  };
1653
+ if (op.op === "addNode" || op.op === "updateNode") {
1654
+ const order = op.set.order;
1655
+ if (order != null && (!Number.isFinite(order) || order <= 0)) return "order-invalid";
1656
+ const current = model.index.get(op.id);
1657
+ const parent = op.set.parent !== void 0 ? op.set.parent : current?.parent;
1658
+ const value = order === null ? void 0 : order ?? current?.order;
1659
+ if (parent && value !== void 0 && model.nodes.some((n) => n.id !== op.id && n.parent === parent && n.order === value)) return "order-invalid";
1660
+ }
1621
1661
  switch (op.op) {
1622
1662
  case "addNode":
1623
1663
  if (!op.id.trim()) return "id-empty";
@@ -1648,7 +1688,7 @@ function applyEdit(model, op, profile) {
1648
1688
  function apply(model, op, profile) {
1649
1689
  const layerOf = (n) => profile ? profile.layerOf(n) : n.layer;
1650
1690
  const fields = (n, set) => {
1651
- const { parent, ...rest } = set;
1691
+ const { parent, order, ...rest } = set;
1652
1692
  const next = { ...n, ...rest };
1653
1693
  if (parent !== void 0) {
1654
1694
  if (parent) {
@@ -1656,6 +1696,8 @@ function apply(model, op, profile) {
1656
1696
  next.layer = model.index.get(parent)?.layer ?? next.layer;
1657
1697
  } else delete next.parent;
1658
1698
  }
1699
+ if (order === null || parent === null) delete next.order;
1700
+ else if (order !== void 0) next.order = order;
1659
1701
  next.layer = layerOf(next);
1660
1702
  return next;
1661
1703
  };
@@ -1710,6 +1752,25 @@ function applyEdits(model, ops, profile) {
1710
1752
  }
1711
1753
  return { model: m, skipped };
1712
1754
  }
1755
+ function checkNativeEdit(model, op) {
1756
+ if (op.op === "addNode" && !/^[^\s@]{1,80}$/.test(op.id)) return "id-format";
1757
+ const basic = checkEdit(model, op);
1758
+ if (basic) return basic;
1759
+ const next = apply(model, op);
1760
+ const oldRoots = model.nodes.filter((n) => !n.parent).length;
1761
+ if (next.nodes.filter((n) => !n.parent).length > Math.max(1, oldRoots)) return "multiple-roots";
1762
+ const siblings = /* @__PURE__ */ new Map();
1763
+ for (const n of next.nodes) if (n.parent) {
1764
+ const xs = siblings.get(n.parent) ?? [];
1765
+ xs.push(n);
1766
+ siblings.set(n.parent, xs);
1767
+ }
1768
+ for (const xs of siblings.values()) {
1769
+ const count = xs.filter((n) => n.order !== void 0).length;
1770
+ if (count && count !== xs.length) return "order-mixed";
1771
+ }
1772
+ return null;
1773
+ }
1713
1774
 
1714
1775
  // src/animate.ts
1715
1776
  var easeOutCubic = (t) => 1 - Math.pow(1 - t, 3);
@@ -2270,6 +2331,7 @@ export {
2270
2331
  originsOfNode,
2271
2332
  indexModel,
2272
2333
  originsOf,
2334
+ encodeParent,
2273
2335
  parsePhysical,
2274
2336
  toPhysical,
2275
2337
  validateStructure,
@@ -2331,6 +2393,7 @@ export {
2331
2393
  edgesOf,
2332
2394
  applyEdit,
2333
2395
  applyEdits,
2396
+ checkNativeEdit,
2334
2397
  easeOutCubic,
2335
2398
  easeInOutCubic,
2336
2399
  easeOutBack,