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
This commit is contained in:
llama 2025-01-25 15:08:47 +08:00 committed by GitHub
parent d463f11d07
commit 8ec94e281c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 1 deletions

View file

@ -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;
};

View file

@ -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();