test(workbench): lock cross-domain edit history
This commit is contained in:
1 parent
bb6c51c489
commit
2fb9db65e5
3 files changed
+78
-24
No files matched your search
@@ -33,6 +33,7 @@ import {
|
|||||||
import { previewArrowFeatures } from './edit/arrow-preview';
|
import { previewArrowFeatures } from './edit/arrow-preview';
|
||||||
import { rendersSame } from './edit/parameter';
|
import { rendersSame } from './edit/parameter';
|
||||||
import { PreviewRequester, type PreviewDraft } from './edit/preview-request';
|
import { PreviewRequester, type PreviewDraft } from './edit/preview-request';
|
||||||
|
import { clearEditHistory, recordEdit, takeRedoDomain, takeUndoDomain, type EditHistory } from './edit/edit-history';
|
||||||
import {
|
import {
|
||||||
anchorSnapshotFor,
|
anchorSnapshotFor,
|
||||||
constraintValueFor,
|
constraintValueFor,
|
||||||
@@ -80,12 +81,10 @@ 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 editHistoryRef = useRef<EditHistory>({ undo: [], redo: [] });
|
||||||
const redoOrderRef = useRef<Array<'session' | 'signal'>>([]);
|
|
||||||
const recordSignalEdit = useMemo(
|
const recordSignalEdit = useMemo(
|
||||||
() => () => {
|
() => () => {
|
||||||
editOrderRef.current.push('signal');
|
recordEdit(editHistoryRef.current, 'signal');
|
||||||
redoOrderRef.current = [];
|
|
||||||
},
|
},
|
||||||
[],
|
[],
|
||||||
);
|
);
|
||||||
@@ -402,8 +401,7 @@ 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');
|
recordEdit(editHistoryRef.current, 'session');
|
||||||
redoOrderRef.current = [];
|
|
||||||
gesture.current = null;
|
gesture.current = null;
|
||||||
touch();
|
touch();
|
||||||
};
|
};
|
||||||
@@ -482,11 +480,7 @@ function App() {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
const undoEdit = () => {
|
const undoEdit = () => {
|
||||||
const domain = editOrderRef.current.at(-1);
|
const domain = takeUndoDomain(editHistoryRef.current);
|
||||||
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 (domain === 'signal' && signalUndoRef.current.length) {
|
if (domain === 'signal' && signalUndoRef.current.length) {
|
||||||
@@ -507,10 +501,7 @@ function App() {
|
|||||||
setSignalDirty(true);
|
setSignalDirty(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (domain === 'session') {
|
if (domain !== 'session') return;
|
||||||
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(),
|
||||||
@@ -520,10 +511,8 @@ function App() {
|
|||||||
previewSession();
|
previewSession();
|
||||||
};
|
};
|
||||||
const redoEdit = () => {
|
const redoEdit = () => {
|
||||||
const domain = redoOrderRef.current.at(-1);
|
const domain = takeRedoDomain(editHistoryRef.current);
|
||||||
if (domain === 'signal' && signalRedoRef.current.length) {
|
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) =>
|
||||||
@@ -541,10 +530,7 @@ function App() {
|
|||||||
setSignalDirty(true);
|
setSignalDirty(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (domain === 'session') {
|
if (domain !== 'session') return;
|
||||||
redoOrderRef.current.pop();
|
|
||||||
editOrderRef.current.push('session');
|
|
||||||
}
|
|
||||||
if (!session?.redo()) return;
|
if (!session?.redo()) return;
|
||||||
touch();
|
touch();
|
||||||
previewSession();
|
previewSession();
|
||||||
@@ -568,8 +554,7 @@ function App() {
|
|||||||
signalDraftRef.current = null;
|
signalDraftRef.current = null;
|
||||||
signalUndoRef.current = [];
|
signalUndoRef.current = [];
|
||||||
signalRedoRef.current = [];
|
signalRedoRef.current = [];
|
||||||
editOrderRef.current = [];
|
clearEditHistory(editHistoryRef.current);
|
||||||
redoOrderRef.current = [];
|
|
||||||
setSignalDirty(false);
|
setSignalDirty(false);
|
||||||
clearStaged();
|
clearStaged();
|
||||||
gesture.current = null;
|
gesture.current = null;
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { describe, expect, it } from 'vitest';
|
||||||
|
import { clearEditHistory, recordEdit, takeRedoDomain, takeUndoDomain, type EditHistory } from './edit-history';
|
||||||
|
|
||||||
|
const history = (): EditHistory => ({ undo: [], redo: [] });
|
||||||
|
|
||||||
|
describe('edit history domains', () => {
|
||||||
|
it('undoes the most recent domain across session and signal stacks', () => {
|
||||||
|
const value = history();
|
||||||
|
recordEdit(value, 'session');
|
||||||
|
recordEdit(value, 'signal');
|
||||||
|
expect(takeUndoDomain(value)).toBe('signal');
|
||||||
|
expect(takeUndoDomain(value)).toBe('session');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('redo preserves the same cross-domain order', () => {
|
||||||
|
const value = history();
|
||||||
|
recordEdit(value, 'session');
|
||||||
|
recordEdit(value, 'signal');
|
||||||
|
takeUndoDomain(value);
|
||||||
|
expect(takeRedoDomain(value)).toBe('signal');
|
||||||
|
expect(value.undo).toEqual(['session', 'signal']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('clears redo after a new edit', () => {
|
||||||
|
const value = history();
|
||||||
|
recordEdit(value, 'session');
|
||||||
|
takeUndoDomain(value);
|
||||||
|
recordEdit(value, 'signal');
|
||||||
|
expect(value.redo).toEqual([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('clears both stacks', () => {
|
||||||
|
const value = history();
|
||||||
|
recordEdit(value, 'session');
|
||||||
|
takeUndoDomain(value);
|
||||||
|
clearEditHistory(value);
|
||||||
|
expect(value).toEqual({ undo: [], redo: [] });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
export type EditDomain = 'session' | 'signal';
|
||||||
|
|
||||||
|
export interface EditHistory {
|
||||||
|
undo: EditDomain[];
|
||||||
|
redo: EditDomain[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function recordEdit(history: EditHistory, domain: EditDomain): void {
|
||||||
|
history.undo.push(domain);
|
||||||
|
history.redo.length = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function takeUndoDomain(history: EditHistory): EditDomain | null {
|
||||||
|
const domain = history.undo.pop();
|
||||||
|
if (!domain) return null;
|
||||||
|
history.redo.push(domain);
|
||||||
|
return domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function takeRedoDomain(history: EditHistory): EditDomain | null {
|
||||||
|
const domain = history.redo.pop();
|
||||||
|
if (!domain) return null;
|
||||||
|
history.undo.push(domain);
|
||||||
|
return domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function clearEditHistory(history: EditHistory): void {
|
||||||
|
history.undo.length = 0;
|
||||||
|
history.redo.length = 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user