@marver-design/marver 0.27.2 → 0.29.0
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/CHANGELOG.md +160 -0
- package/README.md +6 -4
- package/dist/{anchor-check-gkxkPAIv.mjs → anchor-check-DhlpKl2K.mjs} +1 -1
- package/dist/{audio-Dw1lfMdb.mjs → audio-DX4POHAj.mjs} +39 -4
- package/dist/{boards-BqUqKNW8.mjs → boards-BeWtjGUM.mjs} +49 -4
- package/dist/boards-EH2onUXM.mjs +995 -0
- package/dist/{build-VgNg4rr8.mjs → build-CWbrb2bl.mjs} +9 -5
- package/dist/{capture-vadKjuCo.mjs → capture-DEtM4qm5.mjs} +259 -34
- package/dist/cli.mjs +40 -19
- package/dist/{comments-YEU1Zhv9.mjs → comments-XNhMmSSu.mjs} +4 -4
- package/dist/config-BezZTiov.mjs +182 -0
- package/dist/conn-BMiHS42T.mjs +222 -0
- package/dist/connectors-DekQvKt9.mjs +292 -0
- package/dist/{context-Ccjrhr1J.mjs → context-BUnFiyA3.mjs} +2 -2
- package/dist/{daemon-Dc9AH0M3.mjs → daemon-Sc_z8gT6.mjs} +2209 -205
- package/dist/{dev-ri6nAlRp.mjs → dev-DWBKQhxf.mjs} +31 -15
- package/dist/{init-W0nxR1OY.mjs → init-CYlFaRzG.mjs} +7 -2
- package/dist/jam-Cj5gBvwD.mjs +373 -0
- package/dist/{link-CdTgn94Y.mjs → link-BFFRgrMt.mjs} +3 -3
- package/dist/listener-DdLEM79A.mjs +80 -0
- package/dist/logo-CXY-1yrn.mjs +305 -0
- package/dist/{ls-BZ5TAtd5.mjs → ls-NqmePKOf.mjs} +1 -1
- package/dist/{manifest-aFyDmL8C.mjs → manifest-DPZ3WNg_.mjs} +1 -1
- package/dist/{marks-CnCCfAbD.mjs → marks-BnNANic9.mjs} +22 -3
- package/dist/{marks-BF9pNEgH.mjs → marks-DBp3h1Mc.mjs} +3 -6
- package/dist/{marver-id-gate-D-R0hSt_.mjs → marver-id-gate-vBLb_Icb.mjs} +1 -1
- package/dist/{perf-DkAUFT6W.mjs → perf-Denssrko.mjs} +45 -27
- package/dist/{perf-jam-BaH3Lg0m.mjs → perf-jam-CLkERSHo.mjs} +9 -2
- package/dist/permit-DDa6wqOs.mjs +187 -0
- package/dist/{plugin-B8UghYuF.mjs → plugin-DqoZB-D6.mjs} +849 -789
- package/dist/{poster-BNgafWMK.mjs → poster-CrsjqhY4.mjs} +1 -1
- package/dist/{profile-jIS6BcAs.mjs → profile-B1FKOM1e.mjs} +1 -1
- package/dist/{publish-bakes-38Nja78p.mjs → publish-bakes-DW-NDgU2.mjs} +3 -3
- package/dist/{queue-CAM_vNmX.mjs → queue-DeSQm24g.mjs} +189 -35
- package/dist/{recover-B8AiI29B.mjs → recover-GtJ9UDAB.mjs} +2 -2
- package/dist/{serve-Dp3nHstu.mjs → serve-DQv3Ti6X.mjs} +8 -4
- package/dist/{session-BAaJsK5c.mjs → session-VDJfXEEm.mjs} +1 -1
- package/dist/{share-Ce4tP7ji.mjs → share-Bu_kuOKL.mjs} +1 -1
- package/dist/{shot-CMQgMVZm.mjs → shot-DBWN8hPP.mjs} +1 -1
- package/dist/{shot-Ch24tiMO.mjs → shot-T04Ixrvq.mjs} +7 -6
- package/dist/sizes-wjXDev3b.mjs +139 -0
- package/dist/{slots-CFNnegCc.mjs → slots-Dn4Onb0V.mjs} +101 -3
- package/dist/speech-worker.mjs +23 -3
- package/dist/{sync-CQdGARJ9.mjs → sync-CMozOwZ0.mjs} +1 -1
- package/dist/transport-C5FnY9ni.mjs +203 -0
- package/dist/{work-Bxz-ObF3.mjs → work-BYWD_1Zf.mjs} +3 -3
- package/dist/{work-CNhFF9pU.mjs → work-DJxH7cCX.mjs} +1 -1
- package/docs/boards-and-folders.md +11 -0
- package/docs/jam-session.md +52 -18
- package/docs/live-jam.md +95 -10
- package/docs/performance.md +1 -1
- package/docs/publish.md +7 -0
- package/package.json +1 -1
- package/src/client/shell/Comments.tsx +5 -4
- package/src/client/shell/ContextMenu.tsx +5 -4
- package/src/client/shell/JamSurface.tsx +135 -29
- package/src/client/shell/Play.tsx +16 -4
- package/src/client/shell/api.ts +7 -3
- package/src/client/shell/app-bridge.ts +11 -3
- package/src/client/shell/comments-store.ts +14 -0
- package/src/client/shell/goto.ts +4 -3
- package/src/client/shell/icons.tsx +1 -0
- package/src/client/shell/jam-capture.ts +136 -37
- package/src/client/shell/jam-keys.ts +170 -18
- package/src/client/shell/jam-lane.tsx +14 -5
- package/src/client/shell/jam-lines.ts +76 -10
- package/src/client/shell/jam-panel.tsx +58 -4
- package/src/client/shell/jam-queue.ts +55 -8
- package/src/client/shell/jam-session.tsx +55 -6
- package/src/client/shell/jam-state.ts +19 -4
- package/src/client/shell/jam-surface-parts.tsx +6 -3
- package/src/client/shell/jam-ui.tsx +10 -1
- package/src/client/shell/jam-voice-wait.ts +21 -0
- package/src/client/shell/jam-voice.ts +14 -5
- package/src/client/shell/live-picture.ts +134 -0
- package/src/client/shell/open-item.ts +83 -0
- package/src/client/shell/open-resolve.ts +50 -0
- package/src/client/shell/store.ts +6 -2
- package/src/client/shell/styles.css +29 -11
- package/src/client/shell/tidy.ts +3 -2
- package/src/client/stage/main.tsx +30 -8
- package/src/shared/app-api.ts +173 -3
- package/src/shared/app-perf.ts +4 -2
- package/src/shared/approval.ts +20 -0
- package/src/shared/events.ts +1 -1
- package/templates/AGENTS-embedded.md +28 -1
- package/templates/AGENTS-studio.md +28 -1
- package/templates/instructions/boards.md +7 -1
- package/templates/instructions/jam.md +94 -0
- package/dist/boards-BHKP5yhI.mjs +0 -342
- package/dist/live-Cgzsicwi.mjs +0 -4
|
@@ -0,0 +1,995 @@
|
|
|
1
|
+
import { A as resolveType, D as readReason, E as readCapability, O as readStatusWord, a as buildTree, c as folderMap, d as isBoardFile, f as isBoardName, g as readTitle, h as readDescription, i as archiveLast, k as readType, l as framesOn, m as parseFolders, o as flatten, r as FOLDERS_FILE } from "./board-tree-BXR8dw4b.mjs";
|
|
2
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, realpathSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { join, sep } from "node:path";
|
|
4
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
5
|
+
//#region src/server/hash.ts
|
|
6
|
+
/** sha256 hex of a string - the CAS token for board files, the registry, the manifest. */
|
|
7
|
+
const hash = (s) => createHash("sha256").update(s).digest("hex");
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/client/shell/tidy.ts
|
|
10
|
+
const isSpace = (x) => !!x && typeof x === "object" && !Array.isArray(x);
|
|
11
|
+
/** Drop the items `isGone` names, each with the spacer that led into it (or, at the start, the
|
|
12
|
+
* one that followed it) - what is left reads as if the item was never there. (`boards place` takes a frame out of a
|
|
13
|
+
* scene recipe the same way.) */
|
|
14
|
+
function dropGone(items, isGone) {
|
|
15
|
+
const out = [];
|
|
16
|
+
let skip = false;
|
|
17
|
+
for (const it of items) {
|
|
18
|
+
if (isGone(it)) {
|
|
19
|
+
if (isSpace(out[out.length - 1])) out.pop();
|
|
20
|
+
else if (!out.length) skip = true;
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
if (skip && isSpace(it)) {
|
|
24
|
+
skip = false;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
skip = false;
|
|
28
|
+
out.push(it);
|
|
29
|
+
}
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
const units = (s, warn) => Number.isInteger(s.space) && s.space > 0 ? s.space : (warn(`invalid space ${JSON.stringify(s.space)} - using 1`), 1);
|
|
33
|
+
const frameGapX = (w) => Math.max(140, w * .12);
|
|
34
|
+
const frameGapY = (h) => Math.max(96, h * .16);
|
|
35
|
+
const sceneGapX = (w) => Math.max(280, w * .2);
|
|
36
|
+
const sceneGapY = (h) => Math.max(96, h * .16);
|
|
37
|
+
/** `h` is the part's extent (card, or its note column when that runs further); `ch` the card's own
|
|
38
|
+
* height, the characteristic size gutters are measured from - a long note never inflates them. */
|
|
39
|
+
const box = (id, parts) => ({
|
|
40
|
+
id,
|
|
41
|
+
parts: parts.map(({ key, dx, dy }) => ({
|
|
42
|
+
key,
|
|
43
|
+
dx,
|
|
44
|
+
dy
|
|
45
|
+
})),
|
|
46
|
+
w: Math.max(0, ...parts.map((p) => p.dx + p.w)),
|
|
47
|
+
h: Math.max(0, ...parts.map((p) => p.dy + p.h)),
|
|
48
|
+
charW: Math.max(0, ...parts.map((p) => p.w)),
|
|
49
|
+
charH: Math.max(0, ...parts.map((p) => p.ch ?? p.h))
|
|
50
|
+
});
|
|
51
|
+
const extent = (n) => Math.max(n.h, n.noteH ?? 0);
|
|
52
|
+
/** A run of nodes laid side by side (a frame's instances, or a variant run). */
|
|
53
|
+
const runBox = (id, run) => {
|
|
54
|
+
const parts = [];
|
|
55
|
+
let dx = 0;
|
|
56
|
+
for (const n of run) {
|
|
57
|
+
dx += n.noteW ?? 0;
|
|
58
|
+
parts.push({
|
|
59
|
+
key: n.key,
|
|
60
|
+
dx,
|
|
61
|
+
dy: 0,
|
|
62
|
+
w: n.w,
|
|
63
|
+
h: extent(n),
|
|
64
|
+
ch: n.h
|
|
65
|
+
});
|
|
66
|
+
dx += n.w + frameGapX(n.w);
|
|
67
|
+
}
|
|
68
|
+
return box(id, parts);
|
|
69
|
+
};
|
|
70
|
+
/**
|
|
71
|
+
* The one flow engine (both scopes, both axes). Places boxes; returns absolute box
|
|
72
|
+
* origins. Lanes share an origin on the cross axis - that IS the alignment.
|
|
73
|
+
* Two phases: COLLECT lanes (resolving atoms, so skipped content never strands a
|
|
74
|
+
* track), then PLACE knowing both neighbors of every boundary.
|
|
75
|
+
*/
|
|
76
|
+
function layoutFlow(flow, resolve, trailing, trailingMode, gapX, gapY, warn) {
|
|
77
|
+
const vertical = !!flow.columns;
|
|
78
|
+
const gapMain = vertical ? gapY : gapX;
|
|
79
|
+
const gapCross = vertical ? gapX : gapY;
|
|
80
|
+
const entries = (vertical ? flow.columns : flow.rows) ?? [];
|
|
81
|
+
const out = /* @__PURE__ */ new Map();
|
|
82
|
+
const seen = /* @__PURE__ */ new Set();
|
|
83
|
+
const lanes = [];
|
|
84
|
+
let pendingLane = 0;
|
|
85
|
+
for (const entry of entries) {
|
|
86
|
+
if (isSpace(entry)) {
|
|
87
|
+
if (pendingLane) {
|
|
88
|
+
warn("consecutive lane spacers - degrading to one ordinary gap");
|
|
89
|
+
pendingLane = -1;
|
|
90
|
+
} else pendingLane = units(entry, warn);
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (!Array.isArray(entry)) continue;
|
|
94
|
+
const items = [];
|
|
95
|
+
let pending = 0;
|
|
96
|
+
let hasBox = false;
|
|
97
|
+
for (const a of entry) {
|
|
98
|
+
if (isSpace(a)) {
|
|
99
|
+
if (!hasBox) {
|
|
100
|
+
warn("leading spacer in lane ignored");
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (pending) {
|
|
104
|
+
warn("consecutive spacers - degrading to one ordinary gap");
|
|
105
|
+
pending = -1;
|
|
106
|
+
items[items.length - 1] = -1;
|
|
107
|
+
} else {
|
|
108
|
+
pending = units(a, warn);
|
|
109
|
+
items.push(pending);
|
|
110
|
+
}
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
if (typeof a !== "string") {
|
|
114
|
+
warn(`ignoring non-string atom ${JSON.stringify(a)}`);
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (seen.has(a)) {
|
|
118
|
+
warn(`"${a}" listed twice - first occurrence wins`);
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const b = resolve(a);
|
|
122
|
+
if (!b) continue;
|
|
123
|
+
seen.add(a);
|
|
124
|
+
items.push(b);
|
|
125
|
+
hasBox = true;
|
|
126
|
+
pending = 0;
|
|
127
|
+
}
|
|
128
|
+
if (pending) {
|
|
129
|
+
warn("trailing spacer in lane ignored");
|
|
130
|
+
items.pop();
|
|
131
|
+
}
|
|
132
|
+
if (!hasBox) {
|
|
133
|
+
if (entry.length) warn("lane with no placeable content dropped");
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (lanes.length === 0 && pendingLane) warn("leading lane spacer ignored");
|
|
137
|
+
lanes.push({
|
|
138
|
+
beforeUnits: lanes.length === 0 ? 0 : pendingLane === -1 ? 1 : Math.max(1, pendingLane || 1),
|
|
139
|
+
items
|
|
140
|
+
});
|
|
141
|
+
pendingLane = 0;
|
|
142
|
+
}
|
|
143
|
+
if (pendingLane) warn("trailing lane spacer ignored");
|
|
144
|
+
const extra = trailing();
|
|
145
|
+
if (extra.length) {
|
|
146
|
+
if (trailingMode === "append") {
|
|
147
|
+
if (lanes.length) lanes[lanes.length - 1].items.push(...extra);
|
|
148
|
+
else lanes.push({
|
|
149
|
+
beforeUnits: 0,
|
|
150
|
+
items: [...extra]
|
|
151
|
+
});
|
|
152
|
+
} else for (const b of extra) lanes.push({
|
|
153
|
+
beforeUnits: lanes.length === 0 ? 0 : 1,
|
|
154
|
+
items: [b]
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const laneChar = (l) => Math.max(0, ...l.items.filter((i) => typeof i !== "number").map((b) => vertical ? b.charW : b.charH));
|
|
158
|
+
const laneExtent = (l) => Math.max(0, ...l.items.filter((i) => typeof i !== "number").map((b) => vertical ? b.w : b.h));
|
|
159
|
+
let cross = 0;
|
|
160
|
+
for (let i = 0; i < lanes.length; i++) {
|
|
161
|
+
const lane = lanes[i];
|
|
162
|
+
if (i > 0) {
|
|
163
|
+
const c = Math.max(laneChar(lanes[i - 1]), laneChar(lane), 1);
|
|
164
|
+
cross += laneExtent(lanes[i - 1]) + gapCross(c) * lane.beforeUnits;
|
|
165
|
+
}
|
|
166
|
+
let main = 0;
|
|
167
|
+
let pendingUnits = 0;
|
|
168
|
+
let prevChar = 0;
|
|
169
|
+
let placedAny = false;
|
|
170
|
+
for (const item of lane.items) {
|
|
171
|
+
if (typeof item === "number") {
|
|
172
|
+
pendingUnits = item;
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (placedAny) {
|
|
176
|
+
const n = pendingUnits === -1 ? 1 : Math.max(1, pendingUnits || 1);
|
|
177
|
+
main += gapMain(Math.max(prevChar, vertical ? item.charH : item.charW)) * n;
|
|
178
|
+
}
|
|
179
|
+
pendingUnits = 0;
|
|
180
|
+
out.set(item.id, {
|
|
181
|
+
x: vertical ? cross : main,
|
|
182
|
+
y: vertical ? main : cross
|
|
183
|
+
});
|
|
184
|
+
main += vertical ? item.h : item.w;
|
|
185
|
+
prevChar = vertical ? item.charH : item.charW;
|
|
186
|
+
placedAny = true;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
191
|
+
/** Scene placement order for the DEFAULT (recipe-less) flow: appearance order, but a
|
|
192
|
+
* variant group is one contiguous run (sorted by variant key) at its first member's slot. */
|
|
193
|
+
function orderWithinScene(members) {
|
|
194
|
+
const consumed = /* @__PURE__ */ new Set();
|
|
195
|
+
const ordered = [];
|
|
196
|
+
for (const n of members) {
|
|
197
|
+
if (consumed.has(n.key)) continue;
|
|
198
|
+
if (!n.group) {
|
|
199
|
+
ordered.push(n);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const run = members.filter((m) => m.group === n.group).sort((a, b) => (a.variant ?? "").localeCompare(b.variant ?? ""));
|
|
203
|
+
for (const m of run) {
|
|
204
|
+
ordered.push(m);
|
|
205
|
+
consumed.add(m.key);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return ordered;
|
|
209
|
+
}
|
|
210
|
+
/** Group leftover nodes into placement boxes: node order, variant runs contiguous
|
|
211
|
+
* and indivisible (P1: leftovers must honor the same contract as listed content). */
|
|
212
|
+
function leftoverBoxes(remaining) {
|
|
213
|
+
const out = [];
|
|
214
|
+
const consumed = /* @__PURE__ */ new Set();
|
|
215
|
+
for (const n of remaining) {
|
|
216
|
+
if (consumed.has(n.key)) continue;
|
|
217
|
+
if (!n.group) {
|
|
218
|
+
consumed.add(n.key);
|
|
219
|
+
out.push(runBox(`${n.frame}#${n.key}`, [n]));
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
const run = remaining.filter((m) => m.group === n.group).sort((a, b) => (a.variant ?? "").localeCompare(b.variant ?? ""));
|
|
223
|
+
for (const m of run) consumed.add(m.key);
|
|
224
|
+
out.push(runBox(`${n.group}#run`, run));
|
|
225
|
+
}
|
|
226
|
+
return out;
|
|
227
|
+
}
|
|
228
|
+
const rel = (id, scene) => id.startsWith(scene + "/") ? id.slice(scene.length + 1) : id;
|
|
229
|
+
/** Lay out ONE scene: recipe if present, else a single default lane. Returns member
|
|
230
|
+
* positions relative to the scene origin. */
|
|
231
|
+
function layoutScene(scene, members, flow, warn) {
|
|
232
|
+
const consumed = /* @__PURE__ */ new Set();
|
|
233
|
+
const boxIndex = /* @__PURE__ */ new Map();
|
|
234
|
+
const resolve = (atom) => {
|
|
235
|
+
const frames = members.filter((n) => rel(n.frame, scene) === atom);
|
|
236
|
+
const run = members.filter((n) => n.group && rel(n.group, scene) === atom);
|
|
237
|
+
if (frames.length && run.length) warn(`scene "${scene}": "${atom}" names a frame AND a variant group - the frame wins`);
|
|
238
|
+
const chosen = frames.length ? frames : run.sort((a, b) => (a.variant ?? "").localeCompare(b.variant ?? ""));
|
|
239
|
+
if (!chosen.length) {
|
|
240
|
+
warn(`unknown "${atom}" in scene "${scene}" layout - skipped`);
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
const fresh = chosen.filter((n) => !consumed.has(n.key));
|
|
244
|
+
if (!fresh.length) {
|
|
245
|
+
warn(`"${atom}" repeats already-placed content - skipped`);
|
|
246
|
+
return null;
|
|
247
|
+
}
|
|
248
|
+
if (!frames.length && fresh.length !== chosen.length) {
|
|
249
|
+
warn(`group "${atom}" already partially placed - skipped (remaining members append as unlisted)`);
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
for (const n of fresh) consumed.add(n.key);
|
|
253
|
+
const b = runBox(atom, fresh);
|
|
254
|
+
boxIndex.set(atom, b);
|
|
255
|
+
return b;
|
|
256
|
+
};
|
|
257
|
+
let flows;
|
|
258
|
+
if (flow && flow.rows && flow.columns) {
|
|
259
|
+
warn(`scene "${scene}": layout has rows AND columns - using the default lane`);
|
|
260
|
+
flows = { rows: [[...new Set(orderWithinScene(members).map((n) => rel(n.frame, scene)))]] };
|
|
261
|
+
} else if (flow && (flow.rows || flow.columns)) flows = flow;
|
|
262
|
+
else flows = { rows: [[...new Set(orderWithinScene(members).map((n) => rel(n.frame, scene)))]] };
|
|
263
|
+
const trailing = () => leftoverBoxes(members.filter((n) => !consumed.has(n.key)));
|
|
264
|
+
const placedBoxes = layoutFlow(flows, resolve, () => {
|
|
265
|
+
const boxes = trailing();
|
|
266
|
+
for (const b of boxes) boxIndex.set(b.id, b);
|
|
267
|
+
return boxes;
|
|
268
|
+
}, "append", frameGapX, frameGapY, warn);
|
|
269
|
+
const out = /* @__PURE__ */ new Map();
|
|
270
|
+
for (const [id, pos] of placedBoxes) {
|
|
271
|
+
const b = boxIndex.get(id);
|
|
272
|
+
if (!b) continue;
|
|
273
|
+
for (const p of b.parts) out.set(p.key, {
|
|
274
|
+
x: pos.x + p.dx,
|
|
275
|
+
y: pos.y + p.dy
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
return out;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Pure layout. Returns positions only - the nodes
|
|
282
|
+
* array is never reordered (iframe law G-1). Two passes: each scene lays out its
|
|
283
|
+
* frames (recipe or default lane), then the board flow places the scene boxes.
|
|
284
|
+
*/
|
|
285
|
+
/** `known`: every scene of the project. A layout naming a known scene with no node on this board
|
|
286
|
+
* (an empty phase scene, frames not pinned here) skips it quietly; only an unknown name warns (#18). */
|
|
287
|
+
function tidy(nodes, layout, warn = () => {}, known) {
|
|
288
|
+
const scenes = [...new Set(nodes.map((n) => n.scene))];
|
|
289
|
+
const sceneMaps = /* @__PURE__ */ new Map();
|
|
290
|
+
const sceneBoxes = /* @__PURE__ */ new Map();
|
|
291
|
+
for (const scene of scenes) {
|
|
292
|
+
const members = nodes.filter((n) => n.scene === scene);
|
|
293
|
+
const m = layoutScene(scene, members, layout?.scenes?.[scene], warn);
|
|
294
|
+
const sceneNoteW = Math.max(0, ...members.map((n) => n.sceneNoteW ?? 0));
|
|
295
|
+
if (sceneNoteW) {
|
|
296
|
+
const extra = sceneNoteW - (members.filter((n) => m.has(n.key)).reduce((best, n) => {
|
|
297
|
+
const p = m.get(n.key), b = best && m.get(best.key);
|
|
298
|
+
return !b || p.y < b.y || p.y === b.y && p.x < b.x ? n : best;
|
|
299
|
+
}, null)?.noteW ?? 0);
|
|
300
|
+
if (extra > 0) for (const [k, p] of m) m.set(k, {
|
|
301
|
+
x: p.x + extra,
|
|
302
|
+
y: p.y
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
sceneMaps.set(scene, m);
|
|
306
|
+
const parts = members.filter((n) => m.has(n.key)).map((n) => ({
|
|
307
|
+
key: n.key,
|
|
308
|
+
dx: m.get(n.key).x,
|
|
309
|
+
dy: m.get(n.key).y,
|
|
310
|
+
w: n.w,
|
|
311
|
+
h: extent(n),
|
|
312
|
+
ch: n.h
|
|
313
|
+
}));
|
|
314
|
+
sceneBoxes.set(scene, box(scene, parts));
|
|
315
|
+
}
|
|
316
|
+
let boardFlow;
|
|
317
|
+
if (layout && layout.rows && layout.columns) {
|
|
318
|
+
warn("layout has rows AND columns - ignoring it (plain tidy)");
|
|
319
|
+
boardFlow = { rows: [] };
|
|
320
|
+
} else if (layout && (layout.rows || layout.columns)) {
|
|
321
|
+
const gone = (a) => typeof a === "string" && !!known?.has(a) && !sceneBoxes.has(a);
|
|
322
|
+
const prune = (flow) => {
|
|
323
|
+
if (!flow || !known) return flow;
|
|
324
|
+
const emptied = /* @__PURE__ */ new Set();
|
|
325
|
+
return dropGone(flow.map((e) => {
|
|
326
|
+
if (!Array.isArray(e) || !e.some(gone)) return e;
|
|
327
|
+
const kept = dropGone(e, gone);
|
|
328
|
+
if (kept.every(isSpace)) emptied.add(kept);
|
|
329
|
+
return kept;
|
|
330
|
+
}), (e) => emptied.has(e));
|
|
331
|
+
};
|
|
332
|
+
boardFlow = {
|
|
333
|
+
rows: prune(layout.rows),
|
|
334
|
+
columns: prune(layout.columns)
|
|
335
|
+
};
|
|
336
|
+
} else boardFlow = { rows: [] };
|
|
337
|
+
const listed = /* @__PURE__ */ new Set();
|
|
338
|
+
for (const entry of [...boardFlow.rows ?? [], ...boardFlow.columns ?? []]) if (Array.isArray(entry)) {
|
|
339
|
+
for (const a of entry) if (typeof a === "string") listed.add(a);
|
|
340
|
+
}
|
|
341
|
+
const trailing = () => scenes.filter((s) => !listed.has(s)).sort().map((s) => sceneBoxes.get(s));
|
|
342
|
+
const placedScenes = layoutFlow(boardFlow, (atom) => {
|
|
343
|
+
const b = sceneBoxes.get(atom);
|
|
344
|
+
if (!b && !known?.has(atom)) warn(`unknown scene "${atom}" in layout - skipped`);
|
|
345
|
+
return b ?? null;
|
|
346
|
+
}, trailing, "lanes", sceneGapX, sceneGapY, warn);
|
|
347
|
+
const out = [];
|
|
348
|
+
for (const [scene, pos] of placedScenes) {
|
|
349
|
+
const m = sceneMaps.get(scene);
|
|
350
|
+
if (!m) continue;
|
|
351
|
+
for (const [key, p] of m) out.push({
|
|
352
|
+
key,
|
|
353
|
+
x: pos.x + p.x,
|
|
354
|
+
y: pos.y + p.y
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
return out;
|
|
358
|
+
}
|
|
359
|
+
/** Guarded parse of agent-authored board.layout. Malformed pieces warn
|
|
360
|
+
* and degrade - they never blank the board, and never vanish silently. */
|
|
361
|
+
function parseLayout(raw, warn) {
|
|
362
|
+
if (raw === void 0 || raw === null) return null;
|
|
363
|
+
if (typeof raw !== "object" || Array.isArray(raw)) {
|
|
364
|
+
warn("layout must be an object - ignored");
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
const o = raw;
|
|
368
|
+
const flow = parseFlow(o, warn);
|
|
369
|
+
const scenes = {};
|
|
370
|
+
if (o.scenes !== void 0) {
|
|
371
|
+
if (!o.scenes || typeof o.scenes !== "object" || Array.isArray(o.scenes)) warn("layout.scenes must be an object map - ignored");
|
|
372
|
+
else for (const [k, v] of Object.entries(o.scenes)) {
|
|
373
|
+
const f = v && typeof v === "object" && !Array.isArray(v) ? parseFlow(v, warn) : null;
|
|
374
|
+
if (f) scenes[k] = f;
|
|
375
|
+
else warn(`layout.scenes["${k}"] is not a rows/columns flow - ignored`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (!flow && !Object.keys(scenes).length) return null;
|
|
379
|
+
return {
|
|
380
|
+
...flow ?? {},
|
|
381
|
+
...Object.keys(scenes).length ? { scenes } : {}
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
function parseFlow(o, warn) {
|
|
385
|
+
const parseEntries = (v) => {
|
|
386
|
+
if (!Array.isArray(v)) {
|
|
387
|
+
if (v !== void 0) warn("layout rows/columns must be an array - ignored");
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
const out = [];
|
|
391
|
+
for (const e of v) if (Array.isArray(e)) out.push(e.filter((a) => {
|
|
392
|
+
const ok = typeof a === "string" || !!a && typeof a === "object" && !Array.isArray(a);
|
|
393
|
+
if (!ok) warn(`ignoring invalid layout atom ${JSON.stringify(a)}`);
|
|
394
|
+
return ok;
|
|
395
|
+
}));
|
|
396
|
+
else if (e && typeof e === "object" && !Array.isArray(e)) out.push(e);
|
|
397
|
+
else warn(`ignoring invalid layout entry ${JSON.stringify(e)}`);
|
|
398
|
+
return out;
|
|
399
|
+
};
|
|
400
|
+
const rows = parseEntries(o.rows);
|
|
401
|
+
const columns = parseEntries(o.columns);
|
|
402
|
+
if (!rows && !columns) return null;
|
|
403
|
+
return {
|
|
404
|
+
...rows ? { rows } : {},
|
|
405
|
+
...columns ? { columns } : {}
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
//#endregion
|
|
409
|
+
//#region src/server/boards.ts
|
|
410
|
+
/**
|
|
411
|
+
* Reading design/boards/ safely - the one enumerator the dev API, the build and the tests
|
|
412
|
+
* share. Only REGULAR files on the board-name grammar count as boards (a symlink, dangling or
|
|
413
|
+
* live, could read or publish JSON from outside the project - it is skipped, and reported so
|
|
414
|
+
* the build can fail closed). The folder registry is read the same way: absent = no folders,
|
|
415
|
+
* malformed = an error the human must fix, never a silently empty registry.
|
|
416
|
+
*/
|
|
417
|
+
/** Does realpath(dir) stay inside realpath(root)? A symlinked design/boards can't escape. */
|
|
418
|
+
function underRoot(root, dir) {
|
|
419
|
+
try {
|
|
420
|
+
const rr = realpathSync(root);
|
|
421
|
+
const rd = realpathSync(dir);
|
|
422
|
+
return rd === rr || rd.startsWith(rr + sep);
|
|
423
|
+
} catch {
|
|
424
|
+
return false;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
/** Is design/boards a directory we may read and write? It must not be a symlink at all (a
|
|
428
|
+
* link to the repo root would list package.json as a board and let a tree write rewrite
|
|
429
|
+
* it; a link outside would publish foreign JSON) and must resolve inside the root. Absent
|
|
430
|
+
* is fine (no boards yet). Returns the error, or null. */
|
|
431
|
+
function checkBoardsDir(root, boardsDir) {
|
|
432
|
+
return checkRealDirs(root, [[join(boardsDir, ".."), "design"], [boardsDir, "design/boards"]]);
|
|
433
|
+
}
|
|
434
|
+
/** Every EXISTING path in `dirs` (root-down order) must be a real directory inside the root -
|
|
435
|
+
* a symlinked `design` with no boards dir yet would otherwise be followed by the mkdir that
|
|
436
|
+
* creates it; a symlinked `design/scenes` would let a brief write land outside the project.
|
|
437
|
+
* An absent one ends the walk (nothing beneath it exists either). Returns the error, or null. */
|
|
438
|
+
function checkRealDirs(root, dirs) {
|
|
439
|
+
for (const [p, label] of dirs) {
|
|
440
|
+
try {
|
|
441
|
+
if (lstatSync(p).isSymbolicLink()) return `${label} must be a real directory, not a symlink`;
|
|
442
|
+
} catch {
|
|
443
|
+
return null;
|
|
444
|
+
}
|
|
445
|
+
if (!underRoot(root, p)) return `${label} escapes the project`;
|
|
446
|
+
}
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
/** A regular file (lstat: a symlink is never followed, a dangling one is not "absent"). */
|
|
450
|
+
const isRegularFile = (p) => {
|
|
451
|
+
try {
|
|
452
|
+
return lstatSync(p).isFile();
|
|
453
|
+
} catch {
|
|
454
|
+
return false;
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
/** Is there ANY node at p (a dangling symlink counts)? */
|
|
458
|
+
const nodeExists = (p) => {
|
|
459
|
+
try {
|
|
460
|
+
lstatSync(p);
|
|
461
|
+
return true;
|
|
462
|
+
} catch {
|
|
463
|
+
return false;
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
/** A board file's JSON, or why it is not JSON - the parser's own words, naming the line and column (#6). */
|
|
467
|
+
function parseBoard(content) {
|
|
468
|
+
try {
|
|
469
|
+
return { json: JSON.parse(content) };
|
|
470
|
+
} catch (e) {
|
|
471
|
+
const msg = e.message, lines = content.split("\n");
|
|
472
|
+
return {
|
|
473
|
+
json: null,
|
|
474
|
+
error: /end of JSON input$/.test(msg) ? `${msg} (line ${lines.length} column ${lines[lines.length - 1].length + 1})` : msg
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
/** Every board file: name, raw content, hash, and its JSON (null when malformed). `skipped`
|
|
479
|
+
* names the entries that looked like boards but were not regular files. */
|
|
480
|
+
function listBoardFiles(boardsDir) {
|
|
481
|
+
const boards = [], skipped = [];
|
|
482
|
+
if (!existsSync(boardsDir)) return {
|
|
483
|
+
boards,
|
|
484
|
+
skipped
|
|
485
|
+
};
|
|
486
|
+
for (const f of readdirSync(boardsDir)) {
|
|
487
|
+
if (!isBoardFile(f)) continue;
|
|
488
|
+
const file = join(boardsDir, f);
|
|
489
|
+
if (!isRegularFile(file)) {
|
|
490
|
+
skipped.push(f);
|
|
491
|
+
continue;
|
|
492
|
+
}
|
|
493
|
+
const content = readFileSync(file, "utf8");
|
|
494
|
+
boards.push({
|
|
495
|
+
name: f.slice(0, -5),
|
|
496
|
+
file,
|
|
497
|
+
content,
|
|
498
|
+
sha256: hash(content),
|
|
499
|
+
...parseBoard(content)
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
return {
|
|
503
|
+
boards,
|
|
504
|
+
skipped
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
function boardFields(json, validName) {
|
|
508
|
+
const o = json;
|
|
509
|
+
const title = readTitle(o?.title);
|
|
510
|
+
const description = readDescription(o?.description);
|
|
511
|
+
const type = readType(o?.type), capability = readCapability(o?.capability), status = readStatusWord(o?.status), reason = readReason(o?.reason);
|
|
512
|
+
return {
|
|
513
|
+
...typeof o?.order === "number" && Number.isFinite(o.order) ? { order: o.order } : {},
|
|
514
|
+
...validName(o?.folder) ? { folder: o.folder } : {},
|
|
515
|
+
...title ? { title } : {},
|
|
516
|
+
...description ? { description } : {},
|
|
517
|
+
...type ? { type } : {},
|
|
518
|
+
...capability ? { capability } : {},
|
|
519
|
+
...status ? { status } : {},
|
|
520
|
+
...reason ? { reason } : {}
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
/** The fields a board's file owns that the shell's save shape never carries: an autosave keeps
|
|
524
|
+
* each one from disk when the incoming board omits it (spec 20 - the fields survive every write). */
|
|
525
|
+
const AUTHOR_FIELDS = [
|
|
526
|
+
"order",
|
|
527
|
+
"folder",
|
|
528
|
+
"title",
|
|
529
|
+
"description",
|
|
530
|
+
"type",
|
|
531
|
+
"capability",
|
|
532
|
+
"status",
|
|
533
|
+
"reason"
|
|
534
|
+
];
|
|
535
|
+
/** The folder registry. `sha256` is the CAS token a tree write must echo (null = "there was
|
|
536
|
+
* no file"), so a write can never silently replace a registry it never saw. */
|
|
537
|
+
function readRegistry(boardsDir) {
|
|
538
|
+
const p = join(boardsDir, FOLDERS_FILE);
|
|
539
|
+
if (!nodeExists(p)) return {
|
|
540
|
+
state: "absent",
|
|
541
|
+
folders: [],
|
|
542
|
+
sha256: null
|
|
543
|
+
};
|
|
544
|
+
if (!isRegularFile(p)) return {
|
|
545
|
+
state: "malformed",
|
|
546
|
+
error: `design/boards/${FOLDERS_FILE} must be a regular file, not a symlink`,
|
|
547
|
+
sha256: null
|
|
548
|
+
};
|
|
549
|
+
const content = readFileSync(p, "utf8");
|
|
550
|
+
let raw;
|
|
551
|
+
try {
|
|
552
|
+
raw = JSON.parse(content);
|
|
553
|
+
} catch {
|
|
554
|
+
return {
|
|
555
|
+
state: "malformed",
|
|
556
|
+
error: `design/boards/${FOLDERS_FILE} is not valid JSON - fix the file`,
|
|
557
|
+
sha256: hash(content)
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
const parsed = parseFolders(raw);
|
|
561
|
+
if (typeof parsed === "string") return {
|
|
562
|
+
state: "malformed",
|
|
563
|
+
error: `design/boards/${FOLDERS_FILE}: ${parsed}`,
|
|
564
|
+
sha256: hash(content)
|
|
565
|
+
};
|
|
566
|
+
return {
|
|
567
|
+
state: "ok",
|
|
568
|
+
folders: parsed,
|
|
569
|
+
sha256: hash(content)
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
/** The registry's write lock - one writer at a time across processes (the dev server's tree write,
|
|
573
|
+
* `folders add`, `init --kind`), so a read-modify-write of `_folders.json` is atomic among Marver's
|
|
574
|
+
* writers. A lock older than 10 s is a crashed writer's and is taken over. `wait` = how long to
|
|
575
|
+
* try (the CLI waits; the dev server never blocks its event loop - it answers 409 instead). */
|
|
576
|
+
const REGISTRY_LOCK = ".folders.lock";
|
|
577
|
+
function withRegistryLock(dir, wait, fn) {
|
|
578
|
+
return withFileLock(join(dir, REGISTRY_LOCK), wait, fn);
|
|
579
|
+
}
|
|
580
|
+
/** One writer at a time across processes, by a lock file made with `wx`; one older than 10 s is a crashed writer's
|
|
581
|
+
* and is taken over. Waits (blocking - the CLI's way) up to `wait` ms; null when it could not get it. */
|
|
582
|
+
function withFileLock(lock, wait, fn) {
|
|
583
|
+
const t0 = Date.now();
|
|
584
|
+
for (;;) try {
|
|
585
|
+
writeFileSync(lock, `${process.pid} ${Date.now()}\n`, { flag: "wx" });
|
|
586
|
+
break;
|
|
587
|
+
} catch (e) {
|
|
588
|
+
if (e.code !== "EEXIST") throw e;
|
|
589
|
+
try {
|
|
590
|
+
if (Date.now() - lstatSync(lock).mtimeMs > 1e4) {
|
|
591
|
+
rmSync(lock, { force: true });
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
} catch {
|
|
595
|
+
continue;
|
|
596
|
+
}
|
|
597
|
+
if (Date.now() - t0 >= wait) return null;
|
|
598
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 25);
|
|
599
|
+
}
|
|
600
|
+
try {
|
|
601
|
+
return fn();
|
|
602
|
+
} finally {
|
|
603
|
+
rmSync(lock, { force: true });
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
/** The same lock, waited for without blocking (the dev server never stalls its event loop): the release, or null. */
|
|
607
|
+
async function takeFileLock(lock, wait) {
|
|
608
|
+
const t0 = Date.now();
|
|
609
|
+
for (;;) try {
|
|
610
|
+
writeFileSync(lock, `${process.pid} ${Date.now()}\n`, { flag: "wx" });
|
|
611
|
+
return () => rmSync(lock, { force: true });
|
|
612
|
+
} catch (e) {
|
|
613
|
+
if (e.code !== "EEXIST") throw e;
|
|
614
|
+
try {
|
|
615
|
+
if (Date.now() - lstatSync(lock).mtimeMs > 1e4) {
|
|
616
|
+
rmSync(lock, { force: true });
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
} catch {
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
if (Date.now() - t0 >= wait) return null;
|
|
623
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
/** The same lock, taken now or not at all - never waits, never blocks: the release, or null while another writer holds it. */
|
|
627
|
+
function tryFileLock(lock) {
|
|
628
|
+
for (;;) try {
|
|
629
|
+
writeFileSync(lock, `${process.pid} ${Date.now()}\n`, { flag: "wx" });
|
|
630
|
+
return () => rmSync(lock, { force: true });
|
|
631
|
+
} catch (e) {
|
|
632
|
+
if (e.code !== "EEXIST") throw e;
|
|
633
|
+
try {
|
|
634
|
+
if (Date.now() - lstatSync(lock).mtimeMs > 1e4) {
|
|
635
|
+
rmSync(lock, { force: true });
|
|
636
|
+
continue;
|
|
637
|
+
}
|
|
638
|
+
} catch {
|
|
639
|
+
continue;
|
|
640
|
+
}
|
|
641
|
+
return null;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
/** A board file's write lock (F1, Codex review 7): every board writer Marver has takes it - `boards place`, `boards pin`
|
|
645
|
+
* (CLI and the sidebar's route), the canvas's save, and the sidebar's rename, status and drag (Codex review of 1e1fa0f:
|
|
646
|
+
* a rename that read the board before a placement committed wrote the placed frame away) - so a read-modify-write of
|
|
647
|
+
* one board is atomic among them. A file, not a route: a spawned run's sandbox blocks localhost. `.<board>.lock` is no
|
|
648
|
+
* board (not `*.json`). */
|
|
649
|
+
const boardLock = (dir, board) => join(dir, `.${board}.lock`);
|
|
650
|
+
/** Append typed folders to the registry (spec 20: `init --kind`, `folders add`). Never renames,
|
|
651
|
+
* moves or retypes a folder that exists - a name already registered is skipped and reported - and
|
|
652
|
+
* never drops a field of an existing entry it does not manage. New folders rank after everything
|
|
653
|
+
* already at the root. The write is compare-and-swap: the registry is re-read just before the
|
|
654
|
+
* atomic rename, and a change since the first read (the shell's drag, another agent) starts the
|
|
655
|
+
* append over from the new file. A malformed registry is an error, never overwritten. */
|
|
656
|
+
function addFolders(root, folders) {
|
|
657
|
+
const dir = join(root, "design", "boards");
|
|
658
|
+
const de = checkBoardsDir(root, dir);
|
|
659
|
+
if (de) throw new Error(de);
|
|
660
|
+
mkdirSync(dir, { recursive: true });
|
|
661
|
+
const file = join(dir, FOLDERS_FILE);
|
|
662
|
+
const done = withRegistryLock(dir, 5e3, () => appendFolders(dir, file, folders));
|
|
663
|
+
if (!done) throw new Error(`design/boards/${FOLDERS_FILE} is being written by another process - try again`);
|
|
664
|
+
return done;
|
|
665
|
+
}
|
|
666
|
+
function appendFolders(dir, file, folders) {
|
|
667
|
+
for (let attempt = 0; attempt < 5; attempt++) {
|
|
668
|
+
const before = nodeExists(file) ? readFileSync(file, "utf8") : null;
|
|
669
|
+
const reg = readRegistry(dir);
|
|
670
|
+
if (reg.state === "malformed") throw new Error(reg.error);
|
|
671
|
+
let raw = [];
|
|
672
|
+
if (before !== null) {
|
|
673
|
+
const j = JSON.parse(before);
|
|
674
|
+
raw = Array.isArray(j.folders) ? j.folders : [];
|
|
675
|
+
}
|
|
676
|
+
const have = new Set(raw.map((f) => f.name));
|
|
677
|
+
const rootRanks = [...raw.filter((f) => f.parent === void 0).map((f) => f.order), ...listBoardFiles(dir).boards.map((b) => {
|
|
678
|
+
const o = b.json;
|
|
679
|
+
return o?.folder ? void 0 : o?.order;
|
|
680
|
+
})].filter((o) => typeof o === "number" && Number.isFinite(o));
|
|
681
|
+
let next = rootRanks.length ? Math.max(...rootRanks) + 1 : 0;
|
|
682
|
+
const added = [], existing = [];
|
|
683
|
+
const rows = [...raw];
|
|
684
|
+
for (const f of folders) {
|
|
685
|
+
if (have.has(f.name)) {
|
|
686
|
+
existing.push(f.name);
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
rows.push({
|
|
690
|
+
name: f.name,
|
|
691
|
+
order: next++,
|
|
692
|
+
...f.title ? { title: f.title } : {},
|
|
693
|
+
...f.type ? { type: f.type } : {}
|
|
694
|
+
});
|
|
695
|
+
have.add(f.name);
|
|
696
|
+
added.push(f.name);
|
|
697
|
+
}
|
|
698
|
+
if (!added.length) return {
|
|
699
|
+
added,
|
|
700
|
+
existing
|
|
701
|
+
};
|
|
702
|
+
const version = rows.some((f) => f.parent !== void 0) ? 2 : 1;
|
|
703
|
+
const tmp = `${file}.${process.pid}.${Date.now()}.${attempt}.tmp`;
|
|
704
|
+
writeFileSync(tmp, JSON.stringify({
|
|
705
|
+
version,
|
|
706
|
+
folders: rows
|
|
707
|
+
}, null, 2) + "\n", { flag: "wx" });
|
|
708
|
+
if ((nodeExists(file) ? readFileSync(file, "utf8") : null) !== before) {
|
|
709
|
+
rmSync(tmp, { force: true });
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
712
|
+
renameSync(tmp, file);
|
|
713
|
+
return {
|
|
714
|
+
added,
|
|
715
|
+
existing
|
|
716
|
+
};
|
|
717
|
+
}
|
|
718
|
+
throw new Error(`design/boards/${FOLDERS_FILE} kept changing while folders were added - try again`);
|
|
719
|
+
}
|
|
720
|
+
function readMembership(root, live) {
|
|
721
|
+
const dir = join(root, "design", "boards");
|
|
722
|
+
const files = checkBoardsDir(root, dir) ? [] : listBoardFiles(dir).boards;
|
|
723
|
+
const reg = readRegistry(dir);
|
|
724
|
+
const folders = reg.state === "ok" ? reg.folders : [];
|
|
725
|
+
const rows = files.map((b) => ({
|
|
726
|
+
name: b.name,
|
|
727
|
+
...boardFields(b.json, isBoardName)
|
|
728
|
+
}));
|
|
729
|
+
const tree = buildTree(rows, folders);
|
|
730
|
+
const fm = folderMap(tree);
|
|
731
|
+
const byName = new Map(folders.map((f) => [f.name, f]));
|
|
732
|
+
const isArchive = (n) => {
|
|
733
|
+
const folder = byName.get(fm.get(n) ?? "");
|
|
734
|
+
return n === "archive" || resolveType(rows.find((r) => r.name === n)?.type, folder?.type, folder?.parent ? byName.get(folder.parent)?.type : void 0) === "archive";
|
|
735
|
+
};
|
|
736
|
+
const boards = archiveLast(flatten(tree), isArchive).map((name) => ({
|
|
737
|
+
name,
|
|
738
|
+
frames: framesOn(files.find((b) => b.name === name)?.json, live)
|
|
739
|
+
}));
|
|
740
|
+
const on = new Set(boards.flatMap((b) => b.frames));
|
|
741
|
+
return {
|
|
742
|
+
boards,
|
|
743
|
+
unplaced: live.filter((f) => !on.has(f))
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
/** What to say about frames on no board, one line each: the boards holding its scene's other frames,
|
|
747
|
+
* and the command that pins it - what `work done`, `marver link` and `comments new` print. */
|
|
748
|
+
function unplacedHint(m, frames) {
|
|
749
|
+
return frames.map((f) => {
|
|
750
|
+
const scene = f.split("/")[0];
|
|
751
|
+
const homes = m.boards.filter((b) => b.frames.some((x) => x !== f && x.split("/")[0] === scene)).map((b) => b.name);
|
|
752
|
+
return `${f} is on no board${homes.length ? ` (its scene's other frames are on ${homes.join(", ")})` : ""} - pin it where it belongs: npx marver boards pin ${homes[0] ?? "<board>"} ${f}`;
|
|
753
|
+
});
|
|
754
|
+
}
|
|
755
|
+
/** `boards pin`: append frames to a board as nodes - the shell sizes and lays them out, as for any
|
|
756
|
+
* node an agent appends. A frame the board already shows is skipped (every frame, on an `auto`
|
|
757
|
+
* board). Never creates a board; a malformed one is the human's to fix. */
|
|
758
|
+
function pinFrames(root, board, frames, live) {
|
|
759
|
+
if (!isBoardName(board) || board === "all-scenes") throw new Error(`"${board}" is not a board name - lowercase letters, numbers and dashes`);
|
|
760
|
+
const dir = join(root, "design", "boards");
|
|
761
|
+
const de = checkBoardsDir(root, dir);
|
|
762
|
+
if (de) throw new Error(de);
|
|
763
|
+
const file = join(dir, `${board}.json`);
|
|
764
|
+
if (!isRegularFile(file)) throw new Error(`no board "${board}" in design/boards/ - make it first (npx marver boards new ${board})`);
|
|
765
|
+
const out = withFileLock(boardLock(dir, board), 5e3, () => pinLocked(file, board, frames, live));
|
|
766
|
+
if (!out) throw new Error(`design/boards/${board}.json is being written by another writer - try again`);
|
|
767
|
+
return out;
|
|
768
|
+
}
|
|
769
|
+
function pinLocked(file, board, frames, live) {
|
|
770
|
+
const content = readFileSync(file, "utf8");
|
|
771
|
+
const { json, error } = parseBoard(content);
|
|
772
|
+
if (error || !json || typeof json !== "object" || Array.isArray(json)) throw new Error(`design/boards/${board}.json is not a board object - fix the file${error ? ` (${error})` : ""}`);
|
|
773
|
+
const shown = new Set(framesOn(json, live));
|
|
774
|
+
const already = frames.filter((f) => shown.has(f));
|
|
775
|
+
const added = [...new Set(frames.filter((f) => !shown.has(f)))];
|
|
776
|
+
if (!added.length) return {
|
|
777
|
+
added,
|
|
778
|
+
already
|
|
779
|
+
};
|
|
780
|
+
const obj = json;
|
|
781
|
+
obj.nodes = [...Array.isArray(obj.nodes) ? obj.nodes : [], ...added.map((frame) => ({ frame }))];
|
|
782
|
+
const tmp = `${file}.${randomBytes(6).toString("hex")}.tmp`;
|
|
783
|
+
writeFileSync(tmp, JSON.stringify(obj, null, 2) + "\n", { flag: "wx" });
|
|
784
|
+
if (readFileSync(file, "utf8") !== content) {
|
|
785
|
+
rmSync(tmp, { force: true });
|
|
786
|
+
throw new Error(`design/boards/${board}.json changed while pinning - try again`);
|
|
787
|
+
}
|
|
788
|
+
renameSync(tmp, file);
|
|
789
|
+
return {
|
|
790
|
+
added,
|
|
791
|
+
already
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
/** A frame's size on the canvas when its node does not say (the shell's defaultSize, store.ts): a slide's stage, else a
|
|
795
|
+
* content frame's width (the declared viewport wins) with its committed height (`_sizes.json`), else its viewport,
|
|
796
|
+
* else the mobile one. */
|
|
797
|
+
function sizeOf(f, viewports, heights) {
|
|
798
|
+
if (!f) return {
|
|
799
|
+
w: 390,
|
|
800
|
+
h: 844,
|
|
801
|
+
auto: false
|
|
802
|
+
};
|
|
803
|
+
if (f.slide) {
|
|
804
|
+
const vp = f.viewport ? viewports[f.viewport] : void 0;
|
|
805
|
+
return {
|
|
806
|
+
w: vp?.width ?? 1280,
|
|
807
|
+
h: vp?.height ?? 720,
|
|
808
|
+
auto: false
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
if (f.contentWidth) {
|
|
812
|
+
const vp = viewports[f.viewport ?? ""];
|
|
813
|
+
const w = vp?.width ?? f.contentWidth;
|
|
814
|
+
return {
|
|
815
|
+
w,
|
|
816
|
+
h: heights[`${f.id}@${w}`] ?? vp?.height ?? Math.round(w * .75),
|
|
817
|
+
auto: true
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
const vp = viewports[f.viewport ?? ""] ?? viewports.mobile ?? {
|
|
821
|
+
width: 390,
|
|
822
|
+
height: 844
|
|
823
|
+
};
|
|
824
|
+
return {
|
|
825
|
+
w: vp.width,
|
|
826
|
+
h: vp.height,
|
|
827
|
+
auto: false
|
|
828
|
+
};
|
|
829
|
+
}
|
|
830
|
+
/** How the canvas lays a board out (store.ts): `composed` - it lays it out itself, re-running its layout whenever a Doc
|
|
831
|
+
* measures, a note lands or the window resizes (a layout recipe, scene rows, or the auto board); `flow` - the recipe it
|
|
832
|
+
* runs (the legacy scene rows as plain rows). */
|
|
833
|
+
function boardLayoutOf(b) {
|
|
834
|
+
const layout = parseLayout(b.layout, () => {});
|
|
835
|
+
const rows = (Array.isArray(b.sceneRows) ? b.sceneRows : []).map((r) => Array.isArray(r) ? r.filter((x) => typeof x === "string") : []).filter((r) => r.length);
|
|
836
|
+
const flow = layout ?? (rows.length ? { rows } : void 0);
|
|
837
|
+
return {
|
|
838
|
+
composed: !!flow || b.auto === true,
|
|
839
|
+
...flow ? { flow } : {}
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
/** A scene's own recipe (`layout.scenes[scene]`) names its frames: the frame's name goes right next to the anchor's
|
|
843
|
+
* there, out of wherever it was (with the spacer that led into it, as the canvas drops a name it cannot show). An
|
|
844
|
+
* anchor the recipe does not name leaves the frame unnamed too: both follow the recipe in list order. */
|
|
845
|
+
function placeInRecipe(layout, scene, frame, anchor, rel) {
|
|
846
|
+
if (!layout || typeof layout !== "object" || Array.isArray(layout)) return;
|
|
847
|
+
const scenes = layout.scenes;
|
|
848
|
+
const flow = scenes && typeof scenes === "object" && !Array.isArray(scenes) ? scenes[scene] : void 0;
|
|
849
|
+
if (!flow || typeof flow !== "object" || Array.isArray(flow)) return;
|
|
850
|
+
const axis = Array.isArray(flow.rows) && !Array.isArray(flow.columns) ? "rows" : Array.isArray(flow.columns) && !Array.isArray(flow.rows) ? "columns" : null;
|
|
851
|
+
if (!axis) return;
|
|
852
|
+
const name = (id) => id.startsWith(`${scene}/`) ? id.slice(scene.length + 1) : id;
|
|
853
|
+
const f = name(frame), a = name(anchor);
|
|
854
|
+
const isLane = (e) => Array.isArray(e);
|
|
855
|
+
const src = flow[axis];
|
|
856
|
+
const lanes = src.map((e) => isLane(e) && e.includes(f) ? dropGone(e, (x) => x === f) : e);
|
|
857
|
+
const kept = dropGone(lanes, (e) => e !== src[lanes.indexOf(e)] && isLane(e) && !e.some((x) => typeof x === "string"));
|
|
858
|
+
const home = kept.find((e) => isLane(e) && e.includes(a));
|
|
859
|
+
if (home) home.splice(home.indexOf(a) + (rel === "after" ? 1 : 0), 0, f);
|
|
860
|
+
flow[axis] = kept;
|
|
861
|
+
}
|
|
862
|
+
/** `boards place <board> <frame> --after|--before <anchor>` (F1): put a frame right after (or before) another in its
|
|
863
|
+
* row, moving the frames after it right to make room - so it also plays right after it. Under the board's write lock,
|
|
864
|
+
* over the file as read. Its node is added, or moved when the frame is already on the board; a content (Doc) node is
|
|
865
|
+
* written with no size, as the canvas saves it. Its node also goes next to the anchor's in the board's list, and its
|
|
866
|
+
* name next to the anchor's in the scene's recipe when it has one: a board the canvas lays out itself (a recipe, scene
|
|
867
|
+
* rows, the auto board) re-runs its layout whenever a Doc measures, and lays a scene out in that order - so the
|
|
868
|
+
* placement survives it (Codex's review: it put the variant back at the end). Checked with the canvas's own layout
|
|
869
|
+
* before it is written: a placement it would undo (a frame of another scene, a variant group or a recipe that keeps
|
|
870
|
+
* its own order) is refused with why, never written. Refused, never guessed: the anchor not on the board, either
|
|
871
|
+
* frame on it more than once, a board in a device view. Asked again once it is there: nothing is written. A board not
|
|
872
|
+
* laid out yet (the anchor has no position) gets no positions - the canvas lays it out when it opens it. `layout`
|
|
873
|
+
* (but for the scene recipe), `sceneRows` and `laidOut` are kept as they are. */
|
|
874
|
+
function placeFrame(root, board, frame, rel, anchor, frames, viewports = {}, heights = {}) {
|
|
875
|
+
if (!isBoardName(board) || board === "all-scenes") throw new Error(`"${board}" is not a board name - lowercase letters, numbers and dashes`);
|
|
876
|
+
if (frame === anchor) throw new Error("name two different frames: the one to place, and the one it goes next to");
|
|
877
|
+
const dir = join(root, "design", "boards");
|
|
878
|
+
const de = checkBoardsDir(root, dir);
|
|
879
|
+
if (de) throw new Error(de);
|
|
880
|
+
const file = join(dir, `${board}.json`);
|
|
881
|
+
if (!isRegularFile(file)) throw new Error(`no board "${board}" in design/boards/ - make it first (npx marver boards new ${board})`);
|
|
882
|
+
const out = withFileLock(boardLock(dir, board), 5e3, () => {
|
|
883
|
+
const content = readFileSync(file, "utf8");
|
|
884
|
+
const { json, error } = parseBoard(content);
|
|
885
|
+
if (error || !json || typeof json !== "object" || Array.isArray(json)) throw new Error(`design/boards/${board}.json is not a board object - fix the file${error ? ` (${error})` : ""}`);
|
|
886
|
+
const b = json;
|
|
887
|
+
if (typeof b.deviceView === "string") throw new Error(`${board} is in a device view (${b.deviceView}) - switch it back to Default on the canvas, then place the frame`);
|
|
888
|
+
const nodes = (Array.isArray(b.nodes) ? b.nodes : []).filter((n) => !!n && typeof n.frame === "string");
|
|
889
|
+
const all = Array.isArray(b.nodes) ? b.nodes : [];
|
|
890
|
+
const anchors = nodes.filter((n) => n.frame === anchor);
|
|
891
|
+
if (!anchors.length) throw new Error(`${anchor} is not on ${board} - put it there first: npx marver boards pin ${board} ${anchor}`);
|
|
892
|
+
if (anchors.length > 1) throw new Error(`${anchor} is on ${board} more than once - place ${frame} by hand on the canvas`);
|
|
893
|
+
const mine = nodes.filter((n) => n.frame === frame);
|
|
894
|
+
if (mine.length > 1) throw new Error(`${frame} is on ${board} more than once - move it by hand on the canvas`);
|
|
895
|
+
const A = anchors[0], M = mine[0];
|
|
896
|
+
const num = (v) => typeof v === "number" && Number.isFinite(v);
|
|
897
|
+
const info = (id) => frames.find((f) => f.id === id);
|
|
898
|
+
const sceneOf = (id) => info(id)?.scene ?? id.split("/")[0];
|
|
899
|
+
const was = JSON.stringify(b);
|
|
900
|
+
const size = (n) => {
|
|
901
|
+
const d = sizeOf(info(n.frame), viewports, heights);
|
|
902
|
+
return {
|
|
903
|
+
w: num(n.w) ? n.w : d.w,
|
|
904
|
+
h: num(n.h) ? n.h : d.h
|
|
905
|
+
};
|
|
906
|
+
};
|
|
907
|
+
const laidOut = num(A.x) && num(A.y);
|
|
908
|
+
const node = M ?? { frame };
|
|
909
|
+
let shifted = 0;
|
|
910
|
+
if (laidOut) {
|
|
911
|
+
const a = size(A);
|
|
912
|
+
const f = M ? size(M) : sizeOf(info(frame), viewports, heights);
|
|
913
|
+
const gap = Math.max(140, Math.max(a.w, f.w) * .12);
|
|
914
|
+
const inRow = (n) => num(n.x) && num(n.y) && n.y < A.y + a.h && n.y + size(n).h > A.y;
|
|
915
|
+
const row = nodes.filter((n) => n !== M && inRow(n));
|
|
916
|
+
if (!(!!M && inRow(M) && (() => {
|
|
917
|
+
const order = [...row, M].sort((p, q) => p.x - q.x);
|
|
918
|
+
return order.indexOf(M) === order.indexOf(A) + (rel === "after" ? 1 : -1);
|
|
919
|
+
})())) {
|
|
920
|
+
const X = rel === "after" ? A.x + a.w + gap : A.x;
|
|
921
|
+
const room = f.w + gap;
|
|
922
|
+
for (const n of row) if (rel === "after" ? n.x > A.x : n.x >= A.x) {
|
|
923
|
+
n.x = Math.round(n.x + room);
|
|
924
|
+
shifted++;
|
|
925
|
+
}
|
|
926
|
+
node.x = Math.round(X);
|
|
927
|
+
node.y = Math.round(A.y);
|
|
928
|
+
if (!M) {
|
|
929
|
+
if (!sizeOf(info(frame), viewports, heights).auto) {
|
|
930
|
+
node.w = Math.round(f.w);
|
|
931
|
+
node.h = Math.round(f.h);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
const rest = all.filter((n) => n !== M);
|
|
937
|
+
rest.splice(rest.indexOf(A) + (rel === "after" ? 1 : 0), 0, node);
|
|
938
|
+
b.nodes = rest;
|
|
939
|
+
if (sceneOf(frame) === sceneOf(anchor)) placeInRecipe(b.layout, sceneOf(anchor), frame, anchor, rel);
|
|
940
|
+
const { composed, flow } = boardLayoutOf(b);
|
|
941
|
+
if (composed || !laidOut) {
|
|
942
|
+
const placed = rest.filter((n) => !!n && typeof n.frame === "string");
|
|
943
|
+
const tn = placed.map((n, i) => {
|
|
944
|
+
const fi = info(n.frame);
|
|
945
|
+
const s = size(n);
|
|
946
|
+
return {
|
|
947
|
+
key: String(i),
|
|
948
|
+
frame: n.frame,
|
|
949
|
+
scene: sceneOf(n.frame),
|
|
950
|
+
group: fi?.variantGroup,
|
|
951
|
+
variant: fi?.variant,
|
|
952
|
+
w: s.w,
|
|
953
|
+
h: s.h
|
|
954
|
+
};
|
|
955
|
+
});
|
|
956
|
+
const at = new Map(tidy(tn, flow).map((p) => [p.key, p]));
|
|
957
|
+
const pf = at.get(String(placed.indexOf(node))), pa = at.get(String(placed.indexOf(A)));
|
|
958
|
+
if (!(!!pf && !!pa && pf.y === pa.y && (rel === "after" ? pf.x > pa.x : pf.x < pa.x) && ![...at.values()].some((p) => p !== pf && p !== pa && p.y === pa.y && (p.x - pa.x) * (p.x - pf.x) < 0))) {
|
|
959
|
+
const how = composed ? `${board} lays its frames out itself` : `the canvas lays ${board} out when it opens it`;
|
|
960
|
+
if (sceneOf(frame) !== sceneOf(anchor)) throw new Error(`${how}, scene by scene - ${frame} (scene ${sceneOf(frame)}) cannot stay ${rel} ${anchor} (scene ${sceneOf(anchor)}): make the frame in ${sceneOf(anchor)}/ and place that, or place it by hand on the canvas`);
|
|
961
|
+
throw new Error(`${how}, and its layout keeps ${sceneOf(anchor)} in an order of its own (a variant group, or the scene's recipe) - ${frame} cannot stay ${rel} ${anchor}: place it by hand on the canvas`);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
const base = {
|
|
965
|
+
board,
|
|
966
|
+
frame,
|
|
967
|
+
anchor,
|
|
968
|
+
rel
|
|
969
|
+
};
|
|
970
|
+
if (JSON.stringify(b) === was) return {
|
|
971
|
+
...base,
|
|
972
|
+
already: true,
|
|
973
|
+
added: false,
|
|
974
|
+
shifted: 0,
|
|
975
|
+
laidOut
|
|
976
|
+
};
|
|
977
|
+
const tmp = `${file}.${randomBytes(6).toString("hex")}.tmp`;
|
|
978
|
+
writeFileSync(tmp, JSON.stringify(b, null, 2) + "\n", { flag: "wx" });
|
|
979
|
+
if (readFileSync(file, "utf8") !== content) {
|
|
980
|
+
rmSync(tmp, { force: true });
|
|
981
|
+
throw new Error(`design/boards/${board}.json changed while placing - try again`);
|
|
982
|
+
}
|
|
983
|
+
renameSync(tmp, file);
|
|
984
|
+
return {
|
|
985
|
+
...base,
|
|
986
|
+
added: !M,
|
|
987
|
+
shifted,
|
|
988
|
+
laidOut
|
|
989
|
+
};
|
|
990
|
+
});
|
|
991
|
+
if (!out) throw new Error(`design/boards/${board}.json is being written by another writer - try again`);
|
|
992
|
+
return out;
|
|
993
|
+
}
|
|
994
|
+
//#endregion
|
|
995
|
+
export { unplacedHint as _, checkBoardsDir as a, listBoardFiles as c, pinFrames as d, placeFrame as f, tryFileLock as g, takeFileLock as h, boardLock as i, nodeExists as l, readRegistry as m, addFolders as n, checkRealDirs as o, readMembership as p, boardFields as r, isRegularFile as s, AUTHOR_FIELDS as t, parseBoard as u, withRegistryLock as v, hash as y };
|