diff --git a/packages/element/src/linearElementEditor.ts b/packages/element/src/linearElementEditor.ts index bd4e740b2..8a9117bf8 100644 --- a/packages/element/src/linearElementEditor.ts +++ b/packages/element/src/linearElementEditor.ts @@ -133,6 +133,7 @@ export class LinearElementEditor { }; if (!pointsEqual(element.points[0], pointFrom(0, 0))) { console.error("Linear element is not normalized", Error().stack); + LinearElementEditor.normalizePoints(element); } this.selectedPointsIndices = null; diff --git a/packages/excalidraw/tests/linearElementEditor.test.tsx b/packages/excalidraw/tests/linearElementEditor.test.tsx index 741799d3b..861998584 100644 --- a/packages/excalidraw/tests/linearElementEditor.test.tsx +++ b/packages/excalidraw/tests/linearElementEditor.test.tsx @@ -1,3 +1,5 @@ +import { newArrowElement } from "@excalidraw/element/newElement"; + import { pointCenter, pointFrom } from "@excalidraw/math"; import { act, queryByTestId, queryByText } from "@testing-library/react"; import React from "react"; @@ -19,7 +21,7 @@ import { import * as textElementUtils from "@excalidraw/element/textElement"; import { wrapText } from "@excalidraw/element/textWrapping"; -import type { GlobalPoint } from "@excalidraw/math"; +import type { GlobalPoint, LocalPoint } from "@excalidraw/math"; import type { ExcalidrawElement, @@ -164,6 +166,24 @@ describe("Test Linear Elements", () => { Keyboard.keyPress(KEYS.DELETE); }; + it("should normalize the element points at creation", () => { + const element = newArrowElement({ + type: "arrow", + points: [pointFrom(0.5, 0), pointFrom(100, 100)], + x: 0, + y: 0, + }); + expect(element.points).toEqual([ + pointFrom(0.5, 0), + pointFrom(100, 100), + ]); + new LinearElementEditor(element); + expect(element.points).toEqual([ + pointFrom(0, 0), + pointFrom(99.5, 100), + ]); + }); + it("should not drag line and add midpoint until dragged beyond a threshold", () => { createTwoPointerLinearElement("line"); const line = h.elements[0] as ExcalidrawLinearElement;