Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor entity insertion and reparenting logic for improved handling

konstantin-paulus9988530c3a9d
diff --git a/apps/web/src/engine/editor.ts b/apps/web/src/engine/editor.ts
index 563d2b91..84dbcbe7 100644
--- a/apps/web/src/engine/editor.ts
+++ b/apps/web/src/engine/editor.ts
@@ -544,7 +544,15 @@ export class DocumentEditor {
const [wrapper] = this.insertElement(parent, element, members[0]);
if (!wrapper) return null;
- const moved = members.filter((member) => this.reparent(member, wrapper));
+ // Moved back to front, each in front of the one after it: the last has
+ // nothing to go in front of, and every other one is placed against a
+ // member that is in there already. Appending them in order reads the
+ // same on the canvas, but not in the file, where an element still
+ // waiting to be inserted lands before a move of one the file has.
+ const moved: Entity[] = [];
+ for (const member of [...members].reverse()) {
+ if (this.reparent(member, wrapper, moved[0])) moved.unshift(member);
+ }
if (moved.length === 0) {
this.remove(wrapper);
return null;
diff --git a/apps/web/src/engine/split.tsx b/apps/web/src/engine/split.tsx
index 7ba0fd31..b2143667 100644
--- a/apps/web/src/engine/split.tsx
+++ b/apps/web/src/engine/split.tsx
@@ -21,6 +21,7 @@ import {
getActiveEntity,
getNextName,
getParentEntity,
+ handOffDecoders,
isGroup,
isSequence,
store,
@@ -103,6 +104,7 @@ export function splitAtPlayhead(world: World): Entity[] {
for (const { original, copy } of pairs) {
trimOut(world, original, frame);
trimIn(world, copy, frame);
+ handOffDecoders(world, original, copy);
}
// Two clips side by side under a scene are two layers of the timeline,
diff --git a/apps/web/src/projects/edits.ts b/apps/web/src/projects/edits.ts
index a553db90..49c7a28f 100644
--- a/apps/web/src/projects/edits.ts
+++ b/apps/web/src/projects/edits.ts
@@ -164,15 +164,24 @@ class EditWriter {
// waits for its name: edits to it, and inserts or moves under or
// beside it.
const waits = (source: string | undefined): boolean => source !== undefined && this.inflight.has(source);
- const heldInserts = new Map([...this.inserts].filter(([, insert]) => waits(insert.parent) || waits(insert.before)));
- const heldMoves = new Map([...this.moves].filter(([source, move]) => waits(source) || waits(move.parent) || waits(move.before)));
+ // An insert waits for one it is going under, or in front of, that is
+ // waiting itself: it has no name to be addressed by yet either. The
+ // inserts come in dependency order, so the one being waited for has
+ // been seen by the time the one waiting is.
+ const ordered = this.orderedInserts();
+ const heldInserts = new Map<string, InsertEdit>();
+ const unnamed = (source: string | undefined): boolean => source !== undefined && (waits(source) || heldInserts.has(source));
+ for (const insert of ordered) {
+ if (unnamed(insert.parent) || unnamed(insert.before)) heldInserts.set(insert.source, insert);
+ }
+ const heldMoves = new Map([...this.moves].filter(([source, move]) => waits(source) || unnamed(move.parent) || unnamed(move.before)));
const held = new Map([...this.pending].filter(([source]) => waits(source)));
const heldTexts = new Map([...this.texts].filter(([source]) => waits(source)));
const heldRemoves = new Set([...this.removes].filter(waits));
const edits: SourceEdit[] = [
// First: the copies an unroll makes are what the rest is addressed to.
...[...this.unrolls.values()].map(({ kind, source, iterations }): SourceEdit => ({ kind, source, iterations })),
- ...[...this.inserts.values()]
+ ...ordered
.filter((insert) => !heldInserts.has(insert.source))
.map(({ kind, source, parent, tag, props, before, text }): SourceEdit => ({
kind,
@@ -230,6 +239,41 @@ class EditWriter {
});
}
+ /**
+ * The inserts in an order a write can take them in: an element after the
+ * one it goes under and after the sibling it is placed in front of, since
+ * neither can be addressed until it has a name. The order they were made
+ * in says that already for a tree built downwards, but not for an element
+ * moved into one made after it — a split's copy into the sequence that
+ * wraps the two halves — where the move is folded into an insert that is
+ * still waiting.
+ */
+ private orderedInserts(): InsertEdit[] {
+ const ordered: InsertEdit[] = [];
+ const placed = new Set<string>();
+ const waiting = [...this.inserts.values()];
+ const owed = (source: string | undefined): boolean => source !== undefined && this.inserts.has(source) && !placed.has(source);
+
+ // A pass that places nothing is the end of it: what is left waits on
+ // itself, and goes last rather than being dropped.
+ for (let moved = true; moved && waiting.length; ) {
+ moved = false;
+ for (let index = 0; index < waiting.length; ) {
+ const insert = waiting[index]!;
+ if (owed(insert.parent) || owed(insert.before)) {
+ index++;
+ continue;
+ }
+ ordered.push(insert);
+ placed.add(insert.source);
+ waiting.splice(index, 1);
+ moved = true;
+ }
+ }
+
+ return [...ordered, ...waiting];
+ }
+
private report(result: WriteResult): void {
if (result.error) {
toast.error('Could not write to the project', { description: result.error });
diff --git a/packages/runtime/src/media/handoff.ts b/packages/runtime/src/media/handoff.ts
new file mode 100644
index 00000000..5ce1f01f
--- /dev/null
+++ b/packages/runtime/src/media/handoff.ts
@@ -0,0 +1,69 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Handing a primed decoder from a node to the copy that takes over from it.
+// A decoder is the expensive part of playing a clip — a demuxer, a decode
+// queue, and the frames around wherever it was last seeked — and a copy that
+// carries on from where the original stops wants exactly the state the
+// original has. Rebuilding it there means a seek, and a blank frame while the
+// seek lands; moving the handle means the copy draws its first frame from the
+// cache that was already warm for it.
+
+import { AssetId, SequenceDecoderHandle, VideoDecoderHandle } from '../traits';
+import { getEntityChildren } from '../queries/hierarchy';
+
+import type { Entity, World } from 'koota';
+
+/**
+ * Moves the live video and sequence decoders of `from`'s subtree to `to`'s.
+ *
+ * For a split: the original ends at the playhead and the copy starts there,
+ * so the copy is what renders next and the original is the one that can
+ * afford to rebuild (`resolveVideoDecoder` gives it a fresh decoder whenever
+ * it is played again). Both halves read the same source frames at the same
+ * scene frames — the cut leaves their origins and playback rates alone — so
+ * the decoder arrives already seeked to where it is wanted.
+ *
+ * Audio is left where it is: its decoder is scheduled against the audio graph
+ * rather than seeked per frame, and both halves are heard.
+ */
+export function handOffDecoders(world: World, from: Entity, to: Entity): void {
+ handOff(from, to);
+
+ // Paired by position: the copy was rendered from what the original was
+ // authored as, so the two subtrees have the same shape. Two that disagree
+ // are not paired at all — a decoder handed to the wrong node would only be
+ // thrown away again when it is asked for the wrong asset.
+ const ours = getEntityChildren(world, from);
+ const theirs = getEntityChildren(world, to);
+ if (ours.length !== theirs.length) return;
+
+ for (const [index, child] of ours.entries()) {
+ handOffDecoders(world, child, theirs[index]!);
+ }
+}
+
+/** The handles of one node, moved to its counterpart. */
+function handOff(from: Entity, to: Entity): void {
+ // The decoder is only worth anything to a node playing what it decodes.
+ if (from.get(AssetId)?.value !== to.get(AssetId)?.value) return;
+
+ const video = from.get(VideoDecoderHandle);
+ if (video) {
+ // Nulled rather than removed: the trait's onRemove disposes what it
+ // finds there, and what it would find is the decoder being handed on.
+ from.set(VideoDecoderHandle, null);
+ to.get(VideoDecoderHandle)?.dispose();
+ to.add(VideoDecoderHandle);
+ to.set(VideoDecoderHandle, video);
+ }
+
+ const sequence = from.get(SequenceDecoderHandle);
+ if (sequence) {
+ from.set(SequenceDecoderHandle, null);
+ to.get(SequenceDecoderHandle)?.dispose();
+ to.add(SequenceDecoderHandle);
+ to.set(SequenceDecoderHandle, sequence);
+ }
+}
diff --git a/packages/runtime/src/media/index.ts b/packages/runtime/src/media/index.ts
index 2b0025a5..61a4d715 100644
--- a/packages/runtime/src/media/index.ts
+++ b/packages/runtime/src/media/index.ts
@@ -9,6 +9,7 @@ export * from './audio-sync';
export * from './caption';
export * from './dispose';
export * from './frame-cache';
+export * from './handoff';
export * from './html';
export * from './image';
export * from './keyframe-index';