@johnoscott/isometric-engine 0.0.0-stage → 0.10.1

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.
Files changed (310) hide show
  1. package/EDITOR-GUIDE.md +355 -0
  2. package/LICENSE +21 -0
  3. package/LICENSES/ajv.txt +22 -0
  4. package/LICENSES/lucide-static.txt +43 -0
  5. package/LICENSES/yaml.txt +13 -0
  6. package/README.md +62 -2
  7. package/assets/app.css +269 -0
  8. package/assets/default-theme.yaml +99 -0
  9. package/assets/icons.svg +1 -0
  10. package/assets/marks.css +39 -0
  11. package/assets/skin.css +181 -0
  12. package/iso-editor.d.ts +4 -0
  13. package/iso-editor.js +550 -0
  14. package/iso-render.d.ts +3 -0
  15. package/iso-render.js +388 -0
  16. package/iso-systems.d.ts +4 -0
  17. package/iso-systems.js +389 -0
  18. package/js/iso-core/compile-schema.js +22 -0
  19. package/js/iso-core/depth.js +55 -0
  20. package/js/iso-core/errors.js +65 -0
  21. package/js/iso-core/glyphs.js +100 -0
  22. package/js/iso-core/index.js +55 -0
  23. package/js/iso-core/kernel.js +158 -0
  24. package/js/iso-core/metrics.json +1 -0
  25. package/js/iso-core/package.json +6 -0
  26. package/js/iso-core/parse.js +79 -0
  27. package/js/iso-core/schema-errors.js +170 -0
  28. package/js/iso-core/shims/crypto.js +125 -0
  29. package/js/iso-core/shims/path.js +49 -0
  30. package/js/iso-core/svg-dom.js +144 -0
  31. package/js/iso-core/theme.js +48 -0
  32. package/js/iso-core/vdom.js +48 -0
  33. package/js/iso-editor/adapter.js +0 -0
  34. package/js/iso-editor/clipboard.js +152 -0
  35. package/js/iso-editor/commands.js +173 -0
  36. package/js/iso-editor/complete.js +55 -0
  37. package/js/iso-editor/doc.js +182 -0
  38. package/js/iso-editor/index.js +105 -0
  39. package/js/iso-editor/model.js +210 -0
  40. package/js/iso-editor/outline.js +111 -0
  41. package/js/iso-editor/package.json +6 -0
  42. package/js/iso-editor/path.js +19 -0
  43. package/js/iso-editor/providers.js +96 -0
  44. package/js/iso-editor/schema-nav.js +117 -0
  45. package/js/iso-editor/splice.js +443 -0
  46. package/js/iso-editor/storage-keys.js +6 -0
  47. package/js/iso-editor/ui/actions.js +116 -0
  48. package/js/iso-editor/ui/canvas-layer.js +0 -0
  49. package/js/iso-editor/ui/canvas.js +1403 -0
  50. package/js/iso-editor/ui/clip.js +133 -0
  51. package/js/iso-editor/ui/dialog.js +26 -0
  52. package/js/iso-editor/ui/flat-canvas.js +54 -0
  53. package/js/iso-editor/ui/icons.js +15 -0
  54. package/js/iso-editor/ui/keys.js +26 -0
  55. package/js/iso-editor/ui/later.js +28 -0
  56. package/js/iso-editor/ui/layer.js +11 -0
  57. package/js/iso-editor/ui/menu.js +105 -0
  58. package/js/iso-editor/ui/monaco-entry.js +2 -0
  59. package/js/iso-editor/ui/mount-kit.js +0 -0
  60. package/js/iso-editor/ui/mount.js +767 -0
  61. package/js/iso-editor/ui/pane.js +0 -0
  62. package/js/iso-editor/ui/part.js +0 -0
  63. package/js/iso-editor/ui/plain-icons.js +34 -0
  64. package/js/iso-editor/ui/props.js +314 -0
  65. package/js/iso-editor/ui/scope-css.js +81 -0
  66. package/js/iso-editor/ui/setup.js +149 -0
  67. package/js/iso-editor/ui/text.js +323 -0
  68. package/js/iso-editor/ui/tree.js +728 -0
  69. package/js/iso-editor/validate.js +33 -0
  70. package/js/iso-editor/window/protocol.js +8 -0
  71. package/js/iso-objects/assemble.js +43 -0
  72. package/js/iso-objects/assets.js +88 -0
  73. package/js/iso-objects/board.js +162 -0
  74. package/js/iso-objects/compile.js +60 -0
  75. package/js/iso-objects/css.js +154 -0
  76. package/js/iso-objects/flat.js +696 -0
  77. package/js/iso-objects/focus.js +16 -0
  78. package/js/iso-objects/geometry.js +76 -0
  79. package/js/iso-objects/index.js +93 -0
  80. package/js/iso-objects/kinds.js +12 -0
  81. package/js/iso-objects/labels.js +325 -0
  82. package/js/iso-objects/package.json +6 -0
  83. package/js/iso-objects/place.js +347 -0
  84. package/js/iso-objects/route.js +461 -0
  85. package/js/iso-objects/scene.js +350 -0
  86. package/js/iso-objects/steps.js +246 -0
  87. package/js/iso-objects/types.js +0 -0
  88. package/js/iso-systems/catalog.js +42 -0
  89. package/js/iso-systems/compile.js +34 -0
  90. package/js/iso-systems/css.js +8 -0
  91. package/js/iso-systems/index.js +16 -0
  92. package/js/iso-systems/nodes.js +124 -0
  93. package/js/iso-systems/package.json +6 -0
  94. package/js/iso-systems/schema.json +311 -0
  95. package/js/iso-systems/validate.js +35 -0
  96. package/js/iso-viewer/bundle.js +20 -0
  97. package/js/iso-viewer/card-motion.js +228 -0
  98. package/js/iso-viewer/dom.js +14 -0
  99. package/js/iso-viewer/dual-view.js +212 -0
  100. package/js/iso-viewer/flat-view.js +124 -0
  101. package/js/iso-viewer/history.js +990 -0
  102. package/js/iso-viewer/index.js +98 -0
  103. package/js/iso-viewer/morph.js +192 -0
  104. package/js/iso-viewer/overlay.js +199 -0
  105. package/js/iso-viewer/package.json +6 -0
  106. package/js/iso-viewer/page.js +138 -0
  107. package/js/iso-viewer/player.js +409 -0
  108. package/js/iso-viewer/still.js +81 -0
  109. package/js/iso-viewer/timeline-contract.js +14 -0
  110. package/js/iso-viewer/timeline-engine.js +475 -0
  111. package/js/iso-viewer/timeline-export.js +36 -0
  112. package/js/iso-viewer/timeline-scale.js +116 -0
  113. package/js/iso-viewer/timeline-scope.js +58 -0
  114. package/js/iso-viewer/timeline-search.js +216 -0
  115. package/js/iso-viewer/timeline-time.js +55 -0
  116. package/js/iso-viewer/view-css.js +68 -0
  117. package/js/iso-viewer/viewport.js +177 -0
  118. package/package.json +511 -4
  119. package/src/iso-core/compile-schema.ts +23 -0
  120. package/src/iso-core/depth.ts +56 -0
  121. package/src/iso-core/errors.ts +73 -0
  122. package/src/iso-core/glyphs.ts +108 -0
  123. package/src/iso-core/index.ts +27 -0
  124. package/src/iso-core/kernel.ts +163 -0
  125. package/src/iso-core/metrics.json +1 -0
  126. package/src/iso-core/package.json +6 -0
  127. package/src/iso-core/parse.ts +91 -0
  128. package/src/iso-core/schema-errors.ts +163 -0
  129. package/src/iso-core/shims/crypto.ts +37 -0
  130. package/src/iso-core/shims/path.ts +24 -0
  131. package/src/iso-core/svg-dom.ts +103 -0
  132. package/src/iso-core/theme.ts +64 -0
  133. package/src/iso-core/vdom.ts +41 -0
  134. package/src/iso-editor/adapter.ts +147 -0
  135. package/src/iso-editor/clipboard.ts +163 -0
  136. package/src/iso-editor/commands.ts +191 -0
  137. package/src/iso-editor/complete.ts +73 -0
  138. package/src/iso-editor/doc.ts +207 -0
  139. package/src/iso-editor/index.ts +67 -0
  140. package/src/iso-editor/model.ts +234 -0
  141. package/src/iso-editor/outline.ts +126 -0
  142. package/src/iso-editor/package.json +6 -0
  143. package/src/iso-editor/path.ts +18 -0
  144. package/src/iso-editor/providers.ts +113 -0
  145. package/src/iso-editor/schema-nav.ts +129 -0
  146. package/src/iso-editor/splice.ts +477 -0
  147. package/src/iso-editor/storage-keys.ts +6 -0
  148. package/src/iso-editor/ui/actions.ts +119 -0
  149. package/src/iso-editor/ui/app.css +230 -0
  150. package/src/iso-editor/ui/canvas-layer.ts +56 -0
  151. package/src/iso-editor/ui/canvas.ts +1227 -0
  152. package/src/iso-editor/ui/clip.ts +97 -0
  153. package/src/iso-editor/ui/dialog.ts +25 -0
  154. package/src/iso-editor/ui/flat-canvas.ts +51 -0
  155. package/src/iso-editor/ui/icons.ts +21 -0
  156. package/src/iso-editor/ui/keys.ts +30 -0
  157. package/src/iso-editor/ui/later.ts +22 -0
  158. package/src/iso-editor/ui/layer.ts +7 -0
  159. package/src/iso-editor/ui/marks.css +39 -0
  160. package/src/iso-editor/ui/menu.ts +67 -0
  161. package/src/iso-editor/ui/monaco-entry.ts +6 -0
  162. package/src/iso-editor/ui/mount-kit.ts +35 -0
  163. package/src/iso-editor/ui/mount.ts +684 -0
  164. package/src/iso-editor/ui/pane.ts +45 -0
  165. package/src/iso-editor/ui/part.ts +45 -0
  166. package/src/iso-editor/ui/plain-icons.ts +83 -0
  167. package/src/iso-editor/ui/props.ts +347 -0
  168. package/src/iso-editor/ui/scope-css.ts +65 -0
  169. package/src/iso-editor/ui/setup.ts +153 -0
  170. package/src/iso-editor/ui/skin.css +181 -0
  171. package/src/iso-editor/ui/text.ts +262 -0
  172. package/src/iso-editor/ui/tree.ts +657 -0
  173. package/src/iso-editor/validate.ts +62 -0
  174. package/src/iso-editor/window/protocol.ts +33 -0
  175. package/src/iso-objects/assemble.ts +57 -0
  176. package/src/iso-objects/assets.ts +122 -0
  177. package/src/iso-objects/board.ts +169 -0
  178. package/src/iso-objects/compile.ts +76 -0
  179. package/src/iso-objects/css.ts +163 -0
  180. package/src/iso-objects/flat.ts +653 -0
  181. package/src/iso-objects/focus.ts +14 -0
  182. package/src/iso-objects/geometry.ts +86 -0
  183. package/src/iso-objects/index.ts +33 -0
  184. package/src/iso-objects/kinds.ts +56 -0
  185. package/src/iso-objects/labels.ts +310 -0
  186. package/src/iso-objects/package.json +6 -0
  187. package/src/iso-objects/place.ts +318 -0
  188. package/src/iso-objects/route.ts +444 -0
  189. package/src/iso-objects/scene.ts +376 -0
  190. package/src/iso-objects/steps.ts +234 -0
  191. package/src/iso-objects/types.ts +68 -0
  192. package/src/iso-systems/catalog.ts +31 -0
  193. package/src/iso-systems/compile.ts +39 -0
  194. package/src/iso-systems/css.ts +9 -0
  195. package/src/iso-systems/index.ts +12 -0
  196. package/src/iso-systems/nodes.ts +126 -0
  197. package/src/iso-systems/package.json +6 -0
  198. package/src/iso-systems/schema.json +311 -0
  199. package/src/iso-systems/validate.ts +34 -0
  200. package/src/iso-viewer/bundle.ts +15 -0
  201. package/src/iso-viewer/card-motion.ts +237 -0
  202. package/src/iso-viewer/dom.ts +13 -0
  203. package/src/iso-viewer/dual-view.ts +212 -0
  204. package/src/iso-viewer/flat-view.ts +127 -0
  205. package/src/iso-viewer/history.ts +781 -0
  206. package/src/iso-viewer/index.ts +40 -0
  207. package/src/iso-viewer/morph.ts +191 -0
  208. package/src/iso-viewer/overlay.ts +212 -0
  209. package/src/iso-viewer/package.json +6 -0
  210. package/src/iso-viewer/page.ts +150 -0
  211. package/src/iso-viewer/player.ts +321 -0
  212. package/src/iso-viewer/still.ts +74 -0
  213. package/src/iso-viewer/timeline-contract.ts +342 -0
  214. package/src/iso-viewer/timeline-engine.ts +420 -0
  215. package/src/iso-viewer/timeline-export.ts +31 -0
  216. package/src/iso-viewer/timeline-scale.ts +129 -0
  217. package/src/iso-viewer/timeline-scope.ts +56 -0
  218. package/src/iso-viewer/timeline-search.ts +198 -0
  219. package/src/iso-viewer/timeline-time.ts +54 -0
  220. package/src/iso-viewer/view-css.ts +59 -0
  221. package/src/iso-viewer/viewport.ts +142 -0
  222. package/types/iso-core/compile-schema.d.ts +11 -0
  223. package/types/iso-core/depth.d.ts +6 -0
  224. package/types/iso-core/errors.d.ts +24 -0
  225. package/types/iso-core/glyphs.d.ts +19 -0
  226. package/types/iso-core/index.d.ts +17 -0
  227. package/types/iso-core/kernel.d.ts +130 -0
  228. package/types/iso-core/metrics.json +1 -0
  229. package/types/iso-core/parse.d.ts +18 -0
  230. package/types/iso-core/schema-errors.d.ts +14 -0
  231. package/types/iso-core/shims/crypto.d.ts +9 -0
  232. package/types/iso-core/svg-dom.d.ts +15 -0
  233. package/types/iso-core/theme.d.ts +38 -0
  234. package/types/iso-core/vdom.d.ts +26 -0
  235. package/types/iso-editor/adapter.d.ts +178 -0
  236. package/types/iso-editor/clipboard.d.ts +23 -0
  237. package/types/iso-editor/commands.d.ts +32 -0
  238. package/types/iso-editor/complete.d.ts +25 -0
  239. package/types/iso-editor/doc.d.ts +50 -0
  240. package/types/iso-editor/index.d.ts +57 -0
  241. package/types/iso-editor/model.d.ts +96 -0
  242. package/types/iso-editor/outline.d.ts +28 -0
  243. package/types/iso-editor/path.d.ts +7 -0
  244. package/types/iso-editor/providers.d.ts +43 -0
  245. package/types/iso-editor/schema-nav.d.ts +34 -0
  246. package/types/iso-editor/splice.d.ts +53 -0
  247. package/types/iso-editor/storage-keys.d.ts +5 -0
  248. package/types/iso-editor/ui/actions.d.ts +23 -0
  249. package/types/iso-editor/ui/canvas-layer.d.ts +65 -0
  250. package/types/iso-editor/ui/canvas.d.ts +307 -0
  251. package/types/iso-editor/ui/clip.d.ts +14 -0
  252. package/types/iso-editor/ui/dialog.d.ts +7 -0
  253. package/types/iso-editor/ui/flat-canvas.d.ts +2 -0
  254. package/types/iso-editor/ui/icons.d.ts +5 -0
  255. package/types/iso-editor/ui/keys.d.ts +7 -0
  256. package/types/iso-editor/ui/later.d.ts +12 -0
  257. package/types/iso-editor/ui/layer.d.ts +2 -0
  258. package/types/iso-editor/ui/menu.d.ts +21 -0
  259. package/types/iso-editor/ui/mount-kit.d.ts +31 -0
  260. package/types/iso-editor/ui/mount.d.ts +146 -0
  261. package/types/iso-editor/ui/pane.d.ts +60 -0
  262. package/types/iso-editor/ui/part.d.ts +40 -0
  263. package/types/iso-editor/ui/plain-icons.d.ts +10 -0
  264. package/types/iso-editor/ui/props.d.ts +57 -0
  265. package/types/iso-editor/ui/scope-css.d.ts +5 -0
  266. package/types/iso-editor/ui/setup.d.ts +49 -0
  267. package/types/iso-editor/ui/text.d.ts +19 -0
  268. package/types/iso-editor/ui/tree.d.ts +100 -0
  269. package/types/iso-editor/validate.d.ts +36 -0
  270. package/types/iso-editor/window/protocol.d.ts +60 -0
  271. package/types/iso-objects/assemble.d.ts +35 -0
  272. package/types/iso-objects/assets.d.ts +49 -0
  273. package/types/iso-objects/board.d.ts +36 -0
  274. package/types/iso-objects/compile.d.ts +35 -0
  275. package/types/iso-objects/css.d.ts +16 -0
  276. package/types/iso-objects/flat.d.ts +130 -0
  277. package/types/iso-objects/focus.d.ts +1 -0
  278. package/types/iso-objects/geometry.d.ts +59 -0
  279. package/types/iso-objects/index.d.ts +26 -0
  280. package/types/iso-objects/kinds.d.ts +53 -0
  281. package/types/iso-objects/labels.d.ts +92 -0
  282. package/types/iso-objects/place.d.ts +25 -0
  283. package/types/iso-objects/route.d.ts +67 -0
  284. package/types/iso-objects/scene.d.ts +63 -0
  285. package/types/iso-objects/steps.d.ts +90 -0
  286. package/types/iso-objects/types.d.ts +140 -0
  287. package/types/iso-systems/catalog.d.ts +4 -0
  288. package/types/iso-systems/compile.d.ts +6 -0
  289. package/types/iso-systems/css.d.ts +3 -0
  290. package/types/iso-systems/index.d.ts +5 -0
  291. package/types/iso-systems/nodes.d.ts +3 -0
  292. package/types/iso-viewer/card-motion.d.ts +56 -0
  293. package/types/iso-viewer/dom.d.ts +1 -0
  294. package/types/iso-viewer/dual-view.d.ts +44 -0
  295. package/types/iso-viewer/flat-view.d.ts +25 -0
  296. package/types/iso-viewer/history.d.ts +91 -0
  297. package/types/iso-viewer/index.d.ts +31 -0
  298. package/types/iso-viewer/morph.d.ts +28 -0
  299. package/types/iso-viewer/overlay.d.ts +135 -0
  300. package/types/iso-viewer/page.d.ts +13 -0
  301. package/types/iso-viewer/still.d.ts +26 -0
  302. package/types/iso-viewer/timeline-contract.d.ts +345 -0
  303. package/types/iso-viewer/timeline-engine.d.ts +20 -0
  304. package/types/iso-viewer/timeline-export.d.ts +5 -0
  305. package/types/iso-viewer/timeline-scale.d.ts +44 -0
  306. package/types/iso-viewer/timeline-scope.d.ts +33 -0
  307. package/types/iso-viewer/timeline-search.d.ts +52 -0
  308. package/types/iso-viewer/timeline-time.d.ts +8 -0
  309. package/types/iso-viewer/view-css.d.ts +7 -0
  310. package/types/iso-viewer/viewport.d.ts +50 -0
@@ -0,0 +1,728 @@
1
+ import { later, isTyping } from "./later.js";
2
+ import { h, icon } from "./icons.js";
3
+ import { openMenu } from "./menu.js";
4
+ import { ask } from "./dialog.js";
5
+ import { applyStep } from "../splice.js";
6
+ import { toPointer, fromPointer, isPrefix } from "../path.js";
7
+ function treeView(el, doc, opts) {
8
+ const K = opts.kit, ui = K.ui;
9
+ const { setCmd, insertCmd, insertItemCmd, deleteCmd, renameCmd, renameKeyCmd, duplicateCmd, moveCmd, moveItemCmd, referencesTo, describe, freshName } = K.commands;
10
+ const kindOf = (k) => K.kinds.find((x) => x.kind === k);
11
+ const isRoot = (n) => n.id === "";
12
+ const inGroup = (p) => K.entryGroups.some((g) => p.length === g.length + 1 && isPrefix(g, p));
13
+ const isGroupPath = (p) => K.entryGroups.some((g) => g.length === p.length && isPrefix(g, p));
14
+ const expanded = /* @__PURE__ */ new Set(["", ...K.kinds.map((k) => toPointer(k.section)), ...K.entryGroups.map((g) => toPointer(g))]);
15
+ let focused = "";
16
+ let anchor = "";
17
+ let editing;
18
+ let rows = [];
19
+ let byId = /* @__PURE__ */ new Map();
20
+ let typeahead = "", typeaheadAt = 0;
21
+ const selected = () => new Set(doc.selection.map(toPointer));
22
+ const readOnly = () => !doc.doc.ok;
23
+ el.setAttribute("role", "tree");
24
+ el.setAttribute("aria-label", ui.label);
25
+ el.setAttribute("aria-multiselectable", "true");
26
+ el.classList.add("tree");
27
+ function paint() {
28
+ const root = K.outline(doc.goodDoc, opts.role(), opts.lifetimes());
29
+ rows = [];
30
+ const walk = (n, depth, parent) => {
31
+ rows.push({ n, depth, parent });
32
+ if (expanded.has(n.id)) for (const c of n.children) walk(c, depth + 1, n);
33
+ if (editing?.mode === "new" && toPointer(editing.mapPath) === n.id && expanded.has(n.id)) rows.push({ n: { id: editing.id, path: [...editing.mapPath, ""], kind: "resource", label: "", shape: "scalar", children: [] }, depth: depth + 1, parent: n });
34
+ };
35
+ walk(root, 0);
36
+ byId = new Map(rows.map((r) => [r.n.id, r]));
37
+ if (!byId.has(focused)) focused = rows[0]?.n.id ?? "";
38
+ const sel = selected();
39
+ const counts = problemCounts();
40
+ const frag = document.createDocumentFragment();
41
+ if (readOnly()) frag.append(h("div", { class: "callout compact warn tree-note", role: "status" }, h("span", { class: "c-body" }, `Fix the YAML at line ${doc.doc.doc.errors[0] ? doc.doc.pos(doc.doc.doc.errors[0].pos[0]).line : "?"} to edit here; the tree shows the last version that read cleanly.`)));
42
+ for (const { n, depth } of rows) frag.append(row(n, depth, sel.has(n.id), counts.get(n.id)));
43
+ const hadFocus = el.contains(document.activeElement);
44
+ el.replaceChildren(frag);
45
+ const f = el.querySelector(`[data-id="${CSS.escape(focused)}"]`);
46
+ if (f) f.tabIndex = 0;
47
+ const inp = el.querySelector(".tedit");
48
+ if (inp) {
49
+ inp.focus();
50
+ if (inp instanceof HTMLInputElement) inp.select();
51
+ } else if (hadFocus && f && !el.contains(document.activeElement)) f.focus({ preventScroll: true });
52
+ }
53
+ function problemCounts() {
54
+ const m = /* @__PURE__ */ new Map();
55
+ for (const p of opts.problems()) {
56
+ if (p.file) continue;
57
+ const parts = fromPointer(p.path);
58
+ for (let i = 1; i <= parts.length; i++) {
59
+ const id = toPointer(parts.slice(0, i));
60
+ const c = m.get(id) ?? { e: 0, w: 0 };
61
+ if (p.severity === "error") c.e++;
62
+ else c.w++;
63
+ m.set(id, c);
64
+ }
65
+ }
66
+ return m;
67
+ }
68
+ function iconFor(n) {
69
+ if (isRoot(n)) return ui.rootIcon;
70
+ if (n.section) return ui.kindIcon(n.section);
71
+ const k = String(n.path[n.path.length - 1] ?? "");
72
+ if (n.path.length === 1 && ui.keyIcon(k)) return ui.keyIcon(k);
73
+ if (inGroup(n.path)) return ui.entryIcon(String(n.path[0]));
74
+ return n.shape === "list" ? "list" : n.shape === "map" ? "list-tree" : "circle-dot";
75
+ }
76
+ function row(n, depth, isSel, count) {
77
+ const open = expanded.has(n.id);
78
+ const hasKids = n.children.length > 0 || n.shape === "missing" && false;
79
+ const r = h("div", {
80
+ class: `trow k-${n.kind}${isSel ? " selected" : ""}${n.shape === "missing" ? " missing" : ""}${n.ext ? " ext" : ""}`,
81
+ role: "treeitem",
82
+ "aria-level": depth + 1,
83
+ "aria-selected": String(isSel),
84
+ "aria-expanded": n.children.length ? String(open) : void 0,
85
+ tabindex: "-1",
86
+ "data-id": n.id,
87
+ draggable: canDrag(n) ? "true" : void 0,
88
+ "aria-label": ariaLabel(n)
89
+ });
90
+ r.style.setProperty("--depth", String(depth));
91
+ const tw = h("span", { class: "twisty", "aria-hidden": "true" }, hasKids ? icon(open ? "chevron-down" : "chevron-right") : "");
92
+ tw.addEventListener("click", (e) => {
93
+ e.stopPropagation();
94
+ toggle(n.id);
95
+ });
96
+ r.append(tw, icon(iconFor(n), "ticon"));
97
+ const ed = editing && editing.id === n.id ? editing : void 0;
98
+ if (ed && (ed.mode === "rename" || ed.mode === "new")) r.append(nameInput(n, ed));
99
+ else r.append(h("span", { class: "tlabel" }, n.label || (n.kind === "item" ? '""' : "")));
100
+ if (ed?.mode === "value") r.append(valueInput(n));
101
+ else if (n.value !== void 0 && n.kind !== "item") r.append(h("span", { class: "tval" }, n.value));
102
+ else if (n.shape === "missing") r.append(h("span", { class: "tval none" }, "none"));
103
+ else if (n.kind === "group" && n.children.length) r.append(h("span", { class: "tcount" }, String(n.children.length)));
104
+ const pill = n.lifetime ? ui.tagPill(n.lifetime) : void 0;
105
+ if (pill) r.append(h("span", { class: `tpill l-${n.lifetime}`, title: pill.title }, n.lifetime));
106
+ if (count?.e) r.append(h("span", { class: "count bad", title: `${count.e} error${count.e > 1 ? "s" : ""}` }, String(count.e)));
107
+ else if (count?.w) r.append(h("span", { class: "count warn", title: `${count.w} warning${count.w > 1 ? "s" : ""}` }, String(count.w)));
108
+ if (!readOnly() && addItems(n).length) {
109
+ const add = h("button", { type: "button", class: "tadd btn sm quiet icon-only", tabindex: "-1", "aria-label": `Add to ${n.label || ui.text.rootAdd}`, title: "Add\u2026" }, icon("plus"));
110
+ add.addEventListener("click", (e) => {
111
+ e.stopPropagation();
112
+ setFocus(n.id);
113
+ openMenu(addItems(n), add, `Add to ${n.label}`);
114
+ });
115
+ r.append(add);
116
+ }
117
+ return r;
118
+ }
119
+ const ariaLabel = (n) => [n.kind === "group" ? `${n.label}, ${n.shape === "missing" ? "none" : n.children.length}` : n.label, n.value !== void 0 && n.kind !== "item" ? `: ${n.value}` : "", n.lifetime && n.lifetime !== "run" ? `, ${n.lifetime}` : ""].join("");
120
+ function toggle(id, to) {
121
+ const want = to ?? !expanded.has(id);
122
+ if (want) expanded.add(id);
123
+ else expanded.delete(id);
124
+ paint();
125
+ }
126
+ function setFocus(id) {
127
+ focused = id;
128
+ for (const x of el.querySelectorAll(".trow")) x.tabIndex = x.dataset.id === id ? 0 : -1;
129
+ const r = el.querySelector(`[data-id="${CSS.escape(id)}"]`);
130
+ r?.focus({ preventScroll: true });
131
+ r?.scrollIntoView({ block: "nearest" });
132
+ }
133
+ function selectIds(ids, focusId = ids[ids.length - 1]) {
134
+ if (focusId !== void 0) {
135
+ focused = focusId;
136
+ setFocus(focused);
137
+ }
138
+ doc.select(ids.map(fromPointer), "tree");
139
+ paint();
140
+ setFocus(focused);
141
+ }
142
+ function clickSelect(id, e) {
143
+ if (e.shiftKey && anchor) {
144
+ const ids = rows.map((r) => r.n.id);
145
+ const a = ids.indexOf(anchor), b = ids.indexOf(id);
146
+ if (a >= 0 && b >= 0) {
147
+ selectIds(ids.slice(Math.min(a, b), Math.max(a, b) + 1), id);
148
+ return;
149
+ }
150
+ }
151
+ if (e.metaKey || e.ctrlKey) {
152
+ const s = selected();
153
+ if (s.has(id)) s.delete(id);
154
+ else s.add(id);
155
+ anchor = id;
156
+ selectIds([...s], id);
157
+ return;
158
+ }
159
+ anchor = id;
160
+ selectIds([id]);
161
+ }
162
+ function reveal(paths) {
163
+ for (const p of paths) for (let i = 0; i < p.length; i++) expanded.add(toPointer(p.slice(0, i)));
164
+ if (paths[0]) {
165
+ let id = toPointer(paths[0]);
166
+ paint();
167
+ while (!byId.has(id) && id) id = id.replace(/\/[^/]*$/, "");
168
+ focused = id;
169
+ }
170
+ paint();
171
+ el.querySelector(`[data-id="${CSS.escape(focused)}"]`)?.scrollIntoView({ block: "nearest" });
172
+ }
173
+ const canDrag = (n) => !readOnly() && !isRoot(n) && n.kind !== "group" && n.path.length > 1 || n.kind === "item";
174
+ const isResource = (n) => n.kind === "resource" && !!n.section;
175
+ const isNameMap = (n) => n.kind === "group" && (!!n.section || isGroupPath(n.path));
176
+ function addItems(n) {
177
+ if (readOnly()) return [];
178
+ const nav = opts.nav();
179
+ const items = [];
180
+ const data = doc.goodDoc.data;
181
+ if (isRoot(n)) {
182
+ for (const k of K.kinds) {
183
+ if (opts.role() !== "main") break;
184
+ items.push({ label: `New ${k.singular}`, icon: ui.kindIcon(k.kind), onSelect: () => newResource(k.kind) });
185
+ }
186
+ const present = new Set(n.children.filter((c) => c.shape !== "missing").map((c) => String(c.path[0])));
187
+ const sectionTops = new Set(K.kinds.map((k) => String(k.section[0])));
188
+ const keys = nav.keys([], data ?? {}).filter((k) => !present.has(k.name) && !sectionTops.has(k.name) && !(data && k.name in data));
189
+ if (keys.length) items.push({ sep: true }, { heading: "Settings" }, ...keys.map((k) => ({ label: k.name, desc: firstLine(k.doc), onSelect: () => addKey([], k.name) })));
190
+ return items;
191
+ }
192
+ if (n.kind === "group" && n.section) {
193
+ const s = n.section;
194
+ for (const x of ui.newItems(s)) items.push({ label: x.label, icon: x.icon, desc: x.desc, onSelect: () => newResource(s, x.template, x.stem) });
195
+ return items;
196
+ }
197
+ if (isGroupPath(n.path) && ui.entryItems) return entryItems(n.path);
198
+ if (n.shape === "missing") return [{ label: `Add ${n.label}`, icon: "plus", onSelect: () => addKey([], String(n.path[0]), true) }];
199
+ if (n.shape === "list") {
200
+ const slot = K.slotAt(toPointer([...n.path, 0]));
201
+ const have = new Set(n.children.map((c) => c.label));
202
+ if (slot) {
203
+ for (const s of slot.targets) for (const name of namesIn(s)) if (!have.has(slot.write ? slot.write(name) : name)) items.push({ label: slot.write ? slot.write(name) : name, icon: ui.kindIcon(s), desc: `${kindOf(s).singular}`, onSelect: () => run(insertItemCmd(n.path, slot.write ? slot.write(name) : name, void 0, `Add ${name} to ${describe(n.path)}`)) });
204
+ for (const e of slot.extra ?? []) if (!have.has(e)) items.push({ label: e, onSelect: () => run(insertItemCmd(n.path, e, void 0, `Add ${e} to ${describe(n.path)}`)) });
205
+ }
206
+ for (const v of nav.values([...n.path, 0])) if (!have.has(v)) items.push({ label: v, onSelect: () => run(insertItemCmd(n.path, v)) });
207
+ items.push({ label: "New item", icon: "plus", onSelect: () => {
208
+ run(insertItemCmd(n.path, ""));
209
+ const p = [...n.path, n.children.length];
210
+ startEdit(toPointer(p), "value");
211
+ } });
212
+ return items;
213
+ }
214
+ if (n.shape === "map" || n.kind === "resource") {
215
+ const here = getIn(data, n.path);
216
+ const present = here && typeof here === "object" && !Array.isArray(here) ? here : {};
217
+ for (const k of nav.keys(n.path, present)) if (!(k.name in present)) items.push({ label: k.name, desc: firstLine(k.doc), onSelect: () => addKey(n.path, k.name) });
218
+ return items;
219
+ }
220
+ return items;
221
+ }
222
+ function entryItems(group) {
223
+ const data = doc.goodDoc.data ?? {};
224
+ const here = getIn(data, group) ?? {};
225
+ return ui.entryItems(group, data, ownedIn).filter((x) => !x.when || x.when(data)).map((x) => ({ label: x.label, icon: x.icon, onSelect: () => {
226
+ run(insertCmd(group, uniqueKey(Object.keys(here), x.entry), x.value, x.cmdLabel ? { label: x.cmdLabel } : {}));
227
+ } }));
228
+ }
229
+ const firstLine = (s) => s?.split(/(?<=\.)\s/)[0]?.replace(/`/g, "");
230
+ const getIn = (o, p) => p.reduce((x, k) => x && typeof x === "object" ? x[k] : void 0, o);
231
+ const namesIn = (s) => {
232
+ const m = getIn(doc.goodDoc.data, kindOf(s).section);
233
+ return m && typeof m === "object" ? Object.keys(m).filter((k) => !k.startsWith("x-")) : [];
234
+ };
235
+ const ownedIn = (s) => {
236
+ const m = getIn(doc.goodDoc.data, kindOf(s).section);
237
+ return m ? Object.keys(m).filter((k) => !k.startsWith("x-") && !(m[k] && typeof m[k] === "object" && "external" in m[k])) : [];
238
+ };
239
+ const uniqueKey = (have, stem) => {
240
+ if (!have.includes(stem)) return stem;
241
+ let i = 2;
242
+ while (have.includes(`${stem}-${i}`)) i++;
243
+ return `${stem}-${i}`;
244
+ };
245
+ function run(c) {
246
+ try {
247
+ return doc.apply(c, "tree");
248
+ } catch (e) {
249
+ opts.note(String(e.message), "bad");
250
+ return false;
251
+ }
252
+ }
253
+ function addKey(mapPath, key, empty = false) {
254
+ const nav = opts.nav();
255
+ const value = empty ? {} : nav.defaultFor([...mapPath, key]);
256
+ if (run(insertCmd(mapPath, key, value))) {
257
+ expanded.add(toPointer(mapPath));
258
+ const id = toPointer([...mapPath, key]);
259
+ focused = id;
260
+ if (value === "" || typeof value === "number") startEdit(id, "value");
261
+ else {
262
+ paint();
263
+ setFocus(id);
264
+ }
265
+ }
266
+ }
267
+ function newResource(s, template = kindOf(s).template, stem = kindOf(s).singular) {
268
+ const mapPath = kindOf(s).section;
269
+ expanded.add(toPointer(mapPath));
270
+ editing = { id: "#new", mode: "new", section: s, mapPath, template, value: freshName(doc.text, stem) };
271
+ paint();
272
+ }
273
+ function startEdit(id, mode) {
274
+ if (readOnly()) return;
275
+ const r = byId.get(id);
276
+ if (!r) {
277
+ paint();
278
+ return;
279
+ }
280
+ if (mode === "rename" && (r.n.kind === "group" || isRoot(r.n) || r.n.kind === "item")) return;
281
+ if (mode === "value" && r.n.shape !== "scalar") return;
282
+ editing = { id, mode };
283
+ focused = id;
284
+ paint();
285
+ }
286
+ function endEdit(refocus = true) {
287
+ editing = void 0;
288
+ paint();
289
+ if (refocus) setFocus(focused);
290
+ }
291
+ function nameInput(n, ed) {
292
+ const now = ed.mode === "new" ? String(ed.value ?? "") : String(n.path[n.path.length - 1]);
293
+ const inp = h("input", { class: "tedit tname", value: now, "aria-label": ed.mode === "new" ? `Name of the new ${ed.section ? kindOf(ed.section).singular : "entry"}` : `Rename ${n.label}`, spellcheck: "false", autocomplete: "off" });
294
+ inp.value = now;
295
+ const check = () => {
296
+ const v = inp.value.trim();
297
+ const needsName = ed.mode === "new" || isResource(n) || inGroup(n.path);
298
+ const bad = !v ? "a name is needed" : needsName && !K.nameRule.test(v) ? ui.nameHint : v !== now && taken(ed.mode === "new" ? ed.mapPath : n.path.slice(0, -1), v, needsName) ? `"${v}" is already used` : "";
299
+ inp.setAttribute("aria-invalid", String(!!bad));
300
+ inp.title = bad;
301
+ return bad ? void 0 : v;
302
+ };
303
+ inp.addEventListener("input", check);
304
+ inp.addEventListener("keydown", (e) => {
305
+ e.stopPropagation();
306
+ if (e.key === "Escape") {
307
+ e.preventDefault();
308
+ endEdit();
309
+ }
310
+ if (e.key === "Enter") {
311
+ e.preventDefault();
312
+ commit();
313
+ }
314
+ });
315
+ let done = false;
316
+ const commit = () => {
317
+ if (done) return;
318
+ const v = check();
319
+ if (v === void 0) {
320
+ if (!inp.value.trim()) {
321
+ done = true;
322
+ endEdit();
323
+ }
324
+ return;
325
+ }
326
+ done = true;
327
+ editing = void 0;
328
+ if (ed.mode === "new") {
329
+ const p = [...ed.mapPath, v];
330
+ if (run(insertCmd(ed.mapPath, v, ed.template(), { label: `Add ${ed.section ? kindOf(ed.section).singular : "entry"} ${v}` }))) {
331
+ focused = toPointer(p);
332
+ doc.select([p], "tree");
333
+ expanded.add(focused);
334
+ }
335
+ } else if (v !== now) {
336
+ const p = n.path;
337
+ if (run(isResource(n) ? renameCmd(p, v) : renameKeyCmd(p, v))) {
338
+ const np = [...p.slice(0, -1), v];
339
+ if (expanded.has(n.id)) {
340
+ expanded.delete(n.id);
341
+ expanded.add(toPointer(np));
342
+ }
343
+ focused = toPointer(np);
344
+ }
345
+ }
346
+ paint();
347
+ setFocus(focused);
348
+ };
349
+ inp.addEventListener("blur", () => {
350
+ if (!done) {
351
+ if (inp.value.trim() && check() !== void 0) commit();
352
+ else {
353
+ done = true;
354
+ editing = void 0;
355
+ paint();
356
+ }
357
+ }
358
+ });
359
+ return inp;
360
+ }
361
+ function taken(mapPath, v, oneNamespace) {
362
+ if (oneNamespace && !isGroupPath(mapPath)) return K.kinds.some((k) => namesIn(k.kind).includes(v));
363
+ const m = getIn(doc.goodDoc.data, mapPath);
364
+ return !!m && typeof m === "object" && v in m;
365
+ }
366
+ function valueInput(n) {
367
+ const nav = opts.nav();
368
+ const now = getIn(doc.goodDoc.data, n.path);
369
+ const vals = nav.values(n.path);
370
+ const slot = K.slotAt(toPointer(n.path));
371
+ const refNames = slot ? slot.targets.flatMap((s) => namesIn(s).map((x) => slot.write ? slot.write(x) : x)).concat(slot.extra ?? []) : [];
372
+ const choices = [.../* @__PURE__ */ new Set([...vals, ...refNames])];
373
+ let el2;
374
+ if (choices.length && (vals.length || slot)) {
375
+ el2 = h("select", { class: "tedit tvalue", "aria-label": `Value of ${n.label}` }, ...choices.map((v) => h("option", { value: v }, v)));
376
+ if (!choices.includes(String(now))) el2.prepend(h("option", { value: String(now ?? "") }, String(now ?? "")));
377
+ el2.value = String(now ?? "");
378
+ el2.addEventListener("change", () => commitValue(n, el2.value));
379
+ } else {
380
+ el2 = h("input", { class: "tedit tvalue", "aria-label": `Value of ${n.label}`, spellcheck: "false", autocomplete: "off" });
381
+ el2.value = now === null || now === void 0 ? "" : String(now);
382
+ }
383
+ el2.addEventListener("keydown", (e) => {
384
+ e.stopPropagation();
385
+ if (e.key === "Escape") {
386
+ e.preventDefault();
387
+ endEdit();
388
+ }
389
+ if (e.key === "Enter") {
390
+ e.preventDefault();
391
+ commitValue(n, el2.value);
392
+ }
393
+ });
394
+ el2.addEventListener("blur", () => {
395
+ if (editing?.id === n.id) commitValue(n, el2.value, false);
396
+ });
397
+ return el2;
398
+ }
399
+ function commitValue(n, raw, refocus = true) {
400
+ if (editing?.id !== n.id) return;
401
+ editing = void 0;
402
+ const types = opts.nav().types(n.path);
403
+ const now = getIn(doc.goodDoc.data, n.path);
404
+ let v = raw;
405
+ if ((types.has("integer") || types.has("number") || typeof now === "number") && /^-?\d+(\.\d+)?$/.test(raw.trim())) v = Number(raw);
406
+ else if ((types.has("boolean") || typeof now === "boolean") && /^(true|false)$/.test(raw.trim())) v = raw.trim() === "true";
407
+ if (v !== now) run(setCmd(n.path, v));
408
+ paint();
409
+ if (refocus) setFocus(focused);
410
+ }
411
+ async function remove(ids) {
412
+ if (readOnly() || !ids.length) return;
413
+ const nodes = ids.map((id) => byId.get(id)?.n).filter((n) => !!n && !isRoot(n) && n.shape !== "missing");
414
+ const top = nodes.filter((n) => !nodes.some((o) => o !== n && o.path.length < n.path.length && isPrefix(o.path, n.path)));
415
+ if (!top.length) return;
416
+ let withRefs = false;
417
+ const refs = top.filter(isResource).flatMap((n) => referencesTo(doc.text, n.section, String(n.path[n.path.length - 1])).filter((r) => !top.some((o) => isPrefix(o.path, fromPointer(r.path)))).map((r) => ({ n, r })));
418
+ if (refs.length) {
419
+ const list = h("ul", { class: "ref-list" }, ...refs.map(({ n, r }) => h("li", null, h("code", null, r.path.slice(1).replace(/\//g, ".")), ` \u2014 ${r.slot.what} (${n.label})`)));
420
+ const what = top.length === 1 ? describe(top[0].path) : `${top.length} items`;
421
+ const choice = await ask(
422
+ `Delete ${what}?`,
423
+ [
424
+ h("p", { class: "sub" }, `${refs.length === 1 ? "One place refers" : `${refs.length} places refer`} to ${top.length === 1 ? "it" : "them"}:`),
425
+ list,
426
+ h("p", { class: "sub" }, "Delete the references too, or leave them (the problems list will show them). Either way, Undo brings everything back.")
427
+ ],
428
+ [{ label: "Cancel", value: "cancel" }, { label: "Delete only", value: "only", danger: true }, { label: "Delete with references", value: "with", danger: true, primary: true }]
429
+ );
430
+ if (!choice) {
431
+ setFocus(focused);
432
+ return;
433
+ }
434
+ withRefs = choice === "with";
435
+ }
436
+ const order = [...top].sort((a, b) => typeof a.path.at(-1) === "number" && typeof b.path.at(-1) === "number" ? b.path.at(-1) - a.path.at(-1) : 0);
437
+ const cmds = order.map((n) => deleteCmd(n.path, { withRefs: withRefs && isResource(n) }));
438
+ const ix = rows.findIndex((r) => r.n.id === top[0].id);
439
+ const next = rows[ix + 1]?.n.id && !top.some((t) => rows[ix + 1].n.id.startsWith(t.id)) ? rows[ix + 1].n.id : rows[ix - 1]?.n.id ?? "";
440
+ const ok = run(cmds.length === 1 ? cmds[0] : composite(`Delete ${top.length} items`, cmds));
441
+ if (ok) {
442
+ focused = next;
443
+ doc.select(next ? [fromPointer(next)] : [], "tree");
444
+ paint();
445
+ setFocus(focused);
446
+ }
447
+ }
448
+ function composite(label, cmds) {
449
+ return {
450
+ label,
451
+ remap: (p) => cmds.reduce((x, c) => x && c.remap ? c.remap(x) : x, p),
452
+ build: (t) => {
453
+ const steps = [];
454
+ let fallback = false;
455
+ for (const c of cmds) {
456
+ const s = c.build(t);
457
+ for (const st of s.steps) {
458
+ steps.push(st);
459
+ t = applyStep(t, st);
460
+ }
461
+ fallback ||= !!s.fallback;
462
+ }
463
+ return { steps, fallback };
464
+ }
465
+ };
466
+ }
467
+ function duplicate(id) {
468
+ const n = byId.get(id)?.n;
469
+ if (!n || readOnly()) return;
470
+ if (!isResource(n)) {
471
+ opts.note(ui.text.duplicateNote, "info");
472
+ return;
473
+ }
474
+ const c = duplicateCmd(n.path);
475
+ if (run(c)) {
476
+ const sel = c.select?.[0];
477
+ if (sel) {
478
+ focused = toPointer(sel);
479
+ doc.select([sel], "tree");
480
+ }
481
+ paint();
482
+ setFocus(focused);
483
+ }
484
+ }
485
+ function menuFor(n, at) {
486
+ const ids = selected().has(n.id) ? [...selected()] : [n.id];
487
+ const many = ids.length > 1;
488
+ const adds = addItems(n);
489
+ const ro = readOnly();
490
+ const items = [];
491
+ if (adds.length && !many) items.push({ heading: "Add" }, ...adds.slice(0, 12), ...adds.length > 12 ? [{ label: "More\u2026", icon: "plus", onSelect: () => openMenu(adds, at, "Add") }] : [], { sep: true });
492
+ if (!many) {
493
+ items.push({ label: "Rename", icon: "pencil", keys: "F2", disabled: ro || n.kind === "group" || isRoot(n) || n.kind === "item", why: ui.text.renameWhy, onSelect: () => startEdit(n.id, "rename") });
494
+ if (n.shape === "scalar") items.push({ label: "Edit value", icon: "pencil", keys: "Enter", disabled: ro, onSelect: () => startEdit(n.id, "value") });
495
+ items.push({ label: "Duplicate", icon: "copy", keys: "\u2318D", disabled: ro || !isResource(n), why: ui.text.duplicateWhy, onSelect: () => duplicate(n.id) });
496
+ }
497
+ if (n.path.length) items.push({ label: "Select parent", icon: "arrow-up-right", keys: "\u2318\u2191", onSelect: () => selectParent() });
498
+ items.push({ label: "Show in text", icon: "code-xml", onSelect: () => opts.showInText(n.path) });
499
+ if (opts.clipItems) items.push({ sep: true }, ...opts.clipItems());
500
+ items.push({ sep: true }, { label: many ? `Delete ${ids.length} items` : "Delete", icon: "trash-2", keys: "\u232B", danger: true, disabled: ro || isRoot(n) || n.shape === "missing", why: isRoot(n) ? ui.text.deleteWhy : "Nothing to delete", onSelect: () => remove(ids) });
501
+ openMenu(items, at, `${n.label || ui.text.root} actions`);
502
+ }
503
+ function selectParent() {
504
+ const ps = doc.selection.length ? doc.selection : [fromPointer(focused)];
505
+ const up = [...new Set(ps.map((p) => {
506
+ let q = p.slice(0, -1);
507
+ while (q.length && !byId.has(toPointer(q))) q = q.slice(0, -1);
508
+ return toPointer(q);
509
+ }))];
510
+ selectIds(up);
511
+ }
512
+ el.addEventListener("click", (e) => {
513
+ const r = e.target.closest(".trow");
514
+ if (!r || e.target.closest(".tedit")) return;
515
+ clickSelect(r.dataset.id, e);
516
+ });
517
+ el.addEventListener("dblclick", (e) => {
518
+ const r = e.target.closest(".trow");
519
+ if (!r || e.target.closest(".tedit, .twisty, .tadd")) return;
520
+ const n = byId.get(r.dataset.id)?.n;
521
+ if (!n) return;
522
+ if (n.shape === "scalar" && e.target.closest(".tval")) startEdit(n.id, "value");
523
+ else if (n.kind === "resource" || n.kind === "key") startEdit(n.id, "rename");
524
+ else toggle(n.id);
525
+ });
526
+ el.addEventListener("contextmenu", (e) => {
527
+ const r = e.target.closest(".trow");
528
+ if (!r) return;
529
+ e.preventDefault();
530
+ const id = r.dataset.id;
531
+ if (!selected().has(id)) clickSelect(id, { shiftKey: false, metaKey: false, ctrlKey: false });
532
+ const n = byId.get(id)?.n;
533
+ if (n) menuFor(n, { x: e.clientX, y: e.clientY });
534
+ });
535
+ el.addEventListener("focusin", (e) => {
536
+ const r = e.target.closest(".trow");
537
+ if (r && !e.target.closest(".tedit")) focused = r.dataset.id;
538
+ });
539
+ el.addEventListener("keydown", (e) => {
540
+ if (e.target.closest(".tedit")) return;
541
+ const ids = rows.map((r) => r.n.id);
542
+ const i = Math.max(0, ids.indexOf(focused));
543
+ const cur = byId.get(focused);
544
+ const n = cur?.n;
545
+ const mod = e.metaKey || e.ctrlKey;
546
+ const go = (j) => {
547
+ const id = ids[Math.max(0, Math.min(ids.length - 1, j))];
548
+ if (e.shiftKey) {
549
+ if (!anchor) anchor = focused;
550
+ clickSelect(id, { shiftKey: true, metaKey: false, ctrlKey: false });
551
+ } else {
552
+ anchor = id;
553
+ selectIds([id]);
554
+ }
555
+ };
556
+ switch (e.key) {
557
+ case "ArrowDown":
558
+ if (mod) return;
559
+ go(i + 1);
560
+ break;
561
+ case "ArrowUp":
562
+ if (mod) {
563
+ selectParent();
564
+ break;
565
+ }
566
+ go(i - 1);
567
+ break;
568
+ case "Home":
569
+ go(0);
570
+ break;
571
+ case "End":
572
+ go(ids.length - 1);
573
+ break;
574
+ case "ArrowRight":
575
+ if (n && n.children.length && !expanded.has(n.id)) toggle(n.id, true);
576
+ else if (n && n.children.length) go(i + 1);
577
+ break;
578
+ case "ArrowLeft":
579
+ if (n && expanded.has(n.id) && n.children.length) toggle(n.id, false);
580
+ else if (cur?.parent) {
581
+ anchor = cur.parent.id;
582
+ selectIds([cur.parent.id]);
583
+ }
584
+ break;
585
+ case "Enter":
586
+ if (!n) return;
587
+ if (n.shape === "scalar" && n.kind !== "group") {
588
+ const now = getIn(doc.goodDoc.data, n.path);
589
+ if (typeof now === "boolean" && !readOnly()) run(setCmd(n.path, !now));
590
+ else startEdit(n.id, "value");
591
+ } else if (n.children.length) toggle(n.id);
592
+ break;
593
+ case "F2":
594
+ if (n) startEdit(n.id, "rename");
595
+ break;
596
+ case "Delete":
597
+ case "Backspace":
598
+ remove(doc.selection.length ? doc.selection.map(toPointer) : [focused]);
599
+ break;
600
+ case "ContextMenu":
601
+ if (n) menuFor(n, el.querySelector(`[data-id="${CSS.escape(focused)}"]`));
602
+ break;
603
+ case "F10":
604
+ if (e.shiftKey && n) menuFor(n, el.querySelector(`[data-id="${CSS.escape(focused)}"]`));
605
+ else return;
606
+ break;
607
+ case "+":
608
+ case "Insert":
609
+ if (n && addItems(n).length) openMenu(addItems(n), el.querySelector(`[data-id="${CSS.escape(focused)}"]`), "Add");
610
+ break;
611
+ default:
612
+ if (mod && e.key.toLowerCase() === "d") {
613
+ duplicate(focused);
614
+ break;
615
+ }
616
+ if (mod && e.key.toLowerCase() === "a") {
617
+ selectIds(ids.filter((x) => x), focused);
618
+ break;
619
+ }
620
+ if (e.key.length === 1 && !mod && /\S/.test(e.key)) {
621
+ const t = Date.now();
622
+ typeahead = t - typeaheadAt > 700 ? e.key.toLowerCase() : typeahead + e.key.toLowerCase();
623
+ typeaheadAt = t;
624
+ const order = [...ids.slice(i + (typeahead.length === 1 ? 1 : 0)), ...ids.slice(0, i + 1)];
625
+ const hit = order.find((id) => (byId.get(id)?.n.label ?? "").toLowerCase().startsWith(typeahead));
626
+ if (hit !== void 0) {
627
+ anchor = hit;
628
+ selectIds([hit]);
629
+ }
630
+ break;
631
+ }
632
+ return;
633
+ }
634
+ e.preventDefault();
635
+ });
636
+ let dragId;
637
+ el.addEventListener("dragstart", (e) => {
638
+ const r = e.target.closest(".trow");
639
+ if (!r) return;
640
+ dragId = r.dataset.id;
641
+ e.dataTransfer?.setData("text/plain", fromPointer(dragId).join("."));
642
+ if (e.dataTransfer) e.dataTransfer.effectAllowed = "move";
643
+ });
644
+ const target = (e) => {
645
+ const r = e.target.closest(".trow");
646
+ if (!r || !dragId || r.dataset.id === dragId) return void 0;
647
+ const a = byId.get(dragId)?.n, b = byId.get(r.dataset.id)?.n;
648
+ if (!a || !b || toPointer(a.path.slice(0, -1)) !== toPointer(b.path.slice(0, -1))) return void 0;
649
+ const box = r.getBoundingClientRect();
650
+ return { r, a, b, after: e.clientY > box.top + box.height / 2 };
651
+ };
652
+ el.addEventListener("dragover", (e) => {
653
+ for (const x of el.querySelectorAll(".drop-before, .drop-after")) x.classList.remove("drop-before", "drop-after");
654
+ const t = target(e);
655
+ if (!t) return;
656
+ e.preventDefault();
657
+ t.r.classList.add(t.after ? "drop-after" : "drop-before");
658
+ });
659
+ el.addEventListener("dragleave", (e) => {
660
+ if (e.target === el) for (const x of el.querySelectorAll(".drop-before, .drop-after")) x.classList.remove("drop-before", "drop-after");
661
+ });
662
+ el.addEventListener("drop", (e) => {
663
+ const t = target(e);
664
+ for (const x of el.querySelectorAll(".drop-before, .drop-after")) x.classList.remove("drop-before", "drop-after");
665
+ dragId = void 0;
666
+ if (!t) return;
667
+ e.preventDefault();
668
+ const { a, b, after } = t;
669
+ const parent = a.path.slice(0, -1);
670
+ const last = (p) => p[p.length - 1];
671
+ if (typeof last(a.path) === "number") {
672
+ const from = last(a.path);
673
+ let to = last(b.path) + (after ? 1 : 0);
674
+ if (to > from) to -= 1;
675
+ if (run(moveItemCmd(parent, from, to))) {
676
+ focused = toPointer([...parent, to]);
677
+ doc.select([[...parent, to]], "tree");
678
+ }
679
+ } else {
680
+ const sibs = (byId.get(toPointer(parent))?.n.children ?? []).map((c) => String(last(c.path)));
681
+ const bi = sibs.indexOf(String(last(b.path)));
682
+ const before = after ? sibs[bi + 1] : sibs[bi];
683
+ if (before !== String(last(a.path))) run(moveCmd(a.path, before === void 0 ? void 0 : before));
684
+ focused = a.id;
685
+ }
686
+ paint();
687
+ setFocus(focused);
688
+ });
689
+ doc.on((c) => {
690
+ if (c.remap) {
691
+ for (const id of [...expanded]) {
692
+ const p = c.remap(fromPointer(id));
693
+ if (p && toPointer(p) !== id) {
694
+ expanded.delete(id);
695
+ expanded.add(toPointer(p));
696
+ }
697
+ }
698
+ const f = c.remap(fromPointer(focused));
699
+ if (f) focused = toPointer(f);
700
+ }
701
+ if (c.kind === "load") {
702
+ editing = void 0;
703
+ focused = "";
704
+ }
705
+ if (isTyping(c)) paintLater();
706
+ else paintLater.now();
707
+ });
708
+ const paintLater = later(() => paint());
709
+ doc.onSelect((c) => {
710
+ if (c.origin !== "tree") reveal(c.paths);
711
+ else paint();
712
+ });
713
+ paint();
714
+ return {
715
+ paint,
716
+ dispose: () => paintLater.cancel(),
717
+ focus: () => setFocus(focused),
718
+ /** for the tests */
719
+ rows: () => rows.map((r) => ({ id: r.n.id, label: r.n.label, depth: r.depth, kind: r.n.kind, value: r.n.value, lifetime: r.n.lifetime })),
720
+ startEdit,
721
+ remove,
722
+ duplicate,
723
+ expand: (id) => toggle(id, true)
724
+ };
725
+ }
726
+ export {
727
+ treeView
728
+ };