| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539 |
- import { pointCenter, pointFrom } from "@excalidraw/math";
- import { act, queryByTestId, queryByText } from "@testing-library/react";
- import { vi } from "vitest";
- import {
- ROUNDNESS,
- VERTICAL_ALIGN,
- KEYS,
- reseed,
- arrayToMap,
- } from "@excalidraw/common";
- import { Excalidraw } from "@excalidraw/excalidraw";
- import * as InteractiveCanvas from "@excalidraw/excalidraw/renderer/interactiveScene";
- import * as StaticScene from "@excalidraw/excalidraw/renderer/staticScene";
- import { API } from "@excalidraw/excalidraw/tests/helpers/api";
- import { Keyboard, Pointer, UI } from "@excalidraw/excalidraw/tests/helpers/ui";
- import {
- screen,
- render,
- fireEvent,
- GlobalTestState,
- unmountComponent,
- } from "@excalidraw/excalidraw/tests/test-utils";
- import type { GlobalPoint, LocalPoint } from "@excalidraw/math";
- import { wrapText } from "../src";
- import * as textElementUtils from "../src/textElement";
- import { getBoundTextElementPosition, getBoundTextMaxWidth } from "../src";
- import { LinearElementEditor } from "../src";
- import { newArrowElement } from "../src";
- import {
- getTextEditor,
- TEXT_EDITOR_SELECTOR,
- } from "../../excalidraw/tests/queries/dom";
- import type {
- ExcalidrawElement,
- ExcalidrawLinearElement,
- ExcalidrawTextElementWithContainer,
- FontString,
- } from "../src/types";
- const renderInteractiveScene = vi.spyOn(
- InteractiveCanvas,
- "renderInteractiveScene",
- );
- const renderStaticScene = vi.spyOn(StaticScene, "renderStaticScene");
- const { h } = window;
- const font = "20px Cascadia, width: Segoe UI Emoji" as FontString;
- describe("Test Linear Elements", () => {
- let container: HTMLElement;
- let interactiveCanvas: HTMLCanvasElement;
- beforeEach(async () => {
- unmountComponent();
- localStorage.clear();
- renderInteractiveScene.mockClear();
- renderStaticScene.mockClear();
- reseed(7);
- const comp = await render(<Excalidraw handleKeyboardGlobally={true} />);
- h.state.width = 1000;
- h.state.height = 1000;
- container = comp.container;
- interactiveCanvas = container.querySelector("canvas.interactive")!;
- });
- const p1 = pointFrom<GlobalPoint>(20, 20);
- const p2 = pointFrom<GlobalPoint>(60, 20);
- const midpoint = pointCenter<GlobalPoint>(p1, p2);
- const delta = 50;
- const mouse = new Pointer("mouse");
- const createTwoPointerLinearElement = (
- type: ExcalidrawLinearElement["type"],
- roundness: ExcalidrawElement["roundness"] = null,
- roughness: ExcalidrawLinearElement["roughness"] = 0,
- ) => {
- const line = API.createElement({
- x: p1[0],
- y: p1[1],
- width: p2[0] - p1[0],
- height: 0,
- type,
- roughness,
- points: [pointFrom(0, 0), pointFrom(p2[0] - p1[0], p2[1] - p1[1])],
- roundness,
- });
- API.setElements([line]);
- mouse.clickAt(p1[0], p1[1]);
- return line;
- };
- const createThreePointerLinearElement = (
- type: ExcalidrawLinearElement["type"],
- roundness: ExcalidrawElement["roundness"] = null,
- roughness: ExcalidrawLinearElement["roughness"] = 0,
- ) => {
- //dragging line from midpoint
- const p3 = [midpoint[0] + delta - p1[0], midpoint[1] + delta - p1[1]];
- const line = API.createElement({
- x: p1[0],
- y: p1[1],
- width: p3[0] - p1[0],
- height: 0,
- type,
- roughness,
- points: [
- pointFrom(0, 0),
- pointFrom(p3[0], p3[1]),
- pointFrom(p2[0] - p1[0], p2[1] - p1[1]),
- ],
- roundness,
- });
- h.app.scene.mutateElement(line, { points: line.points });
- API.setElements([line]);
- mouse.clickAt(p1[0], p1[1]);
- return line;
- };
- const enterLineEditingMode = (
- line: ExcalidrawLinearElement,
- selectProgrammatically = false,
- ) => {
- if (selectProgrammatically) {
- API.setSelectedElements([line]);
- } else {
- mouse.clickAt(p1[0], p1[1]);
- }
- Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.keyPress(KEYS.ENTER);
- });
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(line.id);
- };
- const drag = (startPoint: GlobalPoint, endPoint: GlobalPoint) => {
- fireEvent.pointerDown(interactiveCanvas, {
- clientX: startPoint[0],
- clientY: startPoint[1],
- });
- fireEvent.pointerMove(interactiveCanvas, {
- clientX: endPoint[0],
- clientY: endPoint[1],
- });
- fireEvent.pointerUp(interactiveCanvas, {
- clientX: endPoint[0],
- clientY: endPoint[1],
- });
- };
- const deletePoint = (point: GlobalPoint) => {
- fireEvent.pointerDown(interactiveCanvas, {
- clientX: point[0],
- clientY: point[1],
- });
- fireEvent.pointerUp(interactiveCanvas, {
- clientX: point[0],
- clientY: point[1],
- });
- Keyboard.keyPress(KEYS.DELETE);
- };
- it("should normalize the element points at creation", () => {
- const element = newArrowElement({
- type: "arrow",
- points: [pointFrom<LocalPoint>(0.5, 0), pointFrom<LocalPoint>(100, 100)],
- x: 0,
- y: 0,
- });
- expect(element.points).toEqual([
- pointFrom<LocalPoint>(0.5, 0),
- pointFrom<LocalPoint>(100, 100),
- ]);
- new LinearElementEditor(element, arrayToMap(h.elements));
- expect(element.points).toEqual([
- pointFrom<LocalPoint>(0, 0),
- pointFrom<LocalPoint>(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;
- const originalX = line.x;
- const originalY = line.y;
- expect(line.points.length).toEqual(2);
- mouse.clickAt(midpoint[0], midpoint[1]);
- drag(midpoint, pointFrom(midpoint[0] + 1, midpoint[1] + 1));
- expect(line.points.length).toEqual(2);
- expect(line.x).toBe(originalX);
- expect(line.y).toBe(originalY);
- expect(line.points.length).toEqual(2);
- drag(midpoint, pointFrom(midpoint[0] + delta, midpoint[1] + delta));
- expect(line.x).toBe(originalX);
- expect(line.y).toBe(originalY);
- expect(line.points.length).toEqual(3);
- });
- it("should allow dragging line from midpoint in 2 pointer lines outside editor", async () => {
- createTwoPointerLinearElement("line");
- const line = h.elements[0] as ExcalidrawLinearElement;
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(`5`);
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`5`);
- expect((h.elements[0] as ExcalidrawLinearElement).points.length).toEqual(2);
- // drag line from midpoint
- drag(midpoint, pointFrom(midpoint[0] + delta, midpoint[1] + delta));
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(`8`);
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`7`);
- expect(line.points.length).toEqual(3);
- expect(line.points).toMatchInlineSnapshot(`
- [
- [
- 0,
- 0,
- ],
- [
- 70,
- 50,
- ],
- [
- 40,
- 0,
- ],
- ]
- `);
- });
- it("should allow entering and exiting line editor via context menu", () => {
- createTwoPointerLinearElement("line");
- fireEvent.contextMenu(GlobalTestState.interactiveCanvas, {
- button: 2,
- clientX: midpoint[0],
- clientY: midpoint[1],
- });
- // Enter line editor
- const contextMenu = document.querySelector(".context-menu");
- fireEvent.contextMenu(GlobalTestState.interactiveCanvas, {
- button: 2,
- clientX: midpoint[0],
- clientY: midpoint[1],
- });
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Edit line")!);
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(h.elements[0].id);
- });
- it("should enter line editor via enter (line)", () => {
- createTwoPointerLinearElement("line");
- expect(h.state.selectedLinearElement?.isEditing).toBe(false);
- mouse.clickAt(midpoint[0], midpoint[1]);
- Keyboard.keyPress(KEYS.ENTER);
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(h.elements[0].id);
- });
- // ctrl+enter alias (to align with arrows)
- it("should enter line editor via ctrl+enter (line)", () => {
- createTwoPointerLinearElement("line");
- expect(h.state.selectedLinearElement?.isEditing).toBe(false);
- mouse.clickAt(midpoint[0], midpoint[1]);
- Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.keyPress(KEYS.ENTER);
- });
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(h.elements[0].id);
- });
- it("should enter line editor via ctrl+enter (arrow)", () => {
- createTwoPointerLinearElement("arrow");
- expect(h.state.selectedLinearElement?.isEditing).toBe(false);
- mouse.clickAt(midpoint[0], midpoint[1]);
- Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.keyPress(KEYS.ENTER);
- });
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(h.elements[0].id);
- });
- it("should enter line editor on ctrl+dblclick (simple arrow)", () => {
- createTwoPointerLinearElement("arrow");
- expect(h.state.selectedLinearElement?.isEditing).toBe(false);
- Keyboard.withModifierKeys({ ctrl: true }, () => {
- mouse.doubleClick();
- });
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(h.elements[0].id);
- });
- it("should enter line editor on ctrl+dblclick (line)", () => {
- createTwoPointerLinearElement("line");
- expect(h.state.selectedLinearElement?.isEditing).toBe(false);
- Keyboard.withModifierKeys({ ctrl: true }, () => {
- mouse.doubleClick();
- });
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(h.elements[0].id);
- });
- it("should enter line editor on dblclick (line)", () => {
- createTwoPointerLinearElement("line");
- expect(h.state.selectedLinearElement?.isEditing).toBe(false);
- mouse.doubleClick();
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(h.elements[0].id);
- });
- it("should not enter line editor on dblclick (arrow)", async () => {
- createTwoPointerLinearElement("arrow");
- expect(h.state.selectedLinearElement?.isEditing).toBe(false);
- mouse.doubleClick();
- expect(h.state.selectedLinearElement?.isEditing).toBe(false);
- await getTextEditor();
- });
- it("shouldn't create text element on double click in line editor (arrow)", async () => {
- createTwoPointerLinearElement("arrow");
- const arrow = h.elements[0] as ExcalidrawLinearElement;
- enterLineEditingMode(arrow);
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(arrow.id);
- mouse.doubleClick();
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(h.state.selectedLinearElement?.elementId).toEqual(arrow.id);
- expect(h.elements.length).toEqual(1);
- expect(document.querySelector(TEXT_EDITOR_SELECTOR)).toBe(null);
- });
- describe("Inside editor", () => {
- it("should not drag line and add midpoint when dragged irrespective of threshold", () => {
- createTwoPointerLinearElement("line");
- const line = h.elements[0] as ExcalidrawLinearElement;
- const originalX = line.x;
- const originalY = line.y;
- enterLineEditingMode(line);
- expect(h.state.selectedLinearElement?.isEditing).toBe(true);
- expect(line.points.length).toEqual(2);
- mouse.clickAt(midpoint[0], midpoint[1]);
- expect(line.points.length).toEqual(2);
- drag(midpoint, pointFrom(midpoint[0] + 1, midpoint[1] + 1));
- expect(line.x).toBe(originalX);
- expect(line.y).toBe(originalY);
- expect(line.points.length).toEqual(3);
- });
- it("should allow dragging line from midpoint in 2 pointer lines", async () => {
- createTwoPointerLinearElement("line");
- const line = h.elements[0] as ExcalidrawLinearElement;
- enterLineEditingMode(line);
- // drag line from midpoint
- drag(midpoint, pointFrom(midpoint[0] + delta, midpoint[1] + delta));
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(
- `11`,
- );
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`7`);
- expect(line.points.length).toEqual(3);
- expect(line.points).toMatchInlineSnapshot(`
- [
- [
- 0,
- 0,
- ],
- [
- 70,
- 50,
- ],
- [
- 40,
- 0,
- ],
- ]
- `);
- });
- it("should update the midpoints when element roundness changed", async () => {
- createThreePointerLinearElement("line");
- const line = h.elements[0] as ExcalidrawLinearElement;
- expect(line.points.length).toEqual(3);
- enterLineEditingMode(line);
- const midPointsWithSharpEdge = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- // update roundness
- fireEvent.click(screen.getByTitle("Round"));
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(
- `9`,
- );
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`6`);
- const midPointsWithRoundEdge = LinearElementEditor.getEditorMidPoints(
- h.elements[0] as ExcalidrawLinearElement,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- expect(midPointsWithRoundEdge[0]).not.toEqual(midPointsWithSharpEdge[0]);
- expect(midPointsWithRoundEdge[1]).not.toEqual(midPointsWithSharpEdge[1]);
- expect(midPointsWithRoundEdge).toMatchInlineSnapshot(`
- [
- [
- "54.27552",
- "46.16120",
- ],
- [
- "76.95494",
- "44.56052",
- ],
- ]
- `);
- });
- it("should update all the midpoints when element position changed", async () => {
- const elementsMap = arrayToMap(h.elements);
- createThreePointerLinearElement("line", {
- type: ROUNDNESS.PROPORTIONAL_RADIUS,
- });
- const line = h.elements[0] as ExcalidrawLinearElement;
- expect(line.points.length).toEqual(3);
- enterLineEditingMode(line);
- const points = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- expect([line.x, line.y]).toEqual(points[0]);
- const midPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- const startPoint = pointCenter(points[0], midPoints[0]!);
- const deltaX = 50;
- const deltaY = 20;
- const endPoint = pointFrom<GlobalPoint>(
- startPoint[0] + deltaX,
- startPoint[1] + deltaY,
- );
- // Move the element
- drag(startPoint, endPoint);
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(
- `11`,
- );
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`7`);
- expect([line.x, line.y]).toEqual([
- points[0][0] + deltaX,
- points[0][1] + deltaY,
- ]);
- const newMidPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- expect(midPoints[0]).not.toEqual(newMidPoints[0]);
- expect(midPoints[1]).not.toEqual(newMidPoints[1]);
- expect(newMidPoints).toMatchInlineSnapshot(`
- [
- [
- "104.27552",
- "66.16120",
- ],
- [
- "126.95494",
- "64.56052",
- ],
- ]
- `);
- });
- describe("When edges are round", () => {
- // This is the expected midpoint for line with round edge
- // hence hardcoding it so if later some bug is introduced
- // this will fail and we can fix it
- const firstSegmentMidpoint = pointFrom<GlobalPoint>(55, 45);
- const lastSegmentMidpoint = pointFrom<GlobalPoint>(75, 40);
- let line: ExcalidrawLinearElement;
- beforeEach(() => {
- line = createThreePointerLinearElement("line");
- expect(line.points.length).toEqual(3);
- enterLineEditingMode(line);
- });
- it("should allow dragging lines from midpoints in between segments", async () => {
- // drag line via first segment midpoint
- drag(
- firstSegmentMidpoint,
- pointFrom(
- firstSegmentMidpoint[0] + delta,
- firstSegmentMidpoint[1] + delta,
- ),
- );
- expect(line.points.length).toEqual(4);
- // drag line from last segment midpoint
- drag(
- lastSegmentMidpoint,
- pointFrom(
- lastSegmentMidpoint[0] + delta,
- lastSegmentMidpoint[1] + delta,
- ),
- );
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(
- `14`,
- );
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`9`);
- expect(line.points.length).toEqual(5);
- expect((h.elements[0] as ExcalidrawLinearElement).points)
- .toMatchInlineSnapshot(`
- [
- [
- 0,
- 0,
- ],
- [
- 85,
- 75,
- ],
- [
- 70,
- 50,
- ],
- [
- 105,
- 70,
- ],
- [
- 40,
- 0,
- ],
- ]
- `);
- });
- it("should update only the first segment midpoint when its point is dragged", async () => {
- const elementsMap = arrayToMap(h.elements);
- const points = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- const midPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- const hitCoords = pointFrom<GlobalPoint>(points[0][0], points[0][1]);
- // Drag from first point
- drag(hitCoords, pointFrom(hitCoords[0] - delta, hitCoords[1] - delta));
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(
- `11`,
- );
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`7`);
- const newPoints = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- expect([newPoints[0][0], newPoints[0][1]]).toEqual([
- points[0][0] - delta,
- points[0][1] - delta,
- ]);
- const newMidPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- expect(midPoints[0]).not.toEqual(newMidPoints[0]);
- expect(midPoints[1]).toEqual(newMidPoints[1]);
- });
- it("should hide midpoints in the segment when points moved close", async () => {
- const elementsMap = arrayToMap(h.elements);
- const points = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- const midPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- const hitCoords = pointFrom<GlobalPoint>(points[0][0], points[0][1]);
- // Drag from first point
- drag(hitCoords, pointFrom(hitCoords[0] + delta, hitCoords[1] + delta));
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(
- `11`,
- );
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`7`);
- const newPoints = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- expect([newPoints[0][0], newPoints[0][1]]).toEqual([
- points[0][0] + delta,
- points[0][1] + delta,
- ]);
- const newMidPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- // This midpoint is hidden since the points are too close
- expect(newMidPoints[0]).toBeNull();
- expect(midPoints[1]).toEqual(newMidPoints[1]);
- });
- it("should remove the midpoint when one of the points in the segment is deleted", async () => {
- const line = h.elements[0] as ExcalidrawLinearElement;
- enterLineEditingMode(line);
- const points = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- arrayToMap(h.elements),
- );
- // dragging line from last segment midpoint
- drag(
- lastSegmentMidpoint,
- pointFrom(lastSegmentMidpoint[0] + 50, lastSegmentMidpoint[1] + 50),
- );
- expect(line.points.length).toEqual(4);
- const midPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- // delete 3rd point
- deletePoint(points[2]);
- expect(line.points.length).toEqual(3);
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(
- `17`,
- );
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`10`);
- const newMidPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- expect(newMidPoints.length).toEqual(2);
- expect(midPoints[0]).toEqual(newMidPoints[0]);
- expect(midPoints[1]).toEqual(newMidPoints[1]);
- });
- });
- describe("When edges are round", () => {
- // This is the expected midpoint for line with round edge
- // hence hardcoding it so if later some bug is introduced
- // this will fail and we can fix it
- const firstSegmentMidpoint = pointFrom<GlobalPoint>(
- 55.9697848965255,
- 47.442326230998205,
- );
- const lastSegmentMidpoint = pointFrom<GlobalPoint>(
- 76.08587175006699,
- 43.294165939653226,
- );
- let line: ExcalidrawLinearElement;
- beforeEach(() => {
- line = createThreePointerLinearElement("line", {
- type: ROUNDNESS.PROPORTIONAL_RADIUS,
- });
- expect(line.points.length).toEqual(3);
- enterLineEditingMode(line);
- });
- it("should allow dragging lines from midpoints in between segments", async () => {
- // drag line from first segment midpoint
- drag(
- firstSegmentMidpoint,
- pointFrom(
- firstSegmentMidpoint[0] + delta,
- firstSegmentMidpoint[1] + delta,
- ),
- );
- expect(line.points.length).toEqual(4);
- // drag line from last segment midpoint
- drag(
- lastSegmentMidpoint,
- pointFrom(
- lastSegmentMidpoint[0] + delta,
- lastSegmentMidpoint[1] + delta,
- ),
- );
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(
- `14`,
- );
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`9`);
- expect(line.points.length).toEqual(5);
- expect((h.elements[0] as ExcalidrawLinearElement).points)
- .toMatchInlineSnapshot(`
- [
- [
- 0,
- 0,
- ],
- [
- "85.96978",
- "77.44233",
- ],
- [
- 70,
- 50,
- ],
- [
- "106.08587",
- "73.29417",
- ],
- [
- 40,
- 0,
- ],
- ]
- `);
- });
- it("should update all the midpoints when its point is dragged", async () => {
- const elementsMap = arrayToMap(h.elements);
- const points = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- const midPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- const hitCoords = pointFrom<GlobalPoint>(points[0][0], points[0][1]);
- // Drag from first point
- drag(hitCoords, pointFrom(hitCoords[0] - delta, hitCoords[1] - delta));
- const newPoints = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- expect([newPoints[0][0], newPoints[0][1]]).toEqual([
- points[0][0] - delta,
- points[0][1] - delta,
- ]);
- const newMidPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- expect(midPoints[0]).not.toEqual(newMidPoints[0]);
- expect(midPoints[1]).not.toEqual(newMidPoints[1]);
- expect(newMidPoints).toMatchInlineSnapshot(`
- [
- [
- "29.28349",
- "20.91105",
- ],
- [
- "78.86048",
- "46.12277",
- ],
- ]
- `);
- });
- it("should hide midpoints in the segment when points moved close", async () => {
- const elementsMap = arrayToMap(h.elements);
- const points = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- const midPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- const hitCoords = pointFrom<GlobalPoint>(points[0][0], points[0][1]);
- // Drag from first point
- drag(hitCoords, pointFrom(hitCoords[0] + delta, hitCoords[1] + delta));
- expect(renderInteractiveScene.mock.calls.length).toMatchInlineSnapshot(
- `11`,
- );
- expect(renderStaticScene.mock.calls.length).toMatchInlineSnapshot(`7`);
- const newPoints = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- expect([newPoints[0][0], newPoints[0][1]]).toEqual([
- points[0][0] + delta,
- points[0][1] + delta,
- ]);
- const newMidPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- // This mid point is hidden due to point being too close
- expect(newMidPoints[0]).toBeNull();
- expect(newMidPoints[1]).not.toEqual(midPoints[1]);
- });
- it("should update all the midpoints when a point is deleted", async () => {
- const elementsMap = arrayToMap(h.elements);
- drag(
- lastSegmentMidpoint,
- pointFrom(
- lastSegmentMidpoint[0] + delta,
- lastSegmentMidpoint[1] + delta,
- ),
- );
- expect(line.points.length).toEqual(4);
- const midPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- const points = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- // delete 3rd point
- deletePoint(points[2]);
- expect(line.points.length).toEqual(3);
- const newMidPoints = LinearElementEditor.getEditorMidPoints(
- line,
- h.app.scene.getNonDeletedElementsMap(),
- h.state,
- );
- expect(newMidPoints.length).toEqual(2);
- expect(midPoints[0]).not.toEqual(newMidPoints[0]);
- expect(midPoints[1]).not.toEqual(newMidPoints[1]);
- expect(newMidPoints).toMatchInlineSnapshot(`
- [
- [
- "54.27552",
- "46.16120",
- ],
- [
- "76.95494",
- "44.56052",
- ],
- ]
- `);
- });
- });
- it("in-editor dragging a line point covered by another element", () => {
- createTwoPointerLinearElement("line");
- const line = h.elements[0] as ExcalidrawLinearElement;
- API.setElements([
- line,
- API.createElement({
- type: "rectangle",
- x: line.x - 50,
- y: line.y - 50,
- width: 100,
- height: 100,
- backgroundColor: "red",
- fillStyle: "solid",
- }),
- ]);
- const dragEndPositionOffset = [100, 100] as const;
- API.setSelectedElements([line]);
- enterLineEditingMode(line, true);
- drag(
- pointFrom(line.points[0][0] + line.x, line.points[0][1] + line.y),
- pointFrom(
- dragEndPositionOffset[0] + line.x,
- dragEndPositionOffset[1] + line.y,
- ),
- );
- expect(line.points).toMatchInlineSnapshot(`
- [
- [
- 0,
- 0,
- ],
- [
- -60,
- -100,
- ],
- ]
- `);
- });
- });
- describe("Test bound text element", () => {
- const DEFAULT_TEXT = "Online whiteboard collaboration made easy";
- const createBoundTextElement = (
- text: string,
- container: ExcalidrawLinearElement,
- ) => {
- const textElement = API.createElement({
- type: "text",
- x: 0,
- y: 0,
- text: wrapText(text, font, getBoundTextMaxWidth(container, null)),
- containerId: container.id,
- width: 30,
- height: 20,
- }) as ExcalidrawTextElementWithContainer;
- container = {
- ...container,
- boundElements: (container.boundElements || []).concat({
- type: "text",
- id: textElement.id,
- }),
- };
- const elements: ExcalidrawElement[] = [];
- h.elements.forEach((element) => {
- if (element.id === container.id) {
- elements.push(container);
- } else {
- elements.push(element);
- }
- });
- const updatedTextElement = { ...textElement, originalText: text };
- API.setElements([...elements, updatedTextElement]);
- return { textElement: updatedTextElement, container };
- };
- describe("Test getBoundTextElementPosition", () => {
- it("should return correct position for 2 pointer arrow", () => {
- createTwoPointerLinearElement("arrow");
- const arrow = h.elements[0] as ExcalidrawLinearElement;
- const { textElement, container } = createBoundTextElement(
- DEFAULT_TEXT,
- arrow,
- );
- const position = LinearElementEditor.getBoundTextElementPosition(
- container,
- textElement,
- arrayToMap(h.elements),
- );
- expect(position).toMatchInlineSnapshot(`
- {
- "x": 25,
- "y": 10,
- }
- `);
- });
- it("should return correct position for arrow with odd points", () => {
- createThreePointerLinearElement("arrow", {
- type: ROUNDNESS.PROPORTIONAL_RADIUS,
- });
- const arrow = h.elements[0] as ExcalidrawLinearElement;
- const { textElement, container } = createBoundTextElement(
- DEFAULT_TEXT,
- arrow,
- );
- const position = LinearElementEditor.getBoundTextElementPosition(
- container,
- textElement,
- arrayToMap(h.elements),
- );
- expect(position).toMatchInlineSnapshot(`
- {
- "x": 75,
- "y": 60,
- }
- `);
- });
- it("should return correct position for arrow with even points", () => {
- createThreePointerLinearElement("arrow", {
- type: ROUNDNESS.PROPORTIONAL_RADIUS,
- });
- const arrow = h.elements[0] as ExcalidrawLinearElement;
- const { textElement, container } = createBoundTextElement(
- DEFAULT_TEXT,
- arrow,
- );
- enterLineEditingMode(container);
- // This is the expected midpoint for line with round edge
- // hence hardcoding it so if later some bug is introduced
- // this will fail and we can fix it
- const firstSegmentMidpoint = pointFrom<GlobalPoint>(
- 55.9697848965255,
- 47.442326230998205,
- );
- // drag line from first segment midpoint
- drag(
- firstSegmentMidpoint,
- pointFrom(
- firstSegmentMidpoint[0] + delta,
- firstSegmentMidpoint[1] + delta,
- ),
- );
- const position = LinearElementEditor.getBoundTextElementPosition(
- container,
- textElement,
- arrayToMap(h.elements),
- );
- expect(position).toMatchInlineSnapshot(`
- {
- "x": "86.17305",
- "y": "76.11251",
- }
- `);
- });
- });
- it("should match styles for text editor", async () => {
- createTwoPointerLinearElement("arrow");
- Keyboard.keyPress(KEYS.ENTER);
- const editor = await getTextEditor();
- expect(editor).toMatchSnapshot();
- });
- it("should bind text to arrow when double clicked", async () => {
- createTwoPointerLinearElement("arrow");
- const arrow = h.elements[0] as ExcalidrawLinearElement;
- expect(h.elements.length).toBe(1);
- expect(h.elements[0].id).toBe(arrow.id);
- mouse.doubleClickAt(arrow.x, arrow.y);
- expect(h.elements.length).toBe(2);
- const text = h.elements[1] as ExcalidrawTextElementWithContainer;
- expect(text.type).toBe("text");
- expect(text.containerId).toBe(arrow.id);
- mouse.down();
- const editor = await getTextEditor();
- fireEvent.change(editor, {
- target: { value: DEFAULT_TEXT },
- });
- Keyboard.exitTextEditor(editor);
- expect(arrow.boundElements).toStrictEqual([
- { id: text.id, type: "text" },
- ]);
- expect(
- (h.elements[1] as ExcalidrawTextElementWithContainer).text,
- ).toMatchSnapshot();
- });
- it("should bind text to arrow when clicked on arrow and enter pressed", async () => {
- const arrow = createTwoPointerLinearElement("arrow");
- expect(h.elements.length).toBe(1);
- expect(h.elements[0].id).toBe(arrow.id);
- Keyboard.keyPress(KEYS.ENTER);
- expect(h.elements.length).toBe(2);
- const textElement = h.elements[1] as ExcalidrawTextElementWithContainer;
- expect(textElement.type).toBe("text");
- expect(textElement.containerId).toBe(arrow.id);
- const editor = await getTextEditor();
- fireEvent.change(editor, {
- target: { value: DEFAULT_TEXT },
- });
- Keyboard.exitTextEditor(editor);
- expect(arrow.boundElements).toStrictEqual([
- { id: textElement.id, type: "text" },
- ]);
- expect(
- (h.elements[1] as ExcalidrawTextElementWithContainer).text,
- ).toMatchSnapshot();
- });
- it("should not bind text to line when double clicked", async () => {
- const line = createTwoPointerLinearElement("line");
- expect(h.elements.length).toBe(1);
- mouse.doubleClickAt(line.x, line.y);
- expect(h.elements.length).toBe(1);
- });
- // TODO fix #7029 and rewrite this test
- it.todo(
- "should not rotate the bound text and update position of bound text and bounding box correctly when arrow rotated",
- );
- it("should resize and position the bound text and bounding box correctly when 3 pointer arrow element resized", () => {
- createThreePointerLinearElement("arrow", {
- type: ROUNDNESS.PROPORTIONAL_RADIUS,
- });
- const arrow = h.elements[0] as ExcalidrawLinearElement;
- const { textElement, container } = createBoundTextElement(
- DEFAULT_TEXT,
- arrow,
- );
- expect(container.width).toBe(70);
- expect(container.height).toBe(50);
- expect(
- getBoundTextElementPosition(
- container,
- textElement,
- arrayToMap(h.elements),
- ),
- ).toMatchInlineSnapshot(`
- {
- "x": 75,
- "y": 60,
- }
- `);
- expect(textElement.text).toMatchSnapshot();
- expect(
- LinearElementEditor.getElementAbsoluteCoords(
- container,
- h.app.scene.getNonDeletedElementsMap(),
- true,
- ),
- ).toMatchInlineSnapshot(`
- [
- 20,
- 20,
- 105,
- 80,
- "55.45894",
- 45,
- ]
- `);
- UI.resize(container, "ne", [300, 200]);
- expect({ width: container.width, height: container.height })
- .toMatchInlineSnapshot(`
- {
- "height": 130,
- "width": "366.11716",
- }
- `);
- expect(
- getBoundTextElementPosition(
- container,
- textElement,
- arrayToMap(h.elements),
- ),
- ).toMatchInlineSnapshot(`
- {
- "x": "271.11716",
- "y": 45,
- }
- `);
- expect(
- (h.elements[1] as ExcalidrawTextElementWithContainer).text,
- ).toMatchSnapshot();
- expect(
- LinearElementEditor.getElementAbsoluteCoords(
- container,
- h.app.scene.getNonDeletedElementsMap(),
- true,
- ),
- ).toMatchInlineSnapshot(`
- [
- 20,
- 35,
- "501.11716",
- 95,
- "205.45894",
- "52.50000",
- ]
- `);
- });
- it("should resize and position the bound text correctly when 2 pointer linear element resized", () => {
- createTwoPointerLinearElement("arrow");
- const arrow = h.elements[0] as ExcalidrawLinearElement;
- const { textElement, container } = createBoundTextElement(
- DEFAULT_TEXT,
- arrow,
- );
- expect(container.width).toBe(40);
- const elementsMap = arrayToMap(h.elements);
- expect(getBoundTextElementPosition(container, textElement, elementsMap))
- .toMatchInlineSnapshot(`
- {
- "x": 25,
- "y": 10,
- }
- `);
- expect(textElement.text).toMatchSnapshot();
- const points = LinearElementEditor.getPointsGlobalCoordinates(
- container,
- elementsMap,
- );
- // Drag from last point
- drag(points[1], pointFrom(points[1][0] + 300, points[1][1]));
- expect({ width: container.width, height: container.height })
- .toMatchInlineSnapshot(`
- {
- "height": 130,
- "width": 340,
- }
- `);
- expect(getBoundTextElementPosition(container, textElement, elementsMap))
- .toMatchInlineSnapshot(`
- {
- "x": 75,
- "y": -5,
- }
- `);
- expect(textElement.text).toMatchSnapshot();
- });
- it("should not render vertical align tool when element selected", () => {
- createTwoPointerLinearElement("arrow");
- const arrow = h.elements[0] as ExcalidrawLinearElement;
- createBoundTextElement(DEFAULT_TEXT, arrow);
- API.setSelectedElements([arrow]);
- expect(queryByTestId(container, "align-top")).toBeNull();
- expect(queryByTestId(container, "align-middle")).toBeNull();
- expect(queryByTestId(container, "align-bottom")).toBeNull();
- });
- it("should wrap the bound text when arrow bound container moves", async () => {
- const rect = UI.createElement("rectangle", {
- x: 400,
- width: 200,
- height: 500,
- });
- const arrow = UI.createElement("arrow", {
- x: -10,
- y: 250,
- width: 410,
- height: 1,
- });
- mouse.select(arrow);
- Keyboard.keyPress(KEYS.ENTER);
- const editor = await getTextEditor();
- fireEvent.change(editor, { target: { value: DEFAULT_TEXT } });
- Keyboard.exitTextEditor(editor);
- const textElement = h.elements[2] as ExcalidrawTextElementWithContainer;
- expect(arrow.endBinding?.elementId).toBe(rect.id);
- expect(arrow.width).toBeCloseTo(399);
- expect(rect.x).toBe(400);
- expect(rect.y).toBe(0);
- expect(
- wrapText(
- textElement.originalText,
- font,
- getBoundTextMaxWidth(arrow, null),
- ),
- ).toMatchSnapshot();
- const handleBindTextResizeSpy = vi.spyOn(
- textElementUtils,
- "handleBindTextResize",
- );
- mouse.select(rect);
- mouse.downAt(rect.x, rect.y);
- mouse.moveTo(200, 0);
- mouse.upAt(200, 0);
- expect(arrow.width).toBeCloseTo(199);
- expect(rect.x).toBe(200);
- expect(rect.y).toBe(0);
- expect(handleBindTextResizeSpy).toHaveBeenCalledWith(
- h.elements[0],
- h.app.scene,
- "nw",
- false,
- );
- expect(
- wrapText(
- textElement.originalText,
- font,
- getBoundTextMaxWidth(arrow, null),
- ),
- ).toMatchSnapshot();
- });
- it("should not render horizontal align tool when element selected", () => {
- createTwoPointerLinearElement("arrow");
- const arrow = h.elements[0] as ExcalidrawLinearElement;
- createBoundTextElement(DEFAULT_TEXT, arrow);
- API.setSelectedElements([arrow]);
- expect(queryByTestId(container, "align-left")).toBeNull();
- expect(queryByTestId(container, "align-horizontal-center")).toBeNull();
- expect(queryByTestId(container, "align-right")).toBeNull();
- });
- it("should update label coords when a label binded via context menu is unbinded", async () => {
- createTwoPointerLinearElement("arrow");
- const text = API.createElement({
- type: "text",
- text: "Hello Excalidraw",
- });
- expect(text.x).toBe(0);
- expect(text.y).toBe(0);
- API.setElements([h.elements[0], text]);
- const container = h.elements[0];
- API.setSelectedElements([container, text]);
- fireEvent.contextMenu(GlobalTestState.interactiveCanvas, {
- button: 2,
- clientX: 20,
- clientY: 30,
- });
- let contextMenu = document.querySelector(".context-menu");
- fireEvent.click(
- queryByText(contextMenu as HTMLElement, "Bind text to the container")!,
- );
- expect(container.boundElements).toStrictEqual([
- { id: h.elements[1].id, type: "text" },
- ]);
- expect(text.containerId).toBe(container.id);
- expect(text.verticalAlign).toBe(VERTICAL_ALIGN.MIDDLE);
- mouse.reset();
- mouse.clickAt(
- container.x + container.width / 2,
- container.y + container.height / 2,
- );
- mouse.down();
- mouse.up();
- API.setSelectedElements([h.elements[0], h.elements[1]]);
- fireEvent.contextMenu(GlobalTestState.interactiveCanvas, {
- button: 2,
- clientX: 20,
- clientY: 30,
- });
- contextMenu = document.querySelector(".context-menu");
- fireEvent.click(queryByText(contextMenu as HTMLElement, "Unbind text")!);
- expect(container.boundElements).toEqual([]);
- expect(text).toEqual(
- expect.objectContaining({
- containerId: null,
- width: 160,
- height: 25,
- x: -40,
- y: 7.5,
- }),
- );
- });
- it("should not update label position when arrow dragged", () => {
- createTwoPointerLinearElement("arrow");
- let arrow = h.elements[0] as ExcalidrawLinearElement;
- createBoundTextElement(DEFAULT_TEXT, arrow);
- let label = h.elements[1] as ExcalidrawTextElementWithContainer;
- expect(arrow.x).toBe(20);
- expect(arrow.y).toBe(20);
- expect(label.x).toBe(0);
- expect(label.y).toBe(0);
- mouse.reset();
- mouse.select(arrow);
- mouse.select(label);
- mouse.downAt(arrow.x, arrow.y);
- mouse.moveTo(arrow.x + 20, arrow.y + 30);
- mouse.up(arrow.x + 20, arrow.y + 30);
- arrow = h.elements[0] as ExcalidrawLinearElement;
- label = h.elements[1] as ExcalidrawTextElementWithContainer;
- expect(arrow.x).toBe(80);
- expect(arrow.y).toBe(100);
- expect(label.x).toBe(0);
- expect(label.y).toBe(0);
- });
- });
- describe("Test moving linear element points", () => {
- it("should move the endpoint in the negative direction correctly when the start point is also moved in the positive direction", async () => {
- const line = createThreePointerLinearElement("arrow");
- const [origStartX, origStartY] = [line.x, line.y];
- act(() => {
- LinearElementEditor.movePoints(
- line,
- h.app.scene,
- new Map([
- [
- 0,
- {
- point: pointFrom(
- line.points[0][0] + 10,
- line.points[0][1] + 10,
- ),
- },
- ],
- [
- line.points.length - 1,
- {
- point: pointFrom(
- line.points[line.points.length - 1][0] - 10,
- line.points[line.points.length - 1][1] - 10,
- ),
- },
- ],
- ]),
- );
- });
- expect(line.x).toBe(origStartX + 10);
- expect(line.y).toBe(origStartY + 10);
- expect(line.points[line.points.length - 1][0]).toBe(20);
- expect(line.points[line.points.length - 1][1]).toBe(-20);
- });
- it("should preserve original angle when dragging endpoint with SHIFT key", () => {
- createTwoPointerLinearElement("line");
- const line = h.elements[0] as ExcalidrawLinearElement;
- enterLineEditingMode(line);
- const elementsMap = arrayToMap(h.elements);
- const points = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- // Calculate original angle between first and last point
- const originalAngle = Math.atan2(
- points[1][1] - points[0][1],
- points[1][0] - points[0][0],
- );
- // Drag the second point (endpoint) with SHIFT key pressed
- const startPoint = pointFrom<GlobalPoint>(points[1][0], points[1][1]);
- const endPoint = pointFrom<GlobalPoint>(
- startPoint[0] + 4,
- startPoint[1] + 4,
- );
- // Perform drag with SHIFT key modifier
- Keyboard.withModifierKeys({ shift: true }, () => {
- mouse.downAt(startPoint[0], startPoint[1]);
- mouse.moveTo(endPoint[0], endPoint[1]);
- mouse.upAt(endPoint[0], endPoint[1]);
- });
- // Get updated points after drag
- const updatedPoints = LinearElementEditor.getPointsGlobalCoordinates(
- line,
- elementsMap,
- );
- // Calculate new angle
- const newAngle = Math.atan2(
- updatedPoints[1][1] - updatedPoints[0][1],
- updatedPoints[1][0] - updatedPoints[0][0],
- );
- // The angle should be preserved (within a small tolerance for floating point precision)
- const angleDifference = Math.abs(newAngle - originalAngle);
- const tolerance = 0.01; // Small tolerance for floating point precision
- expect(angleDifference).toBeLessThan(tolerance);
- });
- });
- });
|