From 0a72ebb65ee9326214d22acb5cc79f79814ea307 Mon Sep 17 00:00:00 2001 From: Andras Viczian Date: Wed, 12 Mar 2025 15:26:34 +0100 Subject: [PATCH 1/2] fixed behaviour for top menu --- packages/excalidraw/actions/actionCanvas.tsx | 40 +++++++++++++++++++- packages/excalidraw/components/App.tsx | 7 +++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/excalidraw/actions/actionCanvas.tsx b/packages/excalidraw/actions/actionCanvas.tsx index 903a6d84a..29499a276 100644 --- a/packages/excalidraw/actions/actionCanvas.tsx +++ b/packages/excalidraw/actions/actionCanvas.tsx @@ -17,7 +17,11 @@ import { THEME, ZOOM_STEP, } from "../constants"; -import { getCommonBounds, getNonDeletedElements } from "../element"; +import { + getCommonBounds, + getNonDeletedElements, + isInvisiblySmallElement, +} from "../element"; import type { ExcalidrawElement } from "../element/types"; import { t } from "../i18n"; import { CODES, KEYS } from "../keys"; @@ -28,7 +32,7 @@ import type { AppState, Offsets } from "../types"; import { getShortcutKey, updateActiveTool } from "../utils"; import { register } from "./register"; import { Tooltip } from "../components/Tooltip"; -import { newElementWith } from "../element/mutateElement"; +import { mutateElement, newElementWith } from "../element/mutateElement"; import { getDefaultAppState, isEraserActive, @@ -39,6 +43,7 @@ import type { SceneBounds } from "../element/bounds"; import { setCursor } from "../cursor"; import { CaptureUpdateAction } from "../store"; import { clamp, roundToStep } from "@excalidraw/math"; +import { isLinearElement } from "../element/typeChecks"; export const actionChangeViewBackgroundColor = register({ name: "changeViewBackgroundColor", @@ -541,13 +546,44 @@ export const actionToggleHandTool = register({ setCursor(app.interactiveCanvas, CURSOR_TYPE.GRAB); } + let newElements = elements; + + const multiPointElement = + appState.multiElement && isLinearElement(appState.multiElement) + ? appState.multiElement + : null; + + if (multiPointElement) { + // pen and mouse have hover + if (appState.lastPointerDownWith !== "touch") { + const { points, lastCommittedPoint } = multiPointElement; + if ( + !lastCommittedPoint || + points[points.length - 1] !== lastCommittedPoint + ) { + mutateElement(multiPointElement, { + points: multiPointElement.points.slice(0, -1), + }); + } + } + if (isInvisiblySmallElement(multiPointElement)) { + // TODO: #7348 in theory this gets recorded by the store, so the invisible elements could be restored by the undo/redo, which might be not what we would want + newElements = newElements.filter( + (el) => el.id !== multiPointElement.id, + ); + } + } + return { + elements: newElements, appState: { ...appState, selectedElementIds: {}, selectedGroupIds: {}, activeEmbeddable: null, activeTool, + newElement: null, + multiElement: null, }, captureUpdate: CaptureUpdateAction.IMMEDIATELY, }; diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index 8eb2a9e2c..d3c27740d 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -3576,7 +3576,7 @@ class App extends React.Component { ...prevState.activeTool, ...updateActiveTool( this.state, - prevState.activeTool.locked + prevState.activeTool.locked && !prevState.newElement ? { type: "selection" } : prevState.activeTool, ), @@ -4692,6 +4692,11 @@ class App extends React.Component { return; } + //cancel adding linearElement if tool is switched + if (this.state.newElement && isLinearElement(this.state.newElement)) { + this.actionManager.executeAction(actionFinalize); + } + const nextActiveTool = updateActiveTool(this.state, tool); if (nextActiveTool.type === "hand") { setCursor(this.interactiveCanvas, CURSOR_TYPE.GRAB); From 8cfb1f166ed10d2f8800d4b6689c52e4bb38db14 Mon Sep 17 00:00:00 2001 From: Andras Viczian Date: Wed, 12 Mar 2025 16:01:54 +0100 Subject: [PATCH 2/2] eslint fixes --- packages/excalidraw/actions/actionCanvas.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/excalidraw/actions/actionCanvas.tsx b/packages/excalidraw/actions/actionCanvas.tsx index ce1a414fd..7bd80ded3 100644 --- a/packages/excalidraw/actions/actionCanvas.tsx +++ b/packages/excalidraw/actions/actionCanvas.tsx @@ -40,13 +40,13 @@ import { centerScrollOn } from "../scene/scroll"; import { getStateForZoom } from "../scene/zoom"; import { CaptureUpdateAction } from "../store"; import { getShortcutKey, updateActiveTool } from "../utils"; +import { isLinearElement } from "../element/typeChecks"; import { register } from "./register"; import type { SceneBounds } from "../element/bounds"; import type { ExcalidrawElement } from "../element/types"; import type { AppState, Offsets } from "../types"; -import { isLinearElement } from "../element/typeChecks"; export const actionChangeViewBackgroundColor = register({ name: "changeViewBackgroundColor",