binding.test.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. import { KEYS, arrayToMap } from "@excalidraw/common";
  2. import { pointFrom } from "@excalidraw/math";
  3. import { actionWrapTextInContainer } from "@excalidraw/excalidraw/actions/actionBoundText";
  4. import { Excalidraw, isLinearElement } from "@excalidraw/excalidraw";
  5. import { API } from "@excalidraw/excalidraw/tests/helpers/api";
  6. import { UI, Pointer, Keyboard } from "@excalidraw/excalidraw/tests/helpers/ui";
  7. import { fireEvent, render } from "@excalidraw/excalidraw/tests/test-utils";
  8. import { getTransformHandles } from "../src/transformHandles";
  9. const { h } = window;
  10. const mouse = new Pointer("mouse");
  11. describe("element binding", () => {
  12. beforeEach(async () => {
  13. await render(<Excalidraw handleKeyboardGlobally={true} />);
  14. });
  15. it("should create valid binding if duplicate start/end points", async () => {
  16. const rect = API.createElement({
  17. type: "rectangle",
  18. x: 0,
  19. y: 0,
  20. width: 50,
  21. height: 50,
  22. });
  23. const arrow = API.createElement({
  24. type: "arrow",
  25. x: 100,
  26. y: 0,
  27. width: 100,
  28. height: 1,
  29. points: [
  30. pointFrom(0, 0),
  31. pointFrom(0, 0),
  32. pointFrom(100, 0),
  33. pointFrom(100, 0),
  34. ],
  35. });
  36. API.setElements([rect, arrow]);
  37. expect(arrow.startBinding).toBe(null);
  38. // select arrow
  39. mouse.clickAt(150, 0);
  40. // move arrow start to potential binding position
  41. mouse.downAt(100, 0);
  42. mouse.moveTo(55, 0);
  43. mouse.up(0, 0);
  44. // Point selection is evaluated like the points are rendered,
  45. // from right to left. So clicking on the first point should move the joint,
  46. // not the start point.
  47. expect(arrow.startBinding).toBe(null);
  48. // Now that the start point is free, move it into overlapping position
  49. mouse.downAt(100, 0);
  50. mouse.moveTo(55, 0);
  51. mouse.up(0, 0);
  52. expect(API.getSelectedElements()).toEqual([arrow]);
  53. expect(arrow.startBinding).toEqual({
  54. elementId: rect.id,
  55. focus: expect.toBeNonNaNNumber(),
  56. gap: expect.toBeNonNaNNumber(),
  57. });
  58. // Move the end point to the overlapping binding position
  59. mouse.downAt(200, 0);
  60. mouse.moveTo(55, 0);
  61. mouse.up(0, 0);
  62. // Both the start and the end points should be bound
  63. expect(arrow.startBinding).toEqual({
  64. elementId: rect.id,
  65. focus: expect.toBeNonNaNNumber(),
  66. gap: expect.toBeNonNaNNumber(),
  67. });
  68. expect(arrow.endBinding).toEqual({
  69. elementId: rect.id,
  70. focus: expect.toBeNonNaNNumber(),
  71. gap: expect.toBeNonNaNNumber(),
  72. });
  73. });
  74. //@TODO fix the test with rotation
  75. it.skip("rotation of arrow should rebind both ends", () => {
  76. const rectLeft = UI.createElement("rectangle", {
  77. x: 0,
  78. width: 200,
  79. height: 500,
  80. });
  81. const rectRight = UI.createElement("rectangle", {
  82. x: 400,
  83. width: 200,
  84. height: 500,
  85. });
  86. const arrow = UI.createElement("arrow", {
  87. x: 210,
  88. y: 250,
  89. width: 180,
  90. height: 1,
  91. });
  92. expect(arrow.startBinding?.elementId).toBe(rectLeft.id);
  93. expect(arrow.endBinding?.elementId).toBe(rectRight.id);
  94. const rotation = getTransformHandles(
  95. arrow,
  96. h.state.zoom,
  97. arrayToMap(h.elements),
  98. "mouse",
  99. ).rotation!;
  100. const rotationHandleX = rotation[0] + rotation[2] / 2;
  101. const rotationHandleY = rotation[1] + rotation[3] / 2;
  102. mouse.down(rotationHandleX, rotationHandleY);
  103. mouse.move(300, 400);
  104. mouse.up();
  105. expect(arrow.angle).toBeGreaterThan(0.7 * Math.PI);
  106. expect(arrow.angle).toBeLessThan(1.3 * Math.PI);
  107. expect(arrow.startBinding?.elementId).toBe(rectRight.id);
  108. expect(arrow.endBinding?.elementId).toBe(rectLeft.id);
  109. });
  110. // TODO fix & reenable once we rewrite tests to work with concurrency
  111. it.skip(
  112. "editing arrow and moving its head to bind it to element A, finalizing the" +
  113. "editing by clicking on element A should end up selecting A",
  114. async () => {
  115. UI.createElement("rectangle", {
  116. y: 0,
  117. size: 100,
  118. });
  119. // Create arrow bound to rectangle
  120. UI.clickTool("arrow");
  121. mouse.down(50, -100);
  122. mouse.up(0, 80);
  123. // Edit arrow with multi-point
  124. mouse.doubleClick();
  125. // move arrow head
  126. mouse.down();
  127. mouse.up(0, 10);
  128. expect(API.getSelectedElement().type).toBe("arrow");
  129. // NOTE this mouse down/up + await needs to be done in order to repro
  130. // the issue, due to https://github.com/excalidraw/excalidraw/blob/46bff3daceb602accf60c40a84610797260fca94/src/components/App.tsx#L740
  131. mouse.reset();
  132. expect(h.state.editingLinearElement).not.toBe(null);
  133. mouse.down(0, 0);
  134. await new Promise((r) => setTimeout(r, 100));
  135. expect(h.state.editingLinearElement).toBe(null);
  136. expect(API.getSelectedElement().type).toBe("rectangle");
  137. mouse.up();
  138. expect(API.getSelectedElement().type).toBe("rectangle");
  139. },
  140. );
  141. it("should unbind arrow when moving it with keyboard", () => {
  142. const rectangle = UI.createElement("rectangle", {
  143. x: 75,
  144. y: 0,
  145. size: 100,
  146. });
  147. // Creates arrow 1px away from bidding with rectangle
  148. const arrow = UI.createElement("arrow", {
  149. x: 0,
  150. y: 0,
  151. size: 50,
  152. });
  153. expect(arrow.endBinding).toBe(null);
  154. mouse.downAt(50, 50);
  155. mouse.moveTo(51, 0);
  156. mouse.up(0, 0);
  157. // Test sticky connection
  158. expect(API.getSelectedElement().type).toBe("arrow");
  159. Keyboard.keyPress(KEYS.ARROW_RIGHT);
  160. expect(arrow.endBinding?.elementId).toBe(rectangle.id);
  161. Keyboard.keyPress(KEYS.ARROW_LEFT);
  162. expect(arrow.endBinding?.elementId).toBe(rectangle.id);
  163. // Sever connection
  164. expect(API.getSelectedElement().type).toBe("arrow");
  165. Keyboard.keyPress(KEYS.ARROW_LEFT);
  166. expect(arrow.endBinding).toBe(null);
  167. Keyboard.keyPress(KEYS.ARROW_RIGHT);
  168. expect(arrow.endBinding).toBe(null);
  169. });
  170. it("should unbind on bound element deletion", () => {
  171. const rectangle = UI.createElement("rectangle", {
  172. x: 60,
  173. y: 0,
  174. size: 100,
  175. });
  176. const arrow = UI.createElement("arrow", {
  177. x: 0,
  178. y: 0,
  179. size: 50,
  180. });
  181. expect(arrow.endBinding?.elementId).toBe(rectangle.id);
  182. mouse.select(rectangle);
  183. expect(API.getSelectedElement().type).toBe("rectangle");
  184. Keyboard.keyDown(KEYS.DELETE);
  185. expect(arrow.endBinding).toBe(null);
  186. });
  187. it("should unbind on text element deletion by submitting empty text", async () => {
  188. const text = API.createElement({
  189. type: "text",
  190. text: "ola",
  191. x: 60,
  192. y: 0,
  193. width: 100,
  194. height: 100,
  195. });
  196. API.setElements([text]);
  197. const arrow = UI.createElement("arrow", {
  198. x: 0,
  199. y: 0,
  200. size: 50,
  201. });
  202. expect(arrow.endBinding?.elementId).toBe(text.id);
  203. // edit text element and submit
  204. // -------------------------------------------------------------------------
  205. UI.clickTool("text");
  206. mouse.clickAt(text.x + 50, text.y + 50);
  207. const editor = document.querySelector(
  208. ".excalidraw-textEditorContainer > textarea",
  209. ) as HTMLTextAreaElement;
  210. expect(editor).not.toBe(null);
  211. fireEvent.change(editor, { target: { value: "" } });
  212. fireEvent.keyDown(editor, { key: KEYS.ESCAPE });
  213. expect(
  214. document.querySelector(".excalidraw-textEditorContainer > textarea"),
  215. ).toBe(null);
  216. expect(arrow.endBinding).toBe(null);
  217. });
  218. it("should keep binding on text update", async () => {
  219. const text = API.createElement({
  220. type: "text",
  221. text: "ola",
  222. x: 60,
  223. y: 0,
  224. width: 100,
  225. height: 100,
  226. });
  227. API.setElements([text]);
  228. const arrow = UI.createElement("arrow", {
  229. x: 0,
  230. y: 0,
  231. size: 50,
  232. });
  233. expect(arrow.endBinding?.elementId).toBe(text.id);
  234. // delete text element by submitting empty text
  235. // -------------------------------------------------------------------------
  236. UI.clickTool("text");
  237. mouse.clickAt(text.x + 50, text.y + 50);
  238. const editor = document.querySelector(
  239. ".excalidraw-textEditorContainer > textarea",
  240. ) as HTMLTextAreaElement;
  241. expect(editor).not.toBe(null);
  242. fireEvent.change(editor, { target: { value: "asdasdasdasdas" } });
  243. fireEvent.keyDown(editor, { key: KEYS.ESCAPE });
  244. expect(
  245. document.querySelector(".excalidraw-textEditorContainer > textarea"),
  246. ).toBe(null);
  247. expect(arrow.endBinding?.elementId).toBe(text.id);
  248. });
  249. it("should update binding when text containerized", async () => {
  250. const rectangle1 = API.createElement({
  251. type: "rectangle",
  252. id: "rectangle1",
  253. width: 100,
  254. height: 100,
  255. boundElements: [
  256. { id: "arrow1", type: "arrow" },
  257. { id: "arrow2", type: "arrow" },
  258. ],
  259. });
  260. const arrow1 = API.createElement({
  261. type: "arrow",
  262. id: "arrow1",
  263. points: [pointFrom(0, 0), pointFrom(0, -87.45777932247563)],
  264. startBinding: {
  265. elementId: "rectangle1",
  266. focus: 0.2,
  267. gap: 7,
  268. fixedPoint: [0.5, 1],
  269. },
  270. endBinding: {
  271. elementId: "text1",
  272. focus: 0.2,
  273. gap: 7,
  274. fixedPoint: [1, 0.5],
  275. },
  276. });
  277. const arrow2 = API.createElement({
  278. type: "arrow",
  279. id: "arrow2",
  280. points: [pointFrom(0, 0), pointFrom(0, -87.45777932247563)],
  281. startBinding: {
  282. elementId: "text1",
  283. focus: 0.2,
  284. gap: 7,
  285. fixedPoint: [0.5, 1],
  286. },
  287. endBinding: {
  288. elementId: "rectangle1",
  289. focus: 0.2,
  290. gap: 7,
  291. fixedPoint: [1, 0.5],
  292. },
  293. });
  294. const text1 = API.createElement({
  295. type: "text",
  296. id: "text1",
  297. text: "ola",
  298. boundElements: [
  299. { id: "arrow1", type: "arrow" },
  300. { id: "arrow2", type: "arrow" },
  301. ],
  302. });
  303. API.setElements([rectangle1, arrow1, arrow2, text1]);
  304. API.setSelectedElements([text1]);
  305. expect(h.state.selectedElementIds[text1.id]).toBe(true);
  306. API.executeAction(actionWrapTextInContainer);
  307. // new text container will be placed before the text element
  308. const container = h.elements.at(-2)!;
  309. expect(container.type).toBe("rectangle");
  310. expect(container.id).not.toBe(rectangle1.id);
  311. expect(container).toEqual(
  312. expect.objectContaining({
  313. boundElements: expect.arrayContaining([
  314. {
  315. type: "text",
  316. id: text1.id,
  317. },
  318. {
  319. type: "arrow",
  320. id: arrow1.id,
  321. },
  322. {
  323. type: "arrow",
  324. id: arrow2.id,
  325. },
  326. ]),
  327. }),
  328. );
  329. expect(arrow1.startBinding?.elementId).toBe(rectangle1.id);
  330. expect(arrow1.endBinding?.elementId).toBe(container.id);
  331. expect(arrow2.startBinding?.elementId).toBe(container.id);
  332. expect(arrow2.endBinding?.elementId).toBe(rectangle1.id);
  333. });
  334. // #6459
  335. it("should unbind arrow only from the latest element", () => {
  336. const rectLeft = UI.createElement("rectangle", {
  337. x: 0,
  338. width: 200,
  339. height: 500,
  340. });
  341. const rectRight = UI.createElement("rectangle", {
  342. x: 400,
  343. width: 200,
  344. height: 500,
  345. });
  346. const arrow = UI.createElement("arrow", {
  347. x: 210,
  348. y: 250,
  349. width: 180,
  350. height: 1,
  351. });
  352. expect(arrow.startBinding?.elementId).toBe(rectLeft.id);
  353. expect(arrow.endBinding?.elementId).toBe(rectRight.id);
  354. // Drag arrow off of bound rectangle range
  355. const handles = getTransformHandles(
  356. arrow,
  357. h.state.zoom,
  358. arrayToMap(h.elements),
  359. "mouse",
  360. ).se!;
  361. Keyboard.keyDown(KEYS.CTRL_OR_CMD);
  362. const elX = handles[0] + handles[2] / 2;
  363. const elY = handles[1] + handles[3] / 2;
  364. mouse.downAt(elX, elY);
  365. mouse.moveTo(300, 400);
  366. mouse.up();
  367. expect(arrow.startBinding).not.toBe(null);
  368. expect(arrow.endBinding).toBe(null);
  369. });
  370. it("should not unbind when duplicating via selection group", () => {
  371. const rectLeft = UI.createElement("rectangle", {
  372. x: 0,
  373. width: 200,
  374. height: 500,
  375. });
  376. const rectRight = UI.createElement("rectangle", {
  377. x: 400,
  378. y: 200,
  379. width: 200,
  380. height: 500,
  381. });
  382. const arrow = UI.createElement("arrow", {
  383. x: 210,
  384. y: 250,
  385. width: 177,
  386. height: 1,
  387. });
  388. expect(arrow.startBinding?.elementId).toBe(rectLeft.id);
  389. expect(arrow.endBinding?.elementId).toBe(rectRight.id);
  390. mouse.downAt(-100, -100);
  391. mouse.moveTo(650, 750);
  392. mouse.up(0, 0);
  393. expect(API.getSelectedElements().length).toBe(3);
  394. mouse.moveTo(5, 5);
  395. Keyboard.withModifierKeys({ alt: true }, () => {
  396. mouse.downAt(5, 5);
  397. mouse.moveTo(1000, 1000);
  398. mouse.up(0, 0);
  399. expect(window.h.elements.length).toBe(6);
  400. window.h.elements.forEach((element) => {
  401. if (isLinearElement(element)) {
  402. expect(element.startBinding).not.toBe(null);
  403. expect(element.endBinding).not.toBe(null);
  404. } else {
  405. expect(element.boundElements).not.toBe(null);
  406. }
  407. });
  408. });
  409. });
  410. });