@johnoscott/isometric-engine 0.12.3 → 0.14.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 (45) hide show
  1. package/EDITOR-GUIDE.md +10 -2
  2. package/README.md +1 -1
  3. package/assets/app.css +12 -0
  4. package/assets/marks.css +2 -0
  5. package/iso-editor.js +189 -188
  6. package/iso-render.js +82 -82
  7. package/iso-systems.js +79 -79
  8. package/js/iso-editor/commands.js +7 -0
  9. package/js/iso-editor/connections.js +24 -0
  10. package/js/iso-editor/make-way.js +62 -0
  11. package/js/iso-editor/model.js +17 -3
  12. package/js/iso-editor/ui/actions.js +22 -24
  13. package/js/iso-editor/ui/canvas.js +564 -223
  14. package/js/iso-editor/ui/mount.js +2 -1
  15. package/js/iso-editor/ui/palette-look.js +53 -0
  16. package/js/iso-editor/ui/props.js +2 -2
  17. package/js/iso-editor/ui/rename.js +114 -0
  18. package/js/iso-editor/ui/setup.js +4 -0
  19. package/js/iso-viewer/viewport.js +17 -5
  20. package/package.json +1 -1
  21. package/src/iso-editor/commands.ts +9 -0
  22. package/src/iso-editor/connections.ts +28 -0
  23. package/src/iso-editor/make-way.ts +89 -0
  24. package/src/iso-editor/model.ts +17 -3
  25. package/src/iso-editor/ui/actions.ts +20 -23
  26. package/src/iso-editor/ui/app.css +10 -0
  27. package/src/iso-editor/ui/canvas.ts +469 -180
  28. package/src/iso-editor/ui/marks.css +2 -0
  29. package/src/iso-editor/ui/mount.ts +4 -3
  30. package/src/iso-editor/ui/palette-look.ts +51 -0
  31. package/src/iso-editor/ui/props.ts +3 -3
  32. package/src/iso-editor/ui/rename.ts +105 -0
  33. package/src/iso-editor/ui/setup.ts +11 -0
  34. package/src/iso-viewer/viewport.ts +25 -5
  35. package/types/iso-editor/connections.d.ts +8 -0
  36. package/types/iso-editor/make-way.d.ts +40 -0
  37. package/types/iso-editor/model.d.ts +8 -2
  38. package/types/iso-editor/ui/actions.d.ts +5 -2
  39. package/types/iso-editor/ui/canvas.d.ts +42 -12
  40. package/types/iso-editor/ui/mount.d.ts +6 -2
  41. package/types/iso-editor/ui/palette-look.d.ts +5 -0
  42. package/types/iso-editor/ui/props.d.ts +1 -1
  43. package/types/iso-editor/ui/rename.d.ts +43 -0
  44. package/types/iso-editor/ui/setup.d.ts +7 -0
  45. package/types/iso-viewer/viewport.d.ts +13 -1
@@ -116,6 +116,13 @@ function commandsFor(a) {
116
116
  const quote = now[0] === '"' || now[0] === "'" ? now[0] : void 0;
117
117
  edits.push({ start: range[0], end: range[1], text: quote === '"' ? JSON.stringify(v) : quote === "'" ? `'${v}'` : scalarText(v, { flow: true, indent: 0 }) });
118
118
  }
119
+ const data = dataOf(t);
120
+ for (const k of [a.pins.key, a.pins.legacyKey]) {
121
+ const layout = k === void 0 ? void 0 : data?.[k]?.layout;
122
+ if (!layout || typeof layout !== "object" || !Object.hasOwn(layout, from) || Object.hasOwn(layout, to)) continue;
123
+ const range = d.rangeOf([k, "layout", from], "key");
124
+ if (range && !edits.some((e) => e.start < range[1] && range[0] < e.end)) edits.push({ start: range[0], end: range[1], text: scalarText(to, { flow: true, indent: 0 }) });
125
+ }
119
126
  return { steps: [edits] };
120
127
  }
121
128
  };
@@ -0,0 +1,24 @@
1
+ import { fromPointer } from "./path.js";
2
+ function connectionsOf(pathOf, isRoute, objectOf) {
3
+ const uses = /* @__PURE__ */ new Map();
4
+ for (const p of pathOf.values()) uses.set(p, (uses.get(p) ?? 0) + 1);
5
+ const out = /* @__PURE__ */ new Map();
6
+ for (const [id, p] of pathOf) if (isRoute(id) && uses.get(p) === 1 && fromPointer(p).length >= 2 && objectOf(p) === void 0) out.set(p, id);
7
+ return out;
8
+ }
9
+ function nearestOn(d, x, y) {
10
+ const n = (d.match(/-?\d*\.?\d+(?:e-?\d+)?/gi) ?? []).map(Number), pts = [];
11
+ for (let i = 0; i + 1 < n.length; i += 2) pts.push([n[i], n[i + 1]]);
12
+ let best;
13
+ for (let i = 0; i + 1 < pts.length; i++) {
14
+ const [ax, ay] = pts[i], [bx, by] = pts[i + 1], l2 = (bx - ax) ** 2 + (by - ay) ** 2;
15
+ const t = l2 ? Math.max(0, Math.min(1, ((x - ax) * (bx - ax) + (y - ay) * (by - ay)) / l2)) : 0;
16
+ const px = ax + (bx - ax) * t, py = ay + (by - ay) * t, dist = Math.hypot(px - x, py - y);
17
+ if (!best || dist < best.dist) best = { x: px, y: py, dist };
18
+ }
19
+ return best;
20
+ }
21
+ export {
22
+ connectionsOf,
23
+ nearestOn
24
+ };
@@ -0,0 +1,62 @@
1
+ const DWELL_HINT = 150;
2
+ const DWELL_GIVE = 700;
3
+ const BACK_AFTER = 250;
4
+ const GLIDE = 180;
5
+ const JIGGLE = { px: 1.5, hz: 8 };
6
+ const MAX_CHAIN = 3;
7
+ const E = 1e-6;
8
+ const hits = (a, b) => a.x0 < b.x1 - E && a.x1 > b.x0 + E && a.y0 < b.y1 - E && a.y1 > b.y0 + E;
9
+ const at = (b, [dx, dy]) => ({ x0: b.x0 + dx, y0: b.y0 + dy, x1: b.x1 + dx, y1: b.y1 + dy });
10
+ function makeWay({ foot, others, going, bounds, max = MAX_CHAIN }) {
11
+ const first = others.filter((o) => hits(o, foot));
12
+ if (!first.length) return /* @__PURE__ */ new Map();
13
+ if (first.some((o) => !o.movable)) return void 0;
14
+ const ways = [[1, 0], [-1, 0], [0, 1], [0, -1]];
15
+ let best;
16
+ for (const w of ways) {
17
+ const along = w[0] * going[0] + w[1] * going[1];
18
+ if (along < 0 && Math.abs(along) >= Math.max(Math.abs(going[0]), Math.abs(going[1])) - E) continue;
19
+ const steps = push(foot, others, w, bounds, max);
20
+ if (!steps) continue;
21
+ const cells = [...steps.values()].reduce((n, [dx, dy]) => n + Math.abs(dx) + Math.abs(dy), 0);
22
+ if (!best || cells < best.cells || cells === best.cells && along > best.along + E) best = { steps, cells, along };
23
+ }
24
+ return best?.steps;
25
+ }
26
+ function push(foot, others, w, bounds, max) {
27
+ const far = /* @__PURE__ */ new Map();
28
+ const byId = new Map(others.map((o) => [o.id, o]));
29
+ const boxOf = (o) => at(o, [w[0] * (far.get(o.id) ?? 0), w[1] * (far.get(o.id) ?? 0)]);
30
+ const queue = [void 0];
31
+ for (let guard = 0; queue.length; guard++) {
32
+ if (guard > 100) return void 0;
33
+ const id = queue.shift(), by = id === void 0 ? foot : boxOf(byId.get(id));
34
+ for (const o of others) {
35
+ if (o.id === id) continue;
36
+ const b = boxOf(o);
37
+ if (!hits(b, by)) continue;
38
+ if (!o.movable) return void 0;
39
+ const gap = w[0] > 0 ? by.x1 - b.x0 : w[0] < 0 ? b.x1 - by.x0 : w[1] > 0 ? by.y1 - b.y0 : b.y1 - by.y0;
40
+ far.set(o.id, (far.get(o.id) ?? 0) + Math.max(1, Math.ceil(gap - E)));
41
+ if (far.size > max) return void 0;
42
+ if (!queue.includes(o.id)) queue.push(o.id);
43
+ }
44
+ }
45
+ const steps = new Map([...far].map(([id, n]) => [id, [w[0] * n, w[1] * n]]));
46
+ for (const id of steps.keys()) {
47
+ const o = byId.get(id), b = boxOf(o);
48
+ if (hits(b, foot) || others.some((q) => q.id !== id && hits(b, boxOf(q)))) return void 0;
49
+ if (bounds && (b.x0 < bounds.x0 - E || b.y0 < bounds.y0 - E || b.x1 > bounds.x1 + E || b.y1 > bounds.y1 + E)) return void 0;
50
+ }
51
+ return steps;
52
+ }
53
+ export {
54
+ BACK_AFTER,
55
+ DWELL_GIVE,
56
+ DWELL_HINT,
57
+ GLIDE,
58
+ JIGGLE,
59
+ MAX_CHAIN,
60
+ hits,
61
+ makeWay
62
+ };
@@ -71,8 +71,10 @@ class EditorModel {
71
71
  this._version++;
72
72
  }
73
73
  // ---------- doing ----------
74
- /** Run a command: build it against the text, apply its steps, push it. Returns false if it changed nothing. */
75
- apply(cmd, origin = "api") {
74
+ /** Run a command: build it against the text, apply its steps, push it. Returns false if it changed nothing. `joins`
75
+ * (0.12.5): the id of an undo step it is part of, when that is the last one, nothing was undone since and the file
76
+ * wasn't saved at it (the saved text must stay a step of its own to come back to). */
77
+ apply(cmd, origin = "api", joins) {
76
78
  this.seal();
77
79
  const splice = cmd.build(this._text);
78
80
  if (!splice.steps.some((s) => s.length)) return false;
@@ -81,7 +83,15 @@ class EditorModel {
81
83
  const remap = cmd.remap ?? ((p) => p);
82
84
  this.selection = cmd.select ?? this.selection.map(remap).filter((p) => p !== null);
83
85
  if (splice.fallback) this.fallbacks++;
84
- this.push({ id: this.nextId++, label: cmd.label, origin, steps: splice.steps, inverse, before, after: { paths: this.selection, text: this.textSel }, time: this.now(), fallback: !!splice.fallback, open: false });
86
+ const top = this.undoStack[this.undoStack.length - 1];
87
+ if (top && joins !== void 0 && top.id === joins && !this.redoStack.length && top.id !== this.cleanId) {
88
+ top.label = cmd.joined?.(top.label) ?? top.label;
89
+ top.steps.push(...splice.steps);
90
+ top.inverse.unshift(...inverse);
91
+ top.after = { paths: this.selection, text: this.textSel };
92
+ top.time = this.now();
93
+ top.fallback ||= !!splice.fallback;
94
+ } else this.push({ id: this.nextId++, label: cmd.label, origin, steps: splice.steps, inverse, before, after: { paths: this.selection, text: this.textSel }, time: this.now(), fallback: !!splice.fallback, open: false });
85
95
  this.emit({ kind: "do", origin, label: cmd.label, steps: splice.steps, version: this._version, remap: cmd.remap });
86
96
  for (const fn of this.selListeners) fn({ paths: this.selection, origin });
87
97
  return true;
@@ -161,6 +171,10 @@ class EditorModel {
161
171
  get canRedo() {
162
172
  return this.redoStack.length > 0;
163
173
  }
174
+ /** the last undo step's id (what `apply` may join) */
175
+ get undoId() {
176
+ return this.undoStack[this.undoStack.length - 1]?.id;
177
+ }
164
178
  get undoLabel() {
165
179
  return this.undoStack[this.undoStack.length - 1]?.label;
166
180
  }
@@ -1,11 +1,12 @@
1
1
  import { h } from "./icons.js";
2
2
  import { ask } from "./dialog.js";
3
+ import { renameField } from "./rename.js";
3
4
  import { applyStep } from "../splice.js";
4
5
  import { fromPointer, isPrefix, toPointer } from "../path.js";
5
6
  const getIn = (o, p) => p.reduce((x, k) => x && typeof x === "object" ? x[k] : void 0, o);
6
- function run(doc, c, origin, note) {
7
+ function run(doc, c, origin, note, joins) {
7
8
  try {
8
- return doc.apply(c, origin);
9
+ return doc.apply(c, origin, joins);
9
10
  } catch (e) {
10
11
  note?.(String(e.message), "bad");
11
12
  return false;
@@ -59,28 +60,25 @@ function actionsFor(kit) {
59
60
  if (ok) doc.select([], origin);
60
61
  return ok;
61
62
  }
62
- async function renameDialog(doc, path, origin, note) {
63
- const now = String(path[path.length - 1]);
64
- const input = h("input", { type: "text", value: now, "aria-label": "New name", spellcheck: "false", autocomplete: "off", class: "mono-input" });
65
- input.value = now;
66
- const why = h("p", { class: "dlg-error", role: "alert" });
63
+ async function rename(doc, path, origin, note, at) {
64
+ const now = String(path[path.length - 1]), res = isRes(path);
65
+ let here = path;
66
+ const off = doc.on((c) => {
67
+ if (here && c.remap) here = c.remap(here);
68
+ });
67
69
  const used = () => kit.kinds.flatMap((k) => Object.keys(getIn(doc.goodDoc.data, k.section) ?? {}));
68
- const check = () => {
69
- const v2 = input.value.trim();
70
- const bad = !kit.nameRule.test(v2) && isRes(path) ? kit.nameHint : v2 !== now && used().includes(v2) ? `"${v2}" is already used` : "";
71
- why.textContent = bad;
72
- return bad ? void 0 : v2;
73
- };
74
- input.addEventListener("input", check);
75
- const go = ask(`Rename ${describe(path)}`, [h("label", { class: "field" }, h("span", null, "Name"), input), why, isRes(path) ? h("p", { class: "sub" }, "Every reference to it is renamed too.") : ""], [{ label: "Cancel", value: "cancel" }, { label: "Rename", value: "ok", primary: true }]);
76
- setTimeout(() => {
77
- input.focus();
78
- input.select();
79
- }, 0);
80
- if (await go !== "ok") return false;
81
- const v = check();
82
- if (!v || v === now) return false;
83
- return run(doc, isRes(path) ? renameCmd(path, v) : renameKeyCmd(path, v), origin, note);
70
+ let ok = false;
71
+ await renameField({
72
+ label: `Rename ${describe(path)}`,
73
+ value: now,
74
+ wrong: (v) => !v ? "A name is needed" : v === now ? "" : res && !kit.nameRule.test(v) ? kit.nameHint : used().includes(v) ? `"${v}" is already used` : "",
75
+ alive: () => !!here && getIn(doc.goodDoc.data, here) !== void 0,
76
+ commit: (v) => {
77
+ if (here) ok = run(doc, { ...res ? renameCmd(here, v) : renameKeyCmd(here, v), joined: (l) => l.replace(` ${now}`, ` ${v}`) }, origin, note, at.joins);
78
+ }
79
+ }, at);
80
+ off();
81
+ return ok;
84
82
  }
85
83
  const duplicate = (doc, path, origin, note) => {
86
84
  const c = duplicateCmd(path);
@@ -107,7 +105,7 @@ function actionsFor(kit) {
107
105
  }
108
106
  return [...out.values()];
109
107
  }
110
- return { deletePaths, renameDialog, duplicate, connected };
108
+ return { deletePaths, rename, duplicate, connected };
111
109
  }
112
110
  export {
113
111
  actionsFor,