reset selectionLinearElement on tool change

This commit is contained in:
dwelle
2025-11-13 13:02:30 +01:00
parent 933cd5607a
commit 7e38c3169c
3 changed files with 20 additions and 44 deletions

View File

@@ -378,6 +378,10 @@ export const removeSelection = () => {
export const distance = (x: number, y: number) => Math.abs(x - y); export const distance = (x: number, y: number) => Math.abs(x - y);
export const isSelectionLikeTool = (type: ToolType | "custom") => {
return type === "selection" || type === "lasso";
};
export const updateActiveTool = ( export const updateActiveTool = (
appState: Pick<AppState, "activeTool">, appState: Pick<AppState, "activeTool">,
data: (( data: ((

View File

@@ -108,6 +108,7 @@ import {
type StylesPanelMode, type StylesPanelMode,
loadDesktopUIModePreference, loadDesktopUIModePreference,
setDesktopUIMode, setDesktopUIMode,
isSelectionLikeTool,
} from "@excalidraw/common"; } from "@excalidraw/common";
import { import {
@@ -5001,10 +5002,7 @@ class App extends React.Component<AppProps, AppState> {
this.state.openDialog?.name === "elementLinkSelector" this.state.openDialog?.name === "elementLinkSelector"
) { ) {
setCursor(this.interactiveCanvas, CURSOR_TYPE.GRAB); setCursor(this.interactiveCanvas, CURSOR_TYPE.GRAB);
} else if ( } else if (isSelectionLikeTool(this.state.activeTool.type)) {
this.state.activeTool.type === "selection" ||
this.state.activeTool.type === "lasso"
) {
resetCursor(this.interactiveCanvas); resetCursor(this.interactiveCanvas);
} else { } else {
setCursorForShape(this.interactiveCanvas, this.state); setCursorForShape(this.interactiveCanvas, this.state);
@@ -5215,6 +5213,9 @@ class App extends React.Component<AppProps, AppState> {
snapLines: prevState.snapLines.length ? [] : prevState.snapLines, snapLines: prevState.snapLines.length ? [] : prevState.snapLines,
originSnapOffset: null, originSnapOffset: null,
activeEmbeddable: null, activeEmbeddable: null,
selectedLinearElement: isSelectionLikeTool(nextActiveTool.type)
? prevState.selectedLinearElement
: null,
} as const; } as const;
if (nextActiveTool.type === "freedraw") { if (nextActiveTool.type === "freedraw") {
@@ -5224,6 +5225,7 @@ class App extends React.Component<AppProps, AppState> {
if (nextActiveTool.type === "lasso") { if (nextActiveTool.type === "lasso") {
return { return {
...prevState, ...prevState,
...commonResets,
activeTool: nextActiveTool, activeTool: nextActiveTool,
...(keepSelection ...(keepSelection
? {} ? {}
@@ -5233,23 +5235,22 @@ class App extends React.Component<AppProps, AppState> {
editingGroupId: null, editingGroupId: null,
multiElement: null, multiElement: null,
}), }),
...commonResets,
}; };
} else if (nextActiveTool.type !== "selection") { } else if (nextActiveTool.type !== "selection") {
return { return {
...prevState, ...prevState,
...commonResets,
activeTool: nextActiveTool, activeTool: nextActiveTool,
selectedElementIds: makeNextSelectedElementIds({}, prevState), selectedElementIds: makeNextSelectedElementIds({}, prevState),
selectedGroupIds: makeNextSelectedElementIds({}, prevState), selectedGroupIds: makeNextSelectedElementIds({}, prevState),
editingGroupId: null, editingGroupId: null,
multiElement: null, multiElement: null,
...commonResets,
}; };
} }
return { return {
...prevState, ...prevState,
activeTool: nextActiveTool,
...commonResets, ...commonResets,
activeTool: nextActiveTool,
}; };
}); });
}; };
@@ -7713,10 +7714,7 @@ class App extends React.Component<AppProps, AppState> {
} }
private clearSelectionIfNotUsingSelection = (): void => { private clearSelectionIfNotUsingSelection = (): void => {
if ( if (!isSelectionLikeTool(this.state.activeTool.type)) {
this.state.activeTool.type !== "selection" &&
this.state.activeTool.type !== "lasso"
) {
this.setState({ this.setState({
selectedElementIds: makeNextSelectedElementIds({}, this.state), selectedElementIds: makeNextSelectedElementIds({}, this.state),
selectedGroupIds: {}, selectedGroupIds: {},
@@ -7733,10 +7731,7 @@ class App extends React.Component<AppProps, AppState> {
event: React.PointerEvent<HTMLElement>, event: React.PointerEvent<HTMLElement>,
pointerDownState: PointerDownState, pointerDownState: PointerDownState,
): boolean => { ): boolean => {
if ( if (isSelectionLikeTool(this.state.activeTool.type)) {
this.state.activeTool.type === "selection" ||
this.state.activeTool.type === "lasso"
) {
const elements = this.scene.getNonDeletedElements(); const elements = this.scene.getNonDeletedElements();
const elementsMap = this.scene.getNonDeletedElementsMap(); const elementsMap = this.scene.getNonDeletedElementsMap();
const selectedElements = this.scene.getSelectedElements(this.state); const selectedElements = this.scene.getSelectedElements(this.state);

View File

@@ -6299,35 +6299,7 @@ exports[`regression tests > draw every type of shape > [end of test] appState 1`
"selectedElementIds": {}, "selectedElementIds": {},
"selectedElementsAreBeingDragged": false, "selectedElementsAreBeingDragged": false,
"selectedGroupIds": {}, "selectedGroupIds": {},
"selectedLinearElement": { "selectedLinearElement": null,
"customLineAngle": null,
"elbowed": false,
"elementId": "id20",
"hoverPointIndex": -1,
"initialState": {
"altFocusPoint": null,
"arrowStartIsInside": false,
"lastClickedPoint": -1,
"origin": null,
"prevSelectedPointsIndices": null,
"segmentMidpoint": {
"added": false,
"index": null,
"value": null,
},
},
"isDragging": false,
"isEditing": false,
"lastCommittedPoint": null,
"lastUncommittedPoint": null,
"pointerDownState": undefined,
"pointerOffset": {
"x": 0,
"y": 0,
},
"segmentMidPointHoveredCoords": null,
"selectedPointsIndices": null,
},
"selectionElement": null, "selectionElement": null,
"shouldCacheIgnoreZoom": false, "shouldCacheIgnoreZoom": false,
"showHyperlinkPopup": false, "showHyperlinkPopup": false,
@@ -6936,11 +6908,16 @@ exports[`regression tests > draw every type of shape > [end of test] undo stack
"delta": Delta { "delta": Delta {
"deleted": { "deleted": {
"selectedElementIds": {}, "selectedElementIds": {},
"selectedLinearElement": null,
}, },
"inserted": { "inserted": {
"selectedElementIds": { "selectedElementIds": {
"id20": true, "id20": true,
}, },
"selectedLinearElement": {
"elementId": "id20",
"isEditing": false,
},
}, },
}, },
}, },