fablecut 1.14.0 → 1.16.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/sequences.js ADDED
@@ -0,0 +1,216 @@
1
+ /* FableCut sequences — several timelines in one project. Zero dependencies.
2
+ Loaded by the editor as a plain script (global `FableCutSeq`) and required
3
+ by the MCP server, project-ops and the tests.
4
+
5
+ The OPEN sequence is the project's top level, as it always was: `clips`,
6
+ `tracks`, `markers`, the canvas, the mix and the delivery settings. Every
7
+ tool, agent and export keeps reading and writing those. The others are kept
8
+ in `project.sequences`, each with its own copy of the same keys:
9
+
10
+ "sequence": "main", // the open one
11
+ "sequences": [
12
+ { "id": "main", "name": "Main" }, // the open one: its timeline is the top level
13
+ { "id": "seq_v", "name": "Vertical", "width": 1080, "height": 1920, "fps": 30, "clips": [ … ], … }
14
+ ]
15
+
16
+ Opening another sequence parks the top-level keys in the open one's entry
17
+ and brings the other's up. Media, folders, collections and compositions
18
+ are the project's, shared by every sequence. A project with one sequence
19
+ has neither key: untouched projects stay byte-identical. */
20
+ (function (root, factory) {
21
+ const node = typeof module === "object" && module.exports;
22
+ const api = factory();
23
+ if (node) module.exports = api;
24
+ else root.FableCutSeq = api;
25
+ })(typeof self !== "undefined" ? self : this, function () {
26
+ "use strict";
27
+
28
+ /** What a sequence owns: its timeline, canvas, frame rate, mix and delivery. */
29
+ const KEYS = ["width", "height", "fps", "background", "exportFrame", "clips", "tracks", "markers", "inPoint", "outPoint",
30
+ "disabledTracks", "lockedTracks", "untargetedTracks", "master", "buses", "delivery", "outputs", "encodeProfile"];
31
+ const MAIN = { id: "main", name: "Main" };
32
+ const MAX = 64;
33
+ const isObj = (v) => v != null && typeof v === "object" && !Array.isArray(v);
34
+ const clone = (v) => (v === undefined ? undefined : JSON.parse(JSON.stringify(v)));
35
+ const uid = () => Math.random().toString(36).slice(2, 8);
36
+ const r2 = (v) => Math.round(v * 100) / 100;
37
+
38
+ /** The sequence entries, the open one included (a project without the key has one: Main). */
39
+ function entries(doc) {
40
+ return doc && Array.isArray(doc.sequences) && doc.sequences.length ? doc.sequences : [{ ...MAIN }];
41
+ }
42
+ function activeId(doc) {
43
+ const list = entries(doc);
44
+ return doc && list.some((s) => s.id === doc.sequence) ? doc.sequence : list[0].id;
45
+ }
46
+ function find(doc, id) { return entries(doc).find((s) => s.id === id) || null; }
47
+ /** The sequences that are not open: [{id, name, clips, …}] — media and comp usage scans read these too. */
48
+ function others(doc) {
49
+ const act = activeId(doc);
50
+ return entries(doc).filter((s) => s.id !== act);
51
+ }
52
+ const lengthOf = (clips) => (Array.isArray(clips) ? clips : []).reduce((m, c) => Math.max(m, (+c.start || 0) + (+c.duration || 0)), 0);
53
+
54
+ /** One row per sequence, in order: {id, name, active, width, height, fps, clips, duration}. */
55
+ function list(doc) {
56
+ const act = activeId(doc);
57
+ return entries(doc).map((s) => {
58
+ const src = s.id === act ? doc : s;
59
+ return { id: s.id, name: s.name || s.id, active: s.id === act, width: src.width, height: src.height, fps: src.fps,
60
+ clips: Array.isArray(src.clips) ? src.clips.length : 0, duration: r2(lengthOf(src.clips)) };
61
+ });
62
+ }
63
+
64
+ /** Check and tidy doc.sequences in place (throws on a bad entry). One sequence named Main → both keys go. */
65
+ function normalize(doc) {
66
+ if (doc.sequences == null) { delete doc.sequence; return doc; }
67
+ if (!Array.isArray(doc.sequences)) throw new Error("sequences must be a list of {id, name, …}");
68
+ if (doc.sequences.length > MAX) throw new Error(`a project holds up to ${MAX} sequences`);
69
+ const seen = new Set();
70
+ const out = [];
71
+ for (const s of doc.sequences) {
72
+ if (!isObj(s)) throw new Error("every sequence is an object {id, name, …}");
73
+ const id = typeof s.id === "string" ? s.id.trim().slice(0, 40) : "";
74
+ if (!id) throw new Error("every sequence needs an id");
75
+ if (seen.has(id)) throw new Error(`two sequences have the id ${id} — sequence ids must be unique`);
76
+ seen.add(id);
77
+ const name = typeof s.name === "string" && s.name.trim() ? s.name.trim().slice(0, 120) : id;
78
+ out.push({ ...s, id, name });
79
+ }
80
+ if (!out.length) { delete doc.sequences; delete doc.sequence; return doc; }
81
+ // without a valid `sequence`, the open one is an entry without a timeline of its own: never strip one that has one
82
+ const named = out.some((s) => s.id === doc.sequence);
83
+ const bare = out.find((s) => !KEYS.some((k) => s[k] !== undefined));
84
+ if (!named && !bare) throw new Error(`sequence must name the open sequence (${out.map((s) => s.id).join(", ")}): every entry carries a timeline of its own`);
85
+ const act = named ? doc.sequence : bare.id;
86
+ for (const s of out) {
87
+ if (s.id === act) { for (const k of KEYS) delete s[k]; continue; }
88
+ // a parked sequence carries its own timeline
89
+ if (s.clips == null) s.clips = [];
90
+ if (!Array.isArray(s.clips)) throw new Error(`sequence ${s.id}: clips must be a list`);
91
+ for (const c of s.clips) if (!isObj(c) || typeof c.id !== "string" || typeof c.start !== "number" || typeof c.duration !== "number")
92
+ throw new Error(`sequence ${s.id}: every clip needs id, numeric start and duration`);
93
+ for (const k of ["width", "height", "fps"]) if (s[k] != null && !(typeof s[k] === "number" && s[k] > 0)) throw new Error(`sequence ${s.id}: ${k} must be a positive number`);
94
+ }
95
+ if (out.length === 1 && out[0].id === MAIN.id && out[0].name === MAIN.name) { delete doc.sequences; delete doc.sequence; return doc; }
96
+ doc.sequences = out;
97
+ doc.sequence = act;
98
+ return doc;
99
+ }
100
+
101
+ /** Make the list explicit (before adding to it). */
102
+ function materialize(doc) {
103
+ if (!Array.isArray(doc.sequences) || !doc.sequences.length) doc.sequences = [{ ...MAIN }];
104
+ doc.sequence = activeId(doc);
105
+ return doc;
106
+ }
107
+ /** The open sequence's keys, copied out of the top level. */
108
+ function takeTop(doc) {
109
+ const out = {};
110
+ for (const k of KEYS) if (doc[k] !== undefined) out[k] = clone(doc[k]);
111
+ if (!Array.isArray(out.clips)) out.clips = [];
112
+ return out;
113
+ }
114
+
115
+ /** Open sequence `id`: the open one's timeline is parked in its entry, `id`'s comes up. */
116
+ function open(doc, id) {
117
+ const target = find(doc, id);
118
+ if (!target) throw new Error(`no sequence ${id} (${entries(doc).map((s) => s.id).join(", ")})`);
119
+ const act = activeId(doc);
120
+ if (id === act) return doc;
121
+ materialize(doc);
122
+ const parked = takeTop(doc);
123
+ const keep = { width: doc.width, height: doc.height, fps: doc.fps };
124
+ doc.sequences = doc.sequences.map((s) => {
125
+ if (s.id === act) return { id: s.id, name: s.name, ...parked };
126
+ if (s.id === id) return { id: s.id, name: s.name };
127
+ return s;
128
+ });
129
+ for (const k of KEYS) {
130
+ if (target[k] !== undefined) doc[k] = target[k];
131
+ else delete doc[k];
132
+ }
133
+ // a sequence always has a canvas and a timeline
134
+ for (const k of ["width", "height", "fps"]) if (!(doc[k] > 0)) doc[k] = keep[k];
135
+ if (!Array.isArray(doc.clips)) doc.clips = [];
136
+ doc.sequence = id;
137
+ return doc;
138
+ }
139
+
140
+ /** A new sequence: {name?, id?, from?: "empty" | "open" | <sequence id>, width?, height?, fps?}.
141
+ * Empty takes the open one's canvas, frame rate and background; a copy takes everything.
142
+ * It is added after the open one (or after `from`) and not opened. */
143
+ function add(doc, opts = {}) {
144
+ materialize(doc);
145
+ const from = opts.from || "empty";
146
+ const id = opts.id ? String(opts.id).trim().slice(0, 40) : "seq_" + uid();
147
+ if (!id) throw new Error("addSequence: the id is empty");
148
+ if (find(doc, id)) throw new Error(`addSequence: sequence ${id} already exists`);
149
+ if (doc.sequences.length >= MAX) throw new Error(`addSequence: a project holds up to ${MAX} sequences`);
150
+ const act = activeId(doc);
151
+ let body, after = act;
152
+ if (from === "empty") {
153
+ body = { width: doc.width, height: doc.height, fps: doc.fps, clips: [] };
154
+ if (doc.background) body.background = doc.background;
155
+ } else if (from === "open" || from === act) {
156
+ body = takeTop(doc);
157
+ } else {
158
+ const src = find(doc, from);
159
+ if (!src) throw new Error(`addSequence: no sequence ${from} to copy`);
160
+ const { id: _i, name: _n, ...rest } = clone(src);
161
+ body = rest;
162
+ after = src.id;
163
+ }
164
+ for (const k of ["width", "height", "fps"]) if (opts[k] != null) {
165
+ const v = +opts[k];
166
+ if (!(v > 0) || (k !== "fps" && (v < 16 || v > 8192)) || (k === "fps" && v > 240)) throw new Error(`addSequence: ${k} ${opts[k]} is out of range`);
167
+ body[k] = k === "fps" ? v : Math.round(v);
168
+ }
169
+ if (from !== "empty" && (opts.width != null || opts.height != null)) delete body.exportFrame; // the crop was for the other size
170
+ const srcName = from === "empty" ? null : (find(doc, from === "open" ? act : from) || {}).name;
171
+ const name = typeof opts.name === "string" && opts.name.trim() ? opts.name.trim().slice(0, 120)
172
+ : srcName ? `${srcName} copy` : `Sequence ${doc.sequences.length + 1}`;
173
+ const entry = { id, name, ...body };
174
+ const at = doc.sequences.findIndex((s) => s.id === after);
175
+ doc.sequences.splice(at + 1, 0, entry);
176
+ return entry;
177
+ }
178
+
179
+ /** Rename (the only thing set on an entry — the open one's timeline is edited at the top level). */
180
+ function rename(doc, id, name) {
181
+ const s = find(doc, id);
182
+ if (!s) throw new Error(`no sequence ${id}`);
183
+ const n = String(name == null ? "" : name).trim().slice(0, 120);
184
+ if (!n) throw new Error("a sequence needs a name");
185
+ materialize(doc);
186
+ doc.sequences = doc.sequences.map((x) => (x.id === id ? { ...x, name: n } : x));
187
+ return normalize(doc);
188
+ }
189
+ /** Move sequence `id` to position `index` (0-based) in the list. */
190
+ function move(doc, id, index) {
191
+ materialize(doc);
192
+ const i = doc.sequences.findIndex((s) => s.id === id);
193
+ if (i < 0) throw new Error(`no sequence ${id}`);
194
+ const [s] = doc.sequences.splice(i, 1);
195
+ doc.sequences.splice(Math.max(0, Math.min(doc.sequences.length, Math.round(+index) || 0)), 0, s);
196
+ return doc;
197
+ }
198
+ /** Delete sequence `id`. The last one stays; deleting the open one opens its neighbour first. */
199
+ function remove(doc, id) {
200
+ const list = entries(doc);
201
+ const i = list.findIndex((s) => s.id === id);
202
+ if (i < 0) throw new Error(`no sequence ${id}`);
203
+ if (list.length === 1) throw new Error("a project keeps at least one sequence");
204
+ if (activeId(doc) === id) open(doc, (list[i + 1] || list[i - 1]).id);
205
+ doc.sequences = doc.sequences.filter((s) => s.id !== id);
206
+ return normalize(doc);
207
+ }
208
+
209
+ /** One line per sequence, for status readouts. */
210
+ function describe(row) {
211
+ const t = Math.round(row.duration), m = Math.floor(t / 60), s = t % 60;
212
+ return `${row.id} "${row.name}"${row.active ? " (open)" : ""} ${row.width}x${row.height}@${row.fps}, ${row.clips} clip(s), ${m}:${String(s).padStart(2, "0")}`;
213
+ }
214
+
215
+ return { KEYS, MAIN, MAX, entries, activeId, find, others, list, normalize, materialize, open, add, rename, move, remove, describe };
216
+ });