From b85d8ebb52fe6b666fb1c0487f97d1464299f761 Mon Sep 17 00:00:00 2001 From: kcfancher Date: Thu, 2 Apr 2026 06:18:28 -0400 Subject: [PATCH] Batch caption text insertion [Modified from eece3bbc5f0578a24a4f90f0ab87fe5ca37e94e9, 2026-03-28 09:13:18 -0400] Batch caption insertion in a BatchCommand so that undo will batch undo all subtitle insertion from either srt file or ai transcriptions. --- apps/web/src/lib/subtitles/insert.ts | 42 +++++++++++++++++----------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/apps/web/src/lib/subtitles/insert.ts b/apps/web/src/lib/subtitles/insert.ts index 5a64b8fe..5d943fc9 100644 --- a/apps/web/src/lib/subtitles/insert.ts +++ b/apps/web/src/lib/subtitles/insert.ts @@ -1,6 +1,11 @@ import type { EditorCore } from "@/core"; import { DEFAULTS } from "@/lib/timeline/defaults"; import type { CaptionChunk } from "@/lib/transcription/types"; +import { + AddTrackCommand, + BatchCommand, + InsertElementCommand, +} from "@/lib/commands"; export function insertCaptionChunksAsTextTrack({ editor, @@ -13,26 +18,31 @@ export function insertCaptionChunksAsTextTrack({ return null; } - const trackId = editor.timeline.addTrack({ - type: "text", - index: 0, - }); + const addTrackCommand = new AddTrackCommand("text", 0); + const trackId = addTrackCommand.getTrackId(); + const commands = [addTrackCommand]; for (let i = 0; i < captions.length; i++) { const caption = captions[i]; - editor.timeline.insertElement({ - placement: { mode: "explicit", trackId }, - element: { - ...DEFAULTS.text.element, - name: `Caption ${i + 1}`, - content: caption.text, - duration: caption.duration, - startTime: caption.startTime, - fontSize: 65, - fontWeight: "bold", - }, - }); + commands.push( + new InsertElementCommand({ + placement: { mode: "explicit", trackId }, + element: { + ...DEFAULTS.text.element, + name: `Caption ${i + 1}`, + content: caption.text, + duration: caption.duration, + startTime: caption.startTime, + fontSize: 65, + fontWeight: "bold", + }, + }), + ); } + editor.command.execute({ + command: new BatchCommand(commands), + }); + return trackId; }