elementLocking.test.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import ReactDOM from "react-dom";
  2. import ExcalidrawApp from "../excalidraw-app";
  3. import { render } from "../tests/test-utils";
  4. import { Keyboard, Pointer, UI } from "../tests/helpers/ui";
  5. import { KEYS } from "../keys";
  6. import { API } from "../tests/helpers/api";
  7. import { actionSelectAll } from "../actions";
  8. import { t } from "../i18n";
  9. import { mutateElement } from "../element/mutateElement";
  10. ReactDOM.unmountComponentAtNode(document.getElementById("root")!);
  11. const mouse = new Pointer("mouse");
  12. const h = window.h;
  13. describe("element locking", () => {
  14. beforeEach(async () => {
  15. await render(<ExcalidrawApp />);
  16. h.elements = [];
  17. });
  18. it("click-selecting a locked element is disabled", () => {
  19. const lockedRectangle = API.createElement({
  20. type: "rectangle",
  21. width: 100,
  22. backgroundColor: "red",
  23. fillStyle: "solid",
  24. locked: true,
  25. });
  26. h.elements = [lockedRectangle];
  27. mouse.clickAt(50, 50);
  28. expect(API.getSelectedElements().length).toBe(0);
  29. });
  30. it("box-selecting a locked element is disabled", () => {
  31. const lockedRectangle = API.createElement({
  32. type: "rectangle",
  33. width: 100,
  34. backgroundColor: "red",
  35. fillStyle: "solid",
  36. locked: true,
  37. x: 100,
  38. y: 100,
  39. });
  40. h.elements = [lockedRectangle];
  41. mouse.downAt(50, 50);
  42. mouse.moveTo(250, 250);
  43. mouse.upAt(250, 250);
  44. expect(API.getSelectedElements().length).toBe(0);
  45. });
  46. it("dragging a locked element is disabled", () => {
  47. const lockedRectangle = API.createElement({
  48. type: "rectangle",
  49. width: 100,
  50. backgroundColor: "red",
  51. fillStyle: "solid",
  52. locked: true,
  53. });
  54. h.elements = [lockedRectangle];
  55. mouse.downAt(50, 50);
  56. mouse.moveTo(100, 100);
  57. mouse.upAt(100, 100);
  58. expect(lockedRectangle).toEqual(expect.objectContaining({ x: 0, y: 0 }));
  59. });
  60. it("you can drag element that's below a locked element", () => {
  61. const rectangle = API.createElement({
  62. type: "rectangle",
  63. width: 100,
  64. backgroundColor: "red",
  65. fillStyle: "solid",
  66. });
  67. const lockedRectangle = API.createElement({
  68. type: "rectangle",
  69. width: 100,
  70. backgroundColor: "red",
  71. fillStyle: "solid",
  72. locked: true,
  73. });
  74. h.elements = [rectangle, lockedRectangle];
  75. mouse.downAt(50, 50);
  76. mouse.moveTo(100, 100);
  77. mouse.upAt(100, 100);
  78. expect(lockedRectangle).toEqual(expect.objectContaining({ x: 0, y: 0 }));
  79. expect(rectangle).toEqual(expect.objectContaining({ x: 50, y: 50 }));
  80. expect(API.getSelectedElements().length).toBe(1);
  81. expect(API.getSelectedElement().id).toBe(rectangle.id);
  82. });
  83. it("selectAll shouldn't select locked elements", () => {
  84. h.elements = [
  85. API.createElement({ type: "rectangle" }),
  86. API.createElement({ type: "rectangle", locked: true }),
  87. ];
  88. h.app.actionManager.executeAction(actionSelectAll);
  89. expect(API.getSelectedElements().length).toBe(1);
  90. });
  91. it("clicking on a locked element should select the unlocked element beneath it", () => {
  92. const rectangle = API.createElement({
  93. type: "rectangle",
  94. width: 100,
  95. backgroundColor: "red",
  96. fillStyle: "solid",
  97. });
  98. const lockedRectangle = API.createElement({
  99. type: "rectangle",
  100. width: 100,
  101. backgroundColor: "red",
  102. fillStyle: "solid",
  103. locked: true,
  104. });
  105. h.elements = [rectangle, lockedRectangle];
  106. expect(API.getSelectedElements().length).toBe(0);
  107. mouse.clickAt(50, 50);
  108. expect(API.getSelectedElements().length).toBe(1);
  109. expect(API.getSelectedElement().id).toBe(rectangle.id);
  110. });
  111. it("right-clicking on a locked element should select it & open its contextMenu", () => {
  112. const rectangle = API.createElement({
  113. type: "rectangle",
  114. width: 100,
  115. backgroundColor: "red",
  116. fillStyle: "solid",
  117. });
  118. const lockedRectangle = API.createElement({
  119. type: "rectangle",
  120. width: 100,
  121. backgroundColor: "red",
  122. fillStyle: "solid",
  123. locked: true,
  124. });
  125. h.elements = [rectangle, lockedRectangle];
  126. expect(API.getSelectedElements().length).toBe(0);
  127. mouse.rightClickAt(50, 50);
  128. expect(API.getSelectedElements().length).toBe(1);
  129. expect(API.getSelectedElement().id).toBe(lockedRectangle.id);
  130. const contextMenu = UI.queryContextMenu();
  131. expect(contextMenu).not.toBeNull();
  132. expect(
  133. contextMenu?.querySelector(
  134. `li[data-testid="toggleLock"] .context-menu-item__label`,
  135. ),
  136. ).toHaveTextContent(t("labels.elementLock.unlock"));
  137. });
  138. it("right-clicking on element covered by locked element should ignore the locked element", () => {
  139. const rectangle = API.createElement({
  140. type: "rectangle",
  141. width: 100,
  142. backgroundColor: "red",
  143. fillStyle: "solid",
  144. });
  145. const lockedRectangle = API.createElement({
  146. type: "rectangle",
  147. width: 100,
  148. backgroundColor: "red",
  149. fillStyle: "solid",
  150. locked: true,
  151. });
  152. h.elements = [rectangle, lockedRectangle];
  153. API.setSelectedElements([rectangle]);
  154. expect(API.getSelectedElements().length).toBe(1);
  155. expect(API.getSelectedElement().id).toBe(rectangle.id);
  156. mouse.rightClickAt(50, 50);
  157. expect(API.getSelectedElements().length).toBe(1);
  158. expect(API.getSelectedElement().id).toBe(rectangle.id);
  159. const contextMenu = UI.queryContextMenu();
  160. expect(contextMenu).not.toBeNull();
  161. });
  162. it("selecting a group selects all elements including locked ones", () => {
  163. const rectangle = API.createElement({
  164. type: "rectangle",
  165. width: 100,
  166. backgroundColor: "red",
  167. fillStyle: "solid",
  168. groupIds: ["g1"],
  169. });
  170. const lockedRectangle = API.createElement({
  171. type: "rectangle",
  172. width: 100,
  173. backgroundColor: "red",
  174. fillStyle: "solid",
  175. locked: true,
  176. groupIds: ["g1"],
  177. x: 200,
  178. y: 200,
  179. });
  180. h.elements = [rectangle, lockedRectangle];
  181. mouse.clickAt(250, 250);
  182. expect(API.getSelectedElements().length).toBe(0);
  183. mouse.clickAt(50, 50);
  184. expect(API.getSelectedElements().length).toBe(2);
  185. });
  186. it("should ignore locked text element in center of container on ENTER", () => {
  187. const container = API.createElement({
  188. type: "rectangle",
  189. width: 100,
  190. });
  191. const textSize = 20;
  192. const text = API.createElement({
  193. type: "text",
  194. text: "ola",
  195. x: container.width / 2 - textSize / 2,
  196. y: container.height / 2 - textSize / 2,
  197. width: textSize,
  198. height: textSize,
  199. containerId: container.id,
  200. locked: true,
  201. });
  202. h.elements = [container, text];
  203. API.setSelectedElements([container]);
  204. Keyboard.keyPress(KEYS.ENTER);
  205. expect(h.state.editingElement?.id).not.toBe(text.id);
  206. expect(h.state.editingElement?.id).toBe(h.elements[1].id);
  207. });
  208. it("should ignore locked text under cursor when clicked with text tool", () => {
  209. const text = API.createElement({
  210. type: "text",
  211. text: "ola",
  212. x: 60,
  213. y: 0,
  214. width: 100,
  215. height: 100,
  216. locked: true,
  217. });
  218. h.elements = [text];
  219. UI.clickTool("text");
  220. mouse.clickAt(text.x + 50, text.y + 50);
  221. const editor = document.querySelector(
  222. ".excalidraw-textEditorContainer > textarea",
  223. ) as HTMLTextAreaElement;
  224. expect(editor).not.toBe(null);
  225. expect(h.state.editingElement?.id).not.toBe(text.id);
  226. expect(h.elements.length).toBe(2);
  227. expect(h.state.editingElement?.id).toBe(h.elements[1].id);
  228. });
  229. it("should ignore text under cursor when double-clicked with selection tool", () => {
  230. const text = API.createElement({
  231. type: "text",
  232. text: "ola",
  233. x: 60,
  234. y: 0,
  235. width: 100,
  236. height: 100,
  237. locked: true,
  238. });
  239. h.elements = [text];
  240. UI.clickTool("selection");
  241. mouse.doubleClickAt(text.x + 50, text.y + 50);
  242. const editor = document.querySelector(
  243. ".excalidraw-textEditorContainer > textarea",
  244. ) as HTMLTextAreaElement;
  245. expect(editor).not.toBe(null);
  246. expect(h.state.editingElement?.id).not.toBe(text.id);
  247. expect(h.elements.length).toBe(2);
  248. expect(h.state.editingElement?.id).toBe(h.elements[1].id);
  249. });
  250. it("locking should include bound text", () => {
  251. const container = API.createElement({
  252. type: "rectangle",
  253. width: 100,
  254. });
  255. const textSize = 20;
  256. const text = API.createElement({
  257. type: "text",
  258. text: "ola",
  259. x: container.width / 2 - textSize / 2,
  260. y: container.height / 2 - textSize / 2,
  261. width: textSize,
  262. height: textSize,
  263. containerId: container.id,
  264. });
  265. mutateElement(container, {
  266. boundElements: [{ id: text.id, type: "text" }],
  267. });
  268. h.elements = [container, text];
  269. UI.clickTool("selection");
  270. mouse.clickAt(container.x + 10, container.y + 10);
  271. Keyboard.withModifierKeys({ ctrl: true, shift: true }, () => {
  272. Keyboard.keyPress(KEYS.L);
  273. });
  274. expect(h.elements).toEqual([
  275. expect.objectContaining({
  276. id: container.id,
  277. locked: true,
  278. }),
  279. expect.objectContaining({
  280. id: text.id,
  281. locked: true,
  282. }),
  283. ]);
  284. });
  285. it("bound text shouldn't be editable via double-click", () => {
  286. const container = API.createElement({
  287. type: "rectangle",
  288. width: 100,
  289. locked: true,
  290. });
  291. const textSize = 20;
  292. const text = API.createElement({
  293. type: "text",
  294. text: "ola",
  295. x: container.width / 2 - textSize / 2,
  296. y: container.height / 2 - textSize / 2,
  297. width: textSize,
  298. height: textSize,
  299. containerId: container.id,
  300. locked: true,
  301. });
  302. mutateElement(container, {
  303. boundElements: [{ id: text.id, type: "text" }],
  304. });
  305. h.elements = [container, text];
  306. UI.clickTool("selection");
  307. mouse.doubleClickAt(container.width / 2, container.height / 2);
  308. const editor = document.querySelector(
  309. ".excalidraw-textEditorContainer > textarea",
  310. ) as HTMLTextAreaElement;
  311. expect(editor).not.toBe(null);
  312. expect(h.state.editingElement?.id).not.toBe(text.id);
  313. expect(h.elements.length).toBe(3);
  314. expect(h.state.editingElement?.id).toBe(h.elements[2].id);
  315. });
  316. it("bound text shouldn't be editable via text tool", () => {
  317. const container = API.createElement({
  318. type: "rectangle",
  319. width: 100,
  320. locked: true,
  321. });
  322. const textSize = 20;
  323. const text = API.createElement({
  324. type: "text",
  325. text: "ola",
  326. x: container.width / 2 - textSize / 2,
  327. y: container.height / 2 - textSize / 2,
  328. width: textSize,
  329. height: textSize,
  330. containerId: container.id,
  331. locked: true,
  332. });
  333. mutateElement(container, {
  334. boundElements: [{ id: text.id, type: "text" }],
  335. });
  336. h.elements = [container, text];
  337. UI.clickTool("text");
  338. mouse.clickAt(container.width / 2, container.height / 2);
  339. const editor = document.querySelector(
  340. ".excalidraw-textEditorContainer > textarea",
  341. ) as HTMLTextAreaElement;
  342. expect(editor).not.toBe(null);
  343. expect(h.state.editingElement?.id).not.toBe(text.id);
  344. expect(h.elements.length).toBe(3);
  345. expect(h.state.editingElement?.id).toBe(h.elements[2].id);
  346. });
  347. });