Skip to content

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

refactor: reuse the gradient parser, and spread the settings patch

Etienne Lescotba1929629f41
diff --git a/src/lib/ai-edition/store/editorSettings.test.ts b/src/lib/ai-edition/store/editorSettings.test.ts
index 32294a223..0f72d7e93 100644
--- a/src/lib/ai-edition/store/editorSettings.test.ts
+++ b/src/lib/ai-edition/store/editorSettings.test.ts
@@ -91,6 +91,15 @@ describe("patchEditorSettings", () => {
expect(snap.shadowIntensity).toBe(0.7);
});
+ it("treats an explicitly undefined key as absent, not as a clear", () => {
+ const seed = patchEditorSettings(baseDoc, { showBlur: true, shadowIntensity: 0.7 });
+ const next = patchEditorSettings(seed, { showBlur: undefined, padding: 12 });
+ const snap = getEditorSettings(next);
+ expect(snap.showBlur).toBe(true);
+ expect(snap.shadowIntensity).toBe(0.7);
+ expect(snap.padding).toBe(12);
+ });
+
it("patches nested cursor settings without clobbering siblings", () => {
const seed = patchEditorSettings(baseDoc, { cursor: { size: 4 } });
const next = patchEditorSettings(seed, { cursor: { smoothing: 0.9 } });
diff --git a/src/lib/ai-edition/store/editorSettings.ts b/src/lib/ai-edition/store/editorSettings.ts
index 85d7838a7..1d986e768 100644
--- a/src/lib/ai-edition/store/editorSettings.ts
+++ b/src/lib/ai-edition/store/editorSettings.ts
@@ -187,23 +187,15 @@ export interface EditorSettingsPatch {
function nextLegacy(current: LegacyShape | null, patch: EditorSettingsPatch): LegacyShape {
const base: LegacyShape = current ?? {};
- const next: LegacyShape = { ...base };
- if (patch.wallpaper !== undefined) next.wallpaper = patch.wallpaper;
- if (patch.aspectRatio !== undefined) next.aspectRatio = patch.aspectRatio;
- if (patch.shadowIntensity !== undefined) next.shadowIntensity = patch.shadowIntensity;
- if (patch.showBlur !== undefined) next.showBlur = patch.showBlur;
- if (patch.showTrimWaveform !== undefined) next.showTrimWaveform = patch.showTrimWaveform;
- if (patch.motionBlurAmount !== undefined) next.motionBlurAmount = patch.motionBlurAmount;
- if (patch.borderRadius !== undefined) next.borderRadius = patch.borderRadius;
- if (patch.padding !== undefined) next.padding = patch.padding;
- if (patch.cropRegion !== undefined) next.cropRegion = patch.cropRegion;
- if (patch.webcamLayoutPreset !== undefined) next.webcamLayoutPreset = patch.webcamLayoutPreset;
- if (patch.webcamMaskShape !== undefined) next.webcamMaskShape = patch.webcamMaskShape;
- if (patch.webcamMirrored !== undefined) next.webcamMirrored = patch.webcamMirrored;
- if (patch.webcamReactiveZoom !== undefined) next.webcamReactiveZoom = patch.webcamReactiveZoom;
- if (patch.webcamSizePreset !== undefined) next.webcamSizePreset = patch.webcamSizePreset;
- if (patch.webcamPosition !== undefined) next.webcamPosition = patch.webcamPosition;
- if (patch.autoFocusAll !== undefined) next.autoFocusAll = patch.autoFocusAll;
+ // Spread, but only over keys the patch actually set — a plain {...base,
+ // ...patch} would write undefined over a value the caller left alone.
+ // `cursor` is handled separately below: its keys are renamed (size ->
+ // cursorSize), so it is not a straight passthrough.
+ const { cursor: _cursor, ...rest } = patch;
+ const defined = Object.fromEntries(
+ Object.entries(rest).filter(([, v]) => v !== undefined),
+ ) as Partial<LegacyShape>;
+ const next: LegacyShape = { ...base, ...defined };
if (patch.cursor) {
const c = patch.cursor;
if (c.size !== undefined) next.cursorSize = c.size;
diff --git a/src/native/sceneDescription.ts b/src/native/sceneDescription.ts
index 66b0cad35..090a54e8a 100644
--- a/src/native/sceneDescription.ts
+++ b/src/native/sceneDescription.ts
@@ -37,6 +37,7 @@ import {
resolveWebcamReactiveZoom,
webcamSizeToFraction,
} from "@/lib/compositeLayout";
+import { parseCssGradient, resolveLinearGradientAngle } from "@/lib/exporter/gradientParser";
import type { CompositorClipInput } from "./contracts";
/** Background behind the screen. Parsed from `settings.wallpaper`. */
@@ -368,33 +369,22 @@ export interface SceneDescription {
/** Strip a trailing position percentage (or any whitespace tail) from a gradient stop token,
* returning the colour piece. The presets never nest parens so a whitespace split is fine. */
-function firstTokenOf(stop: string): string {
- const trimmed = stop.trim();
- if (trimmed.length === 0) return trimmed;
- const spaceIdx = trimmed.indexOf(" ");
- return spaceIdx === -1 ? trimmed : trimmed.slice(0, spaceIdx);
-}
-
/** Parse the settings wallpaper string into the discriminated SceneBackground union. */
function parseWallpaper(wallpaper: string) {
if (wallpaper.startsWith("#")) {
return { kind: "color", color: wallpaper } as const;
}
if (wallpaper.startsWith("linear-gradient(")) {
- // Strip the outer wrapper once — assume well-formed `linear-gradient(...)`.
- const openIdx = wallpaper.indexOf("(");
- const closeIdx = wallpaper.lastIndexOf(")");
- const inner = wallpaper.slice(openIdx + 1, closeIdx);
- // Presets never contain nested parens; a flat comma split is sufficient.
- const tokens = inner
- .split(",")
- .map((t) => t.trim())
- .filter((t) => t.length > 0);
- const angleMatch = /^-?\d+(\.\d+)?deg$/.exec(tokens[0] ?? "");
- const angleDeg = angleMatch ? parseFloat(tokens[0]) : 180;
- const stopsRaw = angleMatch ? tokens.slice(1) : tokens;
- const stops = stopsRaw.map(firstTokenOf).filter((s) => s.length > 0);
- return { kind: "gradient", angleDeg, stops } as const;
+ // parseCssGradient handles nested parens (rgba()/hsl() stops) and the
+ // "to bottom right" keyword directions that a flat comma split drops.
+ const parsed = parseCssGradient(wallpaper);
+ if (parsed) {
+ return {
+ kind: "gradient",
+ angleDeg: resolveLinearGradientAngle(parsed.descriptor),
+ stops: parsed.stops.map((stop) => stop.color),
+ } as const;
+ }
}
return { kind: "image", path: wallpaper } as const;
}