feat(map): add reference layers and stabilize workspace controls
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState, type CSSProperties, type InputHTMLAttributes, type PointerEvent } from "react";
|
||||
import { useEffect, useRef, useState, type CSSProperties, type InputHTMLAttributes, type KeyboardEvent, type PointerEvent } from "react";
|
||||
import { cn } from "./cn.js";
|
||||
import { Dropdown } from "./Dropdown.js";
|
||||
|
||||
@@ -11,6 +11,38 @@ export interface RangeControlProps extends Omit<InputHTMLAttributes<HTMLInputEle
|
||||
onChange: (value: number) => void;
|
||||
}
|
||||
|
||||
const clampRangeValue = (value: number, min: number, max: number) => (
|
||||
Math.max(min, Math.min(max, value))
|
||||
);
|
||||
|
||||
const decimalPlaces = (value: number) => {
|
||||
const [coefficient, exponentText] = String(value).toLowerCase().split("e");
|
||||
const coefficientDecimals = coefficient?.split(".")[1]?.length ?? 0;
|
||||
if (exponentText === undefined) return coefficientDecimals;
|
||||
const exponent = Number.parseInt(exponentText, 10);
|
||||
if (!Number.isFinite(exponent)) return coefficientDecimals;
|
||||
return Math.max(0, coefficientDecimals - exponent);
|
||||
};
|
||||
|
||||
const normalizeEditedRangeValue = (
|
||||
value: number,
|
||||
min: number,
|
||||
max: number,
|
||||
step: RangeControlProps["step"],
|
||||
) => {
|
||||
const clamped = clampRangeValue(value, min, max);
|
||||
if (step === "any") return clamped;
|
||||
const numericStep = step === undefined ? 1 : Number(step);
|
||||
if (!Number.isFinite(numericStep) || numericStep <= 0) return clamped;
|
||||
const precision = Math.min(12, Math.max(decimalPlaces(min), decimalPlaces(numericStep)));
|
||||
const aligned = min + Math.round((clamped - min) / numericStep) * numericStep;
|
||||
return clampRangeValue(Number(aligned.toFixed(precision)), min, max);
|
||||
};
|
||||
|
||||
const editableNumber = (value: number) => (
|
||||
Number.isInteger(value) ? String(value) : String(Number(value.toFixed(12)))
|
||||
);
|
||||
|
||||
export function RangeControl({
|
||||
label,
|
||||
value,
|
||||
@@ -20,31 +52,111 @@ export function RangeControl({
|
||||
formatValue = String,
|
||||
onChange,
|
||||
className,
|
||||
disabled = false,
|
||||
tabIndex,
|
||||
...props
|
||||
}: RangeControlProps) {
|
||||
const rangeRef = useRef<HTMLInputElement>(null);
|
||||
const editorRef = useRef<HTMLInputElement>(null);
|
||||
const cancelNextBlurRef = useRef(false);
|
||||
const [editing, setEditing] = useState(false);
|
||||
const [draft, setDraft] = useState(() => editableNumber(value));
|
||||
const safeMax = max === min ? min + 1 : max;
|
||||
const progress = Math.max(0, Math.min(100, ((value - min) / (safeMax - min)) * 100));
|
||||
const style = { "--nodedc-range-progress": `${progress}%` } as CSSProperties;
|
||||
const displayValue = formatValue(value);
|
||||
const editorCharacters = Math.max(
|
||||
1,
|
||||
Math.min(14, editing ? draft.length || editableNumber(value).length : displayValue.length),
|
||||
);
|
||||
const style = {
|
||||
"--nodedc-range-progress": `${progress}%`,
|
||||
"--nodedc-range-editor-characters": editorCharacters,
|
||||
} as CSSProperties;
|
||||
|
||||
useEffect(() => {
|
||||
if (!editing) setDraft(editableNumber(value));
|
||||
}, [editing, value]);
|
||||
|
||||
const finishEditing = (commit: boolean, restoreRangeFocus: boolean) => {
|
||||
if (commit) {
|
||||
const parsed = Number(draft.trim().replace(",", "."));
|
||||
if (Number.isFinite(parsed)) onChange(normalizeEditedRangeValue(parsed, min, max, step));
|
||||
}
|
||||
setEditing(false);
|
||||
if (restoreRangeFocus) requestAnimationFrame(() => rangeRef.current?.focus());
|
||||
};
|
||||
|
||||
const handleEditorKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
|
||||
event.stopPropagation();
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
event.currentTarget.blur();
|
||||
}
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
cancelNextBlurRef.current = true;
|
||||
setDraft(editableNumber(value));
|
||||
setEditing(false);
|
||||
event.currentTarget.blur();
|
||||
requestAnimationFrame(() => rangeRef.current?.focus());
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<label className={cn("nodedc-range", className)} style={style}>
|
||||
<div className={cn("nodedc-range", className)} style={style} data-editing={editing || undefined}>
|
||||
<input
|
||||
ref={rangeRef}
|
||||
{...props}
|
||||
type="range"
|
||||
value={value}
|
||||
min={min}
|
||||
max={safeMax}
|
||||
step={step}
|
||||
disabled={disabled}
|
||||
tabIndex={editing ? -1 : tabIndex}
|
||||
aria-label={label}
|
||||
aria-valuetext={formatValue(value)}
|
||||
aria-valuetext={displayValue}
|
||||
onChange={(event) => onChange(Number(event.target.value))}
|
||||
{...props}
|
||||
/>
|
||||
<span className="nodedc-range__label">{label}</span>
|
||||
<span className="nodedc-range__value">{formatValue(value)}</span>
|
||||
<span className="nodedc-range__value">{displayValue}</span>
|
||||
<span className="nodedc-range__fill-text" aria-hidden="true">
|
||||
<span className="nodedc-range__label">{label}</span>
|
||||
<span className="nodedc-range__value">{formatValue(value)}</span>
|
||||
<span className="nodedc-range__value">{displayValue}</span>
|
||||
</span>
|
||||
</label>
|
||||
<input
|
||||
ref={editorRef}
|
||||
className="nodedc-range__editor"
|
||||
type="text"
|
||||
inputMode="decimal"
|
||||
value={editing ? draft : editableNumber(value)}
|
||||
aria-label={`${label}: точное значение`}
|
||||
disabled={disabled}
|
||||
spellCheck={false}
|
||||
data-active={editing || undefined}
|
||||
onPointerDown={() => {
|
||||
if (!editing) {
|
||||
setDraft(editableNumber(value));
|
||||
setEditing(true);
|
||||
}
|
||||
}}
|
||||
onFocus={() => {
|
||||
if (!editing) {
|
||||
setDraft(editableNumber(value));
|
||||
setEditing(true);
|
||||
}
|
||||
}}
|
||||
onChange={(event) => setDraft(event.target.value)}
|
||||
onKeyDown={handleEditorKeyDown}
|
||||
onBlur={() => {
|
||||
if (cancelNextBlurRef.current) {
|
||||
cancelNextBlurRef.current = false;
|
||||
return;
|
||||
}
|
||||
finishEditing(true, false);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user