fablecut 1.18.0 → 1.19.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/server.js CHANGED
@@ -46,6 +46,7 @@ const Templates = require("./templates");
46
46
  const { probeFile } = require("./media-probe");
47
47
  const MediaLib = require("./media-lib");
48
48
  const { createProxies } = require("./proxy");
49
+ const { createMediaFiles, canonicalSrc } = require("./media-files");
49
50
  const { createTranscripts } = require("./transcripts");
50
51
  const Delivery = require("./delivery");
51
52
  const { createProjects } = require("./projects");
@@ -127,6 +128,100 @@ function broadcast(event = "change", data = event) {
127
128
  const proxies = createProxies({ dir: PROXIES_DIR, resolveSrc, probe: probeFile, broadcast: (ev, data) => broadcast(ev, data) });
128
129
  /* Word transcripts, one per version of a file (fablecut_transcribe, the Transcript tab). */
129
130
  const transcripts = createTranscripts({ dir: path.join(ANALYSIS_DIR, "transcripts"), resolveSrc });
131
+ const mediaFiles = createMediaFiles({ mediaDir: MEDIA_DIR });
132
+ /* A /media/… src → its absolute path (whether or not the file is there now). */
133
+ function srcFile(src) {
134
+ if (!src || !String(src).startsWith("/media/")) return null;
135
+ let rel; try { rel = decodeURIComponent(String(src).slice(7)); } catch { return null; }
136
+ const f = path.normalize(path.join(MEDIA_DIR, rel));
137
+ return f.startsWith(path.normalize(MEDIA_DIR + path.sep)) ? f : null;
138
+ }
139
+ const srcKey = canonicalSrc;
140
+ /* Every project's document: the open one (./project.json) and the parked ones. A known
141
+ project whose document can't be read is listed in `unreadable`, so nobody takes its
142
+ silence for "uses nothing". */
143
+ function projectDocs() {
144
+ const docs = [], unreadable = [];
145
+ const active = projects.peekActive();
146
+ // `text` is the file as read: a write planned from it checks the file is still that
147
+ const read = (file) => { const text = fs.readFileSync(file, "utf8"); return { text, doc: JSON.parse(text.replace(/^/, "")) }; };
148
+ try { docs.push({ id: active, file: PROJECT_FILE, open: true, ...read(PROJECT_FILE) }); }
149
+ catch { unreadable.push("the open project"); }
150
+ let ids = [];
151
+ // no projects folder yet is the first-run state; any other failure means projects weren't checked
152
+ try { ids = fs.readdirSync(projects.DIR); } catch (e) { if (e.code !== "ENOENT") unreadable.push("the projects folder"); }
153
+ for (const id of ids) {
154
+ if (id === active || !fs.existsSync(path.join(projects.DIR, id, "meta.json"))) continue;
155
+ const file = path.join(projects.DIR, id, "project.json");
156
+ try { docs.push({ id, file, open: false, ...read(file) }); }
157
+ catch { unreadable.push(`project ${id}`); }
158
+ }
159
+ return { docs, unreadable };
160
+ }
161
+ /* Media entries (in any project) that use these /media/… srcs, with how many clips place each. */
162
+ function mediaUses(docs, srcs) {
163
+ const want = new Set(srcs.map(srcKey));
164
+ const out = [];
165
+ for (const d of docs) {
166
+ const text = JSON.stringify(d.doc);
167
+ for (const m of d.doc.media || []) {
168
+ if (!want.has(srcKey(m.src))) continue;
169
+ const clips = text.split(`"mediaId":${JSON.stringify(m.id)}`).length - 1;
170
+ out.push({ project: d.doc.name || d.id, open: d.open, media: m.id, name: m.name, src: m.src, clips });
171
+ }
172
+ }
173
+ return out;
174
+ }
175
+ /* Point media entries at files that moved ([{from, to}] srcs); a name that was the old file
176
+ name follows it. All or nothing: every new document is staged first, each project must
177
+ still be the one the plan was made from (else CONFLICT), and if one can't be put in place
178
+ the ones already replaced go back (atomically too). The error is thrown so the caller can
179
+ undo the move; `e.rollbackFailed` names projects that couldn't be put back. */
180
+ function repointMedia(moved, { force = false } = {}) {
181
+ const map = new Map(moved.filter((m) => m.from && m.to).map((m) => [srcKey(m.from), m.to]));
182
+ const plan = [];
183
+ const scan = projectDocs();
184
+ // a project that couldn't be read now (it could before) would keep the old place
185
+ if (scan.unreadable.length && !force) throw new Error(`can't read ${scan.unreadable.join(", ")}`);
186
+ for (const d of scan.docs) {
187
+ let n = 0;
188
+ for (const m of d.doc.media || []) {
189
+ const to = map.get(srcKey(m.src));
190
+ if (!to) continue;
191
+ if (m.name === path.basename(srcKey(m.src))) m.name = path.basename(srcKey(to));
192
+ m.src = to; n++;
193
+ }
194
+ if (n) plan.push({ d, n });
195
+ }
196
+ const staged = [], written = [];
197
+ const drop = () => { for (const s of staged) { try { fs.rmSync(s.tmp, { force: true }); } catch { } } };
198
+ try {
199
+ for (const { d } of plan) {
200
+ if (d.open) d.doc.revision = (d.doc.revision || 0) + 1;
201
+ const tmp = d.file + ".files.tmp";
202
+ fs.writeFileSync(tmp, JSON.stringify(d.doc, null, 2));
203
+ staged.push({ d, tmp });
204
+ }
205
+ } catch (e) { drop(); throw e; }
206
+ try {
207
+ for (const s of staged) {
208
+ // an edit saved since the plan was read would be lost: refuse instead
209
+ if (fs.readFileSync(s.d.file, "utf8") !== s.d.text) throw new Error(`CONFLICT — ${s.d.open ? "the open project" : `project "${s.d.doc.name || s.d.id}"`} changed meanwhile; try again`);
210
+ fs.renameSync(s.tmp, s.d.file);
211
+ written.push(s.d);
212
+ }
213
+ } catch (e) {
214
+ drop();
215
+ const failed = [];
216
+ for (const d of written) {
217
+ try { const tmp = d.file + ".files.undo"; fs.writeFileSync(tmp, d.text); fs.renameSync(tmp, d.file); }
218
+ catch { failed.push(d.open ? "the open project" : `project "${d.doc.name || d.id}"`); }
219
+ }
220
+ if (failed.length) e.rollbackFailed = failed;
221
+ throw e;
222
+ }
223
+ return plan.map(({ d, n }) => ({ project: d.doc.name || d.id, open: d.open, media: n }));
224
+ }
130
225
  let debounce = null;
131
226
  let profilesDebounce = null;
132
227
  function onFsChange() {
@@ -173,7 +268,8 @@ function readBody(req) {
173
268
  /** Claim final + sibling `.part` paths with exclusive create (`wx`) so concurrent
174
269
  * exports cannot pick the same free name before either file exists. */
175
270
  function reserveExportPaths(dir, baseName, ext) {
176
- const base = baseName.replace(/\.(mp4|mov|m4v|mkv|webm)$/i, "");
271
+ // as the Export dialog does: no extension typed twice, no leading dot (a dotfile isn't served)
272
+ const base = baseName.replace(/\.(mp4|mov|m4v|mkv|webm|mxf)$/i, "").replace(/^[.\s]+/, "") || "export";
177
273
  let i = 0;
178
274
  for (;;) {
179
275
  const stem = i === 0 ? base : `${base}_${i}`;
@@ -791,6 +887,113 @@ const server = http.createServer(async (req, res) => {
791
887
  return;
792
888
  }
793
889
 
890
+ /* API: files in media/ (media-files.js) — folders, moves and a trash; nothing is deleted.
891
+ POST /api/files {action:"list", dir?} → {dir, folders, files:[{name, src, size}]}
892
+ {action:"mkdir", dir} → {dir}
893
+ {action:"move", from, to} → {from, to, moved:[{from, to}], repointed:[{project, open, media}]}
894
+ {action:"trash", paths, force?} → {trashed:[{id, src}], offline:[…uses]}
895
+ (409 while a project uses one or can't be read to check — unless force:true)
896
+ {action:"trash-list"} → {items:[{id, src, kind, size, trashed}]}
897
+ {action:"restore", id} → {restored:{id, src}}
898
+ A move repoints every project's media entries at the new place (the open one gets a new
899
+ revision, so editor tabs reload) and takes the file's transcript with it; if a project
900
+ can't be written the move is undone. Every change also goes out as SSE "change". */
901
+ if (p === "/api/files" && req.method === "POST") {
902
+ try {
903
+ const opts = JSON.parse((await readBody(req)).toString("utf8") || "{}");
904
+ const a = opts.action;
905
+ if (a === "list") { sendJSON(res, 200, mediaFiles.list(opts.dir)); return; }
906
+ if (a === "mkdir") { sendJSON(res, 200, mediaFiles.mkdir(opts.dir)); broadcast("change"); return; }
907
+ if (a === "trash-list") { sendJSON(res, 200, { items: mediaFiles.trashList() }); return; }
908
+ if (a === "move") {
909
+ if (opts.force !== undefined && typeof opts.force !== "boolean") { sendJSON(res, 400, { error: "force is true or false" }); return; }
910
+ // a project that can't be read can't be repointed: it would keep the old place
911
+ const { unreadable } = projectDocs();
912
+ if (unreadable.length && opts.force !== true) {
913
+ sendJSON(res, 409, { error: `can't check ${unreadable.join(", ")} (can't be read) — fix it first, or pass force:true to move anyway (the file goes offline there)`, unreadable });
914
+ return;
915
+ }
916
+ let r, partial = null;
917
+ try { r = mediaFiles.move(opts.from, opts.to); }
918
+ catch (e) {
919
+ // copied whole across drives, but part of the original stayed: the move counts, with a warning
920
+ if (!e.published || !e.result) throw e;
921
+ r = e.result; partial = e.message;
922
+ }
923
+ // each file's transcript goes along; the ones that couldn't are reported (the file needs transcribing again)
924
+ const follow = (pairs) => {
925
+ const lost = [], went = [];
926
+ for (const m of pairs) {
927
+ const f = srcFile(m.from), t = srcFile(m.to);
928
+ if (!f || !t) continue;
929
+ const got = transcripts.follow(f, t);
930
+ if (got === false) lost.push(m.to); else if (got) went.push(m);
931
+ }
932
+ return { lost, went };
933
+ };
934
+ const { lost, went } = follow(r.moved);
935
+ let repointed;
936
+ try { repointed = repointMedia(r.moved, { force: opts.force === true }); }
937
+ catch (e) {
938
+ const notBack = e.rollbackFailed ? `; ${e.rollbackFailed.join(", ")} couldn't be put back — check it in the editor and relink` : "";
939
+ // a project couldn't be updated: put the files back exactly where every project expects them
940
+ // (never into something that appeared there meanwhile), with the transcripts that went along
941
+ try {
942
+ if (partial) throw new Error("part of the original is still there");
943
+ mediaFiles.move(r.to, r.from, { exact: true });
944
+ follow(went.map((m) => ({ from: m.to, to: m.from })));
945
+ }
946
+ catch (e2) {
947
+ broadcast("change");
948
+ throw new Error(`moved, but a project couldn't be updated (${e.message}) and moving back failed too (${e2.message}) — the files are at ${r.to}: relink them in the editor${notBack}`);
949
+ }
950
+ if (notBack) broadcast("change");
951
+ throw new Error(`not moved: a project couldn't be updated (${e.message})${notBack}`);
952
+ }
953
+ broadcast("change");
954
+ const out = { from: r.from, to: r.to, moved: r.moved, repointed };
955
+ const warn = [];
956
+ if (partial) warn.push(partial);
957
+ if (lost.length) warn.push(`the transcript of ${lost.length} file${lost.length === 1 ? "" : "s"} couldn't be moved with it — transcribe ${lost.length === 1 ? "it" : "them"} again`);
958
+ if (warn.length) out.warning = warn.join("; ");
959
+ sendJSON(res, 200, out);
960
+ return;
961
+ }
962
+ if (a === "trash") {
963
+ if (opts.force !== undefined && typeof opts.force !== "boolean") { sendJSON(res, 400, { error: "force is true or false" }); return; }
964
+ const paths = Array.isArray(opts.paths) ? opts.paths : [opts.paths];
965
+ const files = paths.flatMap((x) => mediaFiles.filesOf(x));
966
+ const { docs, unreadable } = projectDocs();
967
+ const uses = mediaUses(docs, files);
968
+ if (opts.force !== true && (uses.length || unreadable.length)) {
969
+ const why = uses.length
970
+ ? "in use — " + uses.map((u) => `"${u.name}" in ${u.open ? "the open project" : `project "${u.project}"`} (${u.clips} clip${u.clips === 1 ? "" : "s"})`).join("; ") +
971
+ ". Take it out of those projects first (removeMedia)"
972
+ : `can't check ${unreadable.join(", ")} (its project.json can't be read) — fix it first`;
973
+ sendJSON(res, 409, { error: why + ", or pass force:true to trash it anyway (it goes offline there until restored)", uses, unreadable });
974
+ return;
975
+ }
976
+ let r;
977
+ try { r = mediaFiles.trash(paths); }
978
+ catch (e) {
979
+ // part of the batch went: say which (with their ids), so they can be restored
980
+ if (!e.partial?.trashed.length) throw e;
981
+ broadcast("change");
982
+ sendJSON(res, 400, { error: `stopped at ${e.partial.failed}: ${e.message} — the ones before it are in the trash`, trashed: e.partial.trashed, failed: e.partial.failed });
983
+ return;
984
+ }
985
+ broadcast("change");
986
+ // a trash that left part of the original (a warning on that item) may still play there
987
+ const offline = uses.filter((u) => !mediaFiles.pathOf(u.src));
988
+ sendJSON(res, 200, { trashed: r.trashed, offline, ...(offline.length < uses.length ? { stillThere: uses.filter((u) => mediaFiles.pathOf(u.src)) } : {}) });
989
+ return;
990
+ }
991
+ if (a === "restore") { const r = mediaFiles.restore(opts.id); broadcast("change"); sendJSON(res, 200, { restored: r.restored }); return; }
992
+ sendJSON(res, 400, { error: "action is list, mkdir, move, trash, trash-list or restore" });
993
+ } catch (e) { sendJSON(res, 400, { error: String(e.message || e) }); }
994
+ return;
995
+ }
996
+
794
997
  /* API: word transcripts (transcripts.js) — one per version of a file, times in file seconds.
795
998
  GET /api/transcript?src= → {transcript} (null when there is none; 404 when the file is missing)
796
999
  POST /api/transcript?src= {doc} → {src, words} — saved by the tab that transcribed it
@@ -993,7 +1196,10 @@ const server = http.createServer(async (req, res) => {
993
1196
  }
994
1197
  let revision = null;
995
1198
  try { revision = JSON.parse(fs.readFileSync(PROJECT_FILE, "utf8").replace(/^\uFEFF/, "")).revision ?? null; } catch {}
996
- const job = exportJobs.request({ range: opts.range, profile: opts.profile, delivery: deliveries ? null : delivery, deliveries, where: opts.where, revision });
1199
+ if (opts.name != null && typeof opts.name !== "string") { sendJSON(res, 400, { error: "name is a string" }); return; }
1200
+ const name = opts.name?.trim() || null; // blank = the default name
1201
+ if (name && deliveries) { sendJSON(res, 400, { error: "name goes with one file, not with outputs (give each output its own filename)" }); return; }
1202
+ const job = exportJobs.request({ range: opts.range, profile: opts.profile, delivery: deliveries ? null : delivery, deliveries, name, where: opts.where, revision, project: projects.peekActive() });
997
1203
  sendJSON(res, 200, deliveries ? { ...job, delivery: `${deliveries.length} outputs from one render`, notes: outNotes }
998
1204
  : delivery ? { ...job, delivery: Delivery.describe(delivery), notes } : job);
999
1205
  } catch (e) {
@@ -1020,7 +1226,7 @@ const server = http.createServer(async (req, res) => {
1020
1226
  if (mk != null && (typeof mk !== "object" || typeof mk.clip !== "string")) {
1021
1227
  sendJSON(res, 400, { error: "mask must be {clip: \"<clip id>\"}" }); return;
1022
1228
  }
1023
- sendJSON(res, 200, exportJobs.request({ kind: "scopes", time, where: opts.where, revision, matte: m || null, mask: mk || null, image: opts.image === true }));
1229
+ sendJSON(res, 200, exportJobs.request({ kind: "scopes", time, where: opts.where, revision, project: projects.peekActive(), matte: m || null, mask: mk || null, image: opts.image === true }));
1024
1230
  } catch (e) {
1025
1231
  sendJSON(res, e.code === 409 || e.code === 400 ? e.code : 500, { error: String(e.message || e) });
1026
1232
  }
@@ -1037,7 +1243,7 @@ const server = http.createServer(async (req, res) => {
1037
1243
  if (!t || typeof t !== "object" || Array.isArray(t) || typeof t.clip !== "string") { sendJSON(res, 400, { error: "track must be {clip: \"<clip id>\", …}" }); return; }
1038
1244
  const body = JSON.stringify(t);
1039
1245
  if (body.length > 8192) { sendJSON(res, 400, { error: "track spec too large" }); return; }
1040
- sendJSON(res, 200, exportJobs.request({ kind: "track", where: opts.where, revision, track: JSON.parse(body) }));
1246
+ sendJSON(res, 200, exportJobs.request({ kind: "track", where: opts.where, revision, project: projects.peekActive(), track: JSON.parse(body) }));
1041
1247
  } catch (e) {
1042
1248
  sendJSON(res, e.code === 409 || e.code === 400 ? e.code : 500, { error: String(e.message || e) });
1043
1249
  }
package/style.css CHANGED
@@ -3048,6 +3048,27 @@ body.track-size-s .clip.c-comp .comp-open-hint {
3048
3048
  font-size: 12px;
3049
3049
  }
3050
3050
 
3051
+ .export-name {
3052
+ width: 100%;
3053
+ box-sizing: border-box;
3054
+ padding: 6px 10px;
3055
+ border: 1px solid var(--border);
3056
+ border-radius: 6px;
3057
+ background: var(--panel);
3058
+ color: var(--text);
3059
+ font: inherit;
3060
+ font-size: 13px;
3061
+ }
3062
+ .export-skip {
3063
+ margin-right: auto;
3064
+ display: flex;
3065
+ align-items: center;
3066
+ gap: 6px;
3067
+ font-size: 12px;
3068
+ color: var(--dim);
3069
+ cursor: pointer;
3070
+ }
3071
+
3051
3072
  .export-profile-note,
3052
3073
  .export-profile-hint {
3053
3074
  margin: 6px 0 0;
@@ -4508,3 +4529,55 @@ body { background: var(--bg); }
4508
4529
  .tracks-content, .timeline-scroll { background-color: var(--well); }
4509
4530
  .track:nth-child(even):not(.drop-hint) { background-color: #ffffff05; } /* a drop target keeps its highlight */
4510
4531
  .insp-section { background: var(--panel-sub); border-radius: 6px; padding: 8px 10px; margin: 0 0 10px; }
4532
+
4533
+ /* ── Command search (Ctrl/Cmd+K) ── */
4534
+ #cmdkOverlay { align-items: flex-start; padding-top: 12vh; }
4535
+ .cmdk {
4536
+ width: min(620px, calc(100vw - 32px));
4537
+ background: var(--panel);
4538
+ border: 1px solid var(--border);
4539
+ border-radius: 10px;
4540
+ box-shadow: 0 18px 48px rgba(0, 0, 0, .5);
4541
+ overflow: hidden;
4542
+ display: flex;
4543
+ flex-direction: column;
4544
+ }
4545
+ .cmdk-input {
4546
+ border: 0;
4547
+ border-bottom: 1px solid var(--border);
4548
+ background: transparent;
4549
+ color: var(--text);
4550
+ font: inherit;
4551
+ font-size: 15px;
4552
+ padding: 14px 16px;
4553
+ outline: none;
4554
+ }
4555
+ .cmdk-list { list-style: none; margin: 0; padding: 4px; max-height: 52vh; overflow-y: auto; }
4556
+ .cmdk-item {
4557
+ display: flex;
4558
+ align-items: center;
4559
+ gap: 10px;
4560
+ padding: 7px 10px;
4561
+ border-radius: 6px;
4562
+ cursor: pointer;
4563
+ font-size: 13px;
4564
+ }
4565
+ .cmdk-item.active { background: color-mix(in srgb, var(--accent) 22%, transparent); }
4566
+ .cmdk-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
4567
+ .cmdk-hint { color: var(--dim); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40%; }
4568
+ .cmdk-group {
4569
+ font-size: 10px;
4570
+ text-transform: uppercase;
4571
+ letter-spacing: .06em;
4572
+ color: var(--dim);
4573
+ border: 1px solid var(--border);
4574
+ border-radius: 4px;
4575
+ padding: 1px 5px;
4576
+ flex: none;
4577
+ }
4578
+ .cmdk-empty { padding: 14px 12px; color: var(--dim); font-size: 13px; }
4579
+ .cmdk-foot { font-size: 11px; padding: 7px 12px; border-top: 1px solid var(--border); }
4580
+ .cmdk-flash { animation: bin-flash 1.2s ease-out; }
4581
+ @media (forced-colors: active) {
4582
+ .cmdk-item.active { outline: 2px solid Highlight; background: Highlight; color: HighlightText; }
4583
+ }
package/transcripts.js CHANGED
@@ -72,6 +72,16 @@ function createTranscripts({ dir, resolveSrc }) {
72
72
  for (const f of list) if (f.startsWith(prefix) && f.endsWith(".json")) { try { fs.unlinkSync(path.join(dir, f)); n++; } catch { } }
73
73
  return n > 0;
74
74
  },
75
+ /** A file moved from `fromFile` to `toFile` (same size and time): its transcript goes with it.
76
+ true = moved, null = the file had none, false = it couldn't be moved. */
77
+ follow(fromFile, toFile) {
78
+ let to;
79
+ try { to = names(toFile); } catch { return false; }
80
+ const stem = path.basename(fromFile, path.extname(fromFile)).replace(/[^\w\-]+/g, "_").slice(0, 40) || "media";
81
+ const old = `${stem}-${hash(path.resolve(fromFile))}-` + to.name.slice(to.prefix.length);
82
+ if (!fs.existsSync(path.join(dir, old))) return null; // the file had no transcript
83
+ try { fs.renameSync(path.join(dir, old), path.join(dir, to.name)); return true; } catch { return false; }
84
+ },
75
85
  };
76
86
  }
77
87