fix(controls): preserve editable color values and settings typography

This commit is contained in:
Codex
2026-11-04 00:39:39 +03:00
parent cb534dbe39
commit ac65b1167f
4 changed files with 48 additions and 6 deletions
+24 -4
View File
@@ -102,6 +102,10 @@ function SingleRangeControl({
"--nodedc-range-editor-characters": editorCharacters,
} as CSSProperties;
// External undo/redo owns the controlled value even while the exact editor is focused.
// Otherwise its next blur would commit an obsolete draft over the restored value.
useLayoutEffect(() => setDraft(editableNumber(value)), [value]);
useEffect(() => {
if (!editing) setDraft(editableNumber(value));
}, [editing, value]);
@@ -271,7 +275,9 @@ function hsvToHex({ h, s, v }: Hsv): string {
return `#${base.map((channel) => Math.round((channel + offset) * 255).toString(16).padStart(2, "0")).join("")}`;
}
export function ColorField({ value, onChange, label = "Цвет", className, disabled = false, ...props }: ColorFieldProps) {
export function ColorField({ value, onChange, label = "Цвет", className, disabled = false, onBlur, onKeyDown, ...props }: ColorFieldProps) {
const [draft, setDraft] = useState(value);
useEffect(() => setDraft(value), [value]);
const validColor = hexToHsv(value);
const [hsv, setHsv] = useState<Hsv>(() => validColor ?? { h: 330, s: 82, v: 100 });
const style = { "--nodedc-color-value": validColor ? hsvToHex(validColor) : hsvToHex(hsv) } as CSSProperties;
@@ -283,6 +289,7 @@ export function ColorField({ value, onChange, label = "Цвет", className, dis
const commit = (next: Hsv) => {
const normalized = { h: clamp(next.h, 0, 360), s: clamp(next.s, 0, 100), v: clamp(next.v, 0, 100) };
setHsv(normalized);
setDraft(hsvToHex(normalized));
onChange(hsvToHex(normalized));
};
@@ -366,12 +373,25 @@ export function ColorField({ value, onChange, label = "Цвет", className, dis
</Dropdown>
<input
className="nodedc-color-field__text"
value={value}
{...props}
value={draft}
aria-label={`${label}: HEX`}
disabled={disabled}
spellCheck={false}
onChange={(event) => onChange(event.target.value)}
{...props}
onChange={(event) => {
const text = event.target.value;
setDraft(text);
if (parseHexColor(text)) onChange(`#${text.trim().replace(/^#/, "").toLowerCase()}`);
}}
onBlur={(event) => {
setDraft(parseHexColor(draft) ? `#${draft.trim().replace(/^#/, "").toLowerCase()}` : value);
onBlur?.(event);
}}
onKeyDown={(event) => {
if (event.key === "Escape") { event.preventDefault(); event.stopPropagation(); setDraft(value); }
if (event.key === "Enter") { event.preventDefault(); event.currentTarget.blur(); }
onKeyDown?.(event);
}}
/>
</div>
);