feat: edit road parameters by number as well as by drag
The map had one input device. A drag projects a pointer onto the manifest's axis; there was no way to state a value, read one, or withdraw one. Both channels now meet at the same three exits in App.tsx — draftValue() previews, settleValue() commits one undoable command, abandonGesture() puts the settled state back — so nothing about the constraint, the debounce or the previewSeq arbitration is implemented twice. EditSession is untouched: focus and blur land on the gesture boundary that pointerdown and pointerup already used, which is why typing 1.53 is one undo step rather than four. Three rules the two devices do not share: - Live drag values bypass React and write the input's value directly, per direct-edit-client.md §7. A layout effect re-applies them after a mid-drag preview replaces the manifest, and drops the override once the manifest reports the value the drag asked for. - Typed values out of range are refused, not clamped. A pointer cannot state an intent it never reached, so a drag clamps; typed text is an intent, and a clamped typo reads as a plausible value that can go unnoticed. The offered extremes round inward — a real max of 2.925 renders as 2.93 under nearest-rounding, which the range it came from would then reject. - Reset disables the constraint rather than deleting it: validateEditDocument() rejects an operation naming a constraint that is not in the document. This is the one edit a drag cannot express, since a drag can set a value but never withdraw one. Interval editing stays behind intervalEditingSupported, which is still false — compileGeometry() does not read profile.interval, and a control that changes nothing is worse than no control. The conversions are implemented and tested so the row appears when the flag flips. Probing a live manifest turned up a defect that is not from this change and is not fixed here: an edge offset advertises ±2.925 while the solver rejects past about ±1. Evidence is in the task's research/, and the requirement moved to 08-31-constraint-range-validation. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
5e4e42c46f
commit
1c27501534
17 files changed
+1335
-15
No files matched your search
@@ -0,0 +1,281 @@
|
||||
// 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<string | null>(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 (
|
||||
<li className={reason ? 'parameter disabled' : 'parameter'}>
|
||||
<span className="parameter-label">
|
||||
{labelForHandle(handle)}
|
||||
{isModified(handle) ? (
|
||||
<>
|
||||
<b className="parameter-dot" title="已修改" />
|
||||
<button type="button" className="parameter-reset" onMouseDown={reset} title="回到编译基线">
|
||||
重置
|
||||
</button>
|
||||
</>
|
||||
) : null}
|
||||
</span>
|
||||
<span className="parameter-value">
|
||||
<input
|
||||
className={rejected ? 'parameter-input rejected' : 'parameter-input'}
|
||||
type="text"
|
||||
inputMode="decimal"
|
||||
disabled={Boolean(reason)}
|
||||
value={text ?? baseline}
|
||||
{...{ [BASELINE_ATTRIBUTE]: baseline }}
|
||||
ref={(node) => 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();
|
||||
}
|
||||
}}
|
||||
/>{' '}
|
||||
<em>m</em>
|
||||
</span>
|
||||
<span className={rejected ? 'parameter-range rejected' : 'parameter-range'}>
|
||||
{reason ?? (
|
||||
<>
|
||||
{rejected ? '超出范围|' : ''}
|
||||
可取{' '}
|
||||
<button type="button" className="parameter-extreme" onMouseDown={fill(lowest)}>
|
||||
{lowest}
|
||||
</button>{' '}
|
||||
~{' '}
|
||||
<button type="button" className="parameter-extreme" onMouseDown={fill(highest)}>
|
||||
{highest}
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
</span>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
export function ParameterPanel({
|
||||
handles,
|
||||
hasSelection,
|
||||
ref,
|
||||
...callbacks
|
||||
}: RowCallbacks & {
|
||||
handles: EditHandle[];
|
||||
hasSelection: boolean;
|
||||
ref?: Ref<ParameterPanelHandle>;
|
||||
}) {
|
||||
const nodes = useRef(new Map<string, HTMLInputElement>());
|
||||
/** Handles under a drag, so a re-render cannot undo the live value. */
|
||||
const live = useRef(new Map<string, number>());
|
||||
|
||||
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 (
|
||||
<section className="parameters">
|
||||
<h1>道路参数</h1>
|
||||
{handles.length ? (
|
||||
<ul>
|
||||
{handles.map((handle) => (
|
||||
<ParameterRow handle={handle} register={register} key={handle.handleId} {...callbacks} />
|
||||
))}
|
||||
</ul>
|
||||
) : (
|
||||
<p className="parameter-empty">{hasSelection ? '当前道路无可编辑参数' : '选中道路以显示参数'}</p>
|
||||
)}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user