Skip to content

Open source CapCut alternative.

TypeScript

fix(cut): regroup edited captions at the current word limit [rebuild]

David Lebecc92e8d9d8
diff --git a/site/src/cut/lib/captionRegroup.test.ts b/site/src/cut/lib/captionRegroup.test.ts
new file mode 100644
index 00000000..550d8de4
--- /dev/null
+++ b/site/src/cut/lib/captionRegroup.test.ts
@@ -0,0 +1,24 @@
+import { expect, test } from "bun:test";
+import { useEditor } from "./store";
+import { emptySubtitles } from "./types";
+
+test("reapplying word grouping splits a script replaced after choosing the setting", async () => {
+ const previous = useEditor.getState();
+ useEditor.setState({
+ subtitles: {
+ ...emptySubtitles(),
+ wordsPerCue: 2,
+ cues: [{ id: "rewritten", start: 0, end: 4, text: "Keep every caption short and clear" }],
+ },
+ cutMixSamples: async () => { throw new Error("No audio in this project"); },
+ });
+ try {
+ await useEditor.getState().setSubtitleWordsPerCue(2);
+ const cues = useEditor.getState().subtitles.cues;
+ expect(cues.map((cue) => cue.text)).toEqual(["Keep every", "caption short", "and clear"]);
+ expect(cues[0].start).toBe(0);
+ expect(cues[cues.length - 1].end).toBe(4);
+ } finally {
+ useEditor.setState(previous);
+ }
+});
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 5cdaa24a..11084c1a 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -4592,7 +4592,10 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
setSubtitleWordsPerCue: async (n) => {
const s = get();
const per = Math.min(MAX_WORDS_PER_CUE, Math.max(MIN_WORDS_PER_CUE, Math.round(n)));
- if (s.subtitles.wordsPerCue === per) return;
+ if (
+ s.subtitles.wordsPerCue === per &&
+ s.subtitles.cues.every((cue) => cue.text.split(/\s+/).filter(Boolean).length <= per)
+ ) return;
const projectId = s.projectId;
const epoch = laneEpoch;
push();