@alpha2-sh/editor 0.5.2 → 0.6.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.
Files changed (47) hide show
  1. package/README.md +60 -3
  2. package/dist/browser/bridge.d.ts +5 -4
  3. package/dist/browser/bridge.js +390 -67
  4. package/dist/browser/extensions-worker.d.ts +4 -3
  5. package/dist/browser/extensions-worker.js +13 -6
  6. package/dist/browser/jobs.d.ts +17 -0
  7. package/dist/browser/jobs.js +40 -0
  8. package/dist/browser/resources.d.ts +9 -4
  9. package/dist/browser/resources.js +17 -5
  10. package/dist/browser/runtime/assets.d.ts +14 -2
  11. package/dist/browser/runtime/assets.js +37 -27
  12. package/dist/browser/runtime/audio.d.ts +12 -2
  13. package/dist/browser/runtime/audio.js +124 -14
  14. package/dist/browser/runtime/engine.js +8 -1
  15. package/dist/browser/runtime/exporter.d.ts +2 -1
  16. package/dist/browser/runtime/exporter.js +15 -4
  17. package/dist/browser/runtime/extension-import.d.ts +30 -10
  18. package/dist/browser/runtime/extension-import.js +52 -9
  19. package/dist/browser/runtime/preview-utils.d.ts +12 -0
  20. package/dist/browser/runtime/preview-utils.js +45 -0
  21. package/dist/browser/runtime/sources.d.ts +2 -2
  22. package/dist/browser/runtime/sources.js +23 -40
  23. package/dist/browser/runtime/wasm.d.ts +5 -5
  24. package/dist/browser/runtime/wasm.js +6 -13
  25. package/dist/browser/types.d.ts +138 -26
  26. package/dist/browser/worker.d.ts +4 -2
  27. package/dist/browser/worker.js +75 -25
  28. package/dist/core/commands.d.ts +46 -3
  29. package/dist/core/commands.js +212 -40
  30. package/dist/core/compositions.d.ts +27 -0
  31. package/dist/core/compositions.js +43 -0
  32. package/dist/core/data.d.ts +34 -0
  33. package/dist/core/data.js +69 -0
  34. package/dist/core/document.d.ts +20 -1
  35. package/dist/core/document.js +138 -8
  36. package/dist/core/keyframes.d.ts +3 -2
  37. package/dist/core/keyframes.js +6 -2
  38. package/dist/core/sameness.d.ts +6 -0
  39. package/dist/core/sameness.js +42 -0
  40. package/dist/core/session.d.ts +85 -7
  41. package/dist/core/session.js +124 -13
  42. package/dist/index.d.ts +1 -0
  43. package/dist/index.js +1 -0
  44. package/dist/native-client/index.d.ts +2 -2
  45. package/dist/native-client/index.js +2 -2
  46. package/dist/ports/index.d.ts +5 -4
  47. package/package.json +3 -3
@@ -1,7 +1,9 @@
1
+ import { withClipId } from "alpha2/edit";
1
2
  import { compositionTree } from "./compositions.js";
2
- import { createEmptyDocument, validateDocument, withInstanceIds, canonicalDocument, indexDocument, inheritDocumentIndex, EditorError, } from "./document.js";
3
- import { executeCommand } from "./commands.js";
3
+ import { compileEditorDocument, createEmptyDocument, validateDocument, withInstanceIds, canonicalDocument, indexDocument, inheritDocumentIndex, EditorError, } from "./document.js";
4
+ import { executeCommand, executeEditResult } from "./commands.js";
4
5
  import { frozenValid, owned } from "./validated.js";
6
+ import { sameJson } from "./sameness.js";
5
7
  /**
6
8
  * Holds an editor document with undo history, selection, clipboard and pointer gestures. Every
7
9
  * document it holds is frozen; edits produce new documents through {@link Command}s. Methods other
@@ -102,6 +104,12 @@ export class EditorSession {
102
104
  generation: this.#generation,
103
105
  canUndo: this.#undo.length > 0,
104
106
  canRedo: this.#redo.length > 0,
107
+ undoLabel: this.#undo.length
108
+ ? this.#labels.get(this.#document)
109
+ : undefined,
110
+ redoLabel: this.#redo.length
111
+ ? this.#labels.get(this.#redo[this.#redo.length - 1])
112
+ : undefined,
105
113
  });
106
114
  }
107
115
  #emit() {
@@ -136,7 +144,11 @@ export class EditorSession {
136
144
  this.#undo.shift();
137
145
  }
138
146
  }
139
- #commit(document) {
147
+ /** History labels, by the document the labelled step made. */
148
+ #labels = new WeakMap();
149
+ #commit(document, label) {
150
+ if (label !== undefined)
151
+ this.#labels.set(document, label);
140
152
  inheritDocumentIndex(this.#document, document);
141
153
  this.#undo.push(this.#document);
142
154
  this.#trimHistory(document);
@@ -161,16 +173,103 @@ export class EditorSession {
161
173
  /**
162
174
  * Runs `command` on the committed document and commits the result as one undo step, cancelling an
163
175
  * active gesture first. A refusal is returned, not thrown, and leaves the session unchanged. A
164
- * command that changes nothing also leaves it unchanged and keeps the redo history.
176
+ * command whose result serializes like the committed document leaves it unchanged too (history,
177
+ * redo and `revision` included) and returns the committed document.
165
178
  */
166
179
  dispatch(command) {
167
180
  this.#active();
168
181
  this.#cancelGesture?.();
169
182
  const result = this.canExecute(command);
170
- if (result.ok && result.value !== this.#document)
171
- this.#commit(result.value);
183
+ if (!result.ok)
184
+ return result;
185
+ if (sameJson(result.value, this.#document))
186
+ return { ok: true, value: this.#document };
187
+ this.#commit(result.value);
172
188
  return result;
173
189
  }
190
+ /**
191
+ * The committed project, for the runtime to compute an edit operation from (`client.edit` in
192
+ * `@alpha2-sh/node`, `applyEdit` from `alpha2/apply-edit`), and the read that pins it for
193
+ * {@link applyEdit}. The project is the stored one, disabled tracks included.
194
+ */
195
+ editProject() {
196
+ this.#active();
197
+ const document = this.#document;
198
+ return { project: document.project, read: Object.freeze({ document }) };
199
+ }
200
+ /**
201
+ * Commits an edit operation's result (`AppliedEdit`) as one undo step, with `then` run on the
202
+ * edited document in the same step, cancelling an active gesture first. Returns the document and
203
+ * the operation's report.
204
+ *
205
+ * Refusals leave the session unchanged. They are `invalid_command` without `read`, `stale_edit`
206
+ * when the committed document is no longer the one `read` pinned (compute again from a new
207
+ * `editProject()`), and those of `executeEditResult` (`locked`, `invalid_result`, a refused
208
+ * `then` command).
209
+ */
210
+ applyEdit(applied, options) {
211
+ this.#active();
212
+ this.#cancelGesture?.();
213
+ if (!options?.read)
214
+ return refused("invalid_command", "applyEdit needs the read of the editProject() the result was computed from");
215
+ if (options.read.document !== this.#document)
216
+ return refused("stale_edit", "The document changed after the edit was computed; compute it again");
217
+ const result = executeEditResult(this.#document, applied, options.then);
218
+ if (!result.ok)
219
+ return result;
220
+ this.#commit(result.value, options.label ?? options.request.op);
221
+ return {
222
+ ok: true,
223
+ value: { document: result.value, report: applied.report },
224
+ };
225
+ }
226
+ /**
227
+ * The project an extraction reads (what the engine renders of the committed document) and the
228
+ * read {@link applyExtraction} checks a timeline-anchored result against.
229
+ */
230
+ extractionProject() {
231
+ this.#active();
232
+ const { project } = compileEditorDocument(this.#document);
233
+ return { project, read: Object.freeze({ timeline: withoutData(project) }) };
234
+ }
235
+ /**
236
+ * Puts an extraction's data sets in the document as one undo step (a `data.put` for each,
237
+ * replacing sets with their ids), cancelling an active gesture first. Returns the document,
238
+ * unchanged (and no step added) for a result without sets or with sets equal to the ones held.
239
+ *
240
+ * Refusals leave the session unchanged. They are `missing_asset` for a source-anchored set
241
+ * whose source is gone, `invalid_command` for a timeline-anchored set without `read`,
242
+ * `stale_extraction` when the timeline changed since the read (data put in between does not
243
+ * count), and those of `data.put`.
244
+ */
245
+ applyExtraction(result, options = {}) {
246
+ this.#active();
247
+ this.#cancelGesture?.();
248
+ const sets = result.dataSets;
249
+ if (!sets.length)
250
+ return { ok: true, value: this.#document };
251
+ const sources = new Set(this.#document.project.sources.map((s) => s.id));
252
+ const lost = sets.find((set) => set.anchor.type === "source" && !sources.has(set.anchor.source_id));
253
+ if (lost?.anchor.type === "source")
254
+ return refused("missing_asset", `Source ${lost.anchor.source_id} was removed after the extraction read it`, "extract", lost.anchor.source_id);
255
+ if (sets.some((set) => set.anchor.type === "timeline")) {
256
+ if (!options.read)
257
+ return refused("invalid_command", "A timeline-anchored result needs the read of the extractionProject() it came from", "extract");
258
+ const now = withoutData(compileEditorDocument(this.#document).project);
259
+ if (now !== options.read.timeline)
260
+ return refused("stale_extraction", "The timeline changed after the extraction read it; run it again", "extract");
261
+ }
262
+ const applied = executeCommand(this.#document, {
263
+ type: "transaction",
264
+ commands: sets.map((dataSet) => ({ type: "data.put", dataSet })),
265
+ });
266
+ if (!applied.ok)
267
+ return applied;
268
+ if (sameJson(applied.value, this.#document))
269
+ return { ok: true, value: this.#document };
270
+ this.#commit(applied.value, options.label ?? "extract");
271
+ return applied;
272
+ }
174
273
  /**
175
274
  * Replaces the document, clearing history, selection and clipboard and increasing `generation`.
176
275
  * Throws an `EditorError` when `document` does not validate, leaving the session unchanged.
@@ -270,8 +369,9 @@ export class EditorSession {
270
369
  }
271
370
  /**
272
371
  * Inserts the clipboard's clips on their original tracks as one undo step, the earliest at tick
273
- * `at` and the rest at their offsets from it. New clip ids come from `idFactory`. With nothing
274
- * copied it returns the document unchanged.
372
+ * `at` and the rest at their offsets from it. New clip ids come from `idFactory`; a pasted clip's
373
+ * bindings that read through itself read through its new id (`withClipId`). With nothing copied
374
+ * it returns the document unchanged.
275
375
  */
276
376
  paste(at, idFactory = () => crypto.randomUUID()) {
277
377
  this.#active();
@@ -285,7 +385,7 @@ export class EditorSession {
285
385
  commands.push({
286
386
  type: "clip.insert",
287
387
  trackId,
288
- clip: { ...clip, id },
388
+ clip: withClipId(clip, id),
289
389
  ...(name ? { name } : {}),
290
390
  ...(compositions.length ? { compositions } : {}),
291
391
  });
@@ -309,9 +409,9 @@ export class EditorSession {
309
409
  * Starts a pointer gesture, cancelling an active one. Each `update` runs a command on the
310
410
  * gesture's starting document and, when valid, shows the result as the snapshot's `draft` without
311
411
  * touching history. `commit` validates the last candidate fully and commits it as one undo step,
312
- * returning whether anything was committed; `cancel` discards it. A commit, cancel, dispatch,
313
- * undo, redo, open, dispose or new `beginGesture` ends the gesture, after which `update` returns
314
- * `stale_gesture`.
412
+ * returning whether anything was committed (nothing is when the candidate serializes like the
413
+ * starting document); `cancel` discards it. A commit, cancel, dispatch, undo, redo, open, dispose
414
+ * or new `beginGesture` ends the gesture, after which `update` returns `stale_gesture`.
315
415
  */
316
416
  beginGesture() {
317
417
  this.#active();
@@ -357,7 +457,7 @@ export class EditorSession {
357
457
  return false;
358
458
  const value = candidate;
359
459
  const hadDraft = end();
360
- if (value === base) {
460
+ if (sameJson(value, base)) {
361
461
  if (hadDraft)
362
462
  this.#emit();
363
463
  return false;
@@ -414,6 +514,17 @@ export class EditorSession {
414
514
  return this.#disposal;
415
515
  }
416
516
  }
517
+ function refused(code, message, operation = "edit", entityId) {
518
+ return {
519
+ ok: false,
520
+ error: new EditorError(code, message, operation, entityId),
521
+ };
522
+ }
523
+ /** `project` as JSON without its data sets. */
524
+ function withoutData(project) {
525
+ const { data: _data, ...rest } = project;
526
+ return JSON.stringify(rest);
527
+ }
417
528
  /** Creates an {@link EditorSession}; see its constructor for the options and their defaults. */
418
529
  export function createEditor(options = {}) {
419
530
  return new EditorSession(options.document, options.historyLimit, options.historyByteLimit);
package/dist/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from "./core/clip-time.js";
6
6
  export * from "./core/session.js";
7
7
  export * from "./core/timeline-index.js";
8
8
  export * from "./core/keyframes.js";
9
+ export { dataValue, isDataBound, type DataBoundValue, type DataValueOptions, } from "./core/data.js";
9
10
  export * from "./core/presets.js";
10
11
  export * from "./core/captions.js";
11
12
  export * from "./core/text.js";
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export * from "./core/clip-time.js";
6
6
  export * from "./core/session.js";
7
7
  export * from "./core/timeline-index.js";
8
8
  export * from "./core/keyframes.js";
9
+ export { dataValue, isDataBound, } from "./core/data.js";
9
10
  export * from "./core/presets.js";
10
11
  export * from "./core/captions.js";
11
12
  export * from "./core/text.js";
@@ -91,8 +91,8 @@ export interface NativeClientOptions {
91
91
  readonly fetch?: typeof fetch;
92
92
  }
93
93
  /**
94
- * A client for the native export service (`@alpha2-sh/node`'s HTTP host). Exports run with the
95
- * editor's retry policy; each job uploads its assets once and reuses them across attempts.
94
+ * A client for an HTTP export service the application runs. Exports run with the editor's retry
95
+ * policy; each job uploads its assets once and reuses them across attempts.
96
96
  */
97
97
  export declare function createNativeClient(options: NativeClientOptions): {
98
98
  /** The service's `/health` report. Rejects when the service answers with an error status. */
@@ -66,8 +66,8 @@ function delay(ms, signal) {
66
66
  });
67
67
  }
68
68
  /**
69
- * A client for the native export service (`@alpha2-sh/node`'s HTTP host). Exports run with the
70
- * editor's retry policy; each job uploads its assets once and reuses them across attempts.
69
+ * A client for an HTTP export service the application runs. Exports run with the editor's retry
70
+ * policy; each job uploads its assets once and reuses them across attempts.
71
71
  */
72
72
  export function createNativeClient(options) {
73
73
  const request = options.fetch ?? fetch;
@@ -3,7 +3,7 @@ import type { ExportFailure } from "alpha2/export-failure";
3
3
  import type { AttemptRenderer, AttemptVideoEncoder, RetryProfile } from "alpha2/export-retry";
4
4
  import type { Project } from "alpha2/project";
5
5
  import type { EditorDocument, AssetDescriptor } from "../core/document.js";
6
- /** Shows a project in a preview surface. Implemented by the browser bridge and native previews. */
6
+ /** Shows a project in a preview surface. */
7
7
  export interface PreviewBridge {
8
8
  /** Replaces the shown project. `revision` increases with every call. */
9
9
  replaceProject(project: Project, revision: number): Promise<void>;
@@ -95,9 +95,10 @@ export type ExportArtifact = ({
95
95
  /**
96
96
  * The exporter's run report: timings, `performance` (with `memory` and `output`), rates. A
97
97
  * browser export adds `videoDecode` (the decode mode used), `videoConversion` (`engine` or
98
- * `browser`), and `decodeFallbacks`. Those list, per source, frames read by element seeks instead
99
- * of decoding (`kind: "element-seek"`, with reason, count and whether permanent), and sources
100
- * that `videoDecode: "software"` could not decode in software (`kind: "software-unavailable"`).
98
+ * `browser`), `silentAudioSources` (ids of sources whose audio was written silent) and
99
+ * `decodeFallbacks`, which lists per source the frames read by element seeks instead of decoding
100
+ * (`kind: "element-seek"`, with reason, count and whether permanent) and sources that
101
+ * `videoDecode: "software"` could not decode in software (`kind: "software-unavailable"`).
101
102
  */
102
103
  readonly report?: Readonly<Record<string, unknown>>;
103
104
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alpha2-sh/editor",
3
- "version": "0.5.2",
3
+ "version": "0.6.0",
4
4
  "description": "Headless Project-based editing sessions and browser adapters for alpha2.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -60,11 +60,11 @@
60
60
  "build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.json"
61
61
  },
62
62
  "peerDependencies": {
63
- "alpha2": "0.5.2",
63
+ "alpha2": "0.6.0",
64
64
  "react": "^19.3.0"
65
65
  },
66
66
  "devDependencies": {
67
- "alpha2": "0.5.2",
67
+ "alpha2": "0.6.0",
68
68
  "react": "19.3.0",
69
69
  "react-dom": "19.3.0",
70
70
  "@types/react": "19.3.0",