From 1f4f5e11aec9d8e1bceb3ece40c6c9d69a3f553e Mon Sep 17 00:00:00 2001 From: David Luzar Date: Wed, 4 Oct 2023 00:16:54 +0200 Subject: [PATCH] refactor: DRY out and simplify setting active tool from toolbar (#7079) --- src/components/Actions.tsx | 80 +++---------------- src/components/App.tsx | 44 +++++----- src/components/LayerUI.tsx | 2 +- src/components/MobileMenu.tsx | 2 +- .../__snapshots__/contextmenu.test.tsx.snap | 28 +++---- .../regressionTests.test.tsx.snap | 75 +++++++++-------- src/tests/dragCreate.test.tsx | 20 ++--- src/tests/move.test.tsx | 6 +- src/tests/multiPointCreate.test.tsx | 10 +-- src/tests/selection.test.tsx | 10 +-- src/types.ts | 4 +- 11 files changed, 116 insertions(+), 165 deletions(-) diff --git a/src/components/Actions.tsx b/src/components/Actions.tsx index 47c8df46..a176ee5a 100644 --- a/src/components/Actions.tsx +++ b/src/components/Actions.tsx @@ -14,13 +14,8 @@ import { hasText, } from "../scene"; import { SHAPES } from "../shapes"; -import { UIAppState, Zoom } from "../types"; -import { - capitalizeString, - isTransparent, - updateActiveTool, - setCursorForShape, -} from "../utils"; +import { AppClassProperties, UIAppState, Zoom } from "../types"; +import { capitalizeString, isTransparent } from "../utils"; import Stack from "./Stack"; import { ToolButton } from "./ToolButton"; import { hasStrokeColor } from "../scene/comparisons"; @@ -215,15 +210,15 @@ export const SelectedShapeActions = ({ export const ShapesSwitcher = ({ interactiveCanvas, activeTool, - setAppState, onImageAction, appState, + app, }: { interactiveCanvas: HTMLCanvasElement | null; activeTool: UIAppState["activeTool"]; - setAppState: React.Component["setState"]; onImageAction: (data: { pointerType: PointerType | null }) => void; appState: UIAppState; + app: AppClassProperties; }) => { const [isExtraToolsMenuOpen, setIsExtraToolsMenuOpen] = useState(false); const device = useDevice(); @@ -251,29 +246,14 @@ export const ShapesSwitcher = ({ data-testid={`toolbar-${value}`} onPointerDown={({ pointerType }) => { if (!appState.penDetected && pointerType === "pen") { - setAppState({ - penDetected: true, - penMode: true, - }); + app.togglePenMode(true); } }} onChange={({ pointerType }) => { if (appState.activeTool.type !== value) { trackEvent("toolbar", value, "ui"); } - const nextActiveTool = updateActiveTool(appState, { - type: value, - }); - setAppState({ - activeTool: nextActiveTool, - activeEmbeddable: null, - multiElement: null, - selectedElementIds: {}, - }); - setCursorForShape(interactiveCanvas, { - ...appState, - activeTool: nextActiveTool, - }); + app.setActiveTool({ type: value }); if (value === "image") { onImageAction({ pointerType }); } @@ -300,23 +280,12 @@ export const ShapesSwitcher = ({ data-testid={`toolbar-frame`} onPointerDown={({ pointerType }) => { if (!appState.penDetected && pointerType === "pen") { - setAppState({ - penDetected: true, - penMode: true, - }); + app.togglePenMode(true); } }} onChange={({ pointerType }) => { trackEvent("toolbar", "frame", "ui"); - const nextActiveTool = updateActiveTool(appState, { - type: "frame", - }); - setAppState({ - activeTool: nextActiveTool, - multiElement: null, - selectedElementIds: {}, - activeEmbeddable: null, - }); + app.setActiveTool({ type: "frame" }); }} selected={activeTool.type === "frame"} /> @@ -331,23 +300,12 @@ export const ShapesSwitcher = ({ data-testid={`toolbar-embeddable`} onPointerDown={({ pointerType }) => { if (!appState.penDetected && pointerType === "pen") { - setAppState({ - penDetected: true, - penMode: true, - }); + app.togglePenMode(true); } }} onChange={({ pointerType }) => { trackEvent("toolbar", "embeddable", "ui"); - const nextActiveTool = updateActiveTool(appState, { - type: "embeddable", - }); - setAppState({ - activeTool: nextActiveTool, - multiElement: null, - selectedElementIds: {}, - activeEmbeddable: null, - }); + app.setActiveTool({ type: "embeddable" }); }} selected={activeTool.type === "embeddable"} /> @@ -368,14 +326,7 @@ export const ShapesSwitcher = ({ > { - const nextActiveTool = updateActiveTool(appState, { - type: "frame", - }); - setAppState({ - activeTool: nextActiveTool, - multiElement: null, - selectedElementIds: {}, - }); + app.setActiveTool({ type: "frame" }); }} icon={frameToolIcon} shortcut={KEYS.F.toLocaleUpperCase()} @@ -386,14 +337,7 @@ export const ShapesSwitcher = ({ { - const nextActiveTool = updateActiveTool(appState, { - type: "embeddable", - }); - setAppState({ - activeTool: nextActiveTool, - multiElement: null, - selectedElementIds: {}, - }); + app.setActiveTool({ type: "embeddable" }); }} icon={EmbedIcon} data-testid="toolbar-embeddable" diff --git a/src/components/App.tsx b/src/components/App.tsx index 3fb4e5e7..b91e0e4b 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -2552,10 +2552,11 @@ class App extends React.Component { }); }; - togglePenMode = () => { + togglePenMode = (force?: boolean) => { this.setState((prevState) => { return { - penMode: !prevState.penMode, + penMode: force ?? !prevState.penMode, + penDetected: true, }; }); }; @@ -3108,7 +3109,7 @@ class App extends React.Component { } }); - private setActiveTool = ( + setActiveTool = ( tool: | { type: @@ -3135,23 +3136,30 @@ class App extends React.Component { if (nextActiveTool.type === "image") { this.onImageAction(); } - if (nextActiveTool.type !== "selection") { - this.setState((prevState) => ({ - activeTool: nextActiveTool, - selectedElementIds: makeNextSelectedElementIds({}, this.state), - selectedGroupIds: {}, - editingGroupId: null, - snapLines: [], - originSnapOffset: null, - })); - } else { - this.setState({ - activeTool: nextActiveTool, - snapLines: [], + + this.setState((prevState) => { + const commonResets = { + snapLines: prevState.snapLines.length ? [] : prevState.snapLines, originSnapOffset: null, activeEmbeddable: null, - }); - } + } as const; + if (nextActiveTool.type !== "selection") { + return { + ...prevState, + activeTool: nextActiveTool, + selectedElementIds: makeNextSelectedElementIds({}, prevState), + selectedGroupIds: makeNextSelectedElementIds({}, prevState), + editingGroupId: null, + multiElement: null, + ...commonResets, + }; + } + return { + ...prevState, + activeTool: nextActiveTool, + ...commonResets, + }; + }); }; private setCursor = (cursor: string) => { diff --git a/src/components/LayerUI.tsx b/src/components/LayerUI.tsx index 3bc8436c..d9d3d8ce 100644 --- a/src/components/LayerUI.tsx +++ b/src/components/LayerUI.tsx @@ -279,7 +279,7 @@ const LayerUI = ({ appState={appState} interactiveCanvas={interactiveCanvas} activeTool={appState.activeTool} - setAppState={setAppState} + app={app} onImageAction={({ pointerType }) => { onImageAction({ insertOnCanvasDirectly: pointerType !== "mouse", diff --git a/src/components/MobileMenu.tsx b/src/components/MobileMenu.tsx index f08b9996..b7f0ba33 100644 --- a/src/components/MobileMenu.tsx +++ b/src/components/MobileMenu.tsx @@ -87,7 +87,7 @@ export const MobileMenu = ({ appState={appState} interactiveCanvas={interactiveCanvas} activeTool={appState.activeTool} - setAppState={setAppState} + app={app} onImageAction={({ pointerType }) => { onImageAction({ insertOnCanvasDirectly: pointerType !== "mouse", diff --git a/src/tests/__snapshots__/contextmenu.test.tsx.snap b/src/tests/__snapshots__/contextmenu.test.tsx.snap index 5f949222..0cdd20b2 100644 --- a/src/tests/__snapshots__/contextmenu.test.tsx.snap +++ b/src/tests/__snapshots__/contextmenu.test.tsx.snap @@ -675,7 +675,7 @@ exports[`contextMenu element > selecting 'Add to library' in context menu adds e exports[`contextMenu element > selecting 'Add to library' in context menu adds element to library > [end of test] number of elements 1`] = `1`; -exports[`contextMenu element > selecting 'Add to library' in context menu adds element to library > [end of test] number of renders 1`] = `7`; +exports[`contextMenu element > selecting 'Add to library' in context menu adds element to library > [end of test] number of renders 1`] = `6`; exports[`contextMenu element > selecting 'Bring forward' in context menu brings element forward > [end of test] appState 1`] = ` { @@ -1051,7 +1051,7 @@ exports[`contextMenu element > selecting 'Bring forward' in context menu brings exports[`contextMenu element > selecting 'Bring forward' in context menu brings element forward > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > selecting 'Bring forward' in context menu brings element forward > [end of test] number of renders 1`] = `13`; +exports[`contextMenu element > selecting 'Bring forward' in context menu brings element forward > [end of test] number of renders 1`] = `12`; exports[`contextMenu element > selecting 'Bring to front' in context menu brings element to front > [end of test] appState 1`] = ` { @@ -1427,7 +1427,7 @@ exports[`contextMenu element > selecting 'Bring to front' in context menu brings exports[`contextMenu element > selecting 'Bring to front' in context menu brings element to front > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > selecting 'Bring to front' in context menu brings element to front > [end of test] number of renders 1`] = `13`; +exports[`contextMenu element > selecting 'Bring to front' in context menu brings element to front > [end of test] number of renders 1`] = `12`; exports[`contextMenu element > selecting 'Copy styles' in context menu copies styles > [end of test] appState 1`] = ` { @@ -1629,7 +1629,7 @@ exports[`contextMenu element > selecting 'Copy styles' in context menu copies st exports[`contextMenu element > selecting 'Copy styles' in context menu copies styles > [end of test] number of elements 1`] = `1`; -exports[`contextMenu element > selecting 'Copy styles' in context menu copies styles > [end of test] number of renders 1`] = `7`; +exports[`contextMenu element > selecting 'Copy styles' in context menu copies styles > [end of test] number of renders 1`] = `6`; exports[`contextMenu element > selecting 'Delete' in context menu deletes element > [end of test] appState 1`] = ` { @@ -1868,7 +1868,7 @@ exports[`contextMenu element > selecting 'Delete' in context menu deletes elemen exports[`contextMenu element > selecting 'Delete' in context menu deletes element > [end of test] number of elements 1`] = `1`; -exports[`contextMenu element > selecting 'Delete' in context menu deletes element > [end of test] number of renders 1`] = `8`; +exports[`contextMenu element > selecting 'Delete' in context menu deletes element > [end of test] number of renders 1`] = `7`; exports[`contextMenu element > selecting 'Duplicate' in context menu duplicates element > [end of test] appState 1`] = ` { @@ -2172,7 +2172,7 @@ exports[`contextMenu element > selecting 'Duplicate' in context menu duplicates exports[`contextMenu element > selecting 'Duplicate' in context menu duplicates element > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > selecting 'Duplicate' in context menu duplicates element > [end of test] number of renders 1`] = `8`; +exports[`contextMenu element > selecting 'Duplicate' in context menu duplicates element > [end of test] number of renders 1`] = `7`; exports[`contextMenu element > selecting 'Group selection' in context menu groups selected elements > [end of test] appState 1`] = ` { @@ -2564,7 +2564,7 @@ exports[`contextMenu element > selecting 'Group selection' in context menu group exports[`contextMenu element > selecting 'Group selection' in context menu groups selected elements > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > selecting 'Group selection' in context menu groups selected elements > [end of test] number of renders 1`] = `13`; +exports[`contextMenu element > selecting 'Group selection' in context menu groups selected elements > [end of test] number of renders 1`] = `12`; exports[`contextMenu element > selecting 'Paste styles' in context menu pastes styles > [end of test] appState 1`] = ` { @@ -3446,7 +3446,7 @@ exports[`contextMenu element > selecting 'Paste styles' in context menu pastes s exports[`contextMenu element > selecting 'Paste styles' in context menu pastes styles > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > selecting 'Paste styles' in context menu pastes styles > [end of test] number of renders 1`] = `20`; +exports[`contextMenu element > selecting 'Paste styles' in context menu pastes styles > [end of test] number of renders 1`] = `19`; exports[`contextMenu element > selecting 'Send backward' in context menu sends element backward > [end of test] appState 1`] = ` { @@ -3822,7 +3822,7 @@ exports[`contextMenu element > selecting 'Send backward' in context menu sends e exports[`contextMenu element > selecting 'Send backward' in context menu sends element backward > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > selecting 'Send backward' in context menu sends element backward > [end of test] number of renders 1`] = `12`; +exports[`contextMenu element > selecting 'Send backward' in context menu sends element backward > [end of test] number of renders 1`] = `11`; exports[`contextMenu element > selecting 'Send to back' in context menu sends element to back > [end of test] appState 1`] = ` { @@ -4198,7 +4198,7 @@ exports[`contextMenu element > selecting 'Send to back' in context menu sends el exports[`contextMenu element > selecting 'Send to back' in context menu sends element to back > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > selecting 'Send to back' in context menu sends element to back > [end of test] number of renders 1`] = `12`; +exports[`contextMenu element > selecting 'Send to back' in context menu sends element to back > [end of test] number of renders 1`] = `11`; exports[`contextMenu element > selecting 'Ungroup selection' in context menu ungroups selected group > [end of test] appState 1`] = ` { @@ -4657,7 +4657,7 @@ exports[`contextMenu element > selecting 'Ungroup selection' in context menu ung exports[`contextMenu element > selecting 'Ungroup selection' in context menu ungroups selected group > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > selecting 'Ungroup selection' in context menu ungroups selected group > [end of test] number of renders 1`] = `14`; +exports[`contextMenu element > selecting 'Ungroup selection' in context menu ungroups selected group > [end of test] number of renders 1`] = `13`; exports[`contextMenu element > shows 'Group selection' in context menu for multiple selected elements > [end of test] appState 1`] = ` { @@ -5240,7 +5240,7 @@ exports[`contextMenu element > shows 'Group selection' in context menu for multi exports[`contextMenu element > shows 'Group selection' in context menu for multiple selected elements > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > shows 'Group selection' in context menu for multiple selected elements > [end of test] number of renders 1`] = `13`; +exports[`contextMenu element > shows 'Group selection' in context menu for multiple selected elements > [end of test] number of renders 1`] = `12`; exports[`contextMenu element > shows 'Ungroup selection' in context menu for group inside selected elements > [end of test] appState 1`] = ` { @@ -5908,7 +5908,7 @@ exports[`contextMenu element > shows 'Ungroup selection' in context menu for gro exports[`contextMenu element > shows 'Ungroup selection' in context menu for group inside selected elements > [end of test] number of elements 1`] = `2`; -exports[`contextMenu element > shows 'Ungroup selection' in context menu for group inside selected elements > [end of test] number of renders 1`] = `14`; +exports[`contextMenu element > shows 'Ungroup selection' in context menu for group inside selected elements > [end of test] number of renders 1`] = `13`; exports[`contextMenu element > shows context menu for canvas > [end of test] appState 1`] = ` { @@ -7104,6 +7104,6 @@ exports[`contextMenu element > shows context menu for element > [end of test] nu exports[`contextMenu element > shows context menu for element > [end of test] number of elements 2`] = `2`; -exports[`contextMenu element > shows context menu for element > [end of test] number of renders 1`] = `7`; +exports[`contextMenu element > shows context menu for element > [end of test] number of renders 1`] = `6`; exports[`contextMenu element > shows context menu for element > [end of test] number of renders 2`] = `6`; diff --git a/src/tests/__snapshots__/regressionTests.test.tsx.snap b/src/tests/__snapshots__/regressionTests.test.tsx.snap index 32dd1754..c990a6e0 100644 --- a/src/tests/__snapshots__/regressionTests.test.tsx.snap +++ b/src/tests/__snapshots__/regressionTests.test.tsx.snap @@ -454,7 +454,7 @@ exports[`given element A and group of elements B and given both are selected whe exports[`given element A and group of elements B and given both are selected when user clicks on B, on pointer up only elements from B should be selected > [end of test] number of elements 1`] = `0`; -exports[`given element A and group of elements B and given both are selected when user clicks on B, on pointer up only elements from B should be selected > [end of test] number of renders 1`] = `22`; +exports[`given element A and group of elements B and given both are selected when user clicks on B, on pointer up only elements from B should be selected > [end of test] number of renders 1`] = `21`; exports[`given element A and group of elements B and given both are selected when user shift-clicks on B, on pointer up only element A should be selected > [end of test] appState 1`] = ` { @@ -912,7 +912,7 @@ exports[`given element A and group of elements B and given both are selected whe exports[`given element A and group of elements B and given both are selected when user shift-clicks on B, on pointer up only element A should be selected > [end of test] number of elements 1`] = `0`; -exports[`given element A and group of elements B and given both are selected when user shift-clicks on B, on pointer up only element A should be selected > [end of test] number of renders 1`] = `20`; +exports[`given element A and group of elements B and given both are selected when user shift-clicks on B, on pointer up only element A should be selected > [end of test] number of renders 1`] = `19`; exports[`regression tests > Cmd/Ctrl-click exclusively select element under pointer > [end of test] appState 1`] = ` { @@ -1743,7 +1743,7 @@ exports[`regression tests > Cmd/Ctrl-click exclusively select element under poin exports[`regression tests > Cmd/Ctrl-click exclusively select element under pointer > [end of test] number of elements 1`] = `0`; -exports[`regression tests > Cmd/Ctrl-click exclusively select element under pointer > [end of test] number of renders 1`] = `30`; +exports[`regression tests > Cmd/Ctrl-click exclusively select element under pointer > [end of test] number of renders 1`] = `29`; exports[`regression tests > Drags selected element when hitting only bounding box and keeps element selected > [end of test] appState 1`] = ` { @@ -1956,7 +1956,7 @@ exports[`regression tests > Drags selected element when hitting only bounding bo exports[`regression tests > Drags selected element when hitting only bounding box and keeps element selected > [end of test] number of elements 1`] = `0`; -exports[`regression tests > Drags selected element when hitting only bounding box and keeps element selected > [end of test] number of renders 1`] = `10`; +exports[`regression tests > Drags selected element when hitting only bounding box and keeps element selected > [end of test] number of renders 1`] = `9`; exports[`regression tests > adjusts z order when grouping > [end of test] appState 1`] = ` { @@ -2410,7 +2410,7 @@ exports[`regression tests > adjusts z order when grouping > [end of test] histor exports[`regression tests > adjusts z order when grouping > [end of test] number of elements 1`] = `0`; -exports[`regression tests > adjusts z order when grouping > [end of test] number of renders 1`] = `19`; +exports[`regression tests > adjusts z order when grouping > [end of test] number of renders 1`] = `18`; exports[`regression tests > alt-drag duplicates an element > [end of test] appState 1`] = ` { @@ -2652,7 +2652,7 @@ exports[`regression tests > alt-drag duplicates an element > [end of test] histo exports[`regression tests > alt-drag duplicates an element > [end of test] number of elements 1`] = `0`; -exports[`regression tests > alt-drag duplicates an element > [end of test] number of renders 1`] = `10`; +exports[`regression tests > alt-drag duplicates an element > [end of test] number of renders 1`] = `9`; exports[`regression tests > arrow keys > [end of test] appState 1`] = ` { @@ -2820,7 +2820,7 @@ exports[`regression tests > arrow keys > [end of test] history 1`] = ` exports[`regression tests > arrow keys > [end of test] number of elements 1`] = `0`; -exports[`regression tests > arrow keys > [end of test] number of renders 1`] = `14`; +exports[`regression tests > arrow keys > [end of test] number of renders 1`] = `13`; exports[`regression tests > can drag element that covers another element, while another elem is selected > [end of test] appState 1`] = ` { @@ -3264,7 +3264,7 @@ exports[`regression tests > can drag element that covers another element, while exports[`regression tests > can drag element that covers another element, while another elem is selected > [end of test] number of elements 1`] = `0`; -exports[`regression tests > can drag element that covers another element, while another elem is selected > [end of test] number of renders 1`] = `19`; +exports[`regression tests > can drag element that covers another element, while another elem is selected > [end of test] number of renders 1`] = `18`; exports[`regression tests > change the properties of a shape > [end of test] appState 1`] = ` { @@ -3561,7 +3561,7 @@ exports[`regression tests > change the properties of a shape > [end of test] his exports[`regression tests > change the properties of a shape > [end of test] number of elements 1`] = `0`; -exports[`regression tests > change the properties of a shape > [end of test] number of renders 1`] = `11`; +exports[`regression tests > change the properties of a shape > [end of test] number of renders 1`] = `10`; exports[`regression tests > click on an element and drag it > [dragged] appState 1`] = ` { @@ -3806,7 +3806,7 @@ exports[`regression tests > click on an element and drag it > [dragged] history exports[`regression tests > click on an element and drag it > [dragged] number of elements 1`] = `1`; -exports[`regression tests > click on an element and drag it > [dragged] number of renders 1`] = `10`; +exports[`regression tests > click on an element and drag it > [dragged] number of renders 1`] = `9`; exports[`regression tests > click on an element and drag it > [end of test] appState 1`] = ` { @@ -4062,7 +4062,7 @@ exports[`regression tests > click on an element and drag it > [end of test] hist exports[`regression tests > click on an element and drag it > [end of test] number of elements 1`] = `0`; -exports[`regression tests > click on an element and drag it > [end of test] number of renders 1`] = `12`; +exports[`regression tests > click on an element and drag it > [end of test] number of renders 1`] = `11`; exports[`regression tests > click to select a shape > [end of test] appState 1`] = ` { @@ -4304,7 +4304,7 @@ exports[`regression tests > click to select a shape > [end of test] history 1`] exports[`regression tests > click to select a shape > [end of test] number of elements 1`] = `0`; -exports[`regression tests > click to select a shape > [end of test] number of renders 1`] = `13`; +exports[`regression tests > click to select a shape > [end of test] number of renders 1`] = `12`; exports[`regression tests > click-drag to select a group > [end of test] appState 1`] = ` { @@ -4648,7 +4648,7 @@ exports[`regression tests > click-drag to select a group > [end of test] history exports[`regression tests > click-drag to select a group > [end of test] number of elements 1`] = `0`; -exports[`regression tests > click-drag to select a group > [end of test] number of renders 1`] = `18`; +exports[`regression tests > click-drag to select a group > [end of test] number of renders 1`] = `17`; exports[`regression tests > deleting last but one element in editing group should unselect the group > [end of test] appState 1`] = ` { @@ -5123,7 +5123,7 @@ exports[`regression tests > deleting last but one element in editing group shoul exports[`regression tests > deleting last but one element in editing group should unselect the group > [end of test] number of elements 1`] = `0`; -exports[`regression tests > deleting last but one element in editing group should unselect the group > [end of test] number of renders 1`] = `20`; +exports[`regression tests > deleting last but one element in editing group should unselect the group > [end of test] number of renders 1`] = `19`; exports[`regression tests > deselects group of selected elements on pointer down when pointer doesn't hit any element > [end of test] appState 1`] = ` { @@ -5420,7 +5420,7 @@ exports[`regression tests > deselects group of selected elements on pointer down exports[`regression tests > deselects group of selected elements on pointer down when pointer doesn't hit any element > [end of test] number of elements 1`] = `0`; -exports[`regression tests > deselects group of selected elements on pointer down when pointer doesn't hit any element > [end of test] number of renders 1`] = `14`; +exports[`regression tests > deselects group of selected elements on pointer down when pointer doesn't hit any element > [end of test] number of renders 1`] = `13`; exports[`regression tests > deselects group of selected elements on pointer up when pointer hits common bounding box without hitting any element > [end of test] appState 1`] = ` { @@ -5689,7 +5689,7 @@ exports[`regression tests > deselects group of selected elements on pointer up w exports[`regression tests > deselects group of selected elements on pointer up when pointer hits common bounding box without hitting any element > [end of test] number of elements 1`] = `0`; -exports[`regression tests > deselects group of selected elements on pointer up when pointer hits common bounding box without hitting any element > [end of test] number of renders 1`] = `14`; +exports[`regression tests > deselects group of selected elements on pointer up when pointer hits common bounding box without hitting any element > [end of test] number of renders 1`] = `13`; exports[`regression tests > deselects selected element on pointer down when pointer doesn't hit any element > [end of test] appState 1`] = ` { @@ -5913,7 +5913,7 @@ exports[`regression tests > deselects selected element on pointer down when poin exports[`regression tests > deselects selected element on pointer down when pointer doesn't hit any element > [end of test] number of elements 1`] = `0`; -exports[`regression tests > deselects selected element on pointer down when pointer doesn't hit any element > [end of test] number of renders 1`] = `9`; +exports[`regression tests > deselects selected element on pointer down when pointer doesn't hit any element > [end of test] number of renders 1`] = `8`; exports[`regression tests > deselects selected element, on pointer up, when click hits element bounding box but doesn't hit the element > [end of test] appState 1`] = ` { @@ -6081,7 +6081,7 @@ exports[`regression tests > deselects selected element, on pointer up, when clic exports[`regression tests > deselects selected element, on pointer up, when click hits element bounding box but doesn't hit the element > [end of test] number of elements 1`] = `0`; -exports[`regression tests > deselects selected element, on pointer up, when click hits element bounding box but doesn't hit the element > [end of test] number of renders 1`] = `9`; +exports[`regression tests > deselects selected element, on pointer up, when click hits element bounding box but doesn't hit the element > [end of test] number of renders 1`] = `8`; exports[`regression tests > double click to edit a group > [end of test] appState 1`] = ` { @@ -6533,7 +6533,7 @@ exports[`regression tests > double click to edit a group > [end of test] history exports[`regression tests > double click to edit a group > [end of test] number of elements 1`] = `0`; -exports[`regression tests > double click to edit a group > [end of test] number of renders 1`] = `19`; +exports[`regression tests > double click to edit a group > [end of test] number of renders 1`] = `18`; exports[`regression tests > drags selected elements from point inside common bounding box that doesn't hit any element and keeps elements selected after dragging > [end of test] appState 1`] = ` { @@ -6850,7 +6850,7 @@ exports[`regression tests > drags selected elements from point inside common bou exports[`regression tests > drags selected elements from point inside common bounding box that doesn't hit any element and keeps elements selected after dragging > [end of test] number of elements 1`] = `0`; -exports[`regression tests > drags selected elements from point inside common bounding box that doesn't hit any element and keeps elements selected after dragging > [end of test] number of renders 1`] = `15`; +exports[`regression tests > drags selected elements from point inside common bounding box that doesn't hit any element and keeps elements selected after dragging > [end of test] number of renders 1`] = `14`; exports[`regression tests > draw every type of shape > [end of test] appState 1`] = ` { @@ -8917,7 +8917,7 @@ exports[`regression tests > draw every type of shape > [end of test] history 1`] exports[`regression tests > draw every type of shape > [end of test] number of elements 1`] = `0`; -exports[`regression tests > draw every type of shape > [end of test] number of renders 1`] = `44`; +exports[`regression tests > draw every type of shape > [end of test] number of renders 1`] = `43`; exports[`regression tests > given a group of selected elements with an element that is not selected inside the group common bounding box when element that is not selected is clicked should switch selection to not selected element on pointer up > [end of test] appState 1`] = ` { @@ -9261,7 +9261,7 @@ exports[`regression tests > given a group of selected elements with an element t exports[`regression tests > given a group of selected elements with an element that is not selected inside the group common bounding box when element that is not selected is clicked should switch selection to not selected element on pointer up > [end of test] number of elements 1`] = `0`; -exports[`regression tests > given a group of selected elements with an element that is not selected inside the group common bounding box when element that is not selected is clicked should switch selection to not selected element on pointer up > [end of test] number of renders 1`] = `18`; +exports[`regression tests > given a group of selected elements with an element that is not selected inside the group common bounding box when element that is not selected is clicked should switch selection to not selected element on pointer up > [end of test] number of renders 1`] = `17`; exports[`regression tests > given a selected element A and a not selected element B with higher z-index than A and given B partially overlaps A when there's a shift-click on the overlapped section B is added to the selection > [end of test] appState 1`] = ` { @@ -9504,7 +9504,7 @@ exports[`regression tests > given a selected element A and a not selected elemen exports[`regression tests > given a selected element A and a not selected element B with higher z-index than A and given B partially overlaps A when there's a shift-click on the overlapped section B is added to the selection > [end of test] number of elements 1`] = `0`; -exports[`regression tests > given a selected element A and a not selected element B with higher z-index than A and given B partially overlaps A when there's a shift-click on the overlapped section B is added to the selection > [end of test] number of renders 1`] = `15`; +exports[`regression tests > given a selected element A and a not selected element B with higher z-index than A and given B partially overlaps A when there's a shift-click on the overlapped section B is added to the selection > [end of test] number of renders 1`] = `14`; exports[`regression tests > given selected element A with lower z-index than unselected element B and given B is partially over A when clicking intersection between A and B B should be selected on pointer up > [end of test] appState 1`] = ` { @@ -12842,7 +12842,7 @@ exports[`regression tests > make a group and duplicate it > [end of test] histor exports[`regression tests > make a group and duplicate it > [end of test] number of elements 1`] = `0`; -exports[`regression tests > make a group and duplicate it > [end of test] number of renders 1`] = `21`; +exports[`regression tests > make a group and duplicate it > [end of test] number of renders 1`] = `20`; exports[`regression tests > noop interaction after undo shouldn't create history entry > [end of test] appState 1`] = ` { @@ -13084,7 +13084,7 @@ exports[`regression tests > noop interaction after undo shouldn't create history exports[`regression tests > noop interaction after undo shouldn't create history entry > [end of test] number of elements 1`] = `0`; -exports[`regression tests > noop interaction after undo shouldn't create history entry > [end of test] number of renders 1`] = `17`; +exports[`regression tests > noop interaction after undo shouldn't create history entry > [end of test] number of renders 1`] = `16`; exports[`regression tests > pinch-to-zoom works > [end of test] appState 1`] = ` { @@ -13375,7 +13375,7 @@ exports[`regression tests > shift click on selected element should deselect it o exports[`regression tests > shift click on selected element should deselect it on pointer up > [end of test] number of elements 1`] = `0`; -exports[`regression tests > shift click on selected element should deselect it on pointer up > [end of test] number of renders 1`] = `9`; +exports[`regression tests > shift click on selected element should deselect it on pointer up > [end of test] number of renders 1`] = `8`; exports[`regression tests > shift-click to multiselect, then drag > [end of test] appState 1`] = ` { @@ -13692,7 +13692,7 @@ exports[`regression tests > shift-click to multiselect, then drag > [end of test exports[`regression tests > shift-click to multiselect, then drag > [end of test] number of elements 1`] = `0`; -exports[`regression tests > shift-click to multiselect, then drag > [end of test] number of renders 1`] = `16`; +exports[`regression tests > shift-click to multiselect, then drag > [end of test] number of renders 1`] = `15`; exports[`regression tests > should group elements and ungroup them > [end of test] appState 1`] = ` { @@ -14253,7 +14253,7 @@ exports[`regression tests > should group elements and ungroup them > [end of tes exports[`regression tests > should group elements and ungroup them > [end of test] number of elements 1`] = `0`; -exports[`regression tests > should group elements and ungroup them > [end of test] number of renders 1`] = `22`; +exports[`regression tests > should group elements and ungroup them > [end of test] number of renders 1`] = `21`; exports[`regression tests > should show fill icons when element has non transparent background > [end of test] appState 1`] = ` { @@ -14464,7 +14464,7 @@ exports[`regression tests > should show fill icons when element has non transpar exports[`regression tests > should show fill icons when element has non transparent background > [end of test] number of elements 1`] = `0`; -exports[`regression tests > should show fill icons when element has non transparent background > [end of test] number of renders 1`] = `10`; +exports[`regression tests > should show fill icons when element has non transparent background > [end of test] number of renders 1`] = `9`; exports[`regression tests > single-clicking on a subgroup of a selected group should not alter selection > [end of test] appState 1`] = ` { @@ -15320,7 +15320,7 @@ exports[`regression tests > single-clicking on a subgroup of a selected group sh exports[`regression tests > single-clicking on a subgroup of a selected group should not alter selection > [end of test] number of elements 1`] = `0`; -exports[`regression tests > single-clicking on a subgroup of a selected group should not alter selection > [end of test] number of renders 1`] = `30`; +exports[`regression tests > single-clicking on a subgroup of a selected group should not alter selection > [end of test] number of renders 1`] = `29`; exports[`regression tests > spacebar + drag scrolls the canvas > [end of test] appState 1`] = ` { @@ -16233,7 +16233,7 @@ exports[`regression tests > supports nested groups > [end of test] history 1`] = exports[`regression tests > supports nested groups > [end of test] number of elements 1`] = `0`; -exports[`regression tests > supports nested groups > [end of test] number of renders 1`] = `27`; +exports[`regression tests > supports nested groups > [end of test] number of renders 1`] = `26`; exports[`regression tests > switches from group of selected elements to another element on pointer down > [end of test] appState 1`] = ` { @@ -16633,7 +16633,7 @@ exports[`regression tests > switches from group of selected elements to another exports[`regression tests > switches from group of selected elements to another element on pointer down > [end of test] number of elements 1`] = `0`; -exports[`regression tests > switches from group of selected elements to another element on pointer down > [end of test] number of renders 1`] = `18`; +exports[`regression tests > switches from group of selected elements to another element on pointer down > [end of test] number of renders 1`] = `17`; exports[`regression tests > switches selected element on pointer down > [end of test] appState 1`] = ` { @@ -16931,7 +16931,7 @@ exports[`regression tests > switches selected element on pointer down > [end of exports[`regression tests > switches selected element on pointer down > [end of test] number of elements 1`] = `0`; -exports[`regression tests > switches selected element on pointer down > [end of test] number of renders 1`] = `13`; +exports[`regression tests > switches selected element on pointer down > [end of test] number of renders 1`] = `12`; exports[`regression tests > two-finger scroll works > [end of test] appState 1`] = ` { @@ -17537,7 +17537,7 @@ exports[`regression tests > undo/redo drawing an element > [end of test] history exports[`regression tests > undo/redo drawing an element > [end of test] number of elements 1`] = `0`; -exports[`regression tests > undo/redo drawing an element > [end of test] number of renders 1`] = `24`; +exports[`regression tests > undo/redo drawing an element > [end of test] number of renders 1`] = `23`; exports[`regression tests > updates fontSize & fontFamily appState > [end of test] appState 1`] = ` { @@ -17601,10 +17601,7 @@ exports[`regression tests > updates fontSize & fontFamily appState > [end of tes "openMenu": null, "openPopup": null, "openSidebar": null, - "originSnapOffset": { - "x": 0, - "y": 0, - }, + "originSnapOffset": null, "pasteDialog": { "data": null, "shown": false, @@ -17663,7 +17660,7 @@ exports[`regression tests > updates fontSize & fontFamily appState > [end of tes exports[`regression tests > updates fontSize & fontFamily appState > [end of test] number of elements 1`] = `0`; -exports[`regression tests > updates fontSize & fontFamily appState > [end of test] number of renders 1`] = `6`; +exports[`regression tests > updates fontSize & fontFamily appState > [end of test] number of renders 1`] = `5`; exports[`regression tests > zoom hotkeys > [end of test] appState 1`] = ` { diff --git a/src/tests/dragCreate.test.tsx b/src/tests/dragCreate.test.tsx index 6e6c1dbe..f1808f87 100644 --- a/src/tests/dragCreate.test.tsx +++ b/src/tests/dragCreate.test.tsx @@ -47,7 +47,7 @@ describe("Test dragCreate", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(6); - expect(renderStaticScene).toHaveBeenCalledTimes(7); + expect(renderStaticScene).toHaveBeenCalledTimes(6); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); @@ -79,7 +79,7 @@ describe("Test dragCreate", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(6); - expect(renderStaticScene).toHaveBeenCalledTimes(7); + expect(renderStaticScene).toHaveBeenCalledTimes(6); expect(h.state.selectionElement).toBeNull(); @@ -112,7 +112,7 @@ describe("Test dragCreate", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(6); - expect(renderStaticScene).toHaveBeenCalledTimes(7); + expect(renderStaticScene).toHaveBeenCalledTimes(6); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); @@ -144,7 +144,7 @@ describe("Test dragCreate", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(6); - expect(renderStaticScene).toHaveBeenCalledTimes(7); + expect(renderStaticScene).toHaveBeenCalledTimes(6); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); @@ -180,7 +180,7 @@ describe("Test dragCreate", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(6); - expect(renderStaticScene).toHaveBeenCalledTimes(7); + expect(renderStaticScene).toHaveBeenCalledTimes(6); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); @@ -221,7 +221,7 @@ describe("Test dragCreate", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(5); - expect(renderStaticScene).toHaveBeenCalledTimes(6); + expect(renderStaticScene).toHaveBeenCalledTimes(5); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(0); }); @@ -241,7 +241,7 @@ describe("Test dragCreate", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(5); - expect(renderStaticScene).toHaveBeenCalledTimes(6); + expect(renderStaticScene).toHaveBeenCalledTimes(5); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(0); }); @@ -261,7 +261,7 @@ describe("Test dragCreate", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(5); - expect(renderStaticScene).toHaveBeenCalledTimes(6); + expect(renderStaticScene).toHaveBeenCalledTimes(5); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(0); }); @@ -288,7 +288,7 @@ describe("Test dragCreate", () => { }); expect(renderInteractiveScene).toHaveBeenCalledTimes(6); - expect(renderStaticScene).toHaveBeenCalledTimes(7); + expect(renderStaticScene).toHaveBeenCalledTimes(6); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(0); }); @@ -315,7 +315,7 @@ describe("Test dragCreate", () => { }); expect(renderInteractiveScene).toHaveBeenCalledTimes(6); - expect(renderStaticScene).toHaveBeenCalledTimes(7); + expect(renderStaticScene).toHaveBeenCalledTimes(6); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(0); }); diff --git a/src/tests/move.test.tsx b/src/tests/move.test.tsx index 6e2d6008..eab21bfb 100644 --- a/src/tests/move.test.tsx +++ b/src/tests/move.test.tsx @@ -43,7 +43,7 @@ describe("move element", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(6); - expect(renderStaticScene).toHaveBeenCalledTimes(7); + expect(renderStaticScene).toHaveBeenCalledTimes(6); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); expect(h.state.selectedElementIds[h.elements[0].id]).toBeTruthy(); @@ -85,7 +85,7 @@ describe("move element", () => { new Pointer("mouse").clickOn(rectB); expect(renderInteractiveScene).toHaveBeenCalledTimes(24); - expect(renderStaticScene).toHaveBeenCalledTimes(20); + expect(renderStaticScene).toHaveBeenCalledTimes(19); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(3); expect(h.state.selectedElementIds[rectB.id]).toBeTruthy(); @@ -131,7 +131,7 @@ describe("duplicate element on move when ALT is clicked", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(6); - expect(renderStaticScene).toHaveBeenCalledTimes(7); + expect(renderStaticScene).toHaveBeenCalledTimes(6); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); expect(h.state.selectedElementIds[h.elements[0].id]).toBeTruthy(); diff --git a/src/tests/multiPointCreate.test.tsx b/src/tests/multiPointCreate.test.tsx index bf23955c..53da14b1 100644 --- a/src/tests/multiPointCreate.test.tsx +++ b/src/tests/multiPointCreate.test.tsx @@ -48,7 +48,7 @@ describe("remove shape in non linear elements", () => { fireEvent.pointerUp(canvas, { clientX: 30, clientY: 30 }); expect(renderInteractiveScene).toHaveBeenCalledTimes(5); - expect(renderStaticScene).toHaveBeenCalledTimes(6); + expect(renderStaticScene).toHaveBeenCalledTimes(5); expect(h.elements.length).toEqual(0); }); @@ -63,7 +63,7 @@ describe("remove shape in non linear elements", () => { fireEvent.pointerUp(canvas, { clientX: 30, clientY: 30 }); expect(renderInteractiveScene).toHaveBeenCalledTimes(5); - expect(renderStaticScene).toHaveBeenCalledTimes(6); + expect(renderStaticScene).toHaveBeenCalledTimes(5); expect(h.elements.length).toEqual(0); }); @@ -78,7 +78,7 @@ describe("remove shape in non linear elements", () => { fireEvent.pointerUp(canvas, { clientX: 30, clientY: 30 }); expect(renderInteractiveScene).toHaveBeenCalledTimes(5); - expect(renderStaticScene).toHaveBeenCalledTimes(6); + expect(renderStaticScene).toHaveBeenCalledTimes(5); expect(h.elements.length).toEqual(0); }); }); @@ -111,7 +111,7 @@ describe("multi point mode in linear elements", () => { }); expect(renderInteractiveScene).toHaveBeenCalledTimes(11); - expect(renderStaticScene).toHaveBeenCalledTimes(10); + expect(renderStaticScene).toHaveBeenCalledTimes(9); expect(h.elements.length).toEqual(1); const element = h.elements[0] as ExcalidrawLinearElement; @@ -154,7 +154,7 @@ describe("multi point mode in linear elements", () => { key: KEYS.ENTER, }); expect(renderInteractiveScene).toHaveBeenCalledTimes(11); - expect(renderStaticScene).toHaveBeenCalledTimes(10); + expect(renderStaticScene).toHaveBeenCalledTimes(9); expect(h.elements.length).toEqual(1); const element = h.elements[0] as ExcalidrawLinearElement; diff --git a/src/tests/selection.test.tsx b/src/tests/selection.test.tsx index f6c83fc7..4f1c2701 100644 --- a/src/tests/selection.test.tsx +++ b/src/tests/selection.test.tsx @@ -310,7 +310,7 @@ describe("select single element on the scene", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(9); - expect(renderStaticScene).toHaveBeenCalledTimes(8); + expect(renderStaticScene).toHaveBeenCalledTimes(7); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); expect(h.state.selectedElementIds[h.elements[0].id]).toBeTruthy(); @@ -342,7 +342,7 @@ describe("select single element on the scene", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(9); - expect(renderStaticScene).toHaveBeenCalledTimes(8); + expect(renderStaticScene).toHaveBeenCalledTimes(7); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); expect(h.state.selectedElementIds[h.elements[0].id]).toBeTruthy(); @@ -374,7 +374,7 @@ describe("select single element on the scene", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(9); - expect(renderStaticScene).toHaveBeenCalledTimes(8); + expect(renderStaticScene).toHaveBeenCalledTimes(7); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); expect(h.state.selectedElementIds[h.elements[0].id]).toBeTruthy(); @@ -419,7 +419,7 @@ describe("select single element on the scene", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(9); - expect(renderStaticScene).toHaveBeenCalledTimes(8); + expect(renderStaticScene).toHaveBeenCalledTimes(7); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); expect(h.state.selectedElementIds[h.elements[0].id]).toBeTruthy(); @@ -463,7 +463,7 @@ describe("select single element on the scene", () => { fireEvent.pointerUp(canvas); expect(renderInteractiveScene).toHaveBeenCalledTimes(9); - expect(renderStaticScene).toHaveBeenCalledTimes(8); + expect(renderStaticScene).toHaveBeenCalledTimes(7); expect(h.state.selectionElement).toBeNull(); expect(h.elements.length).toEqual(1); expect(h.state.selectedElementIds[h.elements[0].id]).toBeTruthy(); diff --git a/src/types.ts b/src/types.ts index af821235..991de9f1 100644 --- a/src/types.ts +++ b/src/types.ts @@ -292,7 +292,7 @@ export type AppState = { showHyperlinkPopup: false | "info" | "editor"; selectedLinearElement: LinearElementEditor | null; - snapLines: SnapLine[]; + snapLines: readonly SnapLine[]; originSnapOffset: { x: number; y: number; @@ -539,6 +539,8 @@ export type AppClassProperties = { onInsertElements: App["onInsertElements"]; onExportImage: App["onExportImage"]; lastViewportPosition: App["lastViewportPosition"]; + togglePenMode: App["togglePenMode"]; + setActiveTool: App["setActiveTool"]; }; export type PointerDownState = Readonly<{