Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(captions): let the first write know the output aspect too

EtienneLescot77c2c6a39389
diff --git a/src/lib/ai-edition/captions/captions.test.ts b/src/lib/ai-edition/captions/captions.test.ts
index f6b0cde9c..15ba27f9b 100644
--- a/src/lib/ai-edition/captions/captions.test.ts
+++ b/src/lib/ai-edition/captions/captions.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import type { AxcutDocument, AxcutTranscript } from "../schema";
import { captionCuesToTextRegions, deriveCaptionCues } from "./cues";
+import type { CaptionSettings } from "./settings";
import {
captionBackgroundCss,
captionBoxRect,
@@ -297,6 +298,22 @@ describe("migrating a pre-anchor document", () => {
expect(getCaptionSettings(doc(), LANDSCAPE).insetY).toBe(5);
expect(getCaptionSettings(doc(), PORTRAIT).insetY).toBe(12.5);
});
+
+ it("freezes the PORTRAIT defaults on the first write to a vertical project", () => {
+ // The first patch is what materialises the defaults into the document, so it
+ // has to know the aspect too. Patching without it stored the landscape inset
+ // into a 9:16 project and the stored value then won for good — putting the
+ // caption under the platform's own chrome, the exact failure the
+ // aspect-derived default exists to prevent.
+ const first = patchCaptionSettings(doc(), { enabled: true }, PORTRAIT);
+ const stored = (first.legacyEditor as { captions: CaptionSettings }).captions;
+ expect(stored.insetY).toBe(12.5);
+ expect(stored.insetX).toBe(captionSafeColumn(PORTRAIT).x);
+
+ // And it stays: a later patch reads what is stored rather than re-deriving.
+ const later = patchCaptionSettings(first, { fontSize: 60 }, PORTRAIT);
+ expect(getCaptionSettings(later, PORTRAIT).insetY).toBe(12.5);
+ });
});
describe("deriveCaptionCues", () => {
diff --git a/src/lib/ai-edition/captions/settings.ts b/src/lib/ai-edition/captions/settings.ts
index cc1898de5..1e776833f 100644
--- a/src/lib/ai-edition/captions/settings.ts
+++ b/src/lib/ai-edition/captions/settings.ts
@@ -454,8 +454,14 @@ export type CaptionSettingsPatch = Partial<CaptionSettings>;
export function patchCaptionSettings(
doc: AxcutDocument,
patch: CaptionSettingsPatch,
+ aspectValue = 16 / 9,
): AxcutDocument {
- const next: CaptionSettings = { ...getCaptionSettings(doc), ...patch };
+ // The aspect matters on the FIRST write to a document that has never carried
+ // caption settings: that write is what materialises the defaults, and a portrait
+ // export wants a much larger inset than a landscape one. Reading without it here
+ // would freeze the landscape default into a 9:16 project — the exact failure the
+ // aspect-derived default exists to prevent.
+ const next: CaptionSettings = { ...getCaptionSettings(doc, aspectValue), ...patch };
next.insetY = clamp(next.insetY, 0, CAPTION_INSET_Y_MAX);
next.insetX = clamp(next.insetX, 0, CAPTION_INSET_X_MAX);
return {
diff --git a/src/lib/ai-edition/store/useCaptions.ts b/src/lib/ai-edition/store/useCaptions.ts
index 36b13c95e..5238fa150 100644
--- a/src/lib/ai-edition/store/useCaptions.ts
+++ b/src/lib/ai-edition/store/useCaptions.ts
@@ -17,8 +17,10 @@ import {
putCaptionTranslation,
removeCaptionTranslation,
} from "../captions";
+import { resolveAspectRatioValue } from "../document/outputFormat";
import type { AxcutDocument } from "../schema";
import { useProjectStore } from "./projectStore";
+import { useEditorSettings } from "./useEditorSettings";
export interface UseCaptionsResult {
settings: CaptionSettings;
@@ -48,7 +50,23 @@ export function useCaptions(): UseCaptionsResult {
const setDocument = useProjectStore((s) => s.setDocument);
const saveDocument = useProjectStore((s) => s.saveDocument);
- const settings = useMemo(() => getCaptionSettings(document), [document]);
+ // The output aspect decides the caption column and the DEFAULT insets, so it has
+ // to reach both the read and every write: the first write to a document that has
+ // never carried caption settings is what freezes those defaults in, and a 9:16
+ // export wants a much larger inset than a 16:9 one. `resolveAspectRatioValue` is
+ // the same resolver the preview and the scene description use — not
+ // `getAspectRatioValue`, which answers 16/9 for the legacy "native" selection and
+ // would disagree with what the compositor is handed.
+ const { settings: editorSettings } = useEditorSettings();
+ const aspectValue = useMemo(
+ () => resolveAspectRatioValue(document, editorSettings.aspectRatio),
+ [document, editorSettings.aspectRatio],
+ );
+
+ const settings = useMemo(
+ () => getCaptionSettings(document, aspectValue),
+ [document, aspectValue],
+ );
const translations = useMemo(() => getCaptionTranslations(document), [document]);
const cues = useMemo(
() => deriveCaptionCues(document, settings, translations),
@@ -65,14 +83,14 @@ export function useCaptions(): UseCaptionsResult {
async (patch: CaptionSettingsPatch) => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- const next = patchCaptionSettings(doc, patch);
+ const next = patchCaptionSettings(doc, patch, aspectValue);
// The optimistic write is not the edit — the save is. Only the one that can
// fail records, and it names `doc` as what Ctrl+Z returns to because by then
// the store already holds `next`.
setDocument(next, { history: false });
await saveDocument(next, { history: true, historyBase: doc });
},
- [setDocument, saveDocument],
+ [setDocument, saveDocument, aspectValue],
);
// See `useEditorSettings.setLive`: one undo step per slider drag, not one per
@@ -95,12 +113,12 @@ export function useCaptions(): UseCaptionsResult {
(patch: CaptionSettingsPatch) => {
const doc = useProjectStore.getState().document;
if (!doc) return;
- const next = patchCaptionSettings(doc, patch);
+ const next = patchCaptionSettings(doc, patch, aspectValue);
if (liveDocRef.current !== doc) liveBaseRef.current = doc;
setDocument(next, { history: false });
liveDocRef.current = next;
},
- [setDocument],
+ [setDocument, aspectValue],
);
const commit = useCallback(async () => {
@@ -133,13 +151,13 @@ export function useCaptions(): UseCaptionsResult {
// language currently on screen is the one being deleted.
const cleared = removeCaptionTranslation(doc, language);
const next =
- getCaptionSettings(cleared).language === language
- ? patchCaptionSettings(cleared, { language: null })
+ getCaptionSettings(cleared, aspectValue).language === language
+ ? patchCaptionSettings(cleared, { language: null }, aspectValue)
: cleared;
setDocument(next, { history: false });
await saveDocument(next, { history: true, historyBase: doc });
},
- [setDocument, saveDocument],
+ [setDocument, saveDocument, aspectValue],
);
return {