This commit is contained in:
Márk Tolmács 2025-05-02 17:52:14 +00:00 committed by GitHub
commit a244db6a59
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 70 additions and 132 deletions

View file

@ -27,7 +27,7 @@ import {
PRECISION, PRECISION,
} from "@excalidraw/math"; } from "@excalidraw/math";
import { isPointOnShape } from "@excalidraw/utils/collision"; import { isPointInShape, isPointOnShape } from "@excalidraw/utils/collision";
import type { LocalPoint, Radians } from "@excalidraw/math"; import type { LocalPoint, Radians } from "@excalidraw/math";
@ -63,7 +63,7 @@ import {
isTextElement, isTextElement,
} from "./typeChecks"; } from "./typeChecks";
import { aabbForElement, getElementShape, pointInsideBounds } from "./shapes"; import { aabbForElement, getElementShape } from "./shapes";
import { updateElbowArrowPoints } from "./elbowArrow"; import { updateElbowArrowPoints } from "./elbowArrow";
import type Scene from "./Scene"; import type Scene from "./Scene";
@ -107,8 +107,7 @@ export const isBindingEnabled = (appState: AppState): boolean => {
}; };
export const FIXED_BINDING_DISTANCE = 5; export const FIXED_BINDING_DISTANCE = 5;
export const BINDING_HIGHLIGHT_THICKNESS = 10; const BINDING_HIGHLIGHT_THICKNESS = 10;
export const BINDING_HIGHLIGHT_OFFSET = 4;
const getNonDeletedElements = ( const getNonDeletedElements = (
scene: Scene, scene: Scene,
@ -230,7 +229,13 @@ const getOriginalBindingIfStillCloseOfLinearElementEdge = (
const element = elementsMap.get(elementId); const element = elementsMap.get(elementId);
if ( if (
isBindableElement(element) && isBindableElement(element) &&
bindingBorderTest(element, coors, elementsMap, zoom) bindingBorderTest(
element,
coors,
elementsMap,
zoom,
isElbowArrow(element),
)
) { ) {
return element; return element;
} }
@ -440,22 +445,13 @@ export const maybeBindLinearElement = (
const normalizePointBinding = ( const normalizePointBinding = (
binding: { focus: number; gap: number }, binding: { focus: number; gap: number },
hoveredElement: ExcalidrawBindableElement, hoveredElement: ExcalidrawBindableElement,
) => { ) => ({
let gap = binding.gap;
const maxGap = maxBindingGap(
hoveredElement,
hoveredElement.width,
hoveredElement.height,
);
if (gap > maxGap) {
gap = BINDING_HIGHLIGHT_THICKNESS + BINDING_HIGHLIGHT_OFFSET;
}
return {
...binding, ...binding,
gap, gap: Math.min(
}; binding.gap,
}; maxBindingGap(hoveredElement, hoveredElement.width, hoveredElement.height),
),
});
export const bindLinearElement = ( export const bindLinearElement = (
linearElement: NonDeleted<ExcalidrawLinearElement>, linearElement: NonDeleted<ExcalidrawLinearElement>,
@ -567,19 +563,7 @@ export const getHoveredElementForBinding = (
elements, elements,
(element) => (element) =>
isBindableElement(element, false) && isBindableElement(element, false) &&
bindingBorderTest( bindingBorderTest(element, pointerCoords, elementsMap, zoom, fullShape),
element,
pointerCoords,
elementsMap,
zoom,
(fullShape ||
!isBindingFallthroughEnabled(
element as ExcalidrawBindableElement,
)) &&
// disable fullshape snapping for frame elements so we
// can bind to frame children
!isFrameLikeElement(element),
),
).filter((element) => { ).filter((element) => {
if (cullRest) { if (cullRest) {
return false; return false;
@ -621,16 +605,7 @@ export const getHoveredElementForBinding = (
elements, elements,
(element) => (element) =>
isBindableElement(element, false) && isBindableElement(element, false) &&
bindingBorderTest( bindingBorderTest(element, pointerCoords, elementsMap, zoom, fullShape),
element,
pointerCoords,
elementsMap,
zoom,
// disable fullshape snapping for frame elements so we
// can bind to frame children
(fullShape || !isBindingFallthroughEnabled(element)) &&
!isFrameLikeElement(element),
),
); );
return hoveredElement as NonDeleted<ExcalidrawBindableElement> | null; return hoveredElement as NonDeleted<ExcalidrawBindableElement> | null;
@ -1128,7 +1103,9 @@ export const snapToMid = (
const horizontalThrehsold = clamp(tolerance * width, 5, 80); const horizontalThrehsold = clamp(tolerance * width, 5, 80);
if ( if (
nonRotated[0] <= x + width / 2 && element.type === "diamond"
? nonRotated[0] <= x + width * (element.roundness ? 0.035 : 0)
: nonRotated[0] <= x + width / 2 &&
nonRotated[1] > center[1] - verticalThrehsold && nonRotated[1] > center[1] - verticalThrehsold &&
nonRotated[1] < center[1] + verticalThrehsold nonRotated[1] < center[1] + verticalThrehsold
) { ) {
@ -1139,7 +1116,9 @@ export const snapToMid = (
angle, angle,
); );
} else if ( } else if (
nonRotated[1] <= y + height / 2 && element.type === "diamond"
? nonRotated[1] <= y + height * (element.roundness ? 0.035 : 0)
: nonRotated[1] <= y + height / 2 &&
nonRotated[0] > center[0] - horizontalThrehsold && nonRotated[0] > center[0] - horizontalThrehsold &&
nonRotated[0] < center[0] + horizontalThrehsold nonRotated[0] < center[0] + horizontalThrehsold
) { ) {
@ -1150,7 +1129,9 @@ export const snapToMid = (
angle, angle,
); );
} else if ( } else if (
nonRotated[0] >= x + width / 2 && element.type === "diamond"
? nonRotated[0] >= x + width * (element.roundness ? 1 - 0.035 : 1)
: nonRotated[0] >= x + width / 2 &&
nonRotated[1] > center[1] - verticalThrehsold && nonRotated[1] > center[1] - verticalThrehsold &&
nonRotated[1] < center[1] + verticalThrehsold nonRotated[1] < center[1] + verticalThrehsold
) { ) {
@ -1161,7 +1142,9 @@ export const snapToMid = (
angle, angle,
); );
} else if ( } else if (
nonRotated[1] >= y + height / 2 && element.type === "diamond"
? nonRotated[1] >= y + height * (element.roundness ? 1 - 0.035 : 1)
: nonRotated[1] >= y + height / 2 &&
nonRotated[0] > center[0] - horizontalThrehsold && nonRotated[0] > center[0] - horizontalThrehsold &&
nonRotated[0] < center[0] + horizontalThrehsold nonRotated[0] < center[0] + horizontalThrehsold
) { ) {
@ -1512,13 +1495,19 @@ export const bindingBorderTest = (
fullShape?: boolean, fullShape?: boolean,
): boolean => { ): boolean => {
const threshold = maxBindingGap(element, element.width, element.height, zoom); const threshold = maxBindingGap(element, element.width, element.height, zoom);
const shape = getElementShape(element, elementsMap); const shape = getElementShape(element, elementsMap);
return ( const shouldTestInside =
isPointOnShape(pointFrom(x, y), shape, threshold) || // disable fullshape snapping for frame elements so we
(fullShape === true && // can bind to frame children
pointInsideBounds(pointFrom(x, y), aabbForElement(element))) (fullShape || !isBindingFallthroughEnabled(element)) &&
); !isFrameLikeElement(element);
return shouldTestInside
? // Since `inShape` tests STRICTLY againt the insides of a shape
// we would need `onShape` as well to include the "borders"
isPointInShape(pointFrom(x, y), shape) ||
isPointOnShape(pointFrom(x, y), shape, threshold)
: isPointOnShape(pointFrom(x, y), shape, threshold);
}; };
export const maxBindingGap = ( export const maxBindingGap = (
@ -1538,7 +1527,7 @@ export const maxBindingGap = (
// bigger bindable boundary for bigger elements // bigger bindable boundary for bigger elements
Math.min(0.25 * smallerDimension, 32), Math.min(0.25 * smallerDimension, 32),
// keep in sync with the zoomed highlight // keep in sync with the zoomed highlight
BINDING_HIGHLIGHT_THICKNESS / zoomValue + BINDING_HIGHLIGHT_OFFSET, BINDING_HIGHLIGHT_THICKNESS / zoomValue + FIXED_BINDING_DISTANCE,
); );
}; };

View file

@ -18,7 +18,6 @@ import {
arrayToMap, arrayToMap,
getFontFamilyString, getFontFamilyString,
getShortcutKey, getShortcutKey,
tupleToCoors,
getLineHeight, getLineHeight,
} from "@excalidraw/common"; } from "@excalidraw/common";
@ -26,9 +25,7 @@ import { getNonDeletedElements } from "@excalidraw/element";
import { import {
bindLinearElement, bindLinearElement,
bindPointToSnapToElementOutline,
calculateFixedPointForElbowArrowBinding, calculateFixedPointForElbowArrowBinding,
getHoveredElementForBinding,
updateBoundElements, updateBoundElements,
} from "@excalidraw/element/binding"; } from "@excalidraw/element/binding";
@ -1607,64 +1604,17 @@ export const actionChangeArrowType = register({
-1, -1,
elementsMap, elementsMap,
); );
const startHoveredElement = const startElement =
!newElement.startBinding && newElement.startBinding &&
getHoveredElementForBinding(
tupleToCoors(startGlobalPoint),
elements,
elementsMap,
appState.zoom,
false,
true,
);
const endHoveredElement =
!newElement.endBinding &&
getHoveredElementForBinding(
tupleToCoors(endGlobalPoint),
elements,
elementsMap,
appState.zoom,
false,
true,
);
const startElement = startHoveredElement
? startHoveredElement
: newElement.startBinding &&
(elementsMap.get( (elementsMap.get(
newElement.startBinding.elementId, newElement.startBinding.elementId,
) as ExcalidrawBindableElement); ) as ExcalidrawBindableElement);
const endElement = endHoveredElement const endElement =
? endHoveredElement newElement.endBinding &&
: newElement.endBinding &&
(elementsMap.get( (elementsMap.get(
newElement.endBinding.elementId, newElement.endBinding.elementId,
) as ExcalidrawBindableElement); ) as ExcalidrawBindableElement);
const finalStartPoint = startHoveredElement
? bindPointToSnapToElementOutline(
newElement,
startHoveredElement,
"start",
)
: startGlobalPoint;
const finalEndPoint = endHoveredElement
? bindPointToSnapToElementOutline(
newElement,
endHoveredElement,
"end",
)
: endGlobalPoint;
startHoveredElement &&
bindLinearElement(
newElement,
startHoveredElement,
"start",
app.scene,
);
endHoveredElement &&
bindLinearElement(newElement, endHoveredElement, "end", app.scene);
const startBinding = const startBinding =
startElement && newElement.startBinding startElement && newElement.startBinding
? { ? {
@ -1695,7 +1645,7 @@ export const actionChangeArrowType = register({
startBinding, startBinding,
endBinding, endBinding,
...updateElbowArrowPoints(newElement, elementsMap, { ...updateElbowArrowPoints(newElement, elementsMap, {
points: [finalStartPoint, finalEndPoint].map( points: [startGlobalPoint, endGlobalPoint].map(
(p): LocalPoint => (p): LocalPoint =>
pointFrom(p[0] - newElement.x, p[1] - newElement.y), pointFrom(p[0] - newElement.x, p[1] - newElement.y),
), ),

View file

@ -335,7 +335,7 @@ exports[`Test Transform > Test arrow bindings > should bind arrows to existing t
"endBinding": { "endBinding": {
"elementId": "text-2", "elementId": "text-2",
"focus": 0, "focus": 0,
"gap": 14, "gap": 16,
}, },
"fillStyle": "solid", "fillStyle": "solid",
"frameId": null, "frameId": null,
@ -1538,7 +1538,7 @@ exports[`Test Transform > should transform the elements correctly when linear el
"endBinding": { "endBinding": {
"elementId": "B", "elementId": "B",
"focus": 0, "focus": 0,
"gap": 14, "gap": 32,
}, },
"fillStyle": "solid", "fillStyle": "solid",
"frameId": null, "frameId": null,

View file

@ -781,7 +781,7 @@ describe("Test Transform", () => {
expect((arrow as ExcalidrawArrowElement).endBinding).toStrictEqual({ expect((arrow as ExcalidrawArrowElement).endBinding).toStrictEqual({
elementId: "rect-1", elementId: "rect-1",
focus: -0, focus: -0,
gap: 14, gap: 25,
}); });
expect(rect.boundElements).toStrictEqual([ expect(rect.boundElements).toStrictEqual([
{ {

View file

@ -15,11 +15,7 @@ import {
throttleRAF, throttleRAF,
} from "@excalidraw/common"; } from "@excalidraw/common";
import { import { maxBindingGap } from "@excalidraw/element/binding";
BINDING_HIGHLIGHT_OFFSET,
BINDING_HIGHLIGHT_THICKNESS,
maxBindingGap,
} from "@excalidraw/element/binding";
import { LinearElementEditor } from "@excalidraw/element/linearElementEditor"; import { LinearElementEditor } from "@excalidraw/element/linearElementEditor";
import { import {
getOmitSidesForDevice, getOmitSidesForDevice,
@ -96,6 +92,9 @@ import type {
RenderableElementsMap, RenderableElementsMap,
} from "../scene/types"; } from "../scene/types";
const BINDING_HIGHLIGHT_OFFSET = 4;
const BINDING_HIGHLIGHT_THICKNESS = 10;
const renderElbowArrowMidPointHighlight = ( const renderElbowArrowMidPointHighlight = (
context: CanvasRenderingContext2D, context: CanvasRenderingContext2D,
appState: InteractiveCanvasAppState, appState: InteractiveCanvasAppState,