diff --git a/apps/web/src/hooks/actions/use-editor-actions.ts b/apps/web/src/hooks/actions/use-editor-actions.ts index 846edb4a..8458979c 100644 --- a/apps/web/src/hooks/actions/use-editor-actions.ts +++ b/apps/web/src/hooks/actions/use-editor-actions.ts @@ -220,10 +220,9 @@ export function useEditorActions() { useActionHandler( "duplicate-selected", () => { - const duplicatedElements = editor.timeline.duplicateElements({ - elements: selectedElements, - }); - setElementSelection({ elements: duplicatedElements }); + editor.timeline.duplicateElements({ + elements: selectedElements, + }); }, undefined, ); @@ -277,14 +276,12 @@ export function useEditorActions() { useActionHandler( "paste-copied", () => { - if (!clipboard?.items.length) return; + if (!clipboard?.items.length) return; - const pastedElements = - editor.timeline.pasteAtTime({ - time: editor.playback.getCurrentTime(), - clipboardItems: clipboard.items, - }) ?? []; - setElementSelection({ elements: pastedElements }); + editor.timeline.pasteAtTime({ + time: editor.playback.getCurrentTime(), + clipboardItems: clipboard.items, + }); }, undefined, ); diff --git a/apps/web/src/lib/commands/timeline/clipboard/paste.ts b/apps/web/src/lib/commands/timeline/clipboard/paste.ts index fc1c46e0..9c979790 100644 --- a/apps/web/src/lib/commands/timeline/clipboard/paste.ts +++ b/apps/web/src/lib/commands/timeline/clipboard/paste.ts @@ -15,6 +15,7 @@ import { export class PasteCommand extends Command { private savedState: TimelineTrack[] | null = null; private pastedElements: { trackId: string; elementId: string }[] = []; + private previousSelection: { trackId: string; elementId: string }[] = []; constructor( private time: number, @@ -28,6 +29,7 @@ export class PasteCommand extends Command { const editor = EditorCore.getInstance(); this.savedState = editor.timeline.getTracks(); + this.previousSelection = editor.selection.getSelectedElements(); this.pastedElements = []; const minStart = Math.min( @@ -95,12 +97,17 @@ export class PasteCommand extends Command { } editor.timeline.updateTracks(updatedTracks); + + if (this.pastedElements.length > 0) { + editor.selection.setSelectedElements({ elements: this.pastedElements }); + } } undo(): void { if (this.savedState) { const editor = EditorCore.getInstance(); editor.timeline.updateTracks(this.savedState); + editor.selection.setSelectedElements({ elements: this.previousSelection }); } } diff --git a/apps/web/src/lib/commands/timeline/element/duplicate-elements.ts b/apps/web/src/lib/commands/timeline/element/duplicate-elements.ts index 9510171c..4bec6444 100644 --- a/apps/web/src/lib/commands/timeline/element/duplicate-elements.ts +++ b/apps/web/src/lib/commands/timeline/element/duplicate-elements.ts @@ -14,6 +14,7 @@ interface DuplicateElementsParams { export class DuplicateElementsCommand extends Command { private duplicatedElements: { trackId: string; elementId: string }[] = []; private savedState: TimelineTrack[] | null = null; + private previousSelection: { trackId: string; elementId: string }[] = []; private elements: DuplicateElementsParams["elements"]; constructor({ elements }: DuplicateElementsParams) { @@ -24,6 +25,7 @@ export class DuplicateElementsCommand extends Command { execute(): void { const editor = EditorCore.getInstance(); this.savedState = editor.timeline.getTracks(); + this.previousSelection = editor.selection.getSelectedElements(); this.duplicatedElements = []; const updatedTracks = [...this.savedState]; @@ -80,12 +82,17 @@ export class DuplicateElementsCommand extends Command { } editor.timeline.updateTracks(updatedTracks); + + if (this.duplicatedElements.length > 0) { + editor.selection.setSelectedElements({ elements: this.duplicatedElements }); + } } undo(): void { if (this.savedState) { const editor = EditorCore.getInstance(); editor.timeline.updateTracks(this.savedState); + editor.selection.setSelectedElements({ elements: this.previousSelection }); } }