Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor time management in Inspector for improved functionality and code organization

konstantin-paulusc7277c7fb75a
diff --git a/apps/web/src/components/sidebar-right/inspector/time.tsx b/apps/web/src/components/sidebar-right/inspector/time.tsx
index 3d5d986b..4ad9eab6 100644
--- a/apps/web/src/components/sidebar-right/inspector/time.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/time.tsx
@@ -37,21 +37,17 @@ import {
FrameRate,
Geometry,
PlaybackRate,
- SourceOut,
findGeometryAsset,
- framesToSeconds,
- getSourceFrameAt,
- getTimelineOrigin,
isGroupLike,
isScene,
secondsToFrames,
} from "@diffusionstudio/runtime";
import { useDerived, useEditor } from "@/engine/hooks";
+import { editTime, trimIn, trimOut } from "@/engine/timing";
import { useLibrary } from "@/engine/library";
import { setSequenceFrameRate } from "@/engine/asset-actions";
-import type { DocumentEditor } from "@/engine/editor";
-import type { Entity, World } from "koota";
+import type { Entity } from "koota";
type TimeAddon = "inOut" | "playbackRate";
type TimeAddons = Partial<Record<TimeAddon, boolean>>;
@@ -95,51 +91,6 @@ function parseTimeInput(value: string) {
return hours * 3600 + minutes * 60 + seconds;
}
-type TimeProp = 'start' | 'end' | 'sourceIn' | 'sourceOut';
-
-/**
- * Writes a time prop from a frame count of this project; the file spells
- * times in seconds. `null` unsets it: the document drops the trait, and the
- * writer spells it as the attribute's absence (`false` is the one PropValue
- * it removes for). A `start` or `sourceIn` of 0 is unset too, since absence
- * is what 0 reads as on those.
- */
-function editTime(world: World, editor: DocumentEditor, entity: Entity, name: TimeProp, frames: number | null) {
- const unset = frames === null || (frames === 0 && (name === 'start' || name === 'sourceIn'));
- const fps = world.get(FrameRate)?.value ?? 30;
- editor.editProperty(entity, name, unset ? false : framesToSeconds(frames, fps));
-}
-
-/**
- * Moves the node's in point to scene frame `frame`, keeping the rest of the
- * clip where it is: the runtime's `trimEntityIn`, as edits. The out point is
- * only implied while the node authors no End, so it is pinned first, or the
- * tail would follow the head; then Start moves and SourceIn rolls forward by
- * as much as the head lost.
- */
-function trimIn(world: World, editor: DocumentEditor, entity: Entity, frame: number) {
- if (!entity.has(End)) {
- editTime(world, editor, entity, 'end', (entity.get(Computed)?.end ?? 0) - getTimelineOrigin(entity));
- }
- // Both read the origin, which the Start write moves.
- const start = frame - getTimelineOrigin(entity);
- const source = getSourceFrameAt(entity, frame);
- editTime(world, editor, entity, 'start', start);
- editTime(world, editor, entity, 'sourceIn', source);
-}
-
-/**
- * Moves the node's out point to scene frame `frame` (the runtime's
- * `trimEntityOut`, as edits). The source window follows only when the node
- * authors one; otherwise End alone says where the clip runs out.
- */
-function trimOut(world: World, editor: DocumentEditor, entity: Entity, frame: number) {
- if (entity.has(SourceOut)) {
- editTime(world, editor, entity, 'sourceOut', getSourceFrameAt(entity, frame));
- }
- editTime(world, editor, entity, 'end', frame - getTimelineOrigin(entity));
-}
-
type TimeSettingsProps = {
selection: Entity[];
};
@@ -200,24 +151,24 @@ export function TimeSettings(props: TimeSettingsProps) {
const handleInChange = (event: Event & { currentTarget: HTMLInputElement }) => {
const parsed = parseTimeInput(event.currentTarget.value);
if (parsed === null) return;
- trimIn(world, editor, entity(), secondsToFrames(parsed, fps()));
+ trimIn(world, entity(), secondsToFrames(parsed, fps()));
};
const handleOutChange = (event: Event & { currentTarget: HTMLInputElement }) => {
const parsed = parseTimeInput(event.currentTarget.value);
if (parsed === null) return;
- trimOut(world, editor, entity(), secondsToFrames(parsed, fps()));
+ trimOut(world, entity(), secondsToFrames(parsed, fps()));
};
const handleLengthChange = (durationSec: number) => {
- trimOut(world, editor, entity(), start() + secondsToFrames(durationSec, fps()));
+ trimOut(world, entity(), start() + secondsToFrames(durationSec, fps()));
};
const toggleTrim = (checked: boolean) => {
if (checked) {
- trimIn(world, editor, entity(), start());
+ trimIn(world, entity(), start());
} else {
- editTime(world, editor, entity(), 'end', null);
+ editTime(world, entity(), 'end', null);
}
};
diff --git a/apps/web/src/engine/editor.ts b/apps/web/src/engine/editor.ts
index 97dc6597..94a11be5 100644
--- a/apps/web/src/engine/editor.ts
+++ b/apps/web/src/engine/editor.ts
@@ -478,6 +478,41 @@ export class DocumentEditor {
return copies;
}
+ /**
+ * Copies `entities` where they stand: each copy goes in the parent its
+ * original is in, directly after it, rather than out of the sequence
+ * around it the way `duplicate` places one. Two clips at the same time do
+ * conflict in a sequence, so this is for a caller that is about to move
+ * the two apart — a split, whose halves end up adjacent rather than on
+ * top of each other — and not for one that leaves them where they land.
+ *
+ * The original is settled first (see `settle`): the copy is placed
+ * against it, so the file has to be able to name that one clip and not
+ * just the loop body it was rendered from. The selection is left alone,
+ * since what a copy in place means is the caller's to say. Returns the
+ * pairs it made, in the order of the originals.
+ */
+ public duplicateInPlace(entities: Entity | Entity[]): { original: Entity; copy: Entity }[] {
+ const pairs: { original: Entity; copy: Entity }[] = [];
+
+ for (const original of this.subtreeRoots(entities)) {
+ const parent = getParentEntity(original);
+ if (!parent) continue;
+
+ this.settle(original);
+ const tree = this.spell(original);
+ if (!tree) continue;
+
+ const siblings = getEntityChildren(this.world, parent);
+ const anchor = siblings[siblings.indexOf(original) + 1];
+
+ const [copy] = this.insertElement(parent, () => renderAuthored(tree), anchor);
+ if (copy) pairs.push({ original, copy });
+ }
+
+ return pairs;
+ }
+
/**
* 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 f04748e3..717014fc 100644
--- a/apps/web/src/engine/index.ts
+++ b/apps/web/src/engine/index.ts
@@ -8,6 +8,7 @@ export * from './canvas';
export * from './camera-controller';
export * from './hooks';
export * from './editor';
+export * from './timing';
export * from './traits';
export * from './input';
export * from './hud';
diff --git a/apps/web/src/engine/timing.ts b/apps/web/src/engine/timing.ts
new file mode 100644
index 00000000..4e8c6fa1
--- /dev/null
+++ b/apps/web/src/engine/timing.ts
@@ -0,0 +1,72 @@
+/* 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/. */
+
+/**
+ * Where a node sits on the timeline, moved the way every editor change moves:
+ * the document for the canvas, an edit for the file. The runtime has
+ * `trimEntityIn`/`trimEntityOut`, which write the traits themselves — that
+ * moves the clip on screen and nowhere else — so everything that trims a clip
+ * (the inspector's fields, the timeline's handles, a split) goes through here
+ * instead, and the file hears about it.
+ */
+
+import {
+ Computed,
+ End,
+ FrameRate,
+ SourceOut,
+ framesToSeconds,
+ getSourceFrameAt,
+ getTimelineOrigin,
+} from '@diffusionstudio/runtime';
+
+import { getDocumentEditor } from './editor';
+
+import type { Entity, World } from 'koota';
+
+/** The time a node authors, in the vocabulary of the JSX rather than the traits'. */
+export type TimeProp = 'start' | 'end' | 'sourceIn' | 'sourceOut';
+
+/**
+ * Writes a time prop from a frame count of this project; the file spells
+ * times in seconds. `null` unsets it: the document drops the trait, and the
+ * writer spells it as the attribute's absence (`false` is the one PropValue
+ * it removes for). A `start` or `sourceIn` of 0 is unset too, since absence
+ * is what 0 reads as on those.
+ */
+export function editTime(world: World, entity: Entity, name: TimeProp, frames: number | null): void {
+ const unset = frames === null || (frames === 0 && (name === 'start' || name === 'sourceIn'));
+ const fps = world.get(FrameRate)?.value ?? 30;
+ getDocumentEditor(world).editProperty(entity, name, unset ? false : framesToSeconds(frames, fps));
+}
+
+/**
+ * Moves the node's in point to scene frame `frame`, keeping the rest of the
+ * clip where it is: the runtime's `trimEntityIn`, as edits. The out point is
+ * only implied while the node authors no End, so it is pinned first, or the
+ * tail would follow the head; then Start moves and SourceIn rolls forward by
+ * as much as the head lost.
+ */
+export function trimIn(world: World, entity: Entity, frame: number): void {
+ if (!entity.has(End)) {
+ editTime(world, entity, 'end', (entity.get(Computed)?.end ?? 0) - getTimelineOrigin(entity));
+ }
+ // Both read the origin, which the Start write moves.
+ const start = frame - getTimelineOrigin(entity);
+ const source = getSourceFrameAt(entity, frame);
+ editTime(world, entity, 'start', start);
+ editTime(world, entity, 'sourceIn', source);
+}
+
+/**
+ * Moves the node's out point to scene frame `frame` (the runtime's
+ * `trimEntityOut`, as edits). The source window follows only when the node
+ * authors one; otherwise End alone says where the clip runs out.
+ */
+export function trimOut(world: World, entity: Entity, frame: number): void {
+ if (entity.has(SourceOut)) {
+ editTime(world, entity, 'sourceOut', getSourceFrameAt(entity, frame));
+ }
+ editTime(world, entity, 'end', frame - getTimelineOrigin(entity));
+}