glassbrain 0.2.0-alpha.5 → 0.2.0-alpha.6

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.
@@ -53,17 +53,17 @@ import {
53
53
  unionSides,
54
54
  untheme,
55
55
  validateModel
56
- } from "./chunk-F4HDKER4.js";
56
+ } from "./chunk-3SAL4526.js";
57
57
  import {
58
58
  DetailPanel,
59
59
  I,
60
60
  Icon,
61
61
  StringsContext,
62
62
  useStrings
63
- } from "./chunk-3JGZ27DC.js";
63
+ } from "./chunk-3P34XHQ2.js";
64
64
  import {
65
65
  EN
66
- } from "./chunk-ISWKAU5G.js";
66
+ } from "./chunk-5DEN6HXJ.js";
67
67
  import {
68
68
  LAYOUTS,
69
69
  inlinePool
@@ -956,6 +956,11 @@ function readOrder(elements) {
956
956
  orderedKids.set(parent, sorted);
957
957
  any = true;
958
958
  }
959
+ const roots = [...nodeIds].filter((id) => !rawParent.has(id));
960
+ if (roots.length > 1 && roots.every((id) => rawOrder.get(id) !== null) && new Set(roots.map((id) => rawOrder.get(id))).size === roots.length) {
961
+ orderedKids.set(VIRTUAL_PARENT, roots.sort((a, b) => rawOrder.get(a) - rawOrder.get(b)));
962
+ any = true;
963
+ }
959
964
  return nodeIds.size ? { parentOf, orderedKids, nodeIds, any } : null;
960
965
  }
961
966
  function shuffled(ids, random) {
@@ -995,7 +1000,7 @@ function orderSeed(elements, info, dims = {}, random = Math.random) {
995
1000
  };
996
1001
  const out = {};
997
1002
  const placeChildren = (parent, ids, parentRadius, sector) => {
998
- const list = (parent === null ? void 0 : info.orderedKids.get(parent)) ?? shuffled(ids, random);
1003
+ const list = info.orderedKids.get(parent ?? VIRTUAL_PARENT) ?? shuffled(ids, random);
999
1004
  if (!list.length) return;
1000
1005
  const span = sector ? (sector.to - sector.from) / list.length : TAU / (list.length + 1);
1001
1006
  list.forEach((id, i) => {
@@ -1022,7 +1027,7 @@ function orderSeed(elements, info, dims = {}, random = Math.random) {
1022
1027
  }
1023
1028
 
1024
1029
  // src/layout/engine.ts
1025
- var CACHE_PREFIX = "gb:pos:v6:";
1030
+ var CACHE_PREFIX = "gb:pos:v7:";
1026
1031
  var CACHE_KEEP = 16;
1027
1032
  function shapeHash(elements, salt) {
1028
1033
  const parts = [];
@@ -2610,7 +2615,11 @@ function NodeForm({ target, model, onSubmit, onCancel, onRetarget, renderEditor,
2610
2615
  [node]
2611
2616
  );
2612
2617
  const [text, setText] = useState3(initial);
2613
- const [orderText, setOrderText] = useState3(node?.order?.toString() ?? "");
2618
+ const initialBefore = useMemo2(() => {
2619
+ const siblings = model.nodes.filter((n) => (n.parent ?? null) === (node?.parent ?? target.parent ?? null)).sort((a, b) => (a.order ?? Infinity) - (b.order ?? Infinity));
2620
+ return node ? siblings[siblings.findIndex((n) => n.id === node.id) + 1]?.id ?? null : null;
2621
+ }, [model, node, target.parent]);
2622
+ const [before, setBefore] = useState3(initialBefore);
2614
2623
  const [confirming, setConfirming] = useState3(false);
2615
2624
  const idErr = creating && id ? checkEdit(model, { op: "addNode", id, set: {} }) : null;
2616
2625
  const below = useMemo2(() => {
@@ -2634,9 +2643,8 @@ function NodeForm({ target, model, onSubmit, onCancel, onRetarget, renderEditor,
2634
2643
  setError(t.badJson);
2635
2644
  return;
2636
2645
  }
2637
- const order = parent && orderText.trim() ? Number(orderText) : null;
2638
2646
  if (creating) {
2639
- if (submit({ op: "addNode", id: id.trim(), set: { ...content, parent, order } })) onCancel();
2647
+ if (submit({ op: "addNode", id: id.trim(), set: { ...content, parent, before } })) onCancel();
2640
2648
  return;
2641
2649
  }
2642
2650
  const set = {};
@@ -2644,7 +2652,7 @@ function NodeForm({ target, model, onSubmit, onCancel, onRetarget, renderEditor,
2644
2652
  if ((content.kind ?? void 0) !== node.kind) set.kind = content.kind;
2645
2653
  if (pretty(content.data ?? {}) !== pretty(node.data ?? {})) set.data = content.data ?? {};
2646
2654
  if (parent !== (node.parent ?? null)) set.parent = parent;
2647
- if (order !== (node.order ?? null)) set.order = order;
2655
+ if (before !== initialBefore || parent !== (node.parent ?? null)) set.before = before;
2648
2656
  if (!Object.keys(set).length) {
2649
2657
  onCancel();
2650
2658
  return;
@@ -2702,7 +2710,10 @@ function NodeForm({ target, model, onSubmit, onCancel, onRetarget, renderEditor,
2702
2710
  id: `${uid}-parent`,
2703
2711
  model,
2704
2712
  value: parent,
2705
- onChange: setParent,
2713
+ onChange: (id2) => {
2714
+ setParent(id2);
2715
+ setBefore(null);
2716
+ },
2706
2717
  empty: t.noParent,
2707
2718
  label: t.parent,
2708
2719
  exclude: below ? (x) => below.has(x) : void 0,
@@ -2711,21 +2722,11 @@ function NodeForm({ target, model, onSubmit, onCancel, onRetarget, renderEditor,
2711
2722
  )
2712
2723
  ] }),
2713
2724
  /* @__PURE__ */ jsxs2("div", { className: "gb-fld", children: [
2714
- /* @__PURE__ */ jsx2("label", { className: "gb-k", htmlFor: `${uid}-order`, children: "Order" }),
2715
- /* @__PURE__ */ jsx2(
2716
- "input",
2717
- {
2718
- id: `${uid}-order`,
2719
- className: "gb-input",
2720
- type: "number",
2721
- min: "0",
2722
- step: "any",
2723
- disabled: !parent,
2724
- placeholder: "Unordered",
2725
- value: orderText,
2726
- onChange: (e) => setOrderText(e.target.value)
2727
- }
2728
- )
2725
+ /* @__PURE__ */ jsx2("label", { className: "gb-k", htmlFor: `${uid}-before`, children: "Before" }),
2726
+ /* @__PURE__ */ jsxs2("select", { id: `${uid}-before`, className: "gb-input", value: before ?? "", onChange: (e) => setBefore(e.target.value || null), children: [
2727
+ /* @__PURE__ */ jsx2("option", { value: "", children: "Last" }),
2728
+ model.nodes.filter((n) => n.id !== node?.id && (n.parent ?? null) === parent).sort((a, b) => (a.order ?? Infinity) - (b.order ?? Infinity)).map((n) => /* @__PURE__ */ jsx2("option", { value: n.id, children: n.label }, n.id))
2729
+ ] })
2729
2730
  ] }),
2730
2731
  /* @__PURE__ */ jsxs2("div", { className: "gb-fld gb-grow", children: [
2731
2732
  /* @__PURE__ */ jsxs2("label", { className: "gb-k", htmlFor: `${uid}-json`, children: [
@@ -1,6 +1,7 @@
1
1
  // src/layers.ts
2
2
  var EMPTY = { layers: [], layerOf: /* @__PURE__ */ new Map() };
3
- function orderedChildren(children, id) {
3
+ function orderedChildren(children, id, unordered = false) {
4
+ if (unordered) return null;
4
5
  const kids = children.get(id) ?? [];
5
6
  if (kids.length < 2) return null;
6
7
  const withOrder = kids.filter((n) => typeof n.order === "number" && Number.isFinite(n.order));
@@ -29,10 +30,10 @@ function deriveLayers(model, colors = [], force = false) {
29
30
  let anchor;
30
31
  if (roots.length === 1) {
31
32
  anchor = roots[0];
32
- ordered = orderedChildren(children, anchor.id);
33
+ ordered = orderedChildren(children, anchor.id, anchor.data?.unordered === true);
33
34
  if (!ordered && force) ordered = children.get(anchor.id) ?? null;
34
- } else if (force && roots.length > 1) {
35
- ordered = roots;
35
+ } else if (roots.length > 1 && (force || roots.every((n) => n.order !== void 0))) {
36
+ ordered = [...roots].sort((a, b) => (a.order ?? Infinity) - (b.order ?? Infinity));
36
37
  }
37
38
  if (!ordered || ordered.length < 2) return EMPTY;
38
39
  const layers = ordered.map((n, i) => ({