refactor: update field schema types
This commit is contained in:
@@ -1,23 +1,27 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import type {
|
||||||
|
FieldSpec,
|
||||||
|
FieldSpecKind,
|
||||||
|
ToolSchema,
|
||||||
|
} from "$lib/registry-schema";
|
||||||
import { RotateCcw } from "@lucide/svelte";
|
import { RotateCcw } from "@lucide/svelte";
|
||||||
import type { ToolSchema } from "$lib/registry-schema";
|
import type { Component } from "svelte";
|
||||||
|
import DimensionField from "./fields/DimensionField.svelte";
|
||||||
import CheckboxControl from "./fields/schema/CheckboxControl.svelte";
|
import CheckboxControl from "./fields/schema/CheckboxControl.svelte";
|
||||||
import ColorControl from "./fields/schema/ColorControl.svelte";
|
import ColorControl from "./fields/schema/ColorControl.svelte";
|
||||||
import ColorPairControl from "./fields/schema/ColorPairControl.svelte";
|
import ColorPairControl from "./fields/schema/ColorPairControl.svelte";
|
||||||
import RangeControl from "./fields/schema/RangeControl.svelte";
|
import RangeControl from "./fields/schema/RangeControl.svelte";
|
||||||
import SelectControl from "./fields/schema/SelectControl.svelte";
|
import SelectControl from "./fields/schema/SelectControl.svelte";
|
||||||
import TextControl from "./fields/schema/TextControl.svelte";
|
import TextControl from "./fields/schema/TextControl.svelte";
|
||||||
import DimensionField from "./fields/DimensionField.svelte";
|
|
||||||
|
|
||||||
interface Props {
|
interface FieldControlProps {
|
||||||
schema: ToolSchema<Record<string, unknown>>;
|
label: string;
|
||||||
values: Record<string, unknown>;
|
value: unknown;
|
||||||
onchange: (id: string, value: unknown) => void;
|
spec: FieldSpec;
|
||||||
onreset: () => void;
|
onchange?: (value: unknown) => void;
|
||||||
}
|
}
|
||||||
let { schema, values, onchange, onreset }: Props = $props();
|
|
||||||
|
|
||||||
const FIELDS = {
|
const FIELDS: Record<FieldSpecKind, Component<FieldControlProps>> = {
|
||||||
number: RangeControl,
|
number: RangeControl,
|
||||||
slider: RangeControl,
|
slider: RangeControl,
|
||||||
color: ColorControl,
|
color: ColorControl,
|
||||||
@@ -26,7 +30,15 @@
|
|||||||
checkbox: CheckboxControl,
|
checkbox: CheckboxControl,
|
||||||
text: TextControl,
|
text: TextControl,
|
||||||
dimension: DimensionField,
|
dimension: DimensionField,
|
||||||
} as const;
|
};
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
schema: ToolSchema<Record<string, unknown>>;
|
||||||
|
values: Record<string, unknown>;
|
||||||
|
onchange: (id: string, value: unknown) => void;
|
||||||
|
onreset: () => void;
|
||||||
|
}
|
||||||
|
let { schema, values, onchange, onreset }: Props = $props();
|
||||||
|
|
||||||
function labelOf(id: string): string {
|
function labelOf(id: string): string {
|
||||||
return id
|
return id
|
||||||
|
|||||||
@@ -76,15 +76,26 @@ export interface ColorPairSpec {
|
|||||||
to: string;
|
to: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type FieldSpec =
|
/**
|
||||||
| NumberSpec
|
* «Объект as const» kind → спека поля. Единственный источник правды для
|
||||||
| SliderSpec
|
* перечня kinds: `FieldSpecKind` = ключи map, `FieldSpec` = значение по любому
|
||||||
| ColorSpec
|
* ключу (тот же union). Добавляем новый составной тип — добавляем сюда, и
|
||||||
| SelectSpec
|
* type-checker укажет, где его не хватает (record контролов, default/sanitize).
|
||||||
| TextSpec
|
*/
|
||||||
| CheckboxSpec
|
export const fieldSpecs = {
|
||||||
| DimensionSpec
|
number: {} as NumberSpec,
|
||||||
| ColorPairSpec;
|
slider: {} as SliderSpec,
|
||||||
|
color: {} as ColorSpec,
|
||||||
|
select: {} as SelectSpec,
|
||||||
|
text: {} as TextSpec,
|
||||||
|
checkbox: {} as CheckboxSpec,
|
||||||
|
dimension: {} as DimensionSpec,
|
||||||
|
"color-pair": {} as ColorPairSpec,
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export type FieldSpecKind = keyof typeof fieldSpecs;
|
||||||
|
export type FieldSpecOf<K extends FieldSpecKind> = (typeof fieldSpecs)[K];
|
||||||
|
export type FieldSpec = FieldSpecOf<FieldSpecKind>;
|
||||||
|
|
||||||
/** `Field<T>`: runtime-спека поля + phantom-тип ожидаемого значения (number|string|boolean). */
|
/** `Field<T>`: runtime-спека поля + phantom-тип ожидаемого значения (number|string|boolean). */
|
||||||
export interface Field<T> {
|
export interface Field<T> {
|
||||||
|
|||||||
Reference in New Issue
Block a user