Skip to content

Commit

Permalink
Possible fix for data loss after undo then redo of partial stroke era…
Browse files Browse the repository at this point in the history
…sing
  • Loading branch information
personalizedrefrigerator committed May 16, 2024
1 parent fa40aed commit 6e6815a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/js-draw/src/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1115,9 +1115,9 @@ export class Editor {
const cmd = commands[j];

if (apply) {
cmd.apply(this);
await cmd.apply(this);
} else {
cmd.unapply(this);
await cmd.unapply(this);
}
}

Expand Down
30 changes: 24 additions & 6 deletions packages/js-draw/src/tools/Eraser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class Eraser extends BaseTool {
private modeValue: MutableReactiveValue<EraserMode>;

private toRemove: AbstractComponent[];
private toAdd: AbstractComponent[];
private toAdd: Set<AbstractComponent> = new Set();

// Commands that each remove one element
private eraseCommands: Erase[] = [];
Expand Down Expand Up @@ -225,15 +225,18 @@ export default class Eraser extends BaseTool {

const finalToErase = [];
for (const item of toErase) {
if (this.toAdd.includes(item)) {
this.toAdd = this.toAdd.filter(i => i !== item);
if (this.toAdd.has(item)) {
this.toAdd.delete(item);
} else {
finalToErase.push(item);
}
}

this.toRemove.push(...finalToErase);
this.toAdd.push(...toAdd);

for (const item of toAdd) {
this.toAdd.add(item);
}
this.eraseCommands.push(new Erase(finalToErase));
this.addCommands.push(...newAddCommands);
}
Expand All @@ -246,7 +249,7 @@ export default class Eraser extends BaseTool {
if (event.allPointers.length === 1 || event.current.device === PointerDevice.Eraser) {
this.lastPoint = event.current.canvasPos;
this.toRemove = [];
this.toAdd = [];
this.toAdd.clear();
this.isFirstEraseEvt = true;

this.drawPreviewAt(event.current.canvasPos);
Expand All @@ -270,7 +273,22 @@ export default class Eraser extends BaseTool {
if (this.addCommands.length > 0) {
this.addCommands.forEach(cmd => cmd.unapply(this.editor));

commands.push(...this.toAdd.map(a => EditorImage.addElement(a)));
// Remove items from toAdd that are also present in toRemove -- adding, then
// removing these does nothing, and can break undo/redo.
for (const item of this.toAdd) {
if (this.toRemove.includes(item)) {
this.toAdd.delete(item);
this.toRemove = this.toRemove.filter(other => other !== item);
}
}
for (const item of this.toRemove) {
if (this.toAdd.has(item)) {
this.toAdd.delete(item);
this.toRemove = this.toRemove.filter(other => other !== item);
}
}

commands.push(...[...this.toAdd].map(a => EditorImage.addElement(a)));

this.addCommands = [];
}
Expand Down

0 comments on commit 6e6815a

Please sign in to comment.