Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apps/web/src/commands/timeline/element/move-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
PlannedElementMove,
PlannedTrackCreation,
} from "@/timeline/group-move";
import { normalizeTimelineElement } from "@/timeline/normalize";
import { findTrackInSceneTracks } from "@/timeline/track-element-update";

export class MoveElementCommand extends Command {
Expand Down Expand Up @@ -81,8 +82,12 @@ export class MoveElementCommand extends Command {
}

movedElementsById.set(move.elementId, {
...sourceElement,
startTime: move.newStartTime,
...normalizeTimelineElement({
element: {
...sourceElement,
startTime: move.newStartTime,
},
}),
});
}

Expand Down
92 changes: 54 additions & 38 deletions apps/web/src/commands/timeline/element/split-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { EditorCore } from "@/core";
import { isRetimableElement } from "@/timeline";
import { splitAnimationsAtTime } from "@/animation";
import { getSourceSpanAtClipTime } from "@/retime";
import {
normalizeTimelineElement,
normalizeTimelineValue,
} from "@/timeline/normalize";

export class SplitElementsCommand extends Command {
private savedState: SceneTracks | null = null;
Expand Down Expand Up @@ -73,9 +77,13 @@ export class SplitElementsCommand extends Command {
return [element];
}

const relativeTime = this.splitTime - element.startTime;
const relativeTime = normalizeTimelineValue({
value: this.splitTime - element.startTime,
});
const leftVisibleDuration = relativeTime;
const rightVisibleDuration = element.duration - relativeTime;
const rightVisibleDuration = normalizeTimelineValue({
value: element.duration - relativeTime,
});
const retimeRef = isRetimableElement(element)
? element.retime
: undefined;
Expand All @@ -97,14 +105,16 @@ export class SplitElementsCommand extends Command {

if (this.retainSide === "left") {
splitResult = [
{
...element,
duration: leftVisibleDuration,
trimEnd: element.trimEnd + rightSourceSpan,
name: `${element.name} (left)`,
animations: leftAnimations,
...(retimeRef !== undefined ? { retime: retimeRef } : {}),
},
normalizeTimelineElement({
element: {
...element,
duration: leftVisibleDuration,
trimEnd: element.trimEnd + rightSourceSpan,
name: `${element.name} (left)`,
animations: leftAnimations,
...(retimeRef !== undefined ? { retime: retimeRef } : {}),
},
}),
];
} else if (this.retainSide === "right") {
const newId = generateUUID();
Expand All @@ -113,16 +123,18 @@ export class SplitElementsCommand extends Command {
elementId: newId,
});
splitResult = [
{
...element,
id: newId,
startTime: this.splitTime,
duration: rightVisibleDuration,
trimStart: element.trimStart + leftSourceSpan,
name: `${element.name} (right)`,
animations: rightAnimations,
...(retimeRef !== undefined ? { retime: retimeRef } : {}),
},
normalizeTimelineElement({
element: {
...element,
id: newId,
startTime: this.splitTime,
duration: rightVisibleDuration,
trimStart: element.trimStart + leftSourceSpan,
name: `${element.name} (right)`,
animations: rightAnimations,
...(retimeRef !== undefined ? { retime: retimeRef } : {}),
},
}),
];
} else {
// "both" - split into two pieces
Expand All @@ -132,24 +144,28 @@ export class SplitElementsCommand extends Command {
elementId: secondElementId,
});
splitResult = [
{
...element,
duration: leftVisibleDuration,
trimEnd: element.trimEnd + rightSourceSpan,
name: `${element.name} (left)`,
animations: leftAnimations,
...(retimeRef !== undefined ? { retime: retimeRef } : {}),
},
{
...element,
id: secondElementId,
startTime: this.splitTime,
duration: rightVisibleDuration,
trimStart: element.trimStart + leftSourceSpan,
name: `${element.name} (right)`,
animations: rightAnimations,
...(retimeRef !== undefined ? { retime: retimeRef } : {}),
},
normalizeTimelineElement({
element: {
...element,
duration: leftVisibleDuration,
trimEnd: element.trimEnd + rightSourceSpan,
name: `${element.name} (left)`,
animations: leftAnimations,
...(retimeRef !== undefined ? { retime: retimeRef } : {}),
},
}),
normalizeTimelineElement({
element: {
...element,
id: secondElementId,
startTime: this.splitTime,
duration: rightVisibleDuration,
trimStart: element.trimStart + leftSourceSpan,
name: `${element.name} (right)`,
animations: rightAnimations,
...(retimeRef !== undefined ? { retime: retimeRef } : {}),
},
}),
];
}

Expand Down
8 changes: 6 additions & 2 deletions apps/web/src/core/managers/timeline-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import type {
RetimeConfig,
} from "@/timeline";
import { calculateTotalDuration } from "@/timeline";
import { normalizeSceneTracks } from "@/timeline/normalize";
import { quantizeMediaTime } from "@/wasm/ticks";
import { findTrackInSceneTracks } from "@/timeline/track-element-update";
import {
canElementBeHidden,
Expand Down Expand Up @@ -210,7 +212,7 @@ export class TimelineManager {
}

getLastFrameTime(): number {
const duration = this.getTotalDuration();
const duration = quantizeMediaTime({ time: this.getTotalDuration() });
const fps = this.editor.project.getActive()?.settings.fps;
if (!fps || duration <= 0) return duration;
return lastFrameTime({ duration, rate: fps }) ?? duration;
Expand Down Expand Up @@ -926,7 +928,9 @@ export class TimelineManager {
updateTracks(newTracks: SceneTracks): void {
this.previewOverlay.clear();
this.previewTracks = null;
this.editor.scenes.updateSceneTracks({ tracks: newTracks });
this.editor.scenes.updateSceneTracks({
tracks: normalizeSceneTracks({ tracks: newTracks }),
});
this.notify();
}
}
151 changes: 151 additions & 0 deletions apps/web/src/services/storage/migrations/__tests__/v27-to-v28.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import { describe, expect, test } from "bun:test";
import { transformProjectV27ToV28 } from "../transformers/v27-to-v28";

describe("V27 to V28 Migration", () => {
test("rounds persisted fractional media times to integer ticks", () => {
const result = transformProjectV27ToV28({
project: {
id: "project-v27-retime",
version: 27,
metadata: {
id: "project-v27-retime",
name: "Project",
duration: 4052374.193548387,
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
},
timelineViewState: {
zoomLevel: 1,
scrollLeft: 120,
playheadTime: 4052374.193548387,
},
scenes: [
{
id: "scene-1",
name: "Scene",
isMain: true,
bookmarks: [{ time: 4052374.193548387, duration: 17.9 }],
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
tracks: {
main: {
id: "track-1",
name: "Main",
type: "video",
muted: false,
hidden: false,
elements: [
{
id: "element-1",
type: "video",
name: "Clip",
startTime: 10.4,
duration: 290322.5806451613,
trimStart: 30.5,
trimEnd: 11.2,
sourceDuration: 290364.2806451613,
retime: {
rate: 1.24,
},
transform: {
position: { x: 0, y: 0 },
scaleX: 1,
scaleY: 1,
rotate: 0,
},
opacity: 1,
animations: {
bindings: {
opacity: {
path: "opacity",
kind: "number",
components: [
{
key: "value",
channelId: "opacity:value",
},
],
},
},
channels: {
"opacity:value": {
kind: "scalar",
keys: [
{
id: "key-1",
time: 120.75,
value: 1,
segmentToNext: "bezier",
tangentMode: "flat",
rightHandle: {
dt: 15.25,
dv: 0.2,
},
},
],
},
},
},
},
],
},
overlay: [],
audio: [],
},
},
],
settings: {
fps: { numerator: 30, denominator: 1 },
canvasSize: { width: 1920, height: 1080 },
background: { type: "color", color: "#000000" },
},
},
});

expect(result.skipped).toBe(false);
expect(result.project.version).toBe(28);

const metadata = result.project.metadata as Record<string, unknown>;
expect(metadata.duration).toBe(4_052_374);

const timelineViewState = result.project.timelineViewState as Record<
string,
unknown
>;
expect(timelineViewState.playheadTime).toBe(4_052_374);

const scene = (result.project.scenes as Array<Record<string, unknown>>)[0];
expect(scene.bookmarks).toEqual([{ time: 4_052_374, duration: 18 }]);

const tracks = scene.tracks as Record<string, unknown>;
const mainTrack = tracks.main as Record<string, unknown>;
const element = (mainTrack.elements as Array<Record<string, unknown>>)[0];
expect(element.startTime).toBe(10);
expect(element.duration).toBe(290_323);
expect(element.trimStart).toBe(31);
expect(element.trimEnd).toBe(11);
expect(element.sourceDuration).toBe(290_364);

const animations = element.animations as Record<string, unknown>;
const channels = animations.channels as Record<
string,
Record<string, unknown>
>;
expect(channels["opacity:value"]).toEqual({
kind: "scalar",
keys: [
{
id: "key-1",
time: 121,
value: 1,
segmentToNext: "bezier",
tangentMode: "flat",
rightHandle: {
dt: 15,
dv: 0.2,
},
},
],
});
});
});
4 changes: 3 additions & 1 deletion apps/web/src/services/storage/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import { V23toV24Migration } from "./v23-to-v24";
import { V24toV25Migration } from "./v24-to-v25";
import { V25toV26Migration } from "./v25-to-v26";
import { V26toV27Migration } from "./v26-to-v27";
import { V27toV28Migration } from "./v27-to-v28";
export { runStorageMigrations } from "./runner";
export type { MigrationProgress } from "./runner";

export const CURRENT_PROJECT_VERSION = 27;
export const CURRENT_PROJECT_VERSION = 28;

export const migrations = [
new V0toV1Migration(),
Expand Down Expand Up @@ -59,4 +60,5 @@ export const migrations = [
new V24toV25Migration(),
new V25toV26Migration(),
new V26toV27Migration(),
new V27toV28Migration(),
];
Loading
Loading