fix: keep aspect ratio flow

This commit is contained in:
2026-10-04 20:52:47 +05:00
parent e73986fefd
commit 4d9cd2cfcb
7 changed files with 87 additions and 9 deletions
@@ -11,7 +11,7 @@
label: string;
value: unknown;
spec: FieldSpec;
onchange?: (value: Dimension) => void;
onchange?: (value: Dimension, axis?: "width" | "height") => void;
}
let { label, value, spec, onchange }: Props = $props();
@@ -31,7 +31,7 @@
});
function setAxis(axis: "width" | "height", n: number) {
onchange?.({ ...current, [axis]: n });
onchange?.({ ...current, [axis]: n }, axis);
}
</script>
@@ -30,7 +30,7 @@
spec: FieldSpec;
toolId: string;
fieldId: string;
onchange?: (value: unknown) => void;
onchange?: (value: unknown, axis?: "width" | "height") => void;
}
const FIELDS: Record<FieldSpecKind, Component<FieldControlProps>> = {
@@ -54,7 +54,7 @@
schema: ToolSchema<Record<string, unknown>>;
values: Record<string, unknown>;
toolId: string;
onchange: (id: string, value: unknown) => void;
onchange: (id: string, value: unknown, axis?: "width" | "height") => void;
onreset: () => void;
}
let { schema, values, toolId, onchange, onreset }: Props = $props();
@@ -80,7 +80,7 @@
spec={schema.fields[id].spec}
{toolId}
fieldId={id}
onchange={(v) => onchange(id, v)}
onchange={(v, axis) => onchange(id, v, axis)}
/>
{/each}
</div>
@@ -17,6 +17,8 @@
applySourceDefaults,
defaultSchemaParams,
sanitizeSchemaParams,
withAspectLock,
type Dimension,
type ToolSchema,
} from "$lib/registry-schema";
import { downloadZip } from "$lib/zip";
@@ -42,6 +44,8 @@
// Поля, изменённые пользователем: при загрузке нового файла им не даём
// перезаписаться source-дефолтами. Reset снимает пометку.
const touched = new SvelteSet<string>();
// Последняя правленая ось dimension-поля — ведущая при lockAspect.
const lastAxis: Record<string, "width" | "height"> = {};
let source = $state<PixelImage | null>(null);
let result = $state<PixelImage | null>(null);
let fileResult = $state<FileResult | null>(null);
@@ -70,9 +74,38 @@
values = defaultSchemaParams(schema);
}
function setValue(id: string, value: unknown) {
function setValue(id: string, value: unknown, axis?: "width" | "height") {
touched.add(id);
values = { ...values, [id]: value };
if (axis) lastAxis[id] = axis;
const next: Record<string, unknown> = { ...values, [id]: value };
const spec = schema?.fields[id]?.spec;
if (source && schema && spec) {
const aspect = source.width / source.height;
if (
spec.kind === "dimension" &&
spec.lockAspectWith &&
next[spec.lockAspectWith] === true
) {
next[id] = withAspectLock(
value as Dimension,
lastAxis[id] ?? "width",
aspect,
);
} else if (spec.kind === "checkbox" && value === true) {
// Включили lockAspect — сразу подгоняем привязанные поля под аспект.
for (const [fid, f] of Object.entries(schema.fields)) {
const fs = f.spec;
if (fs.kind === "dimension" && fs.lockAspectWith === id) {
next[fid] = withAspectLock(
next[fid] as Dimension,
lastAxis[fid] ?? "width",
aspect,
);
}
}
}
}
values = next;
}
function reset() {