Skip to content

The VS Code of video editing, built for agents.

TypeScript

Add wrap method to DocumentEditor for entity grouping and enhance shortcuts

konstantin-paulusb748276a2624
diff --git a/apps/web/src/engine/editor.ts b/apps/web/src/engine/editor.ts
index 94a11be5..e133b834 100644
--- a/apps/web/src/engine/editor.ts
+++ b/apps/web/src/engine/editor.ts
@@ -513,6 +513,46 @@ export class DocumentEditor {
return pairs;
}
+ /**
+ * Puts `entities` inside a new element of their own: `element` is rendered
+ * where the first of them stood and they all move into it, in the order
+ * they were already in. Only the ones sharing that first one's parent take
+ * part — a wrap has one place to put things.
+ *
+ * Nothing is rewritten on the way in, which is what a `<sequence>` wants:
+ * it has no space or time of its own, so its children keep the position
+ * and the start they had. A wrapper that does sit somewhere would need its
+ * children's positions rewritten into it, and this does not do that.
+ *
+ * The selection is left alone. Returns the wrapper, or null when there is
+ * nothing to wrap, nowhere to write it, or nothing that would go in — an
+ * empty element is not worth putting in the file.
+ */
+ public wrap(entities: Entity | Entity[], element: () => unknown): Entity | null {
+ const roots = new Set(this.subtreeRoots(entities));
+ const first = [...roots][0];
+ if (!first) return null;
+
+ const parent = getParentEntity(first);
+ if (!parent) return null;
+
+ const members = getEntityChildren(this.world, parent).filter((entity) => roots.has(entity));
+ if (members.length === 0) return null;
+
+ // In front of the first member, so the wrapper takes their place among
+ // the siblings rather than landing at the end of them.
+ const [wrapper] = this.insertElement(parent, element, members[0]);
+ if (!wrapper) return null;
+
+ const moved = members.filter((member) => this.reparent(member, wrapper));
+ if (moved.length === 0) {
+ this.remove(wrapper);
+ return null;
+ }
+
+ return wrapper;
+ }
+
/**
* Takes a copy of `entities` (the tops of their subtrees, as `duplicate`
* would) for `paste`. Nothing is reported: the file only changes when the
diff --git a/apps/web/src/engine/index.ts b/apps/web/src/engine/index.ts
index 717014fc..16889a2a 100644
--- a/apps/web/src/engine/index.ts
+++ b/apps/web/src/engine/index.ts
@@ -9,6 +9,7 @@ export * from './camera-controller';
export * from './hooks';
export * from './editor';
export * from './timing';
+export * from './split';
export * from './traits';
export * from './input';
export * from './hud';
diff --git a/apps/web/src/engine/input/shortcuts.ts b/apps/web/src/engine/input/shortcuts.ts
index 780cbc1d..4f6e10a8 100644
--- a/apps/web/src/engine/input/shortcuts.ts
+++ b/apps/web/src/engine/input/shortcuts.ts
@@ -12,6 +12,7 @@
import { getActiveEntity, getEntityChildren, getParentEntity, getSelection, isGroupLike, Position, Selected } from '@diffusionstudio/runtime';
import { getDocumentEditor } from '../editor';
+import { splitAtPlayhead } from '../split';
import { Keys } from '../traits';
import type { World } from 'koota';
@@ -103,6 +104,7 @@ const SHORTCUTS: readonly Shortcut[] = [
{ key: 'backspace', action: deleteSelection },
{ key: 'delete', action: deleteSelection },
{ key: 'd', mod: true, action: duplicateSelection },
+ { key: 'b', mod: true, action: splitAtPlayhead },
{ key: 'c', mod: true, action: copySelection },
{ key: 'v', mod: true, action: pasteSelection },
{ key: 'arrowleft', action: nudge(-NUDGE, 0) },
diff --git a/apps/web/src/engine/split.tsx b/apps/web/src/engine/split.tsx
new file mode 100644
index 00000000..7ba0fd31
--- /dev/null
+++ b/apps/web/src/engine/split.tsx
@@ -0,0 +1,124 @@
+/* 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/. */
+
+/**
+ * Cutting a clip in two at the playhead. The clip is copied where it stands,
+ * then the original is trimmed to end at the playhead and the copy to start
+ * there, so the two halves play exactly what the one clip did. Everything
+ * goes through the editor, so the file ends up saying the same thing the
+ * canvas shows: an element beside the one it came from, with its own times.
+ */
+
+import { Sequence } from '@diffusionstudio/reconciler';
+import {
+ AdjustmentLayer,
+ ChildOf,
+ Computed,
+ Geometry,
+ Group,
+ Selected,
+ getActiveEntity,
+ getNextName,
+ getParentEntity,
+ isGroup,
+ isSequence,
+ store,
+} from '@diffusionstudio/runtime';
+import { Or } from 'koota';
+
+import { getDocumentEditor } from './editor';
+import { trimIn, trimOut } from './timing';
+
+import type { Entity, World } from 'koota';
+
+/** The node kinds that sit on a timeline, and so are the ones a cut is about. */
+const NODES = Or(Geometry, Group, AdjustmentLayer);
+
+/**
+ * What the cut applies to: the selection, or — with nothing selected — every
+ * clip directly under the active scene. The scene is the editing context, so
+ * it is never a target, even when it is what is selected.
+ */
+function splitTargets(world: World, scene: Entity): Entity[] {
+ const selection = [...world.query(Selected, NODES)].filter((entity) => entity !== scene);
+ if (selection.length > 0) return selection;
+
+ return [...world.query(NODES, ChildOf(scene))];
+}
+
+/**
+ * The clips the playhead actually cuts, with any sequence among the targets
+ * opened up: a sequence is not a clip — its time is whatever its children
+ * span — so the cut goes to whichever of them the playhead is over.
+ *
+ * `Computed.visibility` is what "the playhead is over it" means, since the
+ * playback system already works it out for every node, ancestors included.
+ * The bounds are then checked directly as well: a clip held visible through a
+ * transition is not one the playhead is inside, and cutting it would leave a
+ * half of nothing.
+ */
+function splitUnits(world: World, targets: Entity[], frame: number): Entity[] {
+ const computed = store(world, Computed);
+ const units = new Set<Entity>();
+
+ const walk = (entity: Entity): void => {
+ if (computed.visibility[entity.id()] !== 1) return;
+
+ if (isSequence(entity)) {
+ for (const child of world.query(NODES, ChildOf(entity))) walk(child);
+ return;
+ }
+
+ const start = computed.start[entity.id()] ?? 0;
+ const end = computed.end[entity.id()] ?? 0;
+ if (frame <= start || frame >= end) return;
+
+ units.add(entity);
+ };
+
+ for (const entity of targets) walk(entity);
+
+ return [...units];
+}
+
+/**
+ * Cuts every clip the playhead is over in two. Returns the tail halves, which
+ * are what the selection is left on: they are the new elements, and carrying
+ * on from the cut is the usual next thing to do to them.
+ */
+export function splitAtPlayhead(world: World): Entity[] {
+ const scene = getActiveEntity(world);
+ if (scene === null) return [];
+
+ const frame = store(world, Computed).localTime[scene.id()] ?? 0;
+ const units = splitUnits(world, splitTargets(world, scene), frame);
+ if (units.length === 0) return [];
+
+ const editor = getDocumentEditor(world);
+ // Copied before anything is trimmed, so each copy is spelled from the
+ // whole clip and still runs to the end the whole clip ran to.
+ const pairs = editor.duplicateInPlace(units);
+
+ for (const { original, copy } of pairs) {
+ trimOut(world, original, frame);
+ trimIn(world, copy, frame);
+ }
+
+ // Two clips side by side under a scene are two layers of the timeline,
+ // where a sequence's children are one row: the halves are wrapped so a
+ // clip that was cut still reads as the one clip it was. A parent that
+ // already groups its children (a group, or a sequence — every sequence is
+ // a group) does that for them.
+ for (const { original, copy } of pairs) {
+ const parent = getParentEntity(original);
+ if (parent === null || isGroup(parent)) continue;
+
+ editor.wrap([original, copy], () => <Sequence name={getNextName(world, 'Sequence')} />);
+ }
+
+ const copies = pairs.map(({ copy }) => copy);
+ if (copies.length > 0) editor.select(copies);
+
+ return copies;
+}