1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import type { EditorCore } from "@/core";
import {
AddTrackCommand,
BatchCommand,
InsertElementCommand,
} from "@/commands";
import { buildSubtitleTextElement } from "./build-subtitle-text-element";
import type { SubtitleCue } from "./types";
export function insertCaptionChunksAsTextTrack({
editor,
captions,
}: {
editor: EditorCore;
captions: SubtitleCue[];
}): string | null {
if (captions.length === 0) {
return null;
}
const addTrackCommand = new AddTrackCommand({ type: "text", index: 0 });
const trackId = addTrackCommand.getTrackId();
const canvasSize = editor.project.getActive().settings.canvasSize;
const insertCommands = captions.map(
(caption, index) =>
new InsertElementCommand({
placement: { mode: "explicit", trackId },
element: buildSubtitleTextElement({
index,
caption,
canvasSize,
}),
}),
);
editor.command.execute({
command: new BatchCommand([addTrackCommand, ...insertCommands]),
});
return trackId;
}
|