Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix(editor): route elbow arrows with a single corner where one fits (#12458)

David Luzarc711e80ec55b
diff --git a/packages/element/src/elbowArrow.ts b/packages/element/src/elbowArrow.ts
index 239d8ffdea..0831af5eab 100644
--- a/packages/element/src/elbowArrow.ts
+++ b/packages/element/src/elbowArrow.ts
@@ -52,8 +52,14 @@ import {
type ExcalidrawElbowArrowElement,
type NonDeletedSceneElementsMap,
} from "./types";
-import { aabbForElement, pointInsideBounds } from "./bounds";
+import {
+ aabbForElement,
+ doBoundsIntersect,
+ getArrowheadSize,
+ pointInsideBounds,
+} from "./bounds";
import { getHoveredElementForBinding } from "./collision";
+import { ELBOW_ARROW_CORNER_RADIUS } from "./shape";
import type { Heading } from "./heading";
import type {
@@ -235,19 +241,16 @@ const handleSegmentRenormalization = (
);
if (filteredNextFixedSegments.length === 0) {
return normalizeArrowElementUpdate(
- getElbowArrowCornerPoints(
- removeElbowArrowShortSegments(
- routeElbowArrow(
- arrow,
- getElbowArrowData(
- arrow,
- elementsMap,
- nextPoints.map((p) =>
- pointFrom<LocalPoint>(p[0] - arrow.x, p[1] - arrow.y),
- ),
- ),
- ) ?? [],
+ getElbowArrowRoute(
+ arrow,
+ getElbowArrowData(
+ arrow,
+ elementsMap,
+ nextPoints.map((p) =>
+ pointFrom<LocalPoint>(p[0] - arrow.x, p[1] - arrow.y),
+ ),
),
+ elementsMap,
),
filteredNextFixedSegments,
null,
@@ -308,15 +311,7 @@ const handleSegmentRelease = (
const y = arrow.y + (prevSegment ? prevSegment.end[1] : 0);
const startBinding = prevSegment ? null : arrow.startBinding;
const endBinding = nextSegment ? null : arrow.endBinding;
- const {
- startHeading,
- endHeading,
- startGlobalPoint,
- endGlobalPoint,
- hoveredStartElement,
- hoveredEndElement,
- ...rest
- } = getElbowArrowData(
+ const elbowArrowData = getElbowArrowData(
{
x,
y,
@@ -342,19 +337,7 @@ const handleSegmentRelease = (
);
const { points: restoredPoints } = normalizeArrowElementUpdate(
- getElbowArrowCornerPoints(
- removeElbowArrowShortSegments(
- routeElbowArrow(arrow, {
- startHeading,
- endHeading,
- startGlobalPoint,
- endGlobalPoint,
- hoveredStartElement,
- hoveredEndElement,
- ...rest,
- }) ?? [],
- ),
- ),
+ getElbowArrowRoute(arrow, elbowArrowData, elementsMap),
fixedSegments,
null,
null,
@@ -1031,15 +1014,7 @@ export const updateElbowArrowPoints = (
);
}
- const {
- startHeading,
- endHeading,
- startGlobalPoint,
- endGlobalPoint,
- hoveredStartElement,
- hoveredEndElement,
- ...rest
- } = getElbowArrowData(
+ const elbowArrowData = getElbowArrowData(
{
x: arrow.x,
y: arrow.y,
@@ -1053,6 +1028,14 @@ export const updateElbowArrowPoints = (
updatedPoints,
options,
);
+ const {
+ startHeading,
+ endHeading,
+ startGlobalPoint,
+ endGlobalPoint,
+ hoveredStartElement,
+ hoveredEndElement,
+ } = elbowArrowData;
// 0. During all element replacement in the scene, we just need to renormalize
// the arrow
@@ -1100,19 +1083,7 @@ export const updateElbowArrowPoints = (
////
if (fixedSegments.length === 0) {
return normalizeArrowElementUpdate(
- getElbowArrowCornerPoints(
- removeElbowArrowShortSegments(
- routeElbowArrow(arrow, {
- startHeading,
- endHeading,
- startGlobalPoint,
- endGlobalPoint,
- hoveredStartElement,
- hoveredEndElement,
- ...rest,
- }) ?? [],
- ),
- ),
+ getElbowArrowRoute(arrow, elbowArrowData, elementsMap),
fixedSegments,
null,
null,
@@ -1166,6 +1137,18 @@ export const updateElbowArrowPoints = (
);
};
+/**
+ * The update routing an elbow arrow again where it is, e.g. once its
+ * arrowheads change, as its route makes room for them
+ */
+export const updateElbowArrowRoute = <T extends ExcalidrawElbowArrowElement>(
+ arrow: Readonly<T>,
+ elementsMap: NonDeletedSceneElementsMap,
+) =>
+ updateElbowArrowPoints(arrow, elementsMap, {
+ points: [arrow.points[0], arrow.points[arrow.points.length - 1]],
+ }) as ElementUpdate<T>;
+
/**
* Retrieves data necessary for calculating the elbow arrow path.
*
@@ -1425,16 +1408,151 @@ const getElbowArrowData = (
};
};
+/** Whether an axis-aligned segment passes through `bounds` */
+const segmentCrossesBounds = (a: GlobalPoint, b: GlobalPoint, bounds: Bounds) =>
+ doBoundsIntersect(
+ [
+ Math.min(a[0], b[0]),
+ Math.min(a[1], b[1]),
+ Math.max(a[0], b[0]),
+ Math.max(a[1], b[1]),
+ ],
+ bounds,
+ );
+
/**
- * Generate the elbow arrow segments
- *
- * @param arrow
- * @param elementsMap
- * @param nextPoints
- * @param options
- * @returns
+ * The route between two bound elements with one corner (when the arrow
+ * leaves one and enters the other at a right angle) or two (when it leaves
+ * and enters them heading the same way), if it fits: leaving the start
+ * element and entering the end one the way their headings tell, clear of
+ * both. Routed on the grid, the arrow keeps a fixed padding to the elements
+ * instead, and steps aside to reach it when they're closer than that (e.g.
+ * when the end element is just below the start's side, or just below the
+ * start).
+ */
+const getSimpleRoute = (
+ arrow: ElbowArrowState,
+ {
+ startHeading,
+ endHeading,
+ startGlobalPoint,
+ endGlobalPoint,
+ hoveredStartElement,
+ hoveredEndElement,
+ }: ElbowArrowData,
+ elementsMap: ElementsMap,
+): GlobalPoint[] | null => {
+ const oneCorner =
+ headingIsHorizontal(startHeading) !== headingIsHorizontal(endHeading);
+ if (
+ !hoveredStartElement ||
+ !hoveredEndElement ||
+ hoveredStartElement.id === hoveredEndElement.id ||
+ (!oneCorner && !compareHeading(startHeading, flipHeading(endHeading)))
+ ) {
+ return null;
+ }
+
+ // with one corner, the legs are as long as the end is ahead of the start
+ // along the start's heading, and the start ahead of the end along the
+ // end's (headings point away from their element), and with two, they
+ // share the former
+ const ahead =
+ (endGlobalPoint[0] - startGlobalPoint[0]) * startHeading[0] +
+ (endGlobalPoint[1] - startGlobalPoint[1]) * startHeading[1];
+ let firstLeg: number;
+ let lastLeg: number;
+ if (oneCorner) {
+ firstLeg = ahead;
+ lastLeg =
+ (startGlobalPoint[0] - endGlobalPoint[0]) * endHeading[0] +
+ (startGlobalPoint[1] - endGlobalPoint[1]) * endHeading[1];
+ } else {
+ // halfway, as the grid routing turns when there's room, unless that
+ // leaves an arrowhead a shorter leg than the padding, and the other end,
+ // without one, can spare some of its own (keeping enough to round its
+ // corner)
+ firstLeg = ahead / 2;
+ if (!arrow.startArrowhead !== !arrow.endArrowhead) {
+ const arrowheadLeg = Math.max(
+ ahead / 2,
+ Math.min(BASE_PADDING, ahead - ELBOW_ARROW_CORNER_RADIUS * 2),
+ );
+ firstLeg = arrow.startArrowhead ? arrowheadLeg : ahead - arrowheadLeg;
+ }
+ lastLeg = ahead - firstLeg;
+ }
+
+ // a leg is to keep its corner rounded (at least a quarter of the radius,
+ // rather than a stub hugging the element), and one with an arrowhead to
+ // fit it too (not to have it reach around the corner)
+ if (
+ firstLeg <=
+ Math.max(
+ ELBOW_ARROW_CORNER_RADIUS / 2,
+ arrow.startArrowhead ? getArrowheadSize(arrow.startArrowhead) : 0,
+ ) ||
+ lastLeg <=
+ Math.max(
+ ELBOW_ARROW_CORNER_RADIUS / 2,
+ arrow.endArrowhead ? getArrowheadSize(arrow.endArrowhead) : 0,
+ )
+ ) {
+ return null;
+ }
+
+ const firstTurn = pointFrom<GlobalPoint>(
+ startGlobalPoint[0] + startHeading[0] * firstLeg,
+ startGlobalPoint[1] + startHeading[1] * firstLeg,
+ );
+ const lastTurn = pointFrom<GlobalPoint>(
+ endGlobalPoint[0] + endHeading[0] * lastLeg,
+ endGlobalPoint[1] + endHeading[1] * lastLeg,
+ );
+
+ const [startBounds, endBounds] = [hoveredStartElement, hoveredEndElement].map(
+ (element) => {
+ const gap = getBindingGap(element);
+ return aabbForElement(element, elementsMap, [gap, gap, gap, gap]);
+ },
+ );
+ // (the first leg leaves the start, and the last enters the end)
+ if (
+ segmentCrossesBounds(startGlobalPoint, firstTurn, endBounds) ||
+ segmentCrossesBounds(firstTurn, lastTurn, startBounds) ||
+ segmentCrossesBounds(firstTurn, lastTurn, endBounds) ||
+ segmentCrossesBounds(lastTurn, endGlobalPoint, startBounds)
+ ) {
+ return null;
+ }
+
+ // (with one corner, both turns are at it, and merged later)
+ return [startGlobalPoint, firstTurn, lastTurn, endGlobalPoint];
+};
+
+/**
+ * The elbow arrow's route: with one or two corners if that fits, or on the
+ * grid otherwise, without short segments and the points between collinear
+ * ones
+ */
+const getElbowArrowRoute = (
+ arrow: ElbowArrowState,
+ elbowArrowData: ElbowArrowData,
+ elementsMap: ElementsMap,
+): GlobalPoint[] =>
+ getElbowArrowCornerPoints(
+ removeElbowArrowShortSegments(
+ getSimpleRoute(arrow, elbowArrowData, elementsMap) ??
+ getElbowArrowGridRoute(arrow, elbowArrowData) ??
+ [],
+ ),
+ );
+
+/**
+ * The elbow arrow's route on the grid, keeping a padding around the elements
+ * it connects, or null if there's none
*/
-const routeElbowArrow = (
+const getElbowArrowGridRoute = (
arrow: ElbowArrowState,
elbowArrowData: ElbowArrowData,
): GlobalPoint[] | null => {
diff --git a/packages/element/src/shape.ts b/packages/element/src/shape.ts
index e69715f954..b85a63d710 100644
--- a/packages/element/src/shape.ts
+++ b/packages/element/src/shape.ts
@@ -1076,10 +1076,28 @@ const _generateElementShape = (
export const ELBOW_ARROW_CORNER_RADIUS = 16;
-const generateElbowArrowShape = (
- points: readonly LocalPoint[],
+export const generateElbowArrowShape = (
+ routePoints: readonly LocalPoint[],
radius: number,
) => {
+ // a jog too short to see (e.g. a fixed segment lined up with the ones
+ // around it, while dragging an element the arrow is bound to) is drawn
+ // straight, as rough.js draws its two corners as a knot: a segment shorter
+ // than 1px between two longer ones heading the same way (not a U-turn, nor
+ // a corner of short segments)
+ const isJog = (i: number) => {
+ if (i < 2 || i > routePoints.length - 2) {
+ return false;
+ }
+ const [a, b, c, d] = routePoints.slice(i - 2, i + 2);
+ return (
+ pointDistance(b, c) < 1 &&
+ pointDistance(a, b) >= 1 &&
+ pointDistance(c, d) >= 1 &&
+ (b[0] - a[0]) * (d[0] - c[0]) + (b[1] - a[1]) * (d[1] - c[1]) > 0
+ );
+ };
+ const points = routePoints.filter((_, i) => !isJog(i) && !isJog(i + 1));
const subpoints = [] as [number, number][];
for (let i = 1; i < points.length - 1; i += 1) {
const prev = points[i - 1];
diff --git a/packages/element/tests/elbowArrow.test.tsx b/packages/element/tests/elbowArrow.test.tsx
index ec0603dea3..dd7255b1d2 100644
--- a/packages/element/tests/elbowArrow.test.tsx
+++ b/packages/element/tests/elbowArrow.test.tsx
@@ -2,6 +2,7 @@ import { ARROW_TYPE, DEFAULT_ZOOM } from "@excalidraw/common";
import { pointFrom } from "@excalidraw/math";
import { Excalidraw } from "@excalidraw/excalidraw";
import { actionSelectAll } from "@excalidraw/excalidraw/actions";
+import { actionChangeArrowhead } from "@excalidraw/excalidraw/actions/actionProperties";
import { actionDuplicateSelection } from "@excalidraw/excalidraw/actions/actionDuplicateSelection";
import { API } from "@excalidraw/excalidraw/tests/helpers/api";
import { Pointer, UI } from "@excalidraw/excalidraw/tests/helpers/ui";
@@ -13,7 +14,7 @@ import {
render,
} from "@excalidraw/excalidraw/tests/test-utils";
import "@excalidraw/utils/test-utils";
-import { bindBindingElement } from "@excalidraw/element";
+import { bindBindingElement, validateElbowPoints } from "@excalidraw/element";
import type { LocalPoint } from "@excalidraw/math";
@@ -310,10 +311,8 @@ describe("elbow arrow ui", () => {
expect(arrow.points.map((point) => point.map(Math.round))).toEqual([
[0, 0],
- [36, 0],
- [36, 90],
- [28, 90],
- [28, 164],
+ [51, 0],
+ [51, 164],
[101, 164],
]);
});
@@ -428,3 +427,252 @@ describe("elbow arrow ui", () => {
]);
});
});
+
+describe("elbow arrow routes with one or two corners", () => {
+ beforeEach(async () => {
+ localStorage.clear();
+ await render(<Excalidraw handleKeyboardGlobally={true} />);
+ });
+
+ const connect = (diamondY: number) => {
+ // the rectangle's right side's middle is at y = 127.5
+ UI.createElement("rectangle", { x: 0, y: 100, width: 120, height: 55 });
+ UI.createElement("diamond", {
+ x: 220,
+ y: diamondY,
+ width: 200,
+ height: 124,
+ });
+ UI.clickTool("arrow");
+ UI.clickOnTestId("elbow-arrow");
+ mouse.reset();
+ mouse.moveTo(120, 127.5);
+ mouse.click();
+ mouse.moveTo(320, diamondY);
+ mouse.click();
+
+ return h.elements.find(
+ (element) => element.type === "arrow",
+ ) as ExcalidrawElbowArrowElement;
+ };
+
+ it("connects elements closer than the padding with a single corner", () => {
+ // 40px from the rectangle's side to the diamond's top: no step to the
+ // padding around the diamond on the way
+ const arrow = connect(167.5);
+
+ expect(arrow.points).toCloselyEqualPoints([
+ [0, 0],
+ [195, 0],
+ [195, 34.15],
+ ]);
+ });
+
+ it("routes around the elements when a single corner doesn't fit", () => {
+ const arrow = connect(167.5);
+
+ // the rectangle below the diamond's top: the corner would be inside it
+ UI.clickTool("selection");
+ mouse.reset();
+ mouse.moveTo(60, 100);
+ mouse.down();
+ mouse.moveTo(60, 180);
+ mouse.up();
+
+ expect(arrow.points.length).toBeGreaterThan(3);
+ });
+
+ it("doesn't put the arrowhead on a leg too short for it", () => {
+ // 25px from the rectangle's side to the diamond's top
+ const arrow = connect(152.5);
+
+ expect(arrow.points.length).toBeGreaterThan(3);
+ });
+
+ it("routes again to fit an arrowhead added to a short leg", () => {
+ API.setAppState({ currentItemEndArrowhead: null });
+ // about 10px from the rectangle's side to the diamond's top
+ const arrow = connect(143.5);
+ expect(arrow.points).toCloselyEqualPoints([
+ [0, 0],
+ [195, 0],
+ [195, 10.15],
+ ]);
+
+ act(() => {
+ h.app.actionManager.executeAction(actionChangeArrowhead, "ui", {
+ position: "end",
+ type: "arrow",
+ });
+ });
+
+ const updated = h.elements.find(
+ (element) => element.id === arrow.id,
+ ) as ExcalidrawElbowArrowElement;
+ const [corner, end] = updated.points.slice(-2);
+ expect(updated.endArrowhead).toBe("arrow");
+ expect(end[1] - corner[1]).toBeGreaterThanOrEqual(25);
+ });
+
+ it("doesn't route a leg too short to keep its corner", () => {
+ // the arrow leaves the box above at x = 134, and enters the one below
+ // at x = 139, a leg too short to round its corner
+ UI.createElement("rectangle", { x: 0, y: 0, width: 128, height: 100 });
+ UI.createElement("rectangle", { x: 100, y: 200, width: 128, height: 100 });
+ UI.clickTool("arrow");
+ UI.clickOnTestId("elbow-arrow");
+ mouse.reset();
+ mouse.moveTo(128, 50);
+ mouse.click();
+ mouse.moveTo(139, 200);
+ mouse.click();
+
+ const arrow = h.elements.find(
+ (element) => element.type === "arrow",
+ ) as ExcalidrawElbowArrowElement;
+ expect(arrow.points.length).toBeGreaterThan(3);
+ expect(validateElbowPoints(arrow.points)).toBe(true);
+ });
+
+ it("keeps clear of an element by its binding gap, thick strokes included", () => {
+ const connectEllipse = (strokeWidth: number) => {
+ API.setAppState({ currentItemEndArrowhead: null });
+ // the ellipse's top is 12px below the rectangle's side's middle, and
+ // the arrow enters its upper left, below its top
+ UI.createElement("rectangle", { x: 0, y: 100, width: 120, height: 55 });
+ UI.createElement("ellipse", {
+ x: 220,
+ y: 139.5,
+ width: 200,
+ height: 124,
+ });
+ API.updateElement(h.elements[1], { strokeWidth });
+ UI.clickTool("arrow");
+ UI.clickOnTestId("elbow-arrow");
+ mouse.reset();
+ mouse.moveTo(120, 127.5);
+ mouse.click();
+ mouse.moveTo(260, 151.9);
+ mouse.click();
+
+ return h.elements.find(
+ (element) => element.type === "arrow",
+ ) as ExcalidrawElbowArrowElement;
+ };
+
+ // a binding gap of 6px: the corner above the ellipse fits
+ expect(connectEllipse(2).points.length).toBe(3);
+
+ API.setElements([]);
+ // 15px: the first leg would run over the stroke
+ expect(connectEllipse(20).points.length).toBeGreaterThan(3);
+ });
+
+ it("connects elements with a single corner when a fixed segment lines up with the start", () => {
+ const arrow = connect(167.5);
+
+ // a step down, the arrow is routed around the diamond, and the segment
+ // above it is fixed at the rectangle's previous middle
+ UI.clickTool("selection");
+ mouse.reset();
+ mouse.moveTo(0, 120);
+ mouse.down();
+ mouse.moveTo(0, 140);
+ mouse.up();
+ h.app.scene.mutateElement(arrow, {
+ fixedSegments: [
+ {
+ index: 3,
+ start: pointFrom<LocalPoint>(44, -20),
+ end: pointFrom<LocalPoint>(195, -20),
+ },
+ ],
+ });
+ expect(arrow.points).toCloselyEqualPoints([
+ [0, 0],
+ [44, 0],
+ [44, -20],
+ [195, -20],
+ [195, 14.15],
+ ]);
+
+ // and back up, the first leg lines up with the fixed segment
+ mouse.reset();
+ mouse.moveTo(0, 140);
+ mouse.down();
+ mouse.moveTo(0, 120);
+ mouse.up();
+
+ expect(arrow.fixedSegments ?? []).toEqual([]);
+ expect(arrow.points).toCloselyEqualPoints([
+ [0, 0],
+ [195, 0],
+ [195, 34.15],
+ ]);
+ });
+
+ // a box, and another one `gap` px below and to the right of it, connected
+ // from the bottom of one to the top of the other
+ const connectBelow = (gap: number) => {
+ UI.createElement("rectangle", { x: 0, y: 0, width: 278, height: 135 });
+ UI.createElement("rectangle", {
+ x: 518,
+ y: 135 + gap,
+ width: 278,
+ height: 135,
+ });
+ UI.clickTool("arrow");
+ UI.clickOnTestId("elbow-arrow");
+ mouse.reset();
+ mouse.moveTo(139, 135);
+ mouse.click();
+ mouse.moveTo(657, 135 + gap);
+ mouse.click();
+
+ return h.elements.find(
+ (element) => element.type === "arrow",
+ ) as ExcalidrawElbowArrowElement;
+ };
+
+ it("connects elements closer than the padding with two corners, turning halfway", () => {
+ API.setAppState({ currentItemEndArrowhead: null });
+ const arrow = connectBelow(84);
+
+ expect(arrow.points).toCloselyEqualPoints([
+ [0, 0],
+ [0, 36],
+ [518, 36],
+ [518, 72],
+ ]);
+ });
+
+ it("turns closer to the end without an arrowhead to leave the arrowhead the padding", () => {
+ const arrow = connectBelow(84);
+
+ // 72px apart: the leg to the arrowhead gets the padding, and the other
+ // leg the rest, still enough to round its corner
+ expect(arrow.points).toCloselyEqualPoints([
+ [0, 0],
+ [0, 32],
+ [518, 32],
+ [518, 72],
+ ]);
+ });
+
+ it("turns halfway as on the grid when there's room", () => {
+ const arrow = connectBelow(120);
+
+ expect(arrow.points).toCloselyEqualPoints([
+ [0, 0],
+ [0, 54],
+ [518, 54],
+ [518, 108],
+ ]);
+ });
+
+ it("doesn't put the arrowhead on a half too short for it", () => {
+ const arrow = connectBelow(40);
+
+ expect(arrow.points.length).toBeGreaterThan(4);
+ });
+});
diff --git a/packages/element/tests/shape.test.ts b/packages/element/tests/shape.test.ts
index ff706e50ef..372830c72a 100644
--- a/packages/element/tests/shape.test.ts
+++ b/packages/element/tests/shape.test.ts
@@ -3,7 +3,11 @@ import { pointFrom } from "@excalidraw/math";
import type { LocalPoint } from "@excalidraw/math";
-import { ShapeCache } from "../src/shape";
+import {
+ ELBOW_ARROW_CORNER_RADIUS,
+ generateElbowArrowShape,
+ ShapeCache,
+} from "../src/shape";
import type { Op } from "roughjs/bin/core";
@@ -29,3 +33,48 @@ describe("arrowhead shapes", () => {
},
);
});
+
+describe("elbow arrow shapes", () => {
+ const getCorners = (points: [number, number][]) =>
+ generateElbowArrowShape(
+ points.map(([x, y]) => pointFrom<LocalPoint>(x, y)),
+ ELBOW_ARROW_CORNER_RADIUS,
+ ).match(/Q/g)?.length ?? 0;
+
+ it("draws a jog too short to see straight", () => {
+ // a fixed segment lined up with the first one, while dragging
+ expect(
+ getCorners([
+ [0, 0],
+ [44, 0],
+ [44, -1e-12],
+ [195, -1e-12],
+ [195, 34],
+ ]),
+ ).toBe(1);
+ });
+
+ it("keeps the corners of short segments in a row", () => {
+ expect(
+ getCorners([
+ [0, 0],
+ [100, 0],
+ [100, 0.5],
+ [100.5, 0.5],
+ [100.5, 100],
+ ]),
+ ).toBe(3);
+ });
+
+ it("keeps a short U-turn", () => {
+ expect(
+ getCorners([
+ [0, 0],
+ [100, 0],
+ [100, 0.5],
+ [0, 0.5],
+ [0, 50],
+ ]),
+ ).toBe(3);
+ });
+});
diff --git a/packages/excalidraw/actions/actionFlip.ts b/packages/excalidraw/actions/actionFlip.ts
index 02690e7888..6ed467f361 100644
--- a/packages/excalidraw/actions/actionFlip.ts
+++ b/packages/excalidraw/actions/actionFlip.ts
@@ -4,7 +4,11 @@ import { getCommonBoundingBox } from "@excalidraw/element";
import { newElementWith } from "@excalidraw/element";
import { deepCopyElement } from "@excalidraw/element";
import { resizeMultipleElements } from "@excalidraw/element";
-import { isArrowElement, isElbowArrow } from "@excalidraw/element";
+import {
+ isArrowElement,
+ isElbowArrow,
+ updateElbowArrowRoute,
+} from "@excalidraw/element";
import { updateFrameMembershipOfSelectedElements } from "@excalidraw/element";
import { CODES, KEYS, arrayToMap } from "@excalidraw/common";
@@ -121,10 +125,13 @@ const flipElements = (
) {
return selectedElements.map((element) => {
const _element = element as ExcalidrawArrowElement;
- return newElementWith(_element, {
+ const flipped = newElementWith(_element, {
startArrowhead: _element.endArrowhead,
endArrowhead: _element.startArrowhead,
});
+ return isElbowArrow(flipped)
+ ? newElementWith(flipped, updateElbowArrowRoute(flipped, elementsMap))
+ : flipped;
});
}
diff --git a/packages/excalidraw/actions/actionProperties.tsx b/packages/excalidraw/actions/actionProperties.tsx
index 5666895a7c..1a8f54b035 100644
--- a/packages/excalidraw/actions/actionProperties.tsx
+++ b/packages/excalidraw/actions/actionProperties.tsx
@@ -72,6 +72,7 @@ import {
import {
updateElbowArrowPoints,
+ updateElbowArrowRoute,
CaptureUpdateAction,
toggleLinePolygonState,
} from "@excalidraw/element";
@@ -1963,7 +1964,7 @@ export const actionChangeArrowhead = register<{
name: "changeArrowhead",
label: "Change arrowheads",
trackEvent: false,
- perform: (elements, appState, value) => {
+ perform: (elements, appState, value, app) => {
invariant(value, "actionChangeArrowhead: value must be defined");
return {
@@ -1971,17 +1972,21 @@ export const actionChangeArrowhead = register<{
if (isLinearElement(el)) {
const { position, type } = value;
- if (position === "start") {
- const element: ExcalidrawLinearElement = newElementWith(el, {
- startArrowhead: type,
- });
- return element;
- } else if (position === "end") {
- const element: ExcalidrawLinearElement = newElementWith(el, {
- endArrowhead: type,
- });
- return element;
- }
+ const element: ExcalidrawLinearElement = newElementWith(
+ el,
+ position === "start"
+ ? { startArrowhead: type }
+ : { endArrowhead: type },
+ );
+ return isElbowArrow(element)
+ ? newElementWith(
+ element,
+ updateElbowArrowRoute(
+ element,
+ app.scene.getNonDeletedElementsMap(),
+ ),
+ )
+ : element;
}
return el;
diff --git a/packages/excalidraw/actions/actionStyles.ts b/packages/excalidraw/actions/actionStyles.ts
index 1411631958..bdf2686e57 100644
--- a/packages/excalidraw/actions/actionStyles.ts
+++ b/packages/excalidraw/actions/actionStyles.ts
@@ -17,6 +17,7 @@ import {
getDefaultRoundnessTypeForElement,
isFrameLikeElement,
isArrowElement,
+ isElbowArrow,
isExcalidrawElement,
getColorUpdate,
isNonDeletedElement,
@@ -26,6 +27,7 @@ import {
getBaseFontSize,
getBaseFontSizeUpdate,
relayoutStickyNotes,
+ updateElbowArrowRoute,
updateBoundElements,
} from "@excalidraw/element";
@@ -188,6 +190,15 @@ export const actionPasteStyles = register({
startArrowhead: elementStylesToCopyFrom.startArrowhead,
endArrowhead: elementStylesToCopyFrom.endArrowhead,
});
+ if (isElbowArrow(newElement)) {
+ newElement = newElementWith(
+ newElement,
+ updateElbowArrowRoute(
+ newElement,
+ app.scene.getNonDeletedElementsMap(),
+ ),
+ );
+ }
}
if (isFrameLikeElement(element)) {