From 8ec94e281c0ee3c7255f6cc76389d2f35fc98875 Mon Sep 17 00:00:00 2001 From: llama <100429699+iamllama@users.noreply.github.com> Date: Sat, 25 Jan 2025 15:08:47 +0800 Subject: [PATCH] Remove unfinished polygon when undoing and redoing in IO mode (#3759) * make removeUnfinishedPolygon return whether a polygon was removed * treat removing an unfinished polygon as a discrete undo step * has to be handled when redoing as well, but not as a discrete step --- ts/routes/image-occlusion/tools/tool-polygon.ts | 11 ++++++++++- ts/routes/image-occlusion/tools/tool-undo-redo.ts | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ts/routes/image-occlusion/tools/tool-polygon.ts b/ts/routes/image-occlusion/tools/tool-polygon.ts index 060da72e9..11ef633bd 100644 --- a/ts/routes/image-occlusion/tools/tool-polygon.ts +++ b/ts/routes/image-occlusion/tools/tool-polygon.ts @@ -235,7 +235,15 @@ export const modifiedPolygon = (canvas: fabric.Canvas, polygon: fabric.Polygon): canvas.add(polygon1); }; -export const removeUnfinishedPolygon = (canvas: fabric.Canvas): void => { +/** + * Removes the currently unfinished polygon, if any, and reset internal state + * @returns whether or not such a polygon was removed and state was reset + */ +export const removeUnfinishedPolygon = (canvas: fabric.Canvas): boolean => { + if (!activeShape) { + // generatePolygon should've already removed points/lines and reset state + return false; + } canvas.remove(activeShape).remove(activeLine); pointsList.forEach((point) => { canvas.remove(point); @@ -249,4 +257,5 @@ export const removeUnfinishedPolygon = (canvas: fabric.Canvas): void => { pointsList = []; drawMode = false; canvas.selection = true; + return true; }; diff --git a/ts/routes/image-occlusion/tools/tool-undo-redo.ts b/ts/routes/image-occlusion/tools/tool-undo-redo.ts index c1754376e..7fdffba32 100644 --- a/ts/routes/image-occlusion/tools/tool-undo-redo.ts +++ b/ts/routes/image-occlusion/tools/tool-undo-redo.ts @@ -9,6 +9,7 @@ import { mdiRedo, mdiUndo } from "$lib/components/icons"; import { saveNeededStore } from "../store"; import { redoKeyCombination, undoKeyCombination } from "./shortcuts"; +import { removeUnfinishedPolygon } from "./tool-polygon"; /** * Undo redo for rectangle and ellipse handled here, @@ -126,6 +127,10 @@ class UndoStack { } undo(): void { + if (this.canvas && removeUnfinishedPolygon(this.canvas)) { + // treat removing the unfinished polygon as an undo step + return; + } if (this.canUndo()) { this.index--; this.updateState(); @@ -134,6 +139,10 @@ class UndoStack { } redo(): void { + if (this.canvas) { + // when redoing, removing an unfinished polygon doesn't make sense as a discrete step + removeUnfinishedPolygon(this.canvas); + } if (this.canRedo()) { this.index++; this.updateState();