From 2ee0a44735d2b930348e9774909638b2c1909738 Mon Sep 17 00:00:00 2001 From: Maze Winther Date: Wed, 29 Apr 2026 01:28:50 +0200 Subject: refactor: unify element params --- apps/web/src/params/index.ts | 92 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) (limited to 'apps/web/src/params/index.ts') diff --git a/apps/web/src/params/index.ts b/apps/web/src/params/index.ts index 23085407..bb57a1c4 100644 --- a/apps/web/src/params/index.ts +++ b/apps/web/src/params/index.ts @@ -1,4 +1,7 @@ -export type ParamValues = Record; +import { snapToStep } from "@/utils/math"; + +export type ParamValue = number | string | boolean; +export type ParamValues = Record; export type ParamGroup = "stroke"; @@ -6,6 +9,8 @@ interface BaseParamDefinition { key: TKey; label: string; group?: ParamGroup; + keyframable?: boolean; + dependencies?: Array<{ param: string; equals: ParamValue }>; } export interface NumberParamDefinition @@ -42,9 +47,92 @@ export interface SelectParamDefinition options: Array<{ value: string; label: string }>; } +export interface TextParamDefinition + extends BaseParamDefinition { + type: "text"; + default: string; +} + +export interface FontParamDefinition + extends BaseParamDefinition { + type: "font"; + default: string; +} + export type ParamDefinition = | NumberParamDefinition | BooleanParamDefinition | ColorParamDefinition - | SelectParamDefinition; + | SelectParamDefinition + | TextParamDefinition + | FontParamDefinition; + +export function getParamValueKind({ + param, +}: { + param: ParamDefinition; +}): "number" | "color" | "discrete" { + if (param.type === "number") { + return "number"; + } + if (param.type === "color") { + return "color"; + } + return "discrete"; +} +export function getParamDefaultInterpolation({ + param, +}: { + param: ParamDefinition; +}): "linear" | "hold" { + return param.type === "number" || param.type === "color" ? "linear" : "hold"; +} + +export function getParamNumericRange({ + param, +}: { + param: ParamDefinition; +}): { min?: number; max?: number; step?: number } | undefined { + if (param.type !== "number") { + return undefined; + } + + return { + min: param.min, + max: param.max, + step: param.step, + }; +} + +export function coerceParamValue({ + param, + value, +}: { + param: ParamDefinition; + value: unknown; +}): ParamValue | null { + if (param.type === "number") { + if (typeof value !== "number" || Number.isNaN(value)) { + return null; + } + + const steppedValue = snapToStep({ value, step: param.step }); + const maxValue = param.max ?? Number.POSITIVE_INFINITY; + return Math.min(maxValue, Math.max(param.min, steppedValue)); + } + + if (param.type === "boolean") { + return typeof value === "boolean" ? value : null; + } + + if (param.type === "color" || param.type === "text" || param.type === "font") { + return typeof value === "string" ? value : null; + } + + if (typeof value !== "string") { + return null; + } + + return param.options.some((option) => option.value === value) ? value : null; +} -- cgit v1.3.1