// The parameter panel: every editable value on the selected road, as a number. // // This is the second input channel next to dragging. Both produce a scalar and // hand it to the same `draftValue()` / `settleValue()` in App.tsx, so this // component owns text and presentation only — never geometry, never a range of // its own. Every value, bound and disabled reason shown here is read off the // handle manifest, for the same reason handle features carry nothing but their // id: a second constraint model on the client is the failure the cross-layer // guide warns about. // // Two rules shape the rest of the file: // // 1. Live drag values do NOT go through React. `direct-edit-client.md` §7 makes // rAF coalescing the floor and "write straight to the source" the rule: the // ghost writes its OpenLayers source directly, and the panel's equivalent of a // source is the input's `value`. `showLive()` mirrors `EditGhostLayer.show()` // deliberately — same call site, same number, one draws the road and one the // digits. // // 2. A value out of the manifest's range is refused, not clamped. Dragging clamps // because a pointer cannot state an intent it never reached and the handle has // to stay on a road shape that can exist (`projection.ts:75`); typed text *is* // an intent, and a clamped typo reads as a plausible value that can go // unnoticed. Same invariant, two devices, two honest ways to say "no". import { useImperativeHandle, useLayoutEffect, useRef, useState, type Ref } from 'react'; import { formatBoundInward, formatParameterValue, isModified, labelForHandle, parseParameterInput, rangeVerdictOf, rendersSame, } from '../edit/parameter'; import { disabledReasonOf } from '../edit/selection'; import type { EditHandle } from '../edit/types'; export interface ParameterPanelHandle { /** Live value during a drag, written straight to the DOM. */ showLive(handleId: string, value: number): void; /** A drag that committed nothing: drop the overrides and fall back to React. */ clearLive(): void; /** * Closes a field the user is still typing in, before its value is read. * * Blur fires before click in the DOM, so clicking "save" would settle the field * in time on its own — but only just, and nothing states that ordering. Saving * calls this first so the guarantee does not rest on event order. */ settleOpenEdit(): void; } /** Baseline text is kept on the node so a restore cannot go stale with a closure. */ const BASELINE_ATTRIBUTE = 'data-baseline'; interface RowCallbacks { /** Typing: preview, do not commit. */ onDraft: (handle: EditHandle, value: number) => void; /** Blur or Enter: one undoable command. */ onSettle: (handle: EditHandle, value: number) => void; /** Escape, or a field left holding something unusable. */ onAbandon: () => void; /** Withdraw the constraint and return the row to the compiler's baseline. */ onReset: (handle: EditHandle) => void; } function ParameterRow({ handle, register, onDraft, onSettle, onAbandon, onReset, }: RowCallbacks & { handle: EditHandle; register: (handleId: string, node: HTMLInputElement | null) => void; }) { /** The text being typed. `null` means "not editing, follow the manifest". */ const [text, setText] = useState(null); /** What the field held at focus, so an untouched blur commits nothing. */ const seeded = useRef(''); const reason = disabledReasonOf(handle); const { current, min, max } = handle.value; const baseline = formatParameterValue(current); // Shown and filled as the same text, so the offered extreme is always typeable. const lowest = formatBoundInward(min, 'min'); const highest = formatBoundInward(max, 'max'); const parsed = text === null ? null : parseParameterInput(text); // `null` is "not a number yet" — `-` on the way to `-1.5` — and is neither // previewable nor an error, so it must not light the field up red. const verdict = parsed === null ? 'ok' : rangeVerdictOf(parsed, min, max); const rejected = verdict !== 'ok'; const draft = (next: string) => { setText(next); const value = parseParameterInput(next); if (value === null) return; if (rangeVerdictOf(value, min, max) !== 'ok') return; onDraft(handle, value); }; const settle = () => { const value = text === null ? null : parseParameterInput(text); const touched = text !== null && text !== seeded.current; setText(null); if (!touched) return; // Committing a value that renders the same as the stored one would turn a // focus/blur with no typing into an edit, because the field shows a rounded // form of what the manifest holds. if (value !== null && rangeVerdictOf(value, min, max) === 'ok' && !rendersSame(value, current)) onSettle(handle, value); // Typed, then left unusable: a previously previewed keystroke may still be // pending, so the settled state has to be put back. else onAbandon(); }; /** * Fills an extreme, so "I want it as wide as possible" costs one click. * * The text comes from `formatBoundInward`, not from the raw bound: a max of * 2.925 renders as 2.93 under nearest-rounding, and filling that would be * rejected by the very range it came from. */ const fill = (text: string) => (event: React.MouseEvent) => { // The field is focused; a plain click would blur it and settle first. event.preventDefault(); draft(text); }; /** Drops the constraint behind this row, which no drag can do. */ const reset = (event: React.MouseEvent) => { // Same reason as `fill`: never let the click settle a half-typed field first. event.preventDefault(); setText(null); onReset(handle); }; return (
  • {labelForHandle(handle)} {isModified(handle) ? ( <> ) : null} register(handle.handleId, node)} onFocus={(event) => { // Seed from what is on screen rather than from the manifest: after a // drag the row can legitimately be ahead of the last settled preview. seeded.current = event.currentTarget.value; setText(event.currentTarget.value); }} onChange={(event) => draft(event.target.value)} onBlur={settle} onKeyDown={(event) => { if (event.key === 'Enter') event.currentTarget.blur(); if (event.key === 'Escape') { setText(null); onAbandon(); event.currentTarget.blur(); } }} />{' '} m {reason ?? ( <> {rejected ? '超出范围|' : ''} 可取{' '} {' '} ~{' '} )}
  • ); } export function ParameterPanel({ handles, hasSelection, ref, ...callbacks }: RowCallbacks & { handles: EditHandle[]; hasSelection: boolean; ref?: Ref; }) { const nodes = useRef(new Map()); /** Handles under a drag, so a re-render cannot undo the live value. */ const live = useRef(new Map()); const register = (handleId: string, node: HTMLInputElement | null) => { if (node) nodes.current.set(handleId, node); else nodes.current.delete(handleId); }; useImperativeHandle(ref, () => ({ showLive(handleId, value) { live.current.set(handleId, value); const node = nodes.current.get(handleId); if (node) node.value = formatParameterValue(value); }, clearLive() { // Only for a drag that committed nothing — otherwise the effect below hands // the row back to React on its own. React does not know the value was // mutated, so a drag that ends where it started produces no re-render and // would leave its last number on screen; the baseline attribute is what the // row actually rendered, so it restores. for (const [handleId] of live.current) { const node = nodes.current.get(handleId); if (node) node.value = node.getAttribute(BASELINE_ATTRIBUTE) ?? ''; } live.current.clear(); }, settleOpenEdit() { // `blur()` runs the row's onBlur synchronously, so the command is on the // stack by the time this returns. for (const node of nodes.current.values()) if (node === document.activeElement) node.blur(); }, })); // A debounced preview lands mid-drag and replaces the manifest, so React // re-renders with a value one debounce behind the pointer. Re-applying the live // value after the DOM update — before paint — keeps the number from flickering // between the two. // // It is also how an override ends: once the manifest reports the value the drag // asked for, the row is handed back to React rather than being force-written // forever. A handle that is no longer rendered drops out with it. useLayoutEffect(() => { const present = new Set(handles.map((item) => item.handleId)); for (const [handleId, value] of live.current) { if (!present.has(handleId)) { live.current.delete(handleId); continue; } const node = nodes.current.get(handleId); if (!node) continue; const text = formatParameterValue(value); if (node.getAttribute(BASELINE_ATTRIBUTE) === text) live.current.delete(handleId); else node.value = text; } }); return (

    道路参数

    {handles.length ? (
      {handles.map((handle) => ( ))}
    ) : (

    {hasSelection ? '当前道路无可编辑参数' : '选中道路以显示参数'}

    )}
    ); }