Skip to content

Commit

Permalink
fix: decouple actionFinalize and actionErase (excalidraw#4984)
Browse files Browse the repository at this point in the history
* Update actionCanvas.tsx

* Update actionFinalize.tsx

* lint

* remove Escape trigger from actionErase

* revert to lastActiveTool only if coming from eraser tool

* unrelated: fix restoring `appState.activeTool`

* one more restoring fix

* fix tests

Co-authored-by: dwelle <[email protected]>
  • Loading branch information
zsviczian and dwelle authored Mar 29, 2022
1 parent f2d2f97 commit 734bb4d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
7 changes: 1 addition & 6 deletions src/actions/actionCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,7 @@ export const actionErase = register({
commitToHistory: true,
};
},
keyTest: (event, appState) => {
return (
event.key === KEYS.E ||
(event.key === KEYS.ESCAPE && isEraserActive(appState))
);
},
keyTest: (event) => event.key === KEYS.E,
PanelComponent: ({ elements, appState, updateData, data }) => (
<ToolButton
type="button"
Expand Down
17 changes: 11 additions & 6 deletions src/actions/actionFinalize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
bindOrUnbindLinearElement,
} from "../element/binding";
import { isBindingElement } from "../element/typeChecks";
import { isEraserActive } from "../appState";

export const actionFinalize = register({
name: "finalize",
Expand Down Expand Up @@ -146,7 +145,14 @@ export const actionFinalize = register({
appState.activeTool.type === "freedraw") &&
multiPointElement
? appState.activeTool
: { ...appState.activeTool, type: "selection" },
: {
...appState.activeTool,
type:
appState.activeTool.type === "eraser" &&
appState.activeTool.lastActiveToolBeforeEraser
? appState.activeTool.lastActiveToolBeforeEraser
: "selection",
},
draggingElement: null,
multiElement: null,
editingElement: null,
Expand All @@ -167,12 +173,11 @@ export const actionFinalize = register({
};
},
keyTest: (event, appState) =>
!isEraserActive(appState) &&
((event.key === KEYS.ESCAPE &&
(event.key === KEYS.ESCAPE &&
(appState.editingLinearElement !== null ||
(!appState.draggingElement && appState.multiElement === null))) ||
((event.key === KEYS.ESCAPE || event.key === KEYS.ENTER) &&
appState.multiElement !== null)),
((event.key === KEYS.ESCAPE || event.key === KEYS.ENTER) &&
appState.multiElement !== null),
PanelComponent: ({ appState, updateData, data }) => (
<ToolButton
type="button"
Expand Down
10 changes: 7 additions & 3 deletions src/data/restore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,13 @@ export const restoreAppState = (
}
return {
...nextAppState,
activeTool: AllowedExcalidrawActiveTools[nextAppState.activeTool.type]
? nextAppState.activeTool
: { ...nextAppState.activeTool, type: "selection" },
activeTool: {
lastActiveToolBeforeEraser: null,
locked: nextAppState.activeTool.locked ?? false,
type: AllowedExcalidrawActiveTools[nextAppState.activeTool.type]
? nextAppState.activeTool.type ?? "selection"
: "selection",
},
// Migrates from previous version where appState.zoom was a number
zoom:
typeof appState.zoom === "number"
Expand Down
4 changes: 3 additions & 1 deletion src/tests/data/restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ describe("restoreAppState", () => {
stubImportedAppState,
stubLocalAppState,
);
expect(restoredAppState.activeTool).toBe(stubImportedAppState.activeTool);
expect(restoredAppState.activeTool).toEqual(
stubImportedAppState.activeTool,
);
expect(restoredAppState.cursorButton).toBe(
stubImportedAppState.cursorButton,
);
Expand Down

0 comments on commit 734bb4d

Please sign in to comment.