Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix(editor): Lost focus point on transition to inside-inside (#10964)

Márk Tolmácsa3b90897b5d7
diff --git a/packages/element/src/arrows/focus.ts b/packages/element/src/arrows/focus.ts
index 2f95e3c2e5..e3710ea885 100644
--- a/packages/element/src/arrows/focus.ts
+++ b/packages/element/src/arrows/focus.ts
@@ -28,6 +28,7 @@ import type {
ExcalidrawArrowElement,
ExcalidrawBindableElement,
NonDeleted,
+ FixedPointBinding,
NonDeletedSceneElementsMap,
PointsPositionUpdates,
} from "../types";
@@ -111,10 +112,17 @@ const focusPointUpdate = (
) => {
const pointUpdates = new Map();
+ const originalAdjacentBinding =
+ appState.selectedLinearElement?.initialState
+ .arrowOtherEndpointInitialBinding;
const bindingField = isStartBinding ? "startBinding" : "endBinding";
const adjacentBindingField = isStartBinding ? "endBinding" : "startBinding";
let currentBinding = arrow[bindingField];
- let adjacentBinding = arrow[adjacentBindingField];
+ let adjacentBinding =
+ originalAdjacentBinding?.mode === "orbit" &&
+ arrow[adjacentBindingField]?.mode === "inside"
+ ? originalAdjacentBinding
+ : arrow[adjacentBindingField];
// Update the dragged focus point related end
if (currentBinding && bindableElement) {
@@ -164,7 +172,7 @@ const focusPointUpdate = (
// Same shape bound on both ends
const boundToSameElementAfterUpdate =
bindableElement && adjacentBinding.elementId === bindableElement.id;
- if (switchToInsideBinding || boundToSameElementAfterUpdate) {
+ if (boundToSameElementAfterUpdate) {
adjacentBinding = {
...adjacentBinding,
mode: "inside",
@@ -340,6 +348,7 @@ export const handleFocusPointPointerDown = (
): {
hitFocusPoint: "start" | "end" | null;
pointerOffset: { x: number; y: number };
+ arrowOtherEndpointInitialBinding: FixedPointBinding | null;
} => {
const pointerPos = pointFrom(
pointerDownState.origin.x,
@@ -377,6 +386,7 @@ export const handleFocusPointPointerDown = (
x: pointerPos[0] - focusPoint[0],
y: pointerPos[1] - focusPoint[1],
},
+ arrowOtherEndpointInitialBinding: arrow.endBinding,
};
}
}
@@ -412,6 +422,7 @@ export const handleFocusPointPointerDown = (
x: pointerPos[0] - focusPoint[0],
y: pointerPos[1] - focusPoint[1],
},
+ arrowOtherEndpointInitialBinding: arrow.startBinding,
};
}
}
@@ -420,6 +431,7 @@ export const handleFocusPointPointerDown = (
return {
hitFocusPoint: null,
pointerOffset: { x: 0, y: 0 },
+ arrowOtherEndpointInitialBinding: null,
};
};
@@ -484,6 +496,10 @@ export const handleFocusPointPointerUp = (
],
});
}
+
+ return {
+ arrowOtherEndpointInitialBinding: null,
+ };
};
export const handleFocusPointHover = (
diff --git a/packages/element/src/binding.ts b/packages/element/src/binding.ts
index 74ca3cb98d..ede9079722 100644
--- a/packages/element/src/binding.ts
+++ b/packages/element/src/binding.ts
@@ -13,6 +13,7 @@ import {
pointDistance,
pointDistanceSq,
pointFrom,
+ pointFromArray,
pointFromVector,
pointRotateRads,
pointsEqual,
@@ -758,6 +759,9 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = (
threshold: 0,
overrideShouldTestInside: true,
});
+ const otherEndpointInitialBinding =
+ appState.selectedLinearElement?.initialState
+ .arrowOtherEndpointInitialBinding;
// Handle outside-outside binding to the same element
if (
@@ -773,10 +777,18 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = (
? globalPoint
: // NOTE: Can only affect the start point because new arrows always drag the end point
opts?.newArrow
- ? getGridPoint(
- appState.selectedLinearElement!.initialState.origin![0],
- appState.selectedLinearElement!.initialState.origin![1],
- opts.gridSize as NullableGridSize,
+ ? pointFromArray<GlobalPoint>(
+ getGridPoint(
+ appState.selectedLinearElement!.initialState.origin![0],
+ appState.selectedLinearElement!.initialState.origin![1],
+ opts.gridSize as NullableGridSize,
+ ),
+ )!
+ : otherBindableElement
+ ? getGlobalFixedPointForBindableElement(
+ otherBinding.fixedPoint,
+ otherBindableElement,
+ elementsMap,
)
: LinearElementEditor.getPointAtIndexGlobalCoordinates(
arrow,
@@ -789,6 +801,12 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = (
element: hit,
focusPoint: endDragged
? globalPoint
+ : otherBindableElement
+ ? getGlobalFixedPointForBindableElement(
+ otherBinding.fixedPoint,
+ otherBindableElement,
+ elementsMap,
+ )
: LinearElementEditor.getPointAtIndexGlobalCoordinates(
arrow,
-1,
@@ -878,36 +896,53 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = (
threshold: maxBindingDistance_simple(appState.zoom),
overrideShouldTestInside: true,
});
+ const otherPointWasInsideAtStart =
+ otherEndpointInitialBinding?.mode === "inside";
const otherNeverOverride = opts?.newArrow
? appState.selectedLinearElement?.initialState.arrowStartIsInside
- : otherBinding?.mode === "inside";
- const other: BindingStrategy = !otherNeverOverride
- ? otherBindableElement &&
+ : otherBinding?.mode === "inside" && otherPointWasInsideAtStart;
+
+ let other: BindingStrategy = { mode: undefined };
+ if (!otherNeverOverride) {
+ if (
+ otherBinding?.mode === "inside" &&
+ !otherPointWasInsideAtStart &&
+ otherBindableElement &&
+ !opts?.newArrow
+ ) {
+ other = {
+ mode: "orbit",
+ element: otherBindableElement,
+ focusPoint: otherFocusPoint || otherEndpoint,
+ };
+ } else if (
+ otherBindableElement &&
!otherFocusPointIsInElement &&
!pointIsCloseToOtherElement &&
appState.selectedLinearElement?.initialState.altFocusPoint
- ? {
- mode: "orbit",
- element: otherBindableElement,
- focusPoint: appState.selectedLinearElement.initialState.altFocusPoint,
- }
- : opts?.angleLocked && otherBindableElement
- ? {
- mode: "orbit",
- element: otherBindableElement,
- focusPoint:
- projectFixedPointOntoDiagonal(
- arrow,
- otherEndpoint,
- otherBindableElement,
- startDragged ? "end" : "start",
- elementsMap,
- appState.zoom,
- false,
- ) || otherEndpoint,
- }
- : { mode: undefined }
- : { mode: undefined };
+ ) {
+ other = {
+ mode: "orbit",
+ element: otherBindableElement,
+ focusPoint: appState.selectedLinearElement.initialState.altFocusPoint,
+ };
+ } else if (opts?.angleLocked && otherBindableElement) {
+ other = {
+ mode: "orbit",
+ element: otherBindableElement,
+ focusPoint:
+ projectFixedPointOntoDiagonal(
+ arrow,
+ otherEndpoint,
+ otherBindableElement,
+ startDragged ? "end" : "start",
+ elementsMap,
+ appState.zoom,
+ appState.isMidpointSnappingEnabled,
+ ) || otherEndpoint,
+ };
+ }
+ }
return {
start: startDragged ? current : other,
diff --git a/packages/element/src/linearElementEditor.ts b/packages/element/src/linearElementEditor.ts
index 044b4fef83..243f1f7b41 100644
--- a/packages/element/src/linearElementEditor.ts
+++ b/packages/element/src/linearElementEditor.ts
@@ -141,6 +141,7 @@ export class LinearElementEditor {
};
arrowStartIsInside: boolean;
altFocusPoint: Readonly<GlobalPoint> | null;
+ arrowOtherEndpointInitialBinding: FixedPointBinding | null;
}>;
/** whether you're dragging a point */
@@ -193,6 +194,7 @@ export class LinearElementEditor {
added: false,
},
arrowStartIsInside: false,
+ arrowOtherEndpointInitialBinding: null,
altFocusPoint: null,
};
this.hoverPointIndex = -1;
@@ -777,6 +779,7 @@ export class LinearElementEditor {
...editingLinearElement.initialState,
origin: null,
arrowStartIsInside: false,
+ arrowOtherEndpointInitialBinding: null,
},
};
}
@@ -1098,6 +1101,7 @@ export class LinearElementEditor {
!!app.state.newElement &&
(app.state.bindMode === "inside" || app.state.bindMode === "skip"),
altFocusPoint: null,
+ arrowOtherEndpointInitialBinding: element.startBinding,
},
selectedPointsIndices: [element.points.length - 1],
lastUncommittedPoint: null,
@@ -1160,6 +1164,9 @@ export class LinearElementEditor {
!!app.state.newElement &&
(app.state.bindMode === "inside" || app.state.bindMode === "skip"),
altFocusPoint: null,
+ arrowOtherEndpointInitialBinding: nextSelectedPointsIndices?.includes(0)
+ ? element.endBinding
+ : element.startBinding,
},
selectedPointsIndices: nextSelectedPointsIndices,
pointerOffset: targetPoint
@@ -2423,22 +2430,21 @@ const pointDraggingUpdates = (
)! as ExcalidrawBindableElement)
: null;
- const endLocalPoint = startIsDraggingOverEndElement
- ? nextArrow.points[nextArrow.points.length - 1]
- : endIsDraggingOverStartElement &&
- app.state.bindMode !== "inside" &&
- getFeatureFlag("COMPLEX_BINDINGS")
- ? nextArrow.points[0]
- : endBindable
- ? updateBoundPoint(
- nextArrow,
- "endBinding",
- nextArrow.endBinding,
- endBindable,
- elementsMap,
- endIsDragged,
- ) || nextArrow.points[nextArrow.points.length - 1]
- : nextArrow.points[nextArrow.points.length - 1];
+ const endLocalPoint =
+ endIsDraggingOverStartElement &&
+ app.state.bindMode !== "inside" &&
+ getFeatureFlag("COMPLEX_BINDINGS")
+ ? nextArrow.points[0]
+ : endBindable
+ ? updateBoundPoint(
+ nextArrow,
+ "endBinding",
+ nextArrow.endBinding,
+ endBindable,
+ elementsMap,
+ endIsDragged,
+ ) || nextArrow.points[nextArrow.points.length - 1]
+ : nextArrow.points[nextArrow.points.length - 1];
// We need to keep the simulated next arrow up-to-date, because
// updateBoundPoint looks at the opposite point
@@ -2472,13 +2478,11 @@ const pointDraggingUpdates = (
: nextArrow.points[0];
const endChanged =
- !startIsDraggingOverEndElement &&
!(
endIsDraggingOverStartElement &&
app.state.bindMode !== "inside" &&
getFeatureFlag("COMPLEX_BINDINGS")
- ) &&
- !!endBindable;
+ ) && !!endBindable;
const startChanged =
pointDistance(startLocalPoint, nextArrow.points[0]) !== 0;
diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx
index 2ee45cd829..5c4aa976f4 100644
--- a/packages/excalidraw/components/App.tsx
+++ b/packages/excalidraw/components/App.tsx
@@ -9239,13 +9239,16 @@ class App extends React.Component<AppProps, AppState> {
) as any;
if (arrow && isBindingElement(arrow)) {
- const { hitFocusPoint, pointerOffset } =
- handleFocusPointPointerDown(
- arrow,
- pointerDownState,
- elementsMap,
- this.state,
- );
+ const {
+ hitFocusPoint,
+ pointerOffset,
+ arrowOtherEndpointInitialBinding,
+ } = handleFocusPointPointerDown(
+ arrow,
+ pointerDownState,
+ elementsMap,
+ this.state,
+ );
// If focus point is hit, update state and prevent element selection
if (hitFocusPoint) {
@@ -9255,6 +9258,10 @@ class App extends React.Component<AppProps, AppState> {
hoveredFocusPointBinding: hitFocusPoint,
draggedFocusPointBinding: hitFocusPoint,
pointerOffset,
+ initialState: {
+ ...linearElementEditor.initialState,
+ arrowOtherEndpointInitialBinding,
+ },
},
});
return false;
@@ -11433,6 +11440,10 @@ class App extends React.Component<AppProps, AppState> {
selectedLinearElement: {
...this.state.selectedLinearElement,
draggedFocusPointBinding: null,
+ initialState: {
+ ...this.state.selectedLinearElement.initialState,
+ arrowOtherEndpointInitialBinding: null,
+ },
},
});
} else if (
diff --git a/packages/excalidraw/tests/__snapshots__/regressionTests.test.tsx.snap b/packages/excalidraw/tests/__snapshots__/regressionTests.test.tsx.snap
index fe1a01329d..9e55a13481 100644
--- a/packages/excalidraw/tests/__snapshots__/regressionTests.test.tsx.snap
+++ b/packages/excalidraw/tests/__snapshots__/regressionTests.test.tsx.snap
@@ -8726,6 +8726,7 @@ exports[`regression tests > key 5 selects arrow tool > [end of test] appState 1`
"hoveredFocusPointBinding": null,
"initialState": {
"altFocusPoint": null,
+ "arrowOtherEndpointInitialBinding": null,
"arrowStartIsInside": false,
"lastClickedPoint": -1,
"origin": null,
@@ -8960,6 +8961,7 @@ exports[`regression tests > key 6 selects line tool > [end of test] appState 1`]
"hoveredFocusPointBinding": null,
"initialState": {
"altFocusPoint": null,
+ "arrowOtherEndpointInitialBinding": null,
"arrowStartIsInside": false,
"lastClickedPoint": -1,
"origin": null,
@@ -9390,6 +9392,7 @@ exports[`regression tests > key a selects arrow tool > [end of test] appState 1`
"hoveredFocusPointBinding": null,
"initialState": {
"altFocusPoint": null,
+ "arrowOtherEndpointInitialBinding": null,
"arrowStartIsInside": false,
"lastClickedPoint": -1,
"origin": null,
@@ -9806,6 +9809,7 @@ exports[`regression tests > key l selects line tool > [end of test] appState 1`]
"hoveredFocusPointBinding": null,
"initialState": {
"altFocusPoint": null,
+ "arrowOtherEndpointInitialBinding": null,
"arrowStartIsInside": false,
"lastClickedPoint": -1,
"origin": null,