fix(workbench): preserve cross-domain undo order
This commit is contained in:
1 parent
31552a5b6f
commit
bb6c51c489
2 files changed
+40
-6
No files matched your search
@@ -80,6 +80,15 @@ function App() {
|
|||||||
const [status, setStatus] = useState('正在加载工作台...');
|
const [status, setStatus] = useState('正在加载工作台...');
|
||||||
const [filter, setFilter] = useState<'all' | 'candidates' | 'other'>('all');
|
const [filter, setFilter] = useState<'all' | 'candidates' | 'other'>('all');
|
||||||
const [marking, setMarking] = useState<Record<string, unknown> | null>(null);
|
const [marking, setMarking] = useState<Record<string, unknown> | null>(null);
|
||||||
|
const editOrderRef = useRef<Array<'session' | 'signal'>>([]);
|
||||||
|
const redoOrderRef = useRef<Array<'session' | 'signal'>>([]);
|
||||||
|
const recordSignalEdit = useMemo(
|
||||||
|
() => () => {
|
||||||
|
editOrderRef.current.push('signal');
|
||||||
|
redoOrderRef.current = [];
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
const [editMode, setEditMode] = useState(() => {
|
const [editMode, setEditMode] = useState(() => {
|
||||||
if (typeof window === 'undefined') return false;
|
if (typeof window === 'undefined') return false;
|
||||||
try {
|
try {
|
||||||
@@ -90,7 +99,7 @@ function App() {
|
|||||||
});
|
});
|
||||||
const overrideDraft = useOverrideDraft({ state, setState, setStatus });
|
const overrideDraft = useOverrideDraft({ state, setState, setStatus });
|
||||||
const { staged, stage, save, clear: clearStaged } = overrideDraft;
|
const { staged, stage, save, clear: clearStaged } = overrideDraft;
|
||||||
const signalDraft = useTrafficSignalDraft({ state, setState });
|
const signalDraft = useTrafficSignalDraft({ state, setState, onEdit: recordSignalEdit });
|
||||||
const {
|
const {
|
||||||
dirty: signalDirty,
|
dirty: signalDirty,
|
||||||
setDirty: setSignalDirty,
|
setDirty: setSignalDirty,
|
||||||
@@ -393,6 +402,8 @@ function App() {
|
|||||||
if (!draft || !session) return;
|
if (!draft || !session) return;
|
||||||
requester?.flush(draft);
|
requester?.flush(draft);
|
||||||
session.commit(draft.constraints ?? [], constraintIds, gestureIdentity());
|
session.commit(draft.constraints ?? [], constraintIds, gestureIdentity());
|
||||||
|
editOrderRef.current.push('session');
|
||||||
|
redoOrderRef.current = [];
|
||||||
gesture.current = null;
|
gesture.current = null;
|
||||||
touch();
|
touch();
|
||||||
};
|
};
|
||||||
@@ -471,9 +482,14 @@ function App() {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
const undoEdit = () => {
|
const undoEdit = () => {
|
||||||
|
const domain = editOrderRef.current.at(-1);
|
||||||
|
if (domain === 'signal') {
|
||||||
|
editOrderRef.current.pop();
|
||||||
|
redoOrderRef.current.push('signal');
|
||||||
|
}
|
||||||
// A saved gesture cannot be un-applied — persisted history is append-only — so
|
// A saved gesture cannot be un-applied — persisted history is append-only — so
|
||||||
// it is reversed by an inverse operation, which needs its own identity.
|
// it is reversed by an inverse operation, which needs its own identity.
|
||||||
if (!session?.canUndo && signalUndoRef.current.length) {
|
if (domain === 'signal' && signalUndoRef.current.length) {
|
||||||
const previous = signalUndoRef.current.pop()!;
|
const previous = signalUndoRef.current.pop()!;
|
||||||
signalRedoRef.current.push(state?.trafficSignals.assemblies.features || []);
|
signalRedoRef.current.push(state?.trafficSignals.assemblies.features || []);
|
||||||
setState((current) =>
|
setState((current) =>
|
||||||
@@ -491,6 +507,10 @@ function App() {
|
|||||||
setSignalDirty(true);
|
setSignalDirty(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (domain === 'session') {
|
||||||
|
editOrderRef.current.pop();
|
||||||
|
redoOrderRef.current.push('session');
|
||||||
|
}
|
||||||
const reversed = session?.undo({
|
const reversed = session?.undo({
|
||||||
operationId: `operation:undo:${Date.now()}`,
|
operationId: `operation:undo:${Date.now()}`,
|
||||||
createdAt: new Date().toISOString(),
|
createdAt: new Date().toISOString(),
|
||||||
@@ -500,7 +520,10 @@ function App() {
|
|||||||
previewSession();
|
previewSession();
|
||||||
};
|
};
|
||||||
const redoEdit = () => {
|
const redoEdit = () => {
|
||||||
if (!session?.canRedo && signalRedoRef.current.length) {
|
const domain = redoOrderRef.current.at(-1);
|
||||||
|
if (domain === 'signal' && signalRedoRef.current.length) {
|
||||||
|
redoOrderRef.current.pop();
|
||||||
|
editOrderRef.current.push('signal');
|
||||||
const next = signalRedoRef.current.pop()!;
|
const next = signalRedoRef.current.pop()!;
|
||||||
signalUndoRef.current.push(state?.trafficSignals.assemblies.features || []);
|
signalUndoRef.current.push(state?.trafficSignals.assemblies.features || []);
|
||||||
setState((current) =>
|
setState((current) =>
|
||||||
@@ -518,6 +541,10 @@ function App() {
|
|||||||
setSignalDirty(true);
|
setSignalDirty(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (domain === 'session') {
|
||||||
|
redoOrderRef.current.pop();
|
||||||
|
editOrderRef.current.push('session');
|
||||||
|
}
|
||||||
if (!session?.redo()) return;
|
if (!session?.redo()) return;
|
||||||
touch();
|
touch();
|
||||||
previewSession();
|
previewSession();
|
||||||
@@ -541,6 +568,8 @@ function App() {
|
|||||||
signalDraftRef.current = null;
|
signalDraftRef.current = null;
|
||||||
signalUndoRef.current = [];
|
signalUndoRef.current = [];
|
||||||
signalRedoRef.current = [];
|
signalRedoRef.current = [];
|
||||||
|
editOrderRef.current = [];
|
||||||
|
redoOrderRef.current = [];
|
||||||
setSignalDirty(false);
|
setSignalDirty(false);
|
||||||
clearStaged();
|
clearStaged();
|
||||||
gesture.current = null;
|
gesture.current = null;
|
||||||
|
|||||||
@@ -32,9 +32,11 @@ const rotateFeatures = (features: GeoFeature[], uid: string, mastHeading: number
|
|||||||
export function useTrafficSignalDraft({
|
export function useTrafficSignalDraft({
|
||||||
state,
|
state,
|
||||||
setState,
|
setState,
|
||||||
|
onEdit,
|
||||||
}: {
|
}: {
|
||||||
state?: WorkbenchState;
|
state?: WorkbenchState;
|
||||||
setState: Dispatch<SetStateAction<WorkbenchState | undefined>>;
|
setState: Dispatch<SetStateAction<WorkbenchState | undefined>>;
|
||||||
|
onEdit?: () => void;
|
||||||
}) {
|
}) {
|
||||||
const [dirty, setDirty] = useState(false);
|
const [dirty, setDirty] = useState(false);
|
||||||
const draftRef = useRef<GeoFeature[] | null>(null);
|
const draftRef = useRef<GeoFeature[] | null>(null);
|
||||||
@@ -47,6 +49,7 @@ export function useTrafficSignalDraft({
|
|||||||
const update = useCallback(
|
const update = useCallback(
|
||||||
async (features: GeoFeature[]) => {
|
async (features: GeoFeature[]) => {
|
||||||
if (state) undoRef.current.push(state.trafficSignals.assemblies.features);
|
if (state) undoRef.current.push(state.trafficSignals.assemblies.features);
|
||||||
|
onEdit?.();
|
||||||
redoRef.current = [];
|
redoRef.current = [];
|
||||||
draftRef.current = features;
|
draftRef.current = features;
|
||||||
setDirty(true);
|
setDirty(true);
|
||||||
@@ -62,7 +65,7 @@ export function useTrafficSignalDraft({
|
|||||||
: current,
|
: current,
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
[setState, state],
|
[onEdit, setState, state],
|
||||||
);
|
);
|
||||||
|
|
||||||
const move = useCallback(
|
const move = useCallback(
|
||||||
@@ -71,6 +74,7 @@ export function useTrafficSignalDraft({
|
|||||||
if (!current) return current;
|
if (!current) return current;
|
||||||
if (!moveActiveRef.current) {
|
if (!moveActiveRef.current) {
|
||||||
undoRef.current.push(current.trafficSignals.assemblies.features);
|
undoRef.current.push(current.trafficSignals.assemblies.features);
|
||||||
|
onEdit?.();
|
||||||
redoRef.current = [];
|
redoRef.current = [];
|
||||||
moveActiveRef.current = true;
|
moveActiveRef.current = true;
|
||||||
}
|
}
|
||||||
@@ -95,7 +99,7 @@ export function useTrafficSignalDraft({
|
|||||||
});
|
});
|
||||||
setDirty(true);
|
setDirty(true);
|
||||||
},
|
},
|
||||||
[setState],
|
[onEdit, setState],
|
||||||
);
|
);
|
||||||
|
|
||||||
const finishMove = useCallback(
|
const finishMove = useCallback(
|
||||||
@@ -120,6 +124,7 @@ export function useTrafficSignalDraft({
|
|||||||
if (!current) return current;
|
if (!current) return current;
|
||||||
if (!rotateActiveRef.current) {
|
if (!rotateActiveRef.current) {
|
||||||
undoRef.current.push(current.trafficSignals.assemblies.features);
|
undoRef.current.push(current.trafficSignals.assemblies.features);
|
||||||
|
onEdit?.();
|
||||||
redoRef.current = [];
|
redoRef.current = [];
|
||||||
rotateActiveRef.current = true;
|
rotateActiveRef.current = true;
|
||||||
}
|
}
|
||||||
@@ -136,7 +141,7 @@ export function useTrafficSignalDraft({
|
|||||||
});
|
});
|
||||||
setDirty(true);
|
setDirty(true);
|
||||||
},
|
},
|
||||||
[setState],
|
[onEdit, setState],
|
||||||
);
|
);
|
||||||
|
|
||||||
const finishRotate = useCallback(
|
const finishRotate = useCallback(
|
||||||
|
|||||||
Reference in new issue
Block a user