import type { NodeParamSpec } from "../lib/nodeCatalog"; import { TipIcon } from "./TipIcon"; /** * Inline parameter editor for a canvas node. * * Schema-driven: takes a `NodeParamSpec[]` from the node's catalog * entry and renders one labelled control per param. Exposes the * runtime values via the parent's `params` map and pushes edits back * through `onChange`. Supports two control types: * * - `select` — native `` with min/max/step * constraints. We also clamp on the way in (defensive) so the * stored value can never persist outside the declared range, even * if the user types past the spinner buttons. * * Hints don't render inline — they live on a small ⓘ icon next to the * label, surfaced as a native browser tooltip on hover. Keeps the * block compact while still letting plain-language explanations be * arbitrarily long. * * `nodrag` on the wrapper is critical: without it, dragging a select * to make a choice or selecting text in the input would be intercepted * by React Flow as a node-move gesture. */ export function NodeParamEditor({ spec, values, onChange, }: { spec: NodeParamSpec[]; values: Record; onChange: (patch: Record) => void; }) { return (
{spec.map((p) => p.type === "select" ? ( onChange({ [p.key]: v })} /> ) : ( onChange({ [p.key]: v })} /> ), )}
); } function SelectField({ spec, value, onChange, }: { spec: Extract; value: string; onChange: (v: string) => void; }) { const known = spec.options.some((o) => o.value === value); return ( ); } function NumberField({ spec, value, onChange, }: { spec: Extract; value: number; onChange: (v: number) => void; }) { const safe = Number.isFinite(value) ? value : (spec.min ?? 0); const precision = spec.displayPrecision ?? (spec.type === "int" ? 0 : 2); // Show range hint underneath if both bounds are set. const rangeText = spec.min !== undefined && spec.max !== undefined ? `${formatNum(spec.min, precision)} – ${formatNum(spec.max, precision)}` : null; return ( ); } /** Field label with a small ⓘ icon when the param spec carries a hint. * Tooltip behaviour lives in the shared `TipIcon` component. */ function FieldLabel({ label, hint, }: { label: string; hint?: string; }) { return ( {label} {hint && } ); } function formatNum(n: number, precision: number): string { if (!Number.isFinite(n)) return "—"; return n.toFixed(precision); }