Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop): normalize loaded caption settings enums

Richie McIlroydab8e2098d42
diff --git a/apps/desktop/src/routes/editor/context.ts b/apps/desktop/src/routes/editor/context.ts
index 3531accc3..1d0945f78 100644
--- a/apps/desktop/src/routes/editor/context.ts
+++ b/apps/desktop/src/routes/editor/context.ts
@@ -21,7 +21,10 @@ import {
import { createStore, produce, reconcile, unwrap } from "solid-js/store";
import { generalSettingsStore } from "~/store";
-import type { EditorCaptionSettings } from "~/store/captions";
+import {
+ type EditorCaptionSettings,
+ normalizeCaptionSettings,
+} from "~/store/captions";
import { defaultKeyboardSettings } from "~/store/keyboard";
import { createPresets } from "~/utils/createPresets";
@@ -240,11 +243,18 @@ export function normalizeProject(
),
}
: undefined;
+ const captions = config.captions
+ ? {
+ ...config.captions,
+ settings: normalizeCaptionSettings(config.captions.settings),
+ }
+ : null;
return {
...config,
keyboard,
timeline,
+ captions,
background: withCornerDefaults(config.background),
camera: withCornerDefaults(config.camera),
};
diff --git a/apps/desktop/src/store/captions.ts b/apps/desktop/src/store/captions.ts
index 75e335c02..27eab47ca 100644
--- a/apps/desktop/src/store/captions.ts
+++ b/apps/desktop/src/store/captions.ts
@@ -163,6 +163,15 @@ export const CAPTION_STYLE_PRESETS: CaptionStylePreset[] = [
];
const classicPreset = CAPTION_STYLE_PRESETS[0];
+const captionAnimations: readonly CaptionAnimation[] = [
+ "none",
+ "bounce",
+ "pop",
+];
+const captionHighlightStyles: readonly CaptionHighlightStyle[] = [
+ "color",
+ "pill",
+];
export const defaultCaptionSettings: EditorCaptionSettings = {
enabled: false,
@@ -176,6 +185,34 @@ export const defaultCaptionSettings: EditorCaptionSettings = {
...classicPreset.style,
};
+function isCaptionAnimation(value: unknown): value is CaptionAnimation {
+ return captionAnimations.includes(value as CaptionAnimation);
+}
+
+function isCaptionHighlightStyle(
+ value: unknown,
+): value is CaptionHighlightStyle {
+ return captionHighlightStyles.includes(value as CaptionHighlightStyle);
+}
+
+export function normalizeCaptionSettings(
+ settings: Partial<CaptionSettings> | null | undefined,
+): EditorCaptionSettings {
+ const animation = isCaptionAnimation(settings?.animation)
+ ? settings.animation
+ : defaultCaptionSettings.animation;
+ const highlightStyle = isCaptionHighlightStyle(settings?.highlightStyle)
+ ? settings.highlightStyle
+ : defaultCaptionSettings.highlightStyle;
+
+ return {
+ ...defaultCaptionSettings,
+ ...settings,
+ animation,
+ highlightStyle,
+ };
+}
+
function createCaptionsStore() {
const [state, setState] = createStore<CaptionsState>({
segments: [],
@@ -237,8 +274,8 @@ function createCaptionsStore() {
const captionsData = await commands.loadCaptions(videoPath);
if (captionsData) {
const loadedSettings = captionsData.settings
- ? { ...defaultCaptionSettings, ...captionsData.settings }
- : { ...defaultCaptionSettings, enabled: true };
+ ? normalizeCaptionSettings(captionsData.settings)
+ : normalizeCaptionSettings({ enabled: true });
setState((prev) => ({
...prev,
segments: captionsData.segments,
@@ -255,10 +292,10 @@ function createCaptionsStore() {
setState("segments", localCaptionsData.segments);
}
if (localCaptionsData.settings) {
- setState("settings", {
- ...defaultCaptionSettings,
- ...localCaptionsData.settings,
- });
+ setState(
+ "settings",
+ normalizeCaptionSettings(localCaptionsData.settings),
+ );
}
} catch (e) {
console.error("Error loading saved captions from localStorage:", e);