summaryrefslogtreecommitdiff
path: root/apps/web/src/lib/commands/timeline/element
diff options
context:
space:
mode:
Diffstat (limited to 'apps/web/src/lib/commands/timeline/element')
-rw-r--r--apps/web/src/lib/commands/timeline/element/delete-elements.ts72
-rw-r--r--apps/web/src/lib/commands/timeline/element/effects/add-effect.ts75
-rw-r--r--apps/web/src/lib/commands/timeline/element/effects/index.ts5
-rw-r--r--apps/web/src/lib/commands/timeline/element/effects/remove-effect.ts66
-rw-r--r--apps/web/src/lib/commands/timeline/element/effects/reorder-effect.ts74
-rw-r--r--apps/web/src/lib/commands/timeline/element/effects/toggle-effect.ts68
-rw-r--r--apps/web/src/lib/commands/timeline/element/effects/update-effect-params.ts87
-rw-r--r--apps/web/src/lib/commands/timeline/element/index.ts11
-rw-r--r--apps/web/src/lib/commands/timeline/element/insert-element.ts291
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/index.ts6
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/remove-effect-param-keyframe.ts69
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/remove-keyframe.ts107
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/retime-keyframe.ts75
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/update-scalar-keyframe-curve.ts85
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/upsert-effect-param-keyframe.ts104
-rw-r--r--apps/web/src/lib/commands/timeline/element/keyframes/upsert-keyframe.ts96
-rw-r--r--apps/web/src/lib/commands/timeline/element/masks/delete-custom-mask-points.ts131
-rw-r--r--apps/web/src/lib/commands/timeline/element/masks/index.ts4
-rw-r--r--apps/web/src/lib/commands/timeline/element/masks/insert-custom-mask-point.ts169
-rw-r--r--apps/web/src/lib/commands/timeline/element/masks/remove-mask.ts65
-rw-r--r--apps/web/src/lib/commands/timeline/element/masks/toggle-mask-inverted.ts76
-rw-r--r--apps/web/src/lib/commands/timeline/element/toggle-source-audio-separation.ts145
-rw-r--r--apps/web/src/lib/commands/timeline/element/update-elements.ts75
23 files changed, 0 insertions, 1956 deletions
diff --git a/apps/web/src/lib/commands/timeline/element/delete-elements.ts b/apps/web/src/lib/commands/timeline/element/delete-elements.ts
deleted file mode 100644
index 0fc7948f..00000000
--- a/apps/web/src/lib/commands/timeline/element/delete-elements.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import type { SceneTracks } from "@/lib/timeline";
-import { EditorCore } from "@/core";
-import type { TimelineTrack } from "@/lib/timeline";
-
-function removeTrackElements<TTrack extends TimelineTrack>({
- track,
- elements,
-}: {
- track: TTrack;
- elements: { trackId: string; elementId: string }[];
-}): TTrack {
- const nextElements = track.elements.filter(
- (element) =>
- !elements.some(
- (target) =>
- target.trackId === track.id && target.elementId === element.id,
- ),
- );
-
- return { ...track, elements: nextElements } as TTrack;
-}
-
-export class DeleteElementsCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly elements: { trackId: string; elementId: string }[];
-
- constructor({
- elements,
- }: {
- elements: { trackId: string; elementId: string }[];
- }) {
- super();
- this.elements = elements;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks: SceneTracks = {
- overlay: this.savedState.overlay.map((track) =>
- removeTrackElements({ track, elements: this.elements }),
- ),
- main: removeTrackElements({
- track: this.savedState.main,
- elements: this.elements,
- }),
- audio: this.savedState.audio.map((track) =>
- removeTrackElements({ track, elements: this.elements }),
- ),
- };
-
- editor.timeline.updateTracks(updatedTracks);
-
- return {
- selection: {
- selectedElements: [],
- selectedKeyframes: [],
- keyframeSelectionAnchor: null,
- selectedMaskPoints: null,
- },
- };
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/effects/add-effect.ts b/apps/web/src/lib/commands/timeline/element/effects/add-effect.ts
deleted file mode 100644
index 902ca341..00000000
--- a/apps/web/src/lib/commands/timeline/element/effects/add-effect.ts
+++ /dev/null
@@ -1,75 +0,0 @@
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { EditorCore } from "@/core";
-import { isVisualElement, updateElementInSceneTracks } from "@/lib/timeline";
-import type { SceneTracks, VisualElement } from "@/lib/timeline";
-import { buildDefaultEffectInstance } from "@/lib/effects";
-
-function addEffectToElement({
- element,
- effectType,
-}: {
- element: VisualElement;
- effectType: string;
-}): VisualElement {
- const instance = buildDefaultEffectInstance({ effectType });
- const currentEffects = element.effects ?? [];
- return { ...element, effects: [...currentEffects, instance] };
-}
-
-export class AddClipEffectCommand extends Command {
- private savedState: SceneTracks | null = null;
- private effectId: string | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly effectType: string;
-
- constructor({
- trackId,
- elementId,
- effectType,
- }: {
- trackId: string;
- elementId: string;
- effectType: string;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.effectType = effectType;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isVisualElement,
- update: (element) => {
- const updated = addEffectToElement({
- element: element as VisualElement,
- effectType: this.effectType,
- });
- const effects = updated.effects ?? [];
- this.effectId = effects[effects.length - 1]?.id ?? null;
- return updated;
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-
- getEffectId(): string | null {
- return this.effectId;
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/effects/index.ts b/apps/web/src/lib/commands/timeline/element/effects/index.ts
deleted file mode 100644
index 133ce661..00000000
--- a/apps/web/src/lib/commands/timeline/element/effects/index.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-export { AddClipEffectCommand } from "./add-effect";
-export { RemoveClipEffectCommand } from "./remove-effect";
-export { ToggleClipEffectCommand } from "./toggle-effect";
-export { UpdateClipEffectParamsCommand } from "./update-effect-params";
-export { ReorderClipEffectsCommand } from "./reorder-effect";
diff --git a/apps/web/src/lib/commands/timeline/element/effects/remove-effect.ts b/apps/web/src/lib/commands/timeline/element/effects/remove-effect.ts
deleted file mode 100644
index f453f2cc..00000000
--- a/apps/web/src/lib/commands/timeline/element/effects/remove-effect.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { EditorCore } from "@/core";
-import { isVisualElement, updateElementInSceneTracks } from "@/lib/timeline";
-import type { SceneTracks, VisualElement } from "@/lib/timeline";
-
-function removeEffectFromElement({
- element,
- effectId,
-}: {
- element: VisualElement;
- effectId: string;
-}): VisualElement {
- const currentEffects = element.effects ?? [];
- const filtered = currentEffects.filter((effect) => effect.id !== effectId);
- return { ...element, effects: filtered };
-}
-
-export class RemoveClipEffectCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly effectId: string;
-
- constructor({
- trackId,
- elementId,
- effectId,
- }: {
- trackId: string;
- elementId: string;
- effectId: string;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.effectId = effectId;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isVisualElement,
- update: (element) => {
- return removeEffectFromElement({
- element: element as VisualElement,
- effectId: this.effectId,
- });
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/effects/reorder-effect.ts b/apps/web/src/lib/commands/timeline/element/effects/reorder-effect.ts
deleted file mode 100644
index 28b7c4e1..00000000
--- a/apps/web/src/lib/commands/timeline/element/effects/reorder-effect.ts
+++ /dev/null
@@ -1,74 +0,0 @@
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { EditorCore } from "@/core";
-import { isVisualElement, updateElementInSceneTracks } from "@/lib/timeline";
-import type { SceneTracks, VisualElement } from "@/lib/timeline";
-
-function reorderEffectsOnElement({
- element,
- fromIndex,
- toIndex,
-}: {
- element: VisualElement;
- fromIndex: number;
- toIndex: number;
-}): VisualElement {
- const effects = [...(element.effects ?? [])];
- const [moved] = effects.splice(fromIndex, 1);
- effects.splice(toIndex, 0, moved);
- return { ...element, effects };
-}
-
-export class ReorderClipEffectsCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly fromIndex: number;
- private readonly toIndex: number;
-
- constructor({
- trackId,
- elementId,
- fromIndex,
- toIndex,
- }: {
- trackId: string;
- elementId: string;
- fromIndex: number;
- toIndex: number;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.fromIndex = fromIndex;
- this.toIndex = toIndex;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isVisualElement,
- update: (element) => {
- return reorderEffectsOnElement({
- element: element as VisualElement,
- fromIndex: this.fromIndex,
- toIndex: this.toIndex,
- });
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/effects/toggle-effect.ts b/apps/web/src/lib/commands/timeline/element/effects/toggle-effect.ts
deleted file mode 100644
index a703cd94..00000000
--- a/apps/web/src/lib/commands/timeline/element/effects/toggle-effect.ts
+++ /dev/null
@@ -1,68 +0,0 @@
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { EditorCore } from "@/core";
-import { isVisualElement, updateElementInSceneTracks } from "@/lib/timeline";
-import type { SceneTracks, VisualElement } from "@/lib/timeline";
-
-export function toggleEffectOnElement({
- element,
- effectId,
-}: {
- element: VisualElement;
- effectId: string;
-}): VisualElement {
- const currentEffects = element.effects ?? [];
- const updated = currentEffects.map((effect) =>
- effect.id === effectId ? { ...effect, enabled: !effect.enabled } : effect,
- );
- return { ...element, effects: updated };
-}
-
-export class ToggleClipEffectCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly effectId: string;
-
- constructor({
- trackId,
- elementId,
- effectId,
- }: {
- trackId: string;
- elementId: string;
- effectId: string;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.effectId = effectId;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isVisualElement,
- update: (element) => {
- return toggleEffectOnElement({
- element: element as VisualElement,
- effectId: this.effectId,
- });
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/effects/update-effect-params.ts b/apps/web/src/lib/commands/timeline/element/effects/update-effect-params.ts
deleted file mode 100644
index 5e75c1f0..00000000
--- a/apps/web/src/lib/commands/timeline/element/effects/update-effect-params.ts
+++ /dev/null
@@ -1,87 +0,0 @@
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { EditorCore } from "@/core";
-import { isVisualElement, updateElementInSceneTracks } from "@/lib/timeline";
-import type { ParamValues } from "@/lib/params";
-import type { SceneTracks, VisualElement } from "@/lib/timeline";
-
-function updateEffectParamsOnElement({
- element,
- effectId,
- params,
-}: {
- element: VisualElement;
- effectId: string;
- params: Partial<ParamValues>;
-}): VisualElement {
- const currentEffects = element.effects ?? [];
- const updated = currentEffects.map((effect) => {
- if (effect.id !== effectId) {
- return effect;
- }
-
- const nextParams = { ...effect.params };
- for (const [key, value] of Object.entries(params)) {
- if (value !== undefined) {
- nextParams[key] = value;
- }
- }
-
- return { ...effect, params: nextParams };
- });
- return { ...element, effects: updated };
-}
-
-export class UpdateClipEffectParamsCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly effectId: string;
- private readonly params: Partial<ParamValues>;
-
- constructor({
- trackId,
- elementId,
- effectId,
- params,
- }: {
- trackId: string;
- elementId: string;
- effectId: string;
- params: Partial<ParamValues>;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.effectId = effectId;
- this.params = params;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isVisualElement,
- update: (element) => {
- return updateEffectParamsOnElement({
- element: element as VisualElement,
- effectId: this.effectId,
- params: this.params,
- });
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/index.ts b/apps/web/src/lib/commands/timeline/element/index.ts
deleted file mode 100644
index ff365801..00000000
--- a/apps/web/src/lib/commands/timeline/element/index.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-export { InsertElementCommand } from "./insert-element";
-export { DeleteElementsCommand } from "./delete-elements";
-export { DuplicateElementsCommand } from "./duplicate-elements";
-export { SplitElementsCommand } from "./split-elements";
-export { UpdateElementsCommand } from "./update-elements";
-export { ToggleSourceAudioSeparationCommand } from "./toggle-source-audio-separation";
-export { MoveElementCommand } from "./move-elements";
-
-export * from "./keyframes";
-export * from "./effects";
-export * from "./masks";
diff --git a/apps/web/src/lib/commands/timeline/element/insert-element.ts b/apps/web/src/lib/commands/timeline/element/insert-element.ts
deleted file mode 100644
index ed05b8c6..00000000
--- a/apps/web/src/lib/commands/timeline/element/insert-element.ts
+++ /dev/null
@@ -1,291 +0,0 @@
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { EditorCore } from "@/core";
-import type {
- CreateTimelineElement,
- SceneTracks,
- TimelineElement,
- TrackType,
-} from "@/lib/timeline";
-import { generateUUID } from "@/utils/id";
-import { requiresMediaId } from "@/lib/timeline/element-utils";
-import type { MediaAsset } from "@/lib/media/types";
-import { DEFAULT_NEW_ELEMENT_DURATION } from "@/lib/timeline/creation";
-import { floatToFrameRate } from "@/lib/fps/utils";
-import { graphicsRegistry, registerDefaultGraphics } from "@/lib/graphics";
-import {
- applyPlacement,
- canElementGoOnTrack,
- resolveTrackPlacement,
- validateElementTrackCompatibility,
-} from "@/lib/timeline/placement";
-
-type InsertElementPlacement =
- | { mode: "explicit"; trackId: string }
- | { mode: "auto"; trackType?: TrackType; insertIndex?: number };
-
-export interface InsertElementParams {
- element: CreateTimelineElement;
- placement: InsertElementPlacement;
-}
-
-export class InsertElementCommand extends Command {
- private elementId: string;
- private savedState: SceneTracks | null = null;
- private targetTrackId: string | null = null;
-
- constructor({ element, placement }: InsertElementParams) {
- super();
- this.elementId = generateUUID();
- this.element = element;
- this.placement = placement;
- }
-
- private element: CreateTimelineElement;
- private placement: InsertElementPlacement;
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- if (!this.validateElementBasics({ element: this.element })) {
- return;
- }
-
- const totalElementsInTimeline =
- this.savedState.main.elements.length +
- this.savedState.overlay.reduce(
- (total, track) => total + track.elements.length,
- 0,
- ) +
- this.savedState.audio.reduce(
- (total, track) => total + track.elements.length,
- 0,
- );
- const isFirstElement = totalElementsInTimeline === 0;
-
- const newElement = this.buildElement({ element: this.element });
- const updateResult = this.applyPlacementResult({
- tracks: this.savedState,
- element: newElement,
- });
-
- if (!updateResult) {
- return;
- }
-
- const { updatedTracks, targetTrackId } = updateResult;
- this.targetTrackId = targetTrackId;
-
- const isVisualMedia =
- newElement.type === "video" || newElement.type === "image";
-
- if (isFirstElement && isVisualMedia) {
- const mediaAssets = editor.media.getAssets();
- const activeProject = editor.project.getActive();
- const asset = mediaAssets.find(
- (item: MediaAsset) => item.id === newElement.mediaId,
- );
-
- if (asset?.width && asset?.height) {
- const nextCanvasSize = { width: asset.width, height: asset.height };
- const shouldSetOriginalCanvasSize =
- !activeProject?.settings.originalCanvasSize;
- editor.project.updateSettings({
- settings: {
- canvasSize: nextCanvasSize,
- ...(shouldSetOriginalCanvasSize
- ? { originalCanvasSize: nextCanvasSize }
- : {}),
- },
- pushHistory: false,
- });
- }
-
- if (asset?.type === "video" && asset?.fps) {
- editor.project.updateSettings({
- settings: { fps: floatToFrameRate(asset.fps) },
- pushHistory: false,
- });
- }
- }
-
- editor.timeline.updateTracks(updatedTracks);
-
- return {
- selection: {
- selectedElements: [
- { trackId: targetTrackId, elementId: this.elementId },
- ],
- selectedKeyframes: [],
- keyframeSelectionAnchor: null,
- selectedMaskPoints: null,
- },
- };
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-
- getElementId(): string {
- return this.elementId;
- }
-
- getTrackId(): string | null {
- return this.targetTrackId;
- }
-
- private buildElement({
- element,
- }: {
- element: CreateTimelineElement;
- }): TimelineElement {
- return {
- ...element,
- id: this.elementId,
- startTime: element.startTime,
- trimStart: element.trimStart ?? 0,
- trimEnd: element.trimEnd ?? 0,
- duration: element.duration ?? DEFAULT_NEW_ELEMENT_DURATION,
- } as TimelineElement;
- }
-
- private validateElementBasics({
- element,
- }: {
- element: CreateTimelineElement;
- }): boolean {
- if (requiresMediaId({ element }) && !("mediaId" in element)) {
- console.error("Element requires mediaId");
- return false;
- }
-
- if (
- element.type === "audio" &&
- element.sourceType === "library" &&
- !element.sourceUrl
- ) {
- console.error("Library audio element must have sourceUrl");
- return false;
- }
-
- if (element.type === "sticker" && !element.stickerId) {
- console.error("Sticker element must have stickerId");
- return false;
- }
-
- if (element.type === "graphic") {
- registerDefaultGraphics();
- if (
- !element.definitionId ||
- !graphicsRegistry.has(element.definitionId)
- ) {
- console.error("Graphic element must have a valid definitionId");
- return false;
- }
- }
-
- if (element.type === "text" && !element.content) {
- console.error("Text element must have content");
- return false;
- }
-
- if (element.type === "effect" && !element.effectType) {
- console.error("Effect element must have effectType");
- return false;
- }
-
- return true;
- }
-
- private applyPlacementResult({
- tracks,
- element,
- }: {
- tracks: SceneTracks;
- element: TimelineElement;
- }): { updatedTracks: SceneTracks; targetTrackId: string } | null {
- const placement = this.placement;
-
- if (
- placement.mode === "auto" &&
- placement.trackType &&
- !canElementGoOnTrack({
- elementType: element.type,
- trackType: placement.trackType,
- })
- ) {
- console.error(
- `${element.type} elements cannot be placed on ${placement.trackType} tracks`,
- );
- return null;
- }
-
- const placementResult = resolveTrackPlacement({
- tracks,
- ...(placement.mode === "auto" && placement.trackType
- ? { trackType: placement.trackType }
- : { elementType: element.type }),
- timeSpans: [
- {
- startTime: element.startTime,
- duration: element.duration,
- },
- ],
- strategy:
- placement.mode === "explicit"
- ? { type: "explicit", trackId: placement.trackId }
- : { type: "firstAvailable" },
- });
- if (!placementResult) {
- if (placement.mode === "explicit") {
- const targetTrack =
- tracks.main.id === placement.trackId
- ? tracks.main
- : (tracks.overlay.find((track) => track.id === placement.trackId) ??
- tracks.audio.find((track) => track.id === placement.trackId));
- if (!targetTrack) {
- console.error("Track not found:", placement.trackId);
- return null;
- }
-
- const validation = validateElementTrackCompatibility({
- element,
- track: targetTrack,
- });
- console.error(validation.errorMessage);
- }
-
- return null;
- }
-
- const elementToPlace =
- placementResult.kind === "existingTrack"
- ? {
- ...element,
- startTime: placementResult.adjustedStartTime ?? element.startTime,
- }
- : element;
-
- const appliedPlacement = applyPlacement({
- tracks,
- placementResult,
- elements: [elementToPlace],
- newTrackInsertIndexOverride:
- placement.mode === "auto" && typeof placement.insertIndex === "number"
- ? placement.insertIndex
- : undefined,
- });
- if (!appliedPlacement) {
- return null;
- }
-
- return {
- updatedTracks: appliedPlacement.updatedTracks,
- targetTrackId: appliedPlacement.targetTrackId,
- };
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/index.ts b/apps/web/src/lib/commands/timeline/element/keyframes/index.ts
deleted file mode 100644
index 319a827b..00000000
--- a/apps/web/src/lib/commands/timeline/element/keyframes/index.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-export * from "./remove-effect-param-keyframe";
-export * from "./remove-keyframe";
-export * from "./retime-keyframe";
-export * from "./update-scalar-keyframe-curve";
-export * from "./upsert-effect-param-keyframe";
-export * from "./upsert-keyframe";
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/remove-effect-param-keyframe.ts b/apps/web/src/lib/commands/timeline/element/keyframes/remove-effect-param-keyframe.ts
deleted file mode 100644
index b1ef4aef..00000000
--- a/apps/web/src/lib/commands/timeline/element/keyframes/remove-effect-param-keyframe.ts
+++ /dev/null
@@ -1,69 +0,0 @@
-import { EditorCore } from "@/core";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { removeEffectParamKeyframe } from "@/lib/animation/effect-param-channel";
-import { updateElementInSceneTracks } from "@/lib/timeline";
-import { isVisualElement } from "@/lib/timeline/element-utils";
-import type { SceneTracks } from "@/lib/timeline";
-
-export class RemoveEffectParamKeyframeCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly effectId: string;
- private readonly paramKey: string;
- private readonly keyframeId: string;
-
- constructor({
- trackId,
- elementId,
- effectId,
- paramKey,
- keyframeId,
- }: {
- trackId: string;
- elementId: string;
- effectId: string;
- paramKey: string;
- keyframeId: string;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.effectId = effectId;
- this.paramKey = paramKey;
- this.keyframeId = keyframeId;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isVisualElement,
- update: (element) => {
- const animations = removeEffectParamKeyframe({
- animations: element.animations,
- effectId: this.effectId,
- paramKey: this.paramKey,
- keyframeId: this.keyframeId,
- });
- return { ...element, animations };
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (!this.savedState) {
- return;
- }
-
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/remove-keyframe.ts b/apps/web/src/lib/commands/timeline/element/keyframes/remove-keyframe.ts
deleted file mode 100644
index 3c3d1c6b..00000000
--- a/apps/web/src/lib/commands/timeline/element/keyframes/remove-keyframe.ts
+++ /dev/null
@@ -1,107 +0,0 @@
-import { EditorCore } from "@/core";
-import {
- hasKeyframesForPath,
- removeElementKeyframe,
- resolveAnimationTarget,
-} from "@/lib/animation";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { updateElementInSceneTracks } from "@/lib/timeline";
-import type { AnimationPath, AnimationValue } from "@/lib/animation/types";
-import type { SceneTracks, TimelineElement } from "@/lib/timeline";
-
-function removeKeyframeAndPersist({
- element,
- propertyPath,
- keyframeId,
- valueAtPlayhead,
-}: {
- element: TimelineElement;
- propertyPath: AnimationPath;
- keyframeId: string;
- valueAtPlayhead: AnimationValue | null;
-}): TimelineElement {
- const target = resolveAnimationTarget({ element, path: propertyPath });
- if (!target) {
- return element;
- }
-
- const nextAnimations = removeElementKeyframe({
- animations: element.animations,
- propertyPath,
- keyframeId,
- });
-
- const isChannelNowEmpty = !hasKeyframesForPath({
- animations: nextAnimations,
- propertyPath,
- });
-
- // When clearing all keyframes, preserve the value at the playhead (what the user was seeing)
- const shouldPersistToBase = isChannelNowEmpty && valueAtPlayhead !== null;
-
- const baseElement = shouldPersistToBase
- ? target.setBaseValue({ value: valueAtPlayhead })
- : element;
-
- return { ...baseElement, animations: nextAnimations };
-}
-
-export class RemoveKeyframeCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly propertyPath: AnimationPath;
- private readonly keyframeId: string;
- private readonly valueAtPlayhead: AnimationValue | null;
-
- constructor({
- trackId,
- elementId,
- propertyPath,
- keyframeId,
- valueAtPlayhead,
- }: {
- trackId: string;
- elementId: string;
- propertyPath: AnimationPath;
- keyframeId: string;
- valueAtPlayhead: AnimationValue | null;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.propertyPath = propertyPath;
- this.keyframeId = keyframeId;
- this.valueAtPlayhead = valueAtPlayhead;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- update: (element) =>
- removeKeyframeAndPersist({
- element,
- propertyPath: this.propertyPath,
- keyframeId: this.keyframeId,
- valueAtPlayhead: this.valueAtPlayhead,
- }),
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (!this.savedState) {
- return;
- }
-
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/retime-keyframe.ts b/apps/web/src/lib/commands/timeline/element/keyframes/retime-keyframe.ts
deleted file mode 100644
index ad7f5651..00000000
--- a/apps/web/src/lib/commands/timeline/element/keyframes/retime-keyframe.ts
+++ /dev/null
@@ -1,75 +0,0 @@
-import { EditorCore } from "@/core";
-import { resolveAnimationTarget, retimeElementKeyframe } from "@/lib/animation";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { updateElementInSceneTracks } from "@/lib/timeline";
-import type { AnimationPath } from "@/lib/animation/types";
-import type { SceneTracks } from "@/lib/timeline";
-
-export class RetimeKeyframeCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly propertyPath: AnimationPath;
- private readonly keyframeId: string;
- private readonly nextTime: number;
-
- constructor({
- trackId,
- elementId,
- propertyPath,
- keyframeId,
- nextTime,
- }: {
- trackId: string;
- elementId: string;
- propertyPath: AnimationPath;
- keyframeId: string;
- nextTime: number;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.propertyPath = propertyPath;
- this.keyframeId = keyframeId;
- this.nextTime = nextTime;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- update: (element) => {
- if (!resolveAnimationTarget({ element, path: this.propertyPath })) {
- return element;
- }
-
- const boundedTime = Math.max(0, Math.min(this.nextTime, element.duration));
- return {
- ...element,
- animations: retimeElementKeyframe({
- animations: element.animations,
- propertyPath: this.propertyPath,
- keyframeId: this.keyframeId,
- time: boundedTime,
- }),
- };
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (!this.savedState) {
- return;
- }
-
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/update-scalar-keyframe-curve.ts b/apps/web/src/lib/commands/timeline/element/keyframes/update-scalar-keyframe-curve.ts
deleted file mode 100644
index 9a4c2304..00000000
--- a/apps/web/src/lib/commands/timeline/element/keyframes/update-scalar-keyframe-curve.ts
+++ /dev/null
@@ -1,85 +0,0 @@
-import { EditorCore } from "@/core";
-import {
- resolveAnimationTarget,
- updateScalarKeyframeCurve,
-} from "@/lib/animation";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { updateElementInSceneTracks } from "@/lib/timeline";
-import type {
- AnimationPath,
- ScalarCurveKeyframePatch,
-} from "@/lib/animation/types";
-import type { SceneTracks } from "@/lib/timeline";
-
-export class UpdateScalarKeyframeCurveCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly propertyPath: AnimationPath;
- private readonly componentKey: string;
- private readonly keyframeId: string;
- private readonly patch: ScalarCurveKeyframePatch;
-
- constructor({
- trackId,
- elementId,
- propertyPath,
- componentKey,
- keyframeId,
- patch,
- }: {
- trackId: string;
- elementId: string;
- propertyPath: AnimationPath;
- componentKey: string;
- keyframeId: string;
- patch: ScalarCurveKeyframePatch;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.propertyPath = propertyPath;
- this.componentKey = componentKey;
- this.keyframeId = keyframeId;
- this.patch = patch;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- update: (element) => {
- if (!resolveAnimationTarget({ element, path: this.propertyPath })) {
- return element;
- }
-
- return {
- ...element,
- animations: updateScalarKeyframeCurve({
- animations: element.animations,
- propertyPath: this.propertyPath,
- componentKey: this.componentKey,
- keyframeId: this.keyframeId,
- patch: this.patch,
- }),
- };
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (!this.savedState) {
- return;
- }
-
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/upsert-effect-param-keyframe.ts b/apps/web/src/lib/commands/timeline/element/keyframes/upsert-effect-param-keyframe.ts
deleted file mode 100644
index f436721a..00000000
--- a/apps/web/src/lib/commands/timeline/element/keyframes/upsert-effect-param-keyframe.ts
+++ /dev/null
@@ -1,104 +0,0 @@
-import { EditorCore } from "@/core";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import {
- buildEffectParamPath,
- resolveAnimationTarget,
- upsertPathKeyframe,
-} from "@/lib/animation";
-import { updateElementInSceneTracks } from "@/lib/timeline";
-import { isVisualElement } from "@/lib/timeline/element-utils";
-import type { AnimationInterpolation } from "@/lib/animation/types";
-import type { SceneTracks } from "@/lib/timeline";
-
-export class UpsertEffectParamKeyframeCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly effectId: string;
- private readonly paramKey: string;
- private readonly time: number;
- private readonly value: number | string | boolean;
- private readonly interpolation: AnimationInterpolation | undefined;
- private readonly keyframeId: string | undefined;
-
- constructor({
- trackId,
- elementId,
- effectId,
- paramKey,
- time,
- value,
- interpolation,
- keyframeId,
- }: {
- trackId: string;
- elementId: string;
- effectId: string;
- paramKey: string;
- time: number;
- value: number | string | boolean;
- interpolation?: AnimationInterpolation;
- keyframeId?: string;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.effectId = effectId;
- this.paramKey = paramKey;
- this.time = time;
- this.value = value;
- this.interpolation = interpolation;
- this.keyframeId = keyframeId;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isVisualElement,
- update: (element) => {
- const boundedTime = Math.max(0, Math.min(this.time, element.duration));
- const propertyPath = buildEffectParamPath({
- effectId: this.effectId,
- paramKey: this.paramKey,
- });
- const target = resolveAnimationTarget({
- element,
- path: propertyPath,
- });
- if (!target) {
- return element;
- }
-
- const animations = upsertPathKeyframe({
- animations: element.animations,
- propertyPath,
- time: boundedTime,
- value: this.value,
- interpolation: this.interpolation,
- keyframeId: this.keyframeId,
- kind: target.kind,
- defaultInterpolation: target.defaultInterpolation,
- coerceValue: target.coerceValue,
- });
- return { ...element, animations };
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (!this.savedState) {
- return;
- }
-
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/keyframes/upsert-keyframe.ts b/apps/web/src/lib/commands/timeline/element/keyframes/upsert-keyframe.ts
deleted file mode 100644
index f77e69b8..00000000
--- a/apps/web/src/lib/commands/timeline/element/keyframes/upsert-keyframe.ts
+++ /dev/null
@@ -1,96 +0,0 @@
-import { EditorCore } from "@/core";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { resolveAnimationTarget, upsertPathKeyframe } from "@/lib/animation";
-import { updateElementInSceneTracks } from "@/lib/timeline";
-import type { SceneTracks } from "@/lib/timeline";
-import type {
- AnimationPath,
- AnimationInterpolation,
- AnimationValue,
-} from "@/lib/animation/types";
-
-export class UpsertKeyframeCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly propertyPath: AnimationPath;
- private readonly time: number;
- private readonly value: AnimationValue;
- private readonly interpolation: AnimationInterpolation | undefined;
- private readonly keyframeId: string | undefined;
-
- constructor({
- trackId,
- elementId,
- propertyPath,
- time,
- value,
- interpolation,
- keyframeId,
- }: {
- trackId: string;
- elementId: string;
- propertyPath: AnimationPath;
- time: number;
- value: AnimationValue;
- interpolation?: AnimationInterpolation;
- keyframeId?: string;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.propertyPath = propertyPath;
- this.time = time;
- this.value = value;
- this.interpolation = interpolation;
- this.keyframeId = keyframeId;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- update: (element) => {
- const target = resolveAnimationTarget({
- element,
- path: this.propertyPath,
- });
- if (!target) {
- return element;
- }
-
- const boundedTime = Math.max(0, Math.min(this.time, element.duration));
- return {
- ...element,
- animations: upsertPathKeyframe({
- animations: element.animations,
- propertyPath: this.propertyPath,
- time: boundedTime,
- value: this.value,
- interpolation: this.interpolation,
- keyframeId: this.keyframeId,
- kind: target.kind,
- defaultInterpolation: target.defaultInterpolation,
- coerceValue: target.coerceValue,
- }),
- };
- },
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (!this.savedState) {
- return;
- }
-
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/masks/delete-custom-mask-points.ts b/apps/web/src/lib/commands/timeline/element/masks/delete-custom-mask-points.ts
deleted file mode 100644
index 346eb8d9..00000000
--- a/apps/web/src/lib/commands/timeline/element/masks/delete-custom-mask-points.ts
+++ /dev/null
@@ -1,131 +0,0 @@
-import { EditorCore } from "@/core";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import {
- getCustomMaskClosedStateAfterPointRemoval,
- removeCustomMaskPoints,
-} from "@/lib/masks/custom-path";
-import type { CustomMask } from "@/lib/masks/types";
-import { isMaskableElement, updateElementInSceneTracks } from "@/lib/timeline";
-import type { MaskableElement, SceneTracks } from "@/lib/timeline";
-
-function deletePointsFromCustomMask({
- mask,
- pointIds,
-}: {
- mask: CustomMask;
- pointIds: string[];
-}): CustomMask {
- const points = mask.params.path;
- const nextPoints = removeCustomMaskPoints({ points, pointIds });
- if (nextPoints.length === points.length) {
- return mask;
- }
-
- return {
- ...mask,
- params: {
- ...mask.params,
- path: nextPoints,
- closed: getCustomMaskClosedStateAfterPointRemoval({
- wasClosed: mask.params.closed,
- remainingPointCount: nextPoints.length,
- }),
- },
- };
-}
-
-function deletePointsFromElementMask({
- element,
- maskId,
- pointIds,
-}: {
- element: MaskableElement;
- maskId: string;
- pointIds: string[];
-}): { element: MaskableElement; didDeletePoints: boolean } {
- const currentMasks = element.masks ?? [];
- let didDeletePoints = false;
- const nextMasks = currentMasks.map((mask) => {
- if (mask.id !== maskId || mask.type !== "custom") {
- return mask;
- }
-
- const nextMask = deletePointsFromCustomMask({
- mask,
- pointIds,
- });
- didDeletePoints ||= nextMask !== mask;
- return nextMask;
- });
-
- return {
- element: didDeletePoints ? { ...element, masks: nextMasks } : element,
- didDeletePoints,
- };
-}
-
-export class DeleteCustomMaskPointsCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly maskId: string;
- private readonly pointIds: string[];
-
- constructor({
- trackId,
- elementId,
- maskId,
- pointIds,
- }: {
- trackId: string;
- elementId: string;
- maskId: string;
- pointIds: string[];
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.maskId = maskId;
- this.pointIds = pointIds;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- let didDeletePoints = false;
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isMaskableElement,
- update: (element) => {
- const result = deletePointsFromElementMask({
- element: element as MaskableElement,
- maskId: this.maskId,
- pointIds: this.pointIds,
- });
- didDeletePoints ||= result.didDeletePoints;
- return result.element;
- },
- });
-
- if (didDeletePoints) {
- editor.timeline.updateTracks(updatedTracks);
- return {
- selection: {
- selectedMaskPoints: null,
- },
- };
- }
-
- return undefined;
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/masks/index.ts b/apps/web/src/lib/commands/timeline/element/masks/index.ts
deleted file mode 100644
index dd6373b1..00000000
--- a/apps/web/src/lib/commands/timeline/element/masks/index.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-export { DeleteCustomMaskPointsCommand } from "./delete-custom-mask-points";
-export { InsertCustomMaskPointCommand } from "./insert-custom-mask-point";
-export { RemoveMaskCommand } from "./remove-mask";
-export { ToggleMaskInvertedCommand } from "./toggle-mask-inverted";
diff --git a/apps/web/src/lib/commands/timeline/element/masks/insert-custom-mask-point.ts b/apps/web/src/lib/commands/timeline/element/masks/insert-custom-mask-point.ts
deleted file mode 100644
index 9f837836..00000000
--- a/apps/web/src/lib/commands/timeline/element/masks/insert-custom-mask-point.ts
+++ /dev/null
@@ -1,169 +0,0 @@
-import { EditorCore } from "@/core";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { insertPointOnCustomMaskSegment } from "@/lib/masks/definitions/custom";
-import type { ElementBounds } from "@/lib/preview/element-bounds";
-import type { CustomMask } from "@/lib/masks/types";
-import { isMaskableElement, updateElementInSceneTracks } from "@/lib/timeline";
-import type { MaskableElement, SceneTracks } from "@/lib/timeline";
-
-function insertPointIntoCustomMask({
- mask,
- segmentIndex,
- canvasPoint,
- bounds,
-}: {
- mask: CustomMask;
- segmentIndex: number;
- canvasPoint: { x: number; y: number };
- bounds: ElementBounds;
-}): { mask: CustomMask; insertedPointId: string | null } {
- const result = insertPointOnCustomMaskSegment({
- params: mask.params,
- segmentIndex,
- canvasPoint,
- bounds,
- });
- if (!result) {
- return {
- mask,
- insertedPointId: null,
- };
- }
-
- return {
- mask: {
- ...mask,
- params: result.params,
- },
- insertedPointId: result.pointId,
- };
-}
-
-function insertPointIntoElementMask({
- element,
- maskId,
- segmentIndex,
- canvasPoint,
- bounds,
-}: {
- element: MaskableElement;
- maskId: string;
- segmentIndex: number;
- canvasPoint: { x: number; y: number };
- bounds: ElementBounds;
-}): {
- element: MaskableElement;
- didInsertPoint: boolean;
- insertedPointId: string | null;
-} {
- const currentMasks = element.masks ?? [];
- let insertedPointId: string | null = null;
- let didInsertPoint = false;
-
- const nextMasks = currentMasks.map((mask) => {
- if (mask.id !== maskId || mask.type !== "custom") {
- return mask;
- }
-
- const result = insertPointIntoCustomMask({
- mask,
- segmentIndex,
- canvasPoint,
- bounds,
- });
- if (result.insertedPointId) {
- insertedPointId = result.insertedPointId;
- didInsertPoint = true;
- }
- return result.mask;
- });
-
- return {
- element: didInsertPoint ? { ...element, masks: nextMasks } : element,
- didInsertPoint,
- insertedPointId,
- };
-}
-
-export class InsertCustomMaskPointCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly maskId: string;
- private readonly segmentIndex: number;
- private readonly canvasPoint: { x: number; y: number };
- private readonly bounds: ElementBounds;
-
- constructor({
- trackId,
- elementId,
- maskId,
- segmentIndex,
- canvasPoint,
- bounds,
- }: {
- trackId: string;
- elementId: string;
- maskId: string;
- segmentIndex: number;
- canvasPoint: { x: number; y: number };
- bounds: ElementBounds;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.maskId = maskId;
- this.segmentIndex = segmentIndex;
- this.canvasPoint = canvasPoint;
- this.bounds = bounds;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- let didInsertPoint = false;
- let insertedPointId: string | null = null;
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isMaskableElement,
- update: (element) => {
- const result = insertPointIntoElementMask({
- element: element as MaskableElement,
- maskId: this.maskId,
- segmentIndex: this.segmentIndex,
- canvasPoint: this.canvasPoint,
- bounds: this.bounds,
- });
- didInsertPoint ||= result.didInsertPoint;
- insertedPointId ??= result.insertedPointId;
- return result.element;
- },
- });
-
- if (!didInsertPoint || !insertedPointId) {
- return undefined;
- }
-
- editor.timeline.updateTracks(updatedTracks);
- return {
- selection: {
- selectedMaskPoints: {
- trackId: this.trackId,
- elementId: this.elementId,
- maskId: this.maskId,
- pointIds: [insertedPointId],
- },
- },
- };
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/masks/remove-mask.ts b/apps/web/src/lib/commands/timeline/element/masks/remove-mask.ts
deleted file mode 100644
index 9f6c5f77..00000000
--- a/apps/web/src/lib/commands/timeline/element/masks/remove-mask.ts
+++ /dev/null
@@ -1,65 +0,0 @@
-import { EditorCore } from "@/core";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { isMaskableElement, updateElementInSceneTracks } from "@/lib/timeline";
-import type { SceneTracks, MaskableElement } from "@/lib/timeline";
-
-function removeMaskFromElement({
- element,
- maskId,
-}: {
- element: MaskableElement;
- maskId: string;
-}): MaskableElement {
- const currentMasks = element.masks ?? [];
- const filteredMasks = currentMasks.filter((mask) => mask.id !== maskId);
- return { ...element, masks: filteredMasks };
-}
-
-export class RemoveMaskCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly maskId: string;
-
- constructor({
- trackId,
- elementId,
- maskId,
- }: {
- trackId: string;
- elementId: string;
- maskId: string;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.maskId = maskId;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isMaskableElement,
- update: (element) =>
- removeMaskFromElement({
- element: element as MaskableElement,
- maskId: this.maskId,
- }),
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/masks/toggle-mask-inverted.ts b/apps/web/src/lib/commands/timeline/element/masks/toggle-mask-inverted.ts
deleted file mode 100644
index 2b5c1694..00000000
--- a/apps/web/src/lib/commands/timeline/element/masks/toggle-mask-inverted.ts
+++ /dev/null
@@ -1,76 +0,0 @@
-import { EditorCore } from "@/core";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import { isMaskableElement, updateElementInSceneTracks } from "@/lib/timeline";
-import type { Mask } from "@/lib/masks/types";
-import type { SceneTracks, MaskableElement } from "@/lib/timeline";
-
-export function toggleMaskInvertedOnElement({
- element,
- maskId,
-}: {
- element: MaskableElement;
- maskId: string;
-}): MaskableElement {
- const currentMasks = element.masks ?? [];
- const toggleMask = <TMask extends Mask>(mask: TMask): TMask => ({
- ...mask,
- params: {
- ...mask.params,
- inverted: !mask.params.inverted,
- },
- });
- const updatedMasks = currentMasks.map((mask) =>
- mask.id !== maskId ? mask : toggleMask(mask),
- );
-
- return { ...element, masks: updatedMasks };
-}
-
-export class ToggleMaskInvertedCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly trackId: string;
- private readonly elementId: string;
- private readonly maskId: string;
-
- constructor({
- trackId,
- elementId,
- maskId,
- }: {
- trackId: string;
- elementId: string;
- maskId: string;
- }) {
- super();
- this.trackId = trackId;
- this.elementId = elementId;
- this.maskId = maskId;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const updatedTracks = updateElementInSceneTracks({
- tracks: this.savedState,
- trackId: this.trackId,
- elementId: this.elementId,
- elementPredicate: isMaskableElement,
- update: (element) =>
- toggleMaskInvertedOnElement({
- element: element as MaskableElement,
- maskId: this.maskId,
- }),
- });
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}
diff --git a/apps/web/src/lib/commands/timeline/element/toggle-source-audio-separation.ts b/apps/web/src/lib/commands/timeline/element/toggle-source-audio-separation.ts
deleted file mode 100644
index 7300f540..00000000
--- a/apps/web/src/lib/commands/timeline/element/toggle-source-audio-separation.ts
+++ /dev/null
@@ -1,145 +0,0 @@
-import { EditorCore } from "@/core";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import {
- buildSeparatedAudioElement,
- canExtractSourceAudio,
- isSourceAudioSeparated,
-} from "@/lib/timeline/audio-separation";
-import {
- applyPlacement,
- resolveTrackPlacement,
-} from "@/lib/timeline/placement";
-import { updateElementInSceneTracks } from "@/lib/timeline/track-element-update";
-import type {
- SceneTracks,
- TimelineElement,
- VideoElement,
-} from "@/lib/timeline/types";
-import { generateUUID } from "@/utils/id";
-
-export class ToggleSourceAudioSeparationCommand extends Command {
- private savedState: SceneTracks | null = null;
-
- constructor(
- private readonly params: {
- trackId: string;
- elementId: string;
- },
- ) {
- super();
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
-
- const sourceTrack = [
- ...this.savedState.overlay,
- this.savedState.main,
- ...this.savedState.audio,
- ].find((track) => track.id === this.params.trackId);
- if (!sourceTrack) {
- return;
- }
- const sourceElement = sourceTrack.elements.find(
- (element) => element.id === this.params.elementId,
- ) as TimelineElement | undefined;
- if (!sourceElement || sourceElement.type !== "video") {
- return;
- }
- const videoElement: VideoElement = sourceElement;
-
- if (isSourceAudioSeparated({ element: videoElement })) {
- editor.timeline.updateTracks(
- updateSourceAudioEnabled({
- tracks: this.savedState,
- trackId: this.params.trackId,
- elementId: this.params.elementId,
- isSourceAudioEnabled: true,
- }),
- );
- return;
- }
-
- const mediaAsset = editor.media
- .getAssets()
- .find((asset) => asset.id === videoElement.mediaId);
- if (!canExtractSourceAudio(videoElement, mediaAsset)) {
- return;
- }
- if (videoElement.duration <= 0) {
- return;
- }
-
- const separatedAudioElement = {
- ...buildSeparatedAudioElement({
- sourceElement: videoElement,
- }),
- id: generateUUID(),
- };
- const placementResult = resolveTrackPlacement({
- tracks: this.savedState,
- trackType: "audio",
- timeSpans: [
- {
- startTime: separatedAudioElement.startTime,
- duration: separatedAudioElement.duration,
- },
- ],
- strategy: { type: "firstAvailable" },
- });
- if (!placementResult) {
- return;
- }
- const appliedPlacement = applyPlacement({
- tracks: this.savedState,
- placementResult,
- elements: [separatedAudioElement],
- });
- if (!appliedPlacement) {
- return;
- }
-
- editor.timeline.updateTracks(
- updateSourceAudioEnabled({
- tracks: appliedPlacement.updatedTracks,
- trackId: this.params.trackId,
- elementId: this.params.elementId,
- isSourceAudioEnabled: false,
- }),
- );
- }
-
- undo(): void {
- if (!this.savedState) {
- return;
- }
-
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
-}
-
-function updateSourceAudioEnabled({
- tracks,
- trackId,
- elementId,
- isSourceAudioEnabled,
-}: {
- tracks: SceneTracks;
- trackId: string;
- elementId: string;
- isSourceAudioEnabled: boolean;
-}): SceneTracks {
- return updateElementInSceneTracks({
- tracks,
- trackId,
- elementId,
- elementPredicate: (element): element is VideoElement =>
- element.type === "video",
- update: (element) => ({
- ...element,
- isSourceAudioEnabled,
- }),
- });
-}
diff --git a/apps/web/src/lib/commands/timeline/element/update-elements.ts b/apps/web/src/lib/commands/timeline/element/update-elements.ts
deleted file mode 100644
index 54d1ca88..00000000
--- a/apps/web/src/lib/commands/timeline/element/update-elements.ts
+++ /dev/null
@@ -1,75 +0,0 @@
-import { EditorCore } from "@/core";
-import { Command, type CommandResult } from "@/lib/commands/base-command";
-import type { SceneTracks, TimelineElement } from "@/lib/timeline";
-import {
- findTrackInSceneTracks,
- updateElementInSceneTracks,
-} from "@/lib/timeline";
-import { applyElementUpdate } from "@/lib/timeline/update-pipeline";
-
-export class UpdateElementsCommand extends Command {
- private savedState: SceneTracks | null = null;
- private readonly updates: Array<{
- trackId: string;
- elementId: string;
- patch: Partial<TimelineElement>;
- }>;
-
- constructor({
- updates,
- }: {
- updates: Array<{
- trackId: string;
- elementId: string;
- patch: Partial<TimelineElement>;
- }>;
- }) {
- super();
- this.updates = updates;
- }
-
- execute(): CommandResult | undefined {
- const editor = EditorCore.getInstance();
- this.savedState = editor.scenes.getActiveScene().tracks;
- let updatedTracks = this.savedState;
-
- for (const updateEntry of this.updates) {
- const currentTrack = findTrackInSceneTracks({
- tracks: updatedTracks,
- trackId: updateEntry.trackId,
- });
- const currentElement = currentTrack?.elements.find(
- (element) => element.id === updateEntry.elementId,
- );
- if (!currentTrack || !currentElement) {
- continue;
- }
-
- const nextElement = applyElementUpdate({
- element: currentElement,
- patch: updateEntry.patch,
- context: {
- tracks: updatedTracks,
- trackId: updateEntry.trackId,
- },
- });
-
- updatedTracks = updateElementInSceneTracks({
- tracks: updatedTracks,
- trackId: updateEntry.trackId,
- elementId: updateEntry.elementId,
- update: () => nextElement,
- });
- }
-
- editor.timeline.updateTracks(updatedTracks);
- return undefined;
- }
-
- undo(): void {
- if (this.savedState) {
- const editor = EditorCore.getInstance();
- editor.timeline.updateTracks(this.savedState);
- }
- }
-}