feat: refine complex junction geometry and workbench

This commit is contained in:
que01 committed 2026-09-23 17:55:55 +08:00
1 parent 7c04d086db
commit 05614d1a69
20 files changed
+315 -267

No files matched your search

+70 -15
View File
@@ -463,6 +463,11 @@ function App() {
* same id in the document, which `validateEditDocument()` rejects outright.
*/
const gesture = useRef<{ operationId: string; createdAt: string } | null>(null);
const pendingDraft = useRef<PreviewDraft | null>(null);
const debugIslandDraft = (stage: string, draft: PreviewDraft | null) => {
const item = draft?.constraints?.find((constraint) => constraint.kind === 'cluster-island-offset');
if (item) console.log('[direct-edit:island]', stage, item.value, item.id, item.provenance?.operationId);
};
const gestureIdentity = () => {
if (!gesture.current)
gesture.current = {
@@ -576,6 +581,16 @@ function App() {
for (const constraint of draft.constraints ?? []) constraints.set(constraint.id, constraint);
for (const operation of draft.operations ?? []) operations.set(operation.id, operation);
}
// Every member draft is built from the same gesture operation. Merge its
// constraint ids instead of letting the last member overwrite the earlier
// operation; otherwise validation sees operation ids that are absent from
// the final constraint set.
for (const operation of operations.values()) {
if (!gesture.current || operation.id !== gesture.current.operationId) continue;
operation.constraintIds = [...constraints.values()]
.filter((constraint) => constraint.provenance.operationId === operation.id)
.map((constraint) => constraint.id);
}
return { constraints: [...constraints.values()], operations: [...operations.values()] };
};
const settleGroup = (handles: EditHandle[], value: number) => {
@@ -590,19 +605,26 @@ function App() {
const defaultKey = [...frequencies.entries()].sort((a, b) => b[1] - a[1])[0]?.[0];
const changed = handles.filter(
(handle) =>
formatParameterValue(handle.value.current) === defaultKey && !rendersSame(value, handle.value.current),
formatParameterValue(handle.value.current) === defaultKey &&
(Boolean(gesture.current) || !rendersSame(value, handle.value.current)),
);
if (!changed.length) {
// Once typing has produced a draft, the group value is authoritative. The
// preview may already have rewritten every manifest value before blur or
// Enter; using that refreshed manifest to filter members would discard the
// pending edit and restore the old document.
const targetHandles = gesture.current ? handles : changed;
if (!targetHandles.length) {
abandonGesture();
return;
}
const draft = draftForGroup(changed, value);
if (draft)
commitGesture(
draft,
changed.map((handle) => `constraint:${handle.handleId}`),
);
else abandonGesture();
const draft = draftForGroup(targetHandles, value);
if (draft) {
pendingDraft.current = draft;
const ids = targetHandles
.map((handle) => `constraint:${handle.handleId}`)
.filter((id) => (draft.constraints ?? []).some((constraint) => constraint.id === id));
commitGesture(draft, ids);
} else abandonGesture();
};
const draftLane = (laneId: string, maneuver: string, placementDistanceMeters: number): PreviewDraft | null => {
const handle = manifest?.handles.find(
@@ -654,6 +676,8 @@ function App() {
*/
const commitGesture = (draft: PreviewDraft | null, constraintIds: string[]) => {
if (!draft || !session) return;
debugIslandDraft('session.commit', draft);
pendingDraft.current = null;
requester?.flush(draft);
session.commit(draft.constraints ?? [], constraintIds, gestureIdentity());
recordEdit(editHistoryRef.current, 'session');
@@ -681,10 +705,15 @@ function App() {
*/
const draftValue = (handle: EditHandle, value: number) => {
const draft = draftFor(handle, value);
if (draft) requester?.schedule(draft);
if (draft) {
pendingDraft.current = draft;
debugIslandDraft('preview.schedule', draft);
requester?.schedule(draft);
}
};
/** Neither channel committed: release the id and put the settled state back. */
const abandonGesture = () => {
pendingDraft.current = null;
gesture.current = null;
panelRef.current?.clearLive();
previewSession();
@@ -719,7 +748,11 @@ function App() {
// captured at pointerdown, so this compares against the pre-drag value. The
// element box is a large hit target, which makes a click-without-drag common
// rather than an edge case — but the same applies to a tapped handle dot.
if (rendersSame(value, handle.value.current)) {
// A focused field can already match the refreshed manifest after its
// preview response. The live gesture identity is the source of truth for
// whether this blur has an edit to commit; comparing against the manifest
// here would abandon that edit and restore the old preview.
if (!gesture.current && rendersSame(value, handle.value.current)) {
abandonGesture();
return;
}
@@ -843,12 +876,22 @@ function App() {
setStatus('已放弃未保存的直接编辑');
};
const saveDirectEdits = async (recompile = true) => {
if (!session || !session.dirty) return true;
// A field still being typed in holds an uncommitted value. Blur fires before
// click so the button press would settle it anyway, but that ordering is not
// something to depend on for whether an edit is persisted.
panelRef.current?.settleOpenEdit();
panelRef.current?.commitPending();
// A preview draft is authoritative user intent even if the input event
// that normally commits it was interrupted by a manifest refresh.
if (session && pendingDraft.current) {
const draft = pendingDraft.current;
debugIslandDraft('save.fallback-commit', draft);
const ids = (draft.constraints ?? []).map((constraint) => constraint.id);
session.commit(draft.constraints ?? [], ids, gestureIdentity());
pendingDraft.current = null;
}
if (!session || !session.dirty) return true;
const fragment = session.fragment();
debugIslandDraft('save.payload', fragment);
let version: number;
try {
const result = await api.saveEdits({
@@ -1324,7 +1367,10 @@ function App() {
.catch((error: Error) => setStatus(`更新失败:${error.message}`));
}}
onCandidateMarkersChange={setJunctionNodeMarkers}
onApply={() => setJunctionSession((current) => (current ? applyJunctionDraft(current) : current))}
onApply={() => {
panelRef.current?.commitPending();
setJunctionSession((current) => (current ? applyJunctionDraft(current) : current));
}}
onDiscard={discardJunctionDraft}
onSave={() =>
void compile().then((ok) => {
@@ -1346,8 +1392,17 @@ function App() {
setJunctionSession((current) => (current ? { ...current, status: 'draft', dirty: true } : current));
}}
onDraftGroup={(handles, value) => {
console.log(
'[direct-edit:island:group-input]',
value,
handles.map((handle) => ({ id: handle.handleId, kind: handle.kind, current: handle.value.current })),
);
const draft = draftForGroup(handles, value);
if (draft) requester?.schedule(draft);
if (draft) {
pendingDraft.current = draft;
debugIslandDraft('preview.schedule.group', draft);
requester?.schedule(draft);
}
setJunctionSession((current) => (current ? { ...current, status: 'draft', dirty: true } : current));
}}
onSettle={(handle, value) => {
@@ -49,7 +49,7 @@ export interface ParameterPanelHandle {
* 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;
commitPending(): void;
}
/** Baseline text is kept on the node so a restore cannot go stale with a closure. */
@@ -124,8 +124,11 @@ function ParameterRow({
// 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);
// The manifest may already contain this value because an authoritative
// preview arrived while the field was focused. Compare with the value at
// focus, not the freshly replaced manifest, or blur would abandon a valid
// edit and clear the preview.
if (value !== null && rangeVerdictOf(value, min, max) === 'ok') 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();
@@ -192,7 +195,13 @@ function ParameterRow({
const next = Math.min(max, Math.max(min, (parsed ?? current) + direction * step));
draft(formatParameterValue(next));
}
if (event.key === 'Enter') event.currentTarget.blur();
if (event.key === 'Enter') {
event.preventDefault();
// Commit from the row's current text first. Calling blur here
// made Enter depend on React's rerender and the refreshed
// manifest, which could turn a valid preview back into 0.
settle();
}
if (event.key === 'Escape') {
setText(null);
onAbandon();
@@ -271,7 +280,7 @@ export function ParameterPanel({
}
live.current.clear();
},
settleOpenEdit() {
commitPending() {
// `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();