elbowArrow.test.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. import { ARROW_TYPE } from "@excalidraw/common";
  2. import { pointFrom } from "@excalidraw/math";
  3. import { Excalidraw } from "@excalidraw/excalidraw";
  4. import { actionSelectAll } from "@excalidraw/excalidraw/actions";
  5. import { actionDuplicateSelection } from "@excalidraw/excalidraw/actions/actionDuplicateSelection";
  6. import { API } from "@excalidraw/excalidraw/tests/helpers/api";
  7. import { Pointer, UI } from "@excalidraw/excalidraw/tests/helpers/ui";
  8. import {
  9. act,
  10. fireEvent,
  11. GlobalTestState,
  12. queryByTestId,
  13. render,
  14. } from "@excalidraw/excalidraw/tests/test-utils";
  15. import "@excalidraw/utils/test-utils";
  16. import { bindBindingElement } from "@excalidraw/element";
  17. import type { LocalPoint } from "@excalidraw/math";
  18. import { Scene } from "../src/Scene";
  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. beforeEach(async () => {
  112. localStorage.clear();
  113. await render(<Excalidraw handleKeyboardGlobally={true} />);
  114. });
  115. it("can properly generate orthogonal arrow points", () => {
  116. const scene = new Scene();
  117. const arrow = API.createElement({
  118. type: "arrow",
  119. elbowed: true,
  120. }) as ExcalidrawElbowArrowElement;
  121. scene.insertElement(arrow);
  122. h.app.scene.mutateElement(arrow, {
  123. points: [
  124. pointFrom<LocalPoint>(-45 - arrow.x, -100.1 - arrow.y),
  125. pointFrom<LocalPoint>(45 - arrow.x, 99.9 - arrow.y),
  126. ],
  127. });
  128. expect(arrow.points).toEqual([
  129. [0, 0],
  130. [0, 100],
  131. [90, 100],
  132. [90, 200],
  133. ]);
  134. expect(arrow.x).toEqual(-45);
  135. expect(arrow.y).toEqual(-100.1);
  136. expect(arrow.width).toEqual(90);
  137. expect(arrow.height).toEqual(200);
  138. });
  139. it("can generate proper points for bound elbow arrow", () => {
  140. const rectangle1 = API.createElement({
  141. type: "rectangle",
  142. x: -150,
  143. y: -150,
  144. width: 100,
  145. height: 100,
  146. }) as ExcalidrawBindableElement;
  147. const rectangle2 = API.createElement({
  148. type: "rectangle",
  149. x: 50,
  150. y: 50,
  151. width: 100,
  152. height: 100,
  153. }) as ExcalidrawBindableElement;
  154. const arrow = API.createElement({
  155. type: "arrow",
  156. elbowed: true,
  157. x: -45,
  158. y: -100.1,
  159. width: 90,
  160. height: 200,
  161. points: [pointFrom(0, 0), pointFrom(90, 200)],
  162. }) as ExcalidrawElbowArrowElement;
  163. API.setElements([rectangle1, rectangle2, arrow]);
  164. bindBindingElement(arrow, rectangle1, "orbit", "start", h.scene);
  165. bindBindingElement(arrow, rectangle2, "orbit", "end", h.scene);
  166. expect(arrow.startBinding).not.toBe(null);
  167. expect(arrow.endBinding).not.toBe(null);
  168. h.scene.mutateElement(arrow, {
  169. points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(90, 200)],
  170. });
  171. expect(arrow.points).toCloselyEqualPoints([
  172. [0, 0],
  173. [39, 0],
  174. [39, 200],
  175. [78, 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(-53, -99);
  209. mouse.click();
  210. mouse.moveTo(53, 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).toCloselyEqualPoints([
  218. [0, 0],
  219. [39, 0],
  220. [39, 200],
  221. [78, 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(-53, -99);
  241. mouse.click();
  242. mouse.moveTo(53, 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. [36, 0],
  255. [36, 90],
  256. [28, 90],
  257. [28, 164],
  258. [101, 164],
  259. ]);
  260. });
  261. it("keeps arrow shape when the whole set of arrow and bindables are duplicated", async () => {
  262. UI.createElement("rectangle", {
  263. x: -150,
  264. y: -150,
  265. width: 100,
  266. height: 100,
  267. });
  268. UI.createElement("rectangle", {
  269. x: 50,
  270. y: 50,
  271. width: 100,
  272. height: 100,
  273. });
  274. UI.clickTool("arrow");
  275. UI.clickOnTestId("elbow-arrow");
  276. mouse.reset();
  277. mouse.moveTo(-53, -99);
  278. mouse.click();
  279. mouse.moveTo(53, 99);
  280. mouse.click();
  281. const arrow = h.scene.getSelectedElements(
  282. h.state,
  283. )[0] as ExcalidrawArrowElement;
  284. const originalArrowId = arrow.id;
  285. expect(arrow.startBinding).not.toBe(null);
  286. expect(arrow.endBinding).not.toBe(null);
  287. act(() => {
  288. h.app.actionManager.executeAction(actionSelectAll);
  289. });
  290. act(() => {
  291. h.app.actionManager.executeAction(actionDuplicateSelection);
  292. });
  293. expect(h.elements.length).toEqual(6);
  294. const duplicatedArrow = h.scene.getSelectedElements(
  295. h.state,
  296. )[2] as ExcalidrawArrowElement;
  297. expect(duplicatedArrow.id).not.toBe(originalArrowId);
  298. expect(duplicatedArrow.type).toBe("arrow");
  299. expect(duplicatedArrow.elbowed).toBe(true);
  300. expect(duplicatedArrow.points).toCloselyEqualPoints([
  301. [0, 0],
  302. [39, 0],
  303. [39, 200],
  304. [78, 200],
  305. ]);
  306. expect(arrow.startBinding).not.toBe(null);
  307. expect(arrow.endBinding).not.toBe(null);
  308. });
  309. it("changes arrow shape to unbind variant if only the connected elbow arrow is duplicated", async () => {
  310. UI.createElement("rectangle", {
  311. x: -150,
  312. y: -150,
  313. width: 100,
  314. height: 100,
  315. });
  316. UI.createElement("rectangle", {
  317. x: 50,
  318. y: 50,
  319. width: 100,
  320. height: 100,
  321. });
  322. UI.clickTool("arrow");
  323. UI.clickOnTestId("elbow-arrow");
  324. mouse.reset();
  325. mouse.moveTo(-53, -99);
  326. mouse.click();
  327. mouse.moveTo(53, 99);
  328. mouse.click();
  329. const arrow = h.scene.getSelectedElements(
  330. h.state,
  331. )[0] as ExcalidrawArrowElement;
  332. const originalArrowId = arrow.id;
  333. expect(arrow.startBinding).not.toBe(null);
  334. expect(arrow.endBinding).not.toBe(null);
  335. act(() => {
  336. h.app.actionManager.executeAction(actionDuplicateSelection);
  337. });
  338. expect(h.elements.length).toEqual(4);
  339. const duplicatedArrow = h.scene.getSelectedElements(
  340. h.state,
  341. )[0] as ExcalidrawArrowElement;
  342. expect(duplicatedArrow.id).not.toBe(originalArrowId);
  343. expect(duplicatedArrow.type).toBe("arrow");
  344. expect(duplicatedArrow.elbowed).toBe(true);
  345. expect(duplicatedArrow.points).toCloselyEqualPoints([
  346. [0, 0],
  347. [0, 100],
  348. [78, 100],
  349. [78, 200],
  350. ]);
  351. });
  352. });