Skip to content

The VS Code of video editing, built for agents.

TypeScript

Refactor Trim component to Workarea for improved clarity and functionality

konstantin-paulus7743b3264576
diff --git a/apps/cli/src/index.ts b/apps/cli/src/index.ts
index b30e05b5..327a19c0 100644
--- a/apps/cli/src/index.ts
+++ b/apps/cli/src/index.ts
@@ -1110,7 +1110,7 @@ program
.command("context")
.alias("ctx")
.description(
- `Print essential context about the open project (project, entity count, scenes, active scene, playhead, workarea trim in seconds when set, font families); call this first to orient.`,
+ `Print essential context about the open project (project, entity count, scenes, active scene, playhead, workarea in seconds when set, font families); call this first to orient.`,
)
.action(() => context());
diff --git a/apps/web/src/components/engine/api/observers.ts b/apps/web/src/components/engine/api/observers.ts
index dd8f4036..89a5576e 100644
--- a/apps/web/src/components/engine/api/observers.ts
+++ b/apps/web/src/components/engine/api/observers.ts
@@ -449,6 +449,12 @@ export function observeWorld(world: EngineWorld) {
utils.persistEntity(world, eid);
});
+ observe(world, "set", c.Workarea, (eid, { start, end }) => {
+ const fallbackEnd = c.Computed.duration[eid] ?? DEFAULT_DURATION_FRAMES;
+ c.Workarea.start[eid] = start ?? c.Workarea.start[eid] ?? 0;
+ c.Workarea.end[eid] = end ?? c.Workarea.end[eid] ?? fallbackEnd;
+ });
+
observe(world, "set", c.Constraint, (eid, { horizontal, vertical }) => {
c.Constraint.horizontal[eid] = horizontal ?? c.Constraint.horizontal[eid] ?? ConstraintType.MIN;
c.Constraint.vertical[eid] = vertical ?? c.Constraint.vertical[eid] ?? ConstraintType.MIN;
diff --git a/apps/web/src/components/engine/components/components.ts b/apps/web/src/components/engine/components/components.ts
index 517ca8d7..9c3c77df 100644
--- a/apps/web/src/components/engine/components/components.ts
+++ b/apps/web/src/components/engine/components/components.ts
@@ -275,6 +275,12 @@ export const Trim = {
end: [] as number[],
};
+// Explicit playback/export window on a scene
+export const Workarea = {
+ start: [] as number[],
+ end: [] as number[],
+};
+
// Constraint anchoring — stores CONSTRAINT_TYPES values (0=MIN, 1=MAX, 2=CENTER).
export const Constraint = {
horizontal: [] as number[],
diff --git a/apps/web/src/components/engine/encode/encoder.ts b/apps/web/src/components/engine/encode/encoder.ts
index 15dc1fad..f500246f 100644
--- a/apps/web/src/components/engine/encode/encoder.ts
+++ b/apps/web/src/components/engine/encode/encoder.ts
@@ -19,7 +19,7 @@ import { hasComponent, Hierarchy, Not, Or, query } from 'bitecs';
import { ChildOf } from '../components';
import { createEngineWorld, switchActiveScene } from '../api';
import { recomputeEntityTimeRange } from '../api/utils';
-import { addComponent, removeComponent } from '../api/events';
+import { addComponent } from '../api/events';
import { playbackSystem } from '../systems/playback';
import { motionSystem } from '../systems/motion';
import { transformSystem } from '../systems/transform';
@@ -43,15 +43,14 @@ export async function createEncoder(sourceWorld: EngineWorld, config: EncoderCon
const sceneHeight = sourceComponents.Computed.height[sourceSceneEid];
const sceneEnd = sourceComponents.Computed.end[sourceSceneEid];
- // Honor the scene's Trim component (the timeline workarea): render only the
- // frames between Trim.start and Trim.end instead of the full scene
- // duration.
- const hasWorkarea = hasComponent(sourceWorld, sourceSceneEid, sourceComponents.Trim);
+ // Honor the scene's Workarea component: render only the frames between
+ // Workarea.start and Workarea.end instead of the full scene duration.
+ const hasWorkarea = hasComponent(sourceWorld, sourceSceneEid, sourceComponents.Workarea);
const workareaStart = hasWorkarea
- ? Math.max(0, Math.min(sceneEnd, sourceComponents.Trim.start[sourceSceneEid] ?? 0))
+ ? Math.max(0, Math.min(sceneEnd, sourceComponents.Workarea.start[sourceSceneEid] ?? 0))
: 0;
const workareaEnd = hasWorkarea
- ? Math.max(workareaStart, Math.min(sceneEnd, sourceComponents.Trim.end[sourceSceneEid] ?? sceneEnd))
+ ? Math.max(workareaStart, Math.min(sceneEnd, sourceComponents.Workarea.end[sourceSceneEid] ?? sceneEnd))
: sceneEnd;
const workareaFrames = workareaEnd - workareaStart;
@@ -114,13 +113,10 @@ export async function createEncoder(sourceWorld: EngineWorld, config: EncoderCon
switchActiveScene(world, sceneEid);
const c = world.components;
- // Drop the scene's Trim in the offline world — the encoder drives the
- // playhead explicitly within the workarea window, and leaving Trim in place
- // would only affect the (skipped) realtime advancePlayhead branch anyway.
- if (hasComponent(world, sceneEid, c.Trim)) {
- // Render-only mutation on the offline world — keep it off the undo stack.
- removeComponent(world, sceneEid, c.Trim, false);
- }
+ // The workarea (Workarea component) is runtime-only and never serialized, so
+ // it isn't cloned into the offline world — nothing to strip here. The encoder
+ // drives the playhead explicitly within the [workareaStart, workareaEnd]
+ // window computed from the source scene above.
c.Computed.localTimeInSeconds[sceneEid] = playheadStartSeconds;
c.Computed.localTime[sceneEid] = Math.round(playheadStartSeconds * frameRate);
c.Playback.playing[sceneEid] = 1;
diff --git a/apps/web/src/components/engine/systems/playback.ts b/apps/web/src/components/engine/systems/playback.ts
index 8388d629..f0a0ee4b 100644
--- a/apps/web/src/components/engine/systems/playback.ts
+++ b/apps/web/src/components/engine/systems/playback.ts
@@ -37,16 +37,16 @@ function advancePlayhead(world: EngineWorld, eid: number): void {
const previousTime = c.Computed.localTimeInSeconds[eid];
let time = previousTime + dt * speed;
- const hasTrim = hasComponent(world, eid, c.Trim);
+ const hasWorkarea = hasComponent(world, eid, c.Workarea);
const duration = c.Computed.duration[eid];
const durationSeconds = duration / fps;
- const maxSeconds = hasTrim
- ? (c.Trim.end[eid] ?? duration) / fps
+ const maxSeconds = hasWorkarea
+ ? (c.Workarea.end[eid] ?? duration) / fps
: durationSeconds;
- const minSeconds = hasTrim
- ? (c.Trim.start[eid] ?? 0) / fps
+ const minSeconds = hasWorkarea
+ ? (c.Workarea.start[eid] ?? 0) / fps
: 0;
if (time >= maxSeconds) {
diff --git a/apps/web/src/components/engine/timeline/render/ruler.ts b/apps/web/src/components/engine/timeline/render/ruler.ts
index 25507373..58134554 100644
--- a/apps/web/src/components/engine/timeline/render/ruler.ts
+++ b/apps/web/src/components/engine/timeline/render/ruler.ts
@@ -63,7 +63,7 @@ export function renderRuler(world: EngineWorld, timeline: TimelineContext) {
const newStart = pixelsToFrames(world, pointer.position.initialX + scrollX * resolution);
const newStop = pixelsToFrames(world, pointer.position.currentX + scrollX * resolution);
- setComponent(world, scene, c.Trim, { start: newStart, end: newStop });
+ setComponent(world, scene, c.Workarea, { start: newStart, end: newStop });
if (pointer.position.currentX > pointer.position.initialX) {
setCurrentFrame(world, newStop);
diff --git a/apps/web/src/components/engine/timeline/render/workarea.ts b/apps/web/src/components/engine/timeline/render/workarea.ts
index c424c12e..5bcc16d0 100644
--- a/apps/web/src/components/engine/timeline/render/workarea.ts
+++ b/apps/web/src/components/engine/timeline/render/workarea.ts
@@ -37,10 +37,10 @@ export function renderWorkarea(world: EngineWorld, timeline: TimelineContext) {
const sid = world.selection.scene;
if (sid === null) return;
const c = world.components;
- if (!hasComponent(world, sid, c.Trim)) return;
+ if (!hasComponent(world, sid, c.Workarea)) return;
- const start = c.Trim.start[sid] ?? 0;
- const stopFrame = c.Trim.end[sid] ?? 0;
+ const start = c.Workarea.start[sid] ?? 0;
+ const stopFrame = c.Workarea.end[sid] ?? 0;
ctx.save();
@@ -65,7 +65,7 @@ export function renderWorkarea(world: EngineWorld, timeline: TimelineContext) {
const deltaFrames = pixelsToFrames(world, pointer.position.deltaX);
- setComponent(world, sid, c.Trim, {
+ setComponent(world, sid, c.Workarea, {
start: workareaDragStart[0] + deltaFrames,
end: workareaDragStart[1] + deltaFrames,
});
@@ -76,7 +76,7 @@ export function renderWorkarea(world: EngineWorld, timeline: TimelineContext) {
}
if (doubleClicked) {
- removeComponent(world, sid, c.Trim);
+ removeComponent(world, sid, c.Workarea);
}
ctx.globalCompositeOperation = 'difference';
@@ -103,7 +103,7 @@ export function renderWorkarea(world: EngineWorld, timeline: TimelineContext) {
const deltaFrames = pixelsToFrames(world, pointer.position.deltaX);
- setComponent(world, sid, c.Trim, {
+ setComponent(world, sid, c.Workarea, {
start: workareaDragStart[0] + deltaFrames,
});
}
@@ -127,7 +127,7 @@ export function renderWorkarea(world: EngineWorld, timeline: TimelineContext) {
const deltaFrames = pixelsToFrames(world, pointer.position.deltaX);
- setComponent(world, sid, c.Trim, {
+ setComponent(world, sid, c.Workarea, {
end: workareaDragStart[1] + deltaFrames,
});
}
diff --git a/apps/web/src/context/editor-api/context.ts b/apps/web/src/context/editor-api/context.ts
index a28ad6b6..1990441e 100644
--- a/apps/web/src/context/editor-api/context.ts
+++ b/apps/web/src/context/editor-api/context.ts
@@ -27,10 +27,10 @@ export function handleContextGet(engine: Accessor<Engine>) {
const fps = world.frameRate;
const workarea =
- activeSceneId !== null && hasComponent(world, activeSceneId, c.Trim)
+ activeSceneId !== null && hasComponent(world, activeSceneId, c.Workarea)
? {
- start: (c.Trim.start[activeSceneId] ?? 0) / fps,
- end: (c.Trim.end[activeSceneId] ?? 0) / fps,
+ start: (c.Workarea.start[activeSceneId] ?? 0) / fps,
+ end: (c.Workarea.end[activeSceneId] ?? 0) / fps,
}
: null;