修复 redo undo 的逻辑错误

This commit is contained in:
JIANG
2026-03-10 17:35:20 +08:00
parent 64dcf9cbdb
commit 62914f80c3
@@ -35,8 +35,13 @@ const DrawPanel: React.FC = () => {
null null
); );
const [drawnFeatures, setDrawnFeatures] = useState<Feature<Geometry>[]>([]); const [drawnFeatures, setDrawnFeatures] = useState<Feature<Geometry>[]>([]);
const [historyStack, setHistoryStack] = useState<Feature<Geometry>[][]>([]); const [history, setHistory] = useState<{
const [historyIndex, setHistoryIndex] = useState<number>(-1); stack: Feature<Geometry>[][];
index: number;
}>({
stack: [[]],
index: 0,
});
const drawInteractionRef = useRef<Draw | null>(null); const drawInteractionRef = useRef<Draw | null>(null);
@@ -88,14 +93,16 @@ const DrawPanel: React.FC = () => {
// 保存到历史记录 // 保存到历史记录
const saveToHistory = useCallback( const saveToHistory = useCallback(
(features: Feature<Geometry>[]) => { (features: Feature<Geometry>[]) => {
setHistoryStack((prevStack) => { setHistory((prev) => {
const newHistory = prevStack.slice(0, historyIndex + 1); const newStack = prev.stack.slice(0, prev.index + 1);
newHistory.push([...features]); newStack.push([...features]);
setHistoryIndex(newHistory.length - 1); return {
return newHistory; stack: newStack,
index: newStack.length - 1,
};
}); });
}, },
[historyIndex] []
); );
// 添加绘图交互 // 添加绘图交互
@@ -153,7 +160,13 @@ const DrawPanel: React.FC = () => {
// 绘图完成事件 // 绘图完成事件
draw.on("drawend", (event: DrawEvent) => { draw.on("drawend", (event: DrawEvent) => {
const feature = event.feature; const feature = event.feature;
const currentFeatures = [...drawnFeatures, feature]; const currentFeatures = [...source.getFeatures()];
// Fallback in case feature has not been synced to source yet.
if (!currentFeatures.includes(feature)) {
currentFeatures.push(feature);
}
setDrawnFeatures(currentFeatures); setDrawnFeatures(currentFeatures);
saveToHistory(currentFeatures); saveToHistory(currentFeatures);
}); });
@@ -244,23 +257,29 @@ const DrawPanel: React.FC = () => {
// 撤销功能 // 撤销功能
const handleUndo = () => { const handleUndo = () => {
if (historyIndex > 0) { if (history.index > 0) {
const newIndex = historyIndex - 1; const newIndex = history.index - 1;
const previousFeatures = historyStack[newIndex]; const previousFeatures = history.stack[newIndex];
updateDrawLayer(previousFeatures); updateDrawLayer(previousFeatures);
setDrawnFeatures(previousFeatures); setDrawnFeatures(previousFeatures);
setHistoryIndex(newIndex); setHistory((prev) => ({
...prev,
index: newIndex,
}));
} }
}; };
// 重做功能 // 重做功能
const handleRedo = () => { const handleRedo = () => {
if (historyIndex < historyStack.length - 1) { if (history.index < history.stack.length - 1) {
const newIndex = historyIndex + 1; const newIndex = history.index + 1;
const nextFeatures = historyStack[newIndex]; const nextFeatures = history.stack[newIndex];
updateDrawLayer(nextFeatures); updateDrawLayer(nextFeatures);
setDrawnFeatures(nextFeatures); setDrawnFeatures(nextFeatures);
setHistoryIndex(newIndex); setHistory((prev) => ({
...prev,
index: newIndex,
}));
} }
}; };
@@ -291,17 +310,9 @@ const DrawPanel: React.FC = () => {
} }
}; };
// 初始化历史记录
useEffect(() => {
// 初始化空的历史记录
if (historyStack.length === 0) {
saveToHistory([]);
}
}, [historyStack.length, saveToHistory]);
// 判断按钮是否应该禁用 // 判断按钮是否应该禁用
const isUndoDisabled = historyIndex <= 0; const isUndoDisabled = history.index <= 0;
const isRedoDisabled = historyIndex >= historyStack.length - 1; const isRedoDisabled = history.index >= history.stack.length - 1;
const isDeleteDisabled = drawnFeatures.length === 0; const isDeleteDisabled = drawnFeatures.length === 0;
const isSaveDisabled = drawnFeatures.length === 0; const isSaveDisabled = drawnFeatures.length === 0;