elbowArrow.test.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import { ARROW_TYPE } from "@excalidraw/common";
  2. import { pointFrom } from "@excalidraw/math";
  3. import { Excalidraw, mutateElement } from "@excalidraw/excalidraw";
  4. import Scene from "@excalidraw/excalidraw/scene/Scene";
  5. import { actionSelectAll } from "@excalidraw/excalidraw/actions";
  6. import { actionDuplicateSelection } from "@excalidraw/excalidraw/actions/actionDuplicateSelection";
  7. import { API } from "@excalidraw/excalidraw/tests/helpers/api";
  8. import { Pointer, UI } from "@excalidraw/excalidraw/tests/helpers/ui";
  9. import {
  10. act,
  11. fireEvent,
  12. GlobalTestState,
  13. queryByTestId,
  14. render,
  15. } from "@excalidraw/excalidraw/tests/test-utils";
  16. import "@excalidraw/utils/test-utils";
  17. import type { LocalPoint } from "@excalidraw/math";
  18. import { bindLinearElement } from "../src/binding";
  19. import type {
  20. ExcalidrawArrowElement,
  21. ExcalidrawBindableElement,
  22. ExcalidrawElbowArrowElement,
  23. } from "../src/types";
  24. const { h } = window;
  25. const mouse = new Pointer("mouse");
  26. describe("elbow arrow segment move", () => {
  27. beforeEach(async () => {
  28. localStorage.clear();
  29. await render(<Excalidraw handleKeyboardGlobally={true} />);
  30. });
  31. it("can move the second segment of a fully connected elbow arrow", () => {
  32. UI.createElement("rectangle", {
  33. x: -100,
  34. y: -50,
  35. width: 100,
  36. height: 100,
  37. });
  38. UI.createElement("rectangle", {
  39. x: 200,
  40. y: 150,
  41. width: 100,
  42. height: 100,
  43. });
  44. UI.clickTool("arrow");
  45. UI.clickOnTestId("elbow-arrow");
  46. mouse.reset();
  47. mouse.moveTo(0, 0);
  48. mouse.click();
  49. mouse.moveTo(200, 200);
  50. mouse.click();
  51. mouse.reset();
  52. mouse.moveTo(100, 100);
  53. mouse.down();
  54. mouse.moveTo(115, 100);
  55. mouse.up();
  56. const arrow = h.scene.getSelectedElements(
  57. h.state,
  58. )[0] as ExcalidrawElbowArrowElement;
  59. expect(h.state.selectedElementIds).toEqual({ [arrow.id]: true });
  60. expect(arrow.fixedSegments?.length).toBe(1);
  61. expect(arrow.points).toCloselyEqualPoints([
  62. [0, 0],
  63. [110, 0],
  64. [110, 200],
  65. [190, 200],
  66. ]);
  67. mouse.reset();
  68. mouse.moveTo(105, 74.275);
  69. mouse.doubleClick();
  70. expect(arrow.points).toCloselyEqualPoints([
  71. [0, 0],
  72. [110, 0],
  73. [110, 200],
  74. [190, 200],
  75. ]);
  76. });
  77. it("can move the second segment of an unconnected elbow arrow", () => {
  78. UI.clickTool("arrow");
  79. UI.clickOnTestId("elbow-arrow");
  80. mouse.reset();
  81. mouse.moveTo(0, 0);
  82. mouse.click();
  83. mouse.moveTo(250, 200);
  84. mouse.click();
  85. mouse.reset();
  86. mouse.moveTo(125, 100);
  87. mouse.down();
  88. mouse.moveTo(130, 100);
  89. mouse.up();
  90. const arrow = h.scene.getSelectedElements(
  91. h.state,
  92. )[0] as ExcalidrawArrowElement;
  93. expect(arrow.points).toCloselyEqualPoints([
  94. [0, 0],
  95. [130, 0],
  96. [130, 200],
  97. [250, 200],
  98. ]);
  99. mouse.reset();
  100. mouse.moveTo(130, 100);
  101. mouse.doubleClick();
  102. expect(arrow.points).toCloselyEqualPoints([
  103. [0, 0],
  104. [125, 0],
  105. [125, 200],
  106. [250, 200],
  107. ]);
  108. });
  109. });
  110. describe("elbow arrow routing", () => {
  111. it("can properly generate orthogonal arrow points", () => {
  112. const scene = new Scene();
  113. const arrow = API.createElement({
  114. type: "arrow",
  115. elbowed: true,
  116. }) as ExcalidrawElbowArrowElement;
  117. scene.insertElement(arrow);
  118. mutateElement(arrow, {
  119. points: [
  120. pointFrom<LocalPoint>(-45 - arrow.x, -100.1 - arrow.y),
  121. pointFrom<LocalPoint>(45 - arrow.x, 99.9 - arrow.y),
  122. ],
  123. });
  124. expect(arrow.points).toEqual([
  125. [0, 0],
  126. [0, 100],
  127. [90, 100],
  128. [90, 200],
  129. ]);
  130. expect(arrow.x).toEqual(-45);
  131. expect(arrow.y).toEqual(-100.1);
  132. expect(arrow.width).toEqual(90);
  133. expect(arrow.height).toEqual(200);
  134. });
  135. it("can generate proper points for bound elbow arrow", () => {
  136. const scene = new Scene();
  137. const rectangle1 = API.createElement({
  138. type: "rectangle",
  139. x: -150,
  140. y: -150,
  141. width: 100,
  142. height: 100,
  143. }) as ExcalidrawBindableElement;
  144. const rectangle2 = API.createElement({
  145. type: "rectangle",
  146. x: 50,
  147. y: 50,
  148. width: 100,
  149. height: 100,
  150. }) as ExcalidrawBindableElement;
  151. const arrow = API.createElement({
  152. type: "arrow",
  153. elbowed: true,
  154. x: -45,
  155. y: -100.1,
  156. width: 90,
  157. height: 200,
  158. points: [pointFrom(0, 0), pointFrom(90, 200)],
  159. }) as ExcalidrawElbowArrowElement;
  160. scene.insertElement(rectangle1);
  161. scene.insertElement(rectangle2);
  162. scene.insertElement(arrow);
  163. const elementsMap = scene.getNonDeletedElementsMap();
  164. bindLinearElement(arrow, rectangle1, "start", elementsMap);
  165. bindLinearElement(arrow, rectangle2, "end", elementsMap);
  166. expect(arrow.startBinding).not.toBe(null);
  167. expect(arrow.endBinding).not.toBe(null);
  168. mutateElement(arrow, {
  169. points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(90, 200)],
  170. });
  171. expect(arrow.points).toEqual([
  172. [0, 0],
  173. [45, 0],
  174. [45, 200],
  175. [90, 200],
  176. ]);
  177. });
  178. });
  179. describe("elbow arrow ui", () => {
  180. beforeEach(async () => {
  181. localStorage.clear();
  182. await render(<Excalidraw handleKeyboardGlobally={true} />);
  183. fireEvent.contextMenu(GlobalTestState.interactiveCanvas, {
  184. button: 2,
  185. clientX: 1,
  186. clientY: 1,
  187. });
  188. const contextMenu = UI.queryContextMenu();
  189. fireEvent.click(queryByTestId(contextMenu!, "stats")!);
  190. });
  191. it("can follow bound shapes", async () => {
  192. UI.createElement("rectangle", {
  193. x: -150,
  194. y: -150,
  195. width: 100,
  196. height: 100,
  197. });
  198. UI.createElement("rectangle", {
  199. x: 50,
  200. y: 50,
  201. width: 100,
  202. height: 100,
  203. });
  204. UI.clickTool("arrow");
  205. UI.clickOnTestId("elbow-arrow");
  206. expect(h.state.currentItemArrowType).toBe(ARROW_TYPE.elbow);
  207. mouse.reset();
  208. mouse.moveTo(-43, -99);
  209. mouse.click();
  210. mouse.moveTo(43, 99);
  211. mouse.click();
  212. const arrow = h.scene.getSelectedElements(
  213. h.state,
  214. )[0] as ExcalidrawArrowElement;
  215. expect(arrow.type).toBe("arrow");
  216. expect(arrow.elbowed).toBe(true);
  217. expect(arrow.points).toEqual([
  218. [0, 0],
  219. [45, 0],
  220. [45, 200],
  221. [90, 200],
  222. ]);
  223. });
  224. it("can follow bound rotated shapes", async () => {
  225. UI.createElement("rectangle", {
  226. x: -150,
  227. y: -150,
  228. width: 100,
  229. height: 100,
  230. });
  231. UI.createElement("rectangle", {
  232. x: 50,
  233. y: 50,
  234. width: 100,
  235. height: 100,
  236. });
  237. UI.clickTool("arrow");
  238. UI.clickOnTestId("elbow-arrow");
  239. mouse.reset();
  240. mouse.moveTo(-43, -99);
  241. mouse.click();
  242. mouse.moveTo(43, 99);
  243. mouse.click();
  244. const arrow = h.scene.getSelectedElements(
  245. h.state,
  246. )[0] as ExcalidrawArrowElement;
  247. mouse.click(51, 51);
  248. const inputAngle = UI.queryStatsProperty("A")?.querySelector(
  249. ".drag-input",
  250. ) as HTMLInputElement;
  251. UI.updateInput(inputAngle, String("40"));
  252. expect(arrow.points.map((point) => point.map(Math.round))).toEqual([
  253. [0, 0],
  254. [35, 0],
  255. [35, 165],
  256. [103, 165],
  257. ]);
  258. });
  259. it("keeps arrow shape when the whole set of arrow and bindables are duplicated", async () => {
  260. UI.createElement("rectangle", {
  261. x: -150,
  262. y: -150,
  263. width: 100,
  264. height: 100,
  265. });
  266. UI.createElement("rectangle", {
  267. x: 50,
  268. y: 50,
  269. width: 100,
  270. height: 100,
  271. });
  272. UI.clickTool("arrow");
  273. UI.clickOnTestId("elbow-arrow");
  274. mouse.reset();
  275. mouse.moveTo(-43, -99);
  276. mouse.click();
  277. mouse.moveTo(43, 99);
  278. mouse.click();
  279. const arrow = h.scene.getSelectedElements(
  280. h.state,
  281. )[0] as ExcalidrawArrowElement;
  282. const originalArrowId = arrow.id;
  283. expect(arrow.startBinding).not.toBe(null);
  284. expect(arrow.endBinding).not.toBe(null);
  285. act(() => {
  286. h.app.actionManager.executeAction(actionSelectAll);
  287. });
  288. act(() => {
  289. h.app.actionManager.executeAction(actionDuplicateSelection);
  290. });
  291. expect(h.elements.length).toEqual(6);
  292. const duplicatedArrow = h.scene.getSelectedElements(
  293. h.state,
  294. )[2] as ExcalidrawArrowElement;
  295. expect(duplicatedArrow.id).not.toBe(originalArrowId);
  296. expect(duplicatedArrow.type).toBe("arrow");
  297. expect(duplicatedArrow.elbowed).toBe(true);
  298. expect(duplicatedArrow.points).toEqual([
  299. [0, 0],
  300. [45, 0],
  301. [45, 200],
  302. [90, 200],
  303. ]);
  304. expect(arrow.startBinding).not.toBe(null);
  305. expect(arrow.endBinding).not.toBe(null);
  306. });
  307. it("changes arrow shape to unbind variant if only the connected elbow arrow is duplicated", async () => {
  308. UI.createElement("rectangle", {
  309. x: -150,
  310. y: -150,
  311. width: 100,
  312. height: 100,
  313. });
  314. UI.createElement("rectangle", {
  315. x: 50,
  316. y: 50,
  317. width: 100,
  318. height: 100,
  319. });
  320. UI.clickTool("arrow");
  321. UI.clickOnTestId("elbow-arrow");
  322. mouse.reset();
  323. mouse.moveTo(-43, -99);
  324. mouse.click();
  325. mouse.moveTo(43, 99);
  326. mouse.click();
  327. const arrow = h.scene.getSelectedElements(
  328. h.state,
  329. )[0] as ExcalidrawArrowElement;
  330. const originalArrowId = arrow.id;
  331. expect(arrow.startBinding).not.toBe(null);
  332. expect(arrow.endBinding).not.toBe(null);
  333. act(() => {
  334. h.app.actionManager.executeAction(actionDuplicateSelection);
  335. });
  336. expect(h.elements.length).toEqual(4);
  337. const duplicatedArrow = h.scene.getSelectedElements(
  338. h.state,
  339. )[0] as ExcalidrawArrowElement;
  340. expect(duplicatedArrow.id).not.toBe(originalArrowId);
  341. expect(duplicatedArrow.type).toBe("arrow");
  342. expect(duplicatedArrow.elbowed).toBe(true);
  343. expect(duplicatedArrow.points).toEqual([
  344. [0, 0],
  345. [0, 100],
  346. [90, 100],
  347. [90, 200],
  348. ]);
  349. });
  350. });