@johnoscott/isometric-engine 0.10.1 → 0.12.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -145,6 +145,10 @@ function parentByIndent(d, lineStart, indent, forList = false) {
145
145
  if (a.where === "key") return a.path.slice(0, -1);
146
146
  if (line.slice(ind).startsWith("- ")) return a.path.slice(0, -1);
147
147
  }
148
+ if (!forList && /^- /.test(line.slice(ind)) && ind + 2 === indent) {
149
+ const a = d.at(s + ind + 2);
150
+ if (a.where === "key") return a.path.slice(0, -1);
151
+ }
148
152
  if (ind < indent || forList && ind <= indent && /:\s*$/.test(line)) {
149
153
  const m = /^(\s*)(- )?([^\s:#][^:#]*?)\s*:/.exec(line);
150
154
  if (m) {
@@ -253,7 +253,9 @@ function canvasView(host, doc, opts) {
253
253
  const live = layer?.extent?.() ?? [];
254
254
  return union([...ptrs.flatMap((p) => elementsFor(p)).filter((e) => e.getBBox().width || e.getBBox().height).map(boxOf), ...live]);
255
255
  }
256
- const V = viewport(host, {
256
+ const docked = ui.paletteLayout === "docked";
257
+ const viewEl = docked ? stage : host;
258
+ const V = viewport(viewEl, {
257
259
  svg: () => svg,
258
260
  onApply: (k) => {
259
261
  svg.classList.toggle("zoom-near", k >= 1);
@@ -1112,6 +1114,174 @@ function canvasView(host, doc, opts) {
1112
1114
  return p;
1113
1115
  }
1114
1116
  const palette = h("div", { class: "palette", role: "toolbar", "aria-label": T.palette, "aria-orientation": "vertical" });
1117
+ const sections = /* @__PURE__ */ new Map();
1118
+ const PAL_KEY = "canvas-palette";
1119
+ let lastSection;
1120
+ try {
1121
+ const v = JSON.parse(opts.store?.get(PAL_KEY) ?? "{}");
1122
+ if (typeof v.last === "string") lastSection = v.last;
1123
+ } catch {
1124
+ }
1125
+ const named = [...new Set(ui.palette.map((i) => i.section).filter((x) => x !== void 0))];
1126
+ const openAtStart = lastSection !== void 0 && named.includes(lastSection) ? lastSection : named[0];
1127
+ const setOpen = (sec, open) => {
1128
+ const g = sections.get(sec);
1129
+ if (!g) return;
1130
+ g.head.setAttribute("aria-expanded", String(open));
1131
+ g.body.hidden = !open;
1132
+ };
1133
+ function sectionBody(sec) {
1134
+ const have = sections.get(sec);
1135
+ if (have) return have.body;
1136
+ const id = `pal-sec-${sections.size}-${Math.random().toString(36).slice(2, 8)}`;
1137
+ const head = h("button", { type: "button", class: "pal-head", "aria-expanded": "false", "aria-controls": id, "data-section-head": sec }, icon("chevron-right", "pal-chev"), h("span", { class: "pal-head-label" }, sec));
1138
+ const body = h("div", { class: "pal-body", id, role: "group", "aria-label": sec, "data-section-body": sec });
1139
+ head.addEventListener("click", () => setOpen(sec, head.getAttribute("aria-expanded") !== "true"));
1140
+ palette.append(h("div", { class: "pal-section", "data-pal-section": sec }, head, body));
1141
+ sections.set(sec, { head, body });
1142
+ setOpen(sec, sec === openAtStart);
1143
+ return body;
1144
+ }
1145
+ const DOCK_KEY = "canvas-palette-dock";
1146
+ const dockPref = {};
1147
+ try {
1148
+ const v = JSON.parse(opts.store?.get(DOCK_KEY) ?? "{}");
1149
+ if (typeof v.w === "number" && Number.isFinite(v.w)) dockPref.w = v.w;
1150
+ if (v.collapsed === true) dockPref.collapsed = true;
1151
+ } catch {
1152
+ }
1153
+ const saveDock = () => {
1154
+ try {
1155
+ opts.store?.set(DOCK_KEY, JSON.stringify(dockPref));
1156
+ } catch {
1157
+ }
1158
+ };
1159
+ const DOCK_MIN = 160, DOCK_MAX = 440;
1160
+ const palHome = docked ? h("aside", { class: "pal-dock", "aria-label": "Palette" }) : palette;
1161
+ const palGrip = h("div", { class: "pal-grip", role: "separator", "aria-orientation": "vertical", "aria-label": "Resize the palette", tabindex: "0" });
1162
+ const search = h("input", { type: "search", class: "pal-search", placeholder: "Search", "aria-label": "Search the palette", autocomplete: "off", spellcheck: "false" });
1163
+ const none = h("p", { class: "pal-none", role: "status", hidden: "" });
1164
+ const collapseBtn = h("button", { type: "button", class: "btn sm quiet icon-only pal-collapse", "aria-expanded": "true", "aria-label": "Collapse the palette", title: "Collapse the palette" }, icon("chevron-left"));
1165
+ const visibleItems = () => [...palette.querySelectorAll(".pal-item")].filter((b) => !b.hidden && !b.closest("[hidden]"));
1166
+ let before;
1167
+ function filter() {
1168
+ const words = search.value.toLowerCase().split(/\s+/).filter(Boolean);
1169
+ if (words.length && !before) before = new Map([...sections].map(([k, g]) => [k, g.head.getAttribute("aria-expanded") === "true"]));
1170
+ let shown = 0;
1171
+ for (const b of palette.querySelectorAll(".pal-item")) {
1172
+ const item = ui.palette[Number(b.dataset.entry)];
1173
+ const hay = `${item.label} ${item.section ?? ""}`.toLowerCase();
1174
+ b.hidden = !words.every((w) => hay.includes(w));
1175
+ if (!b.hidden) shown++;
1176
+ }
1177
+ for (const [sec, g] of sections) {
1178
+ const any = [...g.body.querySelectorAll(".pal-item")].some((b) => !b.hidden);
1179
+ g.head.parentElement.hidden = words.length > 0 && !any;
1180
+ if (words.length) setOpen(sec, any);
1181
+ }
1182
+ if (!words.length && before) {
1183
+ for (const [sec, open] of before) setOpen(sec, open);
1184
+ before = void 0;
1185
+ }
1186
+ none.hidden = shown > 0;
1187
+ none.textContent = shown ? "" : `Nothing matches "${search.value.trim()}"`;
1188
+ }
1189
+ const setDockW = (w, keep = true) => {
1190
+ const v = Math.round(Math.min(DOCK_MAX, Math.max(DOCK_MIN, w)));
1191
+ host.style.setProperty("--pal-w", `${v}px`);
1192
+ palGrip.setAttribute("aria-valuenow", String(v));
1193
+ if (keep) {
1194
+ dockPref.w = v;
1195
+ saveDock();
1196
+ }
1197
+ };
1198
+ const setCollapsed = (c, keep = true) => {
1199
+ host.classList.toggle("pal-collapsed", c);
1200
+ collapseBtn.setAttribute("aria-expanded", String(!c));
1201
+ const say = c ? "Show the palette" : "Collapse the palette";
1202
+ collapseBtn.setAttribute("aria-label", say);
1203
+ collapseBtn.title = say;
1204
+ collapseBtn.replaceChildren(icon(c ? "chevron-right" : "chevron-left"));
1205
+ if (keep) {
1206
+ if (c) dockPref.collapsed = true;
1207
+ else delete dockPref.collapsed;
1208
+ saveDock();
1209
+ }
1210
+ };
1211
+ if (docked) {
1212
+ host.classList.add("pal-docked");
1213
+ palette.classList.add("pal-list");
1214
+ palHome.append(h("div", { class: "pal-dock-head" }, collapseBtn, h("label", { class: "pal-search-wrap" }, icon("search"), search)), palette, none);
1215
+ palGrip.setAttribute("aria-valuemin", String(DOCK_MIN));
1216
+ palGrip.setAttribute("aria-valuemax", String(DOCK_MAX));
1217
+ setDockW(dockPref.w ?? 220, false);
1218
+ setCollapsed(!!dockPref.collapsed, false);
1219
+ collapseBtn.addEventListener("click", () => {
1220
+ setCollapsed(!host.classList.contains("pal-collapsed"));
1221
+ if (!host.classList.contains("pal-collapsed")) search.focus();
1222
+ });
1223
+ search.addEventListener("input", filter);
1224
+ search.addEventListener("keydown", (e) => {
1225
+ if (e.key === "Escape" && search.value) {
1226
+ e.preventDefault();
1227
+ e.stopPropagation();
1228
+ search.value = "";
1229
+ filter();
1230
+ } else if (e.key === "ArrowDown") {
1231
+ const first = visibleItems()[0];
1232
+ if (first) {
1233
+ e.preventDefault();
1234
+ first.focus();
1235
+ }
1236
+ } else if (e.key === "Enter") {
1237
+ const first = visibleItems()[0];
1238
+ if (first) {
1239
+ e.preventDefault();
1240
+ first.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
1241
+ }
1242
+ }
1243
+ });
1244
+ palette.addEventListener("keydown", (e) => {
1245
+ if (e.key !== "ArrowDown" && e.key !== "ArrowUp") return;
1246
+ const all = [...palette.querySelectorAll(".pal-head, .pal-item")].filter((b) => !b.hidden && !b.parentElement?.closest("[hidden]") && (b.classList.contains("pal-head") || !b.closest(".pal-body[hidden]")));
1247
+ const i = all.indexOf(document.activeElement);
1248
+ if (i < 0) return;
1249
+ e.preventDefault();
1250
+ if (e.key === "ArrowUp" && i === 0) search.focus();
1251
+ else all[Math.min(all.length - 1, Math.max(0, i + (e.key === "ArrowDown" ? 1 : -1)))].focus();
1252
+ });
1253
+ palGrip.addEventListener("pointerdown", (e) => {
1254
+ if (e.button !== 0) return;
1255
+ try {
1256
+ palGrip.setPointerCapture(e.pointerId);
1257
+ } catch {
1258
+ }
1259
+ const x0 = e.clientX, w0 = palHome.getBoundingClientRect().width;
1260
+ const mv = (ev) => setDockW(w0 + ev.clientX - x0, false);
1261
+ const up2 = (ev) => {
1262
+ palGrip.removeEventListener("pointermove", mv);
1263
+ palGrip.removeEventListener("pointerup", up2);
1264
+ setDockW(w0 + ev.clientX - x0);
1265
+ };
1266
+ palGrip.addEventListener("pointermove", mv);
1267
+ palGrip.addEventListener("pointerup", up2);
1268
+ });
1269
+ palGrip.addEventListener("keydown", (e) => {
1270
+ const d = e.key === "ArrowLeft" ? -16 : e.key === "ArrowRight" ? 16 : 0;
1271
+ if (d) {
1272
+ e.preventDefault();
1273
+ setDockW(palHome.getBoundingClientRect().width + d);
1274
+ }
1275
+ });
1276
+ }
1277
+ const usedSection = (item) => {
1278
+ if (item.section === void 0 || item.section === lastSection) return;
1279
+ lastSection = item.section;
1280
+ try {
1281
+ opts.store?.set(PAL_KEY, JSON.stringify({ last: lastSection }));
1282
+ } catch {
1283
+ }
1284
+ };
1115
1285
  ui.palette.forEach((item, idx) => {
1116
1286
  const { kind: sec, icon: ic, label } = item;
1117
1287
  const b = h("button", { type: "button", class: "pal-item", "data-section": sec, "data-entry": String(idx), title: `Add ${label.toLowerCase()} (click), or drag it where it goes` }, icon(ic), h("span", { class: "pal-label" }, label));
@@ -1126,7 +1296,7 @@ function canvasView(host, doc, opts) {
1126
1296
  let ghost;
1127
1297
  const over = (ev) => {
1128
1298
  const r = stage.getBoundingClientRect();
1129
- return ev.clientX >= r.left && ev.clientX <= r.right && ev.clientY >= r.top && ev.clientY <= r.bottom && !palette.contains(document.elementFromPoint(ev.clientX, ev.clientY));
1299
+ return ev.clientX >= r.left && ev.clientX <= r.right && ev.clientY >= r.top && ev.clientY <= r.bottom && !palHome.contains(document.elementFromPoint(ev.clientX, ev.clientY));
1130
1300
  };
1131
1301
  const mv = (ev) => {
1132
1302
  if (!ghost && Math.hypot(ev.clientX - x0, ev.clientY - y0) < 4) return;
@@ -1147,8 +1317,13 @@ function canvasView(host, doc, opts) {
1147
1317
  const dragged = !!ghost;
1148
1318
  ghost?.remove();
1149
1319
  if (!drop) return;
1150
- if (!dragged) addFromPalette(sec, void 0, item);
1151
- else if (over(ev)) addFromPalette(sec, { x: ev.clientX, y: ev.clientY }, item);
1320
+ if (!dragged) {
1321
+ usedSection(item);
1322
+ addFromPalette(sec, void 0, item);
1323
+ } else if (over(ev)) {
1324
+ usedSection(item);
1325
+ addFromPalette(sec, { x: ev.clientX, y: ev.clientY }, item);
1326
+ }
1152
1327
  };
1153
1328
  const upH = (ev) => done(ev, true), cancelH = (ev) => done(ev, false);
1154
1329
  const esc = (ev) => {
@@ -1166,12 +1341,14 @@ function canvasView(host, doc, opts) {
1166
1341
  b.addEventListener("keydown", (e) => {
1167
1342
  if (e.key === "Enter" || e.key === " ") {
1168
1343
  e.preventDefault();
1344
+ usedSection(item);
1169
1345
  addFromPalette(sec, void 0, item);
1170
1346
  }
1171
1347
  });
1172
- palette.append(b);
1348
+ (item.section === void 0 ? palette : sectionBody(item.section)).append(b);
1173
1349
  });
1174
- host.append(palette);
1350
+ if (docked) host.prepend(palHome, palGrip);
1351
+ else host.append(palette);
1175
1352
  function resetLayout() {
1176
1353
  const key2 = K.pins.keyOf(doc.goodDoc.data);
1177
1354
  const x = doc.goodDoc.data?.[key2];
@@ -1269,7 +1446,7 @@ function canvasView(host, doc, opts) {
1269
1446
  if (e.key === "F10" && !e.shiftKey) return;
1270
1447
  {
1271
1448
  const b = unionBox(sel);
1272
- const r = host.getBoundingClientRect();
1449
+ const r = viewEl.getBoundingClientRect();
1273
1450
  const k = scale();
1274
1451
  openMenu(menuItems(sel.length ? toPointer(sel[0]) : void 0), b ? { x: r.left + (b.x + b.w - V.view.x) * k, y: r.top + (b.y - V.view.y) * k } : { x: r.left + 40, y: r.top + 40 }, "Canvas");
1275
1452
  }
@@ -1390,7 +1567,7 @@ function canvasView(host, doc, opts) {
1390
1567
  boxFor,
1391
1568
  pageXYOf: (p) => {
1392
1569
  const b = boxFor(p);
1393
- const r = host.getBoundingClientRect();
1570
+ const r = viewEl.getBoundingClientRect();
1394
1571
  const k = scale();
1395
1572
  return b && V.view ? { x: r.left + (b.x + b.w / 2 - V.view.x) * k, y: r.top + (b.y + b.h / 2 - V.view.y) * k } : void 0;
1396
1573
  }
@@ -2,6 +2,7 @@ const PLAIN_ICON_SVG = {
2
2
  "arrow-up-right": '<path d="M7 7h10v10" /> <path d="M7 17 17 7" />',
3
3
  "box": '<path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z" /> <path d="m3.3 7 8.7 5 8.7-5" /> <path d="M12 22V12" />',
4
4
  "chevron-down": '<path d="m6 9 6 6 6-6" />',
5
+ "chevron-left": '<path d="m15 18-6-6 6-6" />',
5
6
  "chevron-right": '<path d="m9 18 6-6-6-6" />',
6
7
  "circle-dot": '<circle cx="12" cy="12" r="1" /> <circle cx="12" cy="12" r="10" />',
7
8
  "circle-x": '<circle cx="12" cy="12" r="10" /> <path d="m15 9-6 6" /> <path d="m9 9 6 6" />',
@@ -17,6 +18,7 @@ const PLAIN_ICON_SVG = {
17
18
  "pencil": '<path d="M21.174 6.812a1 1 0 0 0-3.986-3.987L3.842 16.174a2 2 0 0 0-.5.83l-1.321 4.352a.5.5 0 0 0 .623.622l4.353-1.32a2 2 0 0 0 .83-.497z" /> <path d="m15 5 4 4" />',
18
19
  "plus": '<path d="M5 12h14" /> <path d="M12 5v14" />',
19
20
  "rotate-ccw": '<path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8" /> <path d="M3 3v5h5" />',
21
+ "search": '<path d="m21 21-4.34-4.34" /> <circle cx="11" cy="11" r="8" />',
20
22
  "trash-2": '<path d="M10 11v6" /> <path d="M14 11v6" /> <path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6" /> <path d="M3 6h18" /> <path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" />',
21
23
  "triangle-alert": '<path d="m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3" /> <path d="M12 9v4" /> <path d="M12 17h.01" />',
22
24
  "x": '<path d="M18 6 6 18" /> <path d="m6 6 12 12" />'
@@ -76,7 +76,8 @@ function setupFor(a, v, textPane) {
76
76
  rootClass: "iso-canvas",
77
77
  panKeyClass: "pan-key",
78
78
  newOrder: a.kinds.map((k) => k.kind),
79
- palette: v.palette ? v.palette.map((e) => ({ kind: e.kind, label: e.label, icon: e.icon ?? icon(e.kind), name: e.name, ...e.value === void 0 ? {} : { value: () => structuredClone(e.value) } })) : a.kinds.map((k) => ({ kind: k.kind, icon: icon(k.kind), label: k.singular[0].toUpperCase() + k.singular.slice(1) })),
79
+ palette: v.palette ? v.palette.map((e) => ({ kind: e.kind, label: e.label, icon: e.icon ?? icon(e.kind), name: e.name, ...e.section === void 0 ? {} : { section: e.section }, ...e.value === void 0 ? {} : { value: () => structuredClone(e.value) } })) : a.kinds.map((k) => ({ kind: k.kind, icon: icon(k.kind), label: k.singular[0].toUpperCase() + k.singular.slice(1) })),
80
+ ...v.paletteLayout === void 0 ? {} : { paletteLayout: v.paletteLayout },
80
81
  text: {
81
82
  stage: "The file, drawn: arrow keys and Tab move through what is drawn; + and \u2212 zoom, 0 fits",
82
83
  dragGroup: "A drag on nothing",
@@ -5,7 +5,8 @@ import { placeBoard } from "./flat.js";
5
5
  function boardOf(scene, visible = () => true) {
6
6
  const items = scene.layout.items.filter((p) => visible(p.id));
7
7
  const cat = scene.layout.catalog;
8
- const isCard = (p) => p.type === "node" && !p.children.some((c) => c.type === "node" && visible(c.id));
8
+ const bare = !scene.layout.items.some((p) => p.type === "node");
9
+ const isCard = (p) => bare ? !p.children.some((c) => visible(c.id)) : p.type === "node" && !p.children.some((c) => c.type === "node" && visible(c.id));
9
10
  const cardsP = items.filter(isCard);
10
11
  const line = (p) => {
11
12
  const a = [];
@@ -57,6 +58,10 @@ function boardOf(scene, visible = () => true) {
57
58
  } else taken.set(String(c), p.id);
58
59
  }
59
60
  const cards = cardsP.map((p) => {
61
+ if (p.region) {
62
+ const r = p.region;
63
+ return { id: p.id, label: r.label ?? p.id, sub: void 0, role: r.role ?? regionKind(cat, r.kind)?.role ?? "grey", ghost: r.ghost || void 0, cell: cell.get(p.id) };
64
+ }
60
65
  const n = p.node, a = nodeKindOr(cat, n.asset);
61
66
  return { id: p.id, label: n.label ?? p.id, sub: n.sub, role: n.role ?? a.role, ghost: n.ghost || void 0, cell: cell.get(p.id) };
62
67
  });
@@ -160,8 +160,9 @@ function drawFlatRegion(k, cat, r, vis, gridStep = 1) {
160
160
  const style = R["label-style"] ?? rk?.labelStyle;
161
161
  if (R.label && style !== "none") {
162
162
  if (style === "face") {
163
- const big = shape === "slab";
164
- const z = big ? b.z0 + 0.28 : b.z0 + Math.min(0.1, (b.z1 - b.z0) * 0.25);
163
+ const h = b.z1 - b.z0, cap = (f) => f.size * 0.73 / S, thin = shape === "slab" && 0.28 + cap(FONTS.slabname) + 0.08 > h;
164
+ const big = shape === "slab" && !thin;
165
+ const z = big ? b.z0 + 0.28 : thin ? b.z0 + Math.max(0.02, (h - cap(FONTS.sm)) / 2) : b.z0 + Math.min(0.1, h * 0.25);
165
166
  s += tag("g", attrs, k.faceText(b.x0 + (big ? 0.6 : 0.3), b.y1, z, R.label, big ? "slabname" : "slabname sm", big ? FONTS.slabname : FONTS.sm, "left", "start"));
166
167
  } else if (style === "face-right") {
167
168
  s += tag("g", attrs, k.faceText(b.x1, b.y0 + 0.6, b.z0 + Math.max(0.14, (b.z1 - b.z0) * 0.2), R.label, "slabname", FONTS.slabname, "right", "end"));
@@ -16,7 +16,7 @@ import { memorySource } from "./timeline-engine.js";
16
16
  import { modeFor, activityOf, mountTimeline } from "./history.js";
17
17
  import { flatSvg, flatCss, measureIn, rectOf } from "./flat-view.js";
18
18
  import { dualView, storedMode, viewToggle, DUAL_VIEW_CSS } from "./dual-view.js";
19
- import { morphSvgs, markConnectors } from "./morph.js";
19
+ import { morphSvgs, markConnectors, buildLayer, ease, clamp } from "./morph.js";
20
20
  export {
21
21
  APPEARS,
22
22
  BASE_FIELDS,
@@ -35,10 +35,13 @@ export {
35
35
  attr,
36
36
  binNext,
37
37
  binStart,
38
+ buildLayer,
38
39
  cardMotion,
40
+ clamp,
39
41
  cls,
40
42
  crumbs,
41
43
  dualView,
44
+ ease,
42
45
  escapeName,
43
46
  extent,
44
47
  findGaps,