zindex.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { ExcalidrawElement } from "./element/types";
  2. import { getElementsInGroup } from "./groups";
  3. import { AppState } from "./types";
  4. import { findIndex, findLastIndex } from "./utils";
  5. /**
  6. * Returns indices of elements to move based on selected elements.
  7. * Includes contiguous deleted elements that are between two selected elements,
  8. * e.g.: [0 (selected), 1 (deleted), 2 (deleted), 3 (selected)]
  9. */
  10. const getIndicesToMove = (
  11. elements: readonly ExcalidrawElement[],
  12. appState: AppState,
  13. ) => {
  14. let selectedIndices: number[] = [];
  15. let deletedIndices: number[] = [];
  16. let includeDeletedIndex = null;
  17. let index = -1;
  18. while (++index < elements.length) {
  19. if (appState.selectedElementIds[elements[index].id]) {
  20. if (deletedIndices.length) {
  21. selectedIndices = selectedIndices.concat(deletedIndices);
  22. deletedIndices = [];
  23. }
  24. selectedIndices.push(index);
  25. includeDeletedIndex = index + 1;
  26. } else if (elements[index].isDeleted && includeDeletedIndex === index) {
  27. includeDeletedIndex = index + 1;
  28. deletedIndices.push(index);
  29. } else {
  30. deletedIndices = [];
  31. }
  32. }
  33. return selectedIndices;
  34. };
  35. const toContiguousGroups = (array: number[]) => {
  36. let cursor = 0;
  37. return array.reduce((acc, value, index) => {
  38. if (index > 0 && array[index - 1] !== value - 1) {
  39. cursor = ++cursor;
  40. }
  41. (acc[cursor] || (acc[cursor] = [])).push(value);
  42. return acc;
  43. }, [] as number[][]);
  44. };
  45. /**
  46. * Returns next candidate index that's available to be moved to. Currently that
  47. * is a non-deleted element, and not inside a group (unless we're editing it).
  48. */
  49. const getTargetIndex = (
  50. appState: AppState,
  51. elements: ExcalidrawElement[],
  52. boundaryIndex: number,
  53. direction: "left" | "right",
  54. ) => {
  55. const sourceElement = elements[boundaryIndex];
  56. const indexFilter = (element: ExcalidrawElement) => {
  57. if (element.isDeleted) {
  58. return false;
  59. }
  60. // if we're editing group, find closest sibling irrespective of whether
  61. // there's a different-group element between them (for legacy reasons)
  62. if (appState.editingGroupId) {
  63. return element.groupIds.includes(appState.editingGroupId);
  64. }
  65. return true;
  66. };
  67. const candidateIndex =
  68. direction === "left"
  69. ? findLastIndex(elements, indexFilter, Math.max(0, boundaryIndex - 1))
  70. : findIndex(elements, indexFilter, boundaryIndex + 1);
  71. const nextElement = elements[candidateIndex];
  72. if (!nextElement) {
  73. return -1;
  74. }
  75. if (appState.editingGroupId) {
  76. if (
  77. // candidate element is a sibling in current editing group → return
  78. sourceElement?.groupIds.join("") === nextElement?.groupIds.join("")
  79. ) {
  80. return candidateIndex;
  81. } else if (!nextElement?.groupIds.includes(appState.editingGroupId)) {
  82. // candidate element is outside current editing group → prevent
  83. return -1;
  84. }
  85. }
  86. if (!nextElement.groupIds.length) {
  87. return candidateIndex;
  88. }
  89. const siblingGroupId = appState.editingGroupId
  90. ? nextElement.groupIds[
  91. nextElement.groupIds.indexOf(appState.editingGroupId) - 1
  92. ]
  93. : nextElement.groupIds[nextElement.groupIds.length - 1];
  94. const elementsInSiblingGroup = getElementsInGroup(elements, siblingGroupId);
  95. if (elementsInSiblingGroup.length) {
  96. // assumes getElementsInGroup() returned elements are sorted
  97. // by zIndex (ascending)
  98. return direction === "left"
  99. ? elements.indexOf(elementsInSiblingGroup[0])
  100. : elements.indexOf(
  101. elementsInSiblingGroup[elementsInSiblingGroup.length - 1],
  102. );
  103. }
  104. return candidateIndex;
  105. };
  106. const shiftElements = (
  107. appState: AppState,
  108. elements: ExcalidrawElement[],
  109. direction: "left" | "right",
  110. ) => {
  111. const indicesToMove = getIndicesToMove(elements, appState);
  112. let groupedIndices = toContiguousGroups(indicesToMove);
  113. if (direction === "right") {
  114. groupedIndices = groupedIndices.reverse();
  115. }
  116. groupedIndices.forEach((indices, i) => {
  117. const leadingIndex = indices[0];
  118. const trailingIndex = indices[indices.length - 1];
  119. const boundaryIndex = direction === "left" ? leadingIndex : trailingIndex;
  120. const targetIndex = getTargetIndex(
  121. appState,
  122. elements,
  123. boundaryIndex,
  124. direction,
  125. );
  126. if (targetIndex === -1 || boundaryIndex === targetIndex) {
  127. return;
  128. }
  129. const leadingElements =
  130. direction === "left"
  131. ? elements.slice(0, targetIndex)
  132. : elements.slice(0, leadingIndex);
  133. const targetElements = elements.slice(leadingIndex, trailingIndex + 1);
  134. const displacedElements =
  135. direction === "left"
  136. ? elements.slice(targetIndex, leadingIndex)
  137. : elements.slice(trailingIndex + 1, targetIndex + 1);
  138. const trailingElements =
  139. direction === "left"
  140. ? elements.slice(trailingIndex + 1)
  141. : elements.slice(targetIndex + 1);
  142. elements =
  143. direction === "left"
  144. ? [
  145. ...leadingElements,
  146. ...targetElements,
  147. ...displacedElements,
  148. ...trailingElements,
  149. ]
  150. : [
  151. ...leadingElements,
  152. ...displacedElements,
  153. ...targetElements,
  154. ...trailingElements,
  155. ];
  156. });
  157. return elements;
  158. };
  159. const shiftElementsToEnd = (
  160. elements: readonly ExcalidrawElement[],
  161. appState: AppState,
  162. direction: "left" | "right",
  163. ) => {
  164. const indicesToMove = getIndicesToMove(elements, appState);
  165. const targetElements: ExcalidrawElement[] = [];
  166. const displacedElements: ExcalidrawElement[] = [];
  167. let leadingIndex: number;
  168. let trailingIndex: number;
  169. if (direction === "left") {
  170. if (appState.editingGroupId) {
  171. const groupElements = getElementsInGroup(
  172. elements,
  173. appState.editingGroupId,
  174. );
  175. if (!groupElements.length) {
  176. return elements;
  177. }
  178. leadingIndex = elements.indexOf(groupElements[0]);
  179. } else {
  180. leadingIndex = 0;
  181. }
  182. trailingIndex = indicesToMove[indicesToMove.length - 1];
  183. } else {
  184. if (appState.editingGroupId) {
  185. const groupElements = getElementsInGroup(
  186. elements,
  187. appState.editingGroupId,
  188. );
  189. if (!groupElements.length) {
  190. return elements;
  191. }
  192. trailingIndex = elements.indexOf(groupElements[groupElements.length - 1]);
  193. } else {
  194. trailingIndex = elements.length - 1;
  195. }
  196. leadingIndex = indicesToMove[0];
  197. }
  198. for (let index = leadingIndex; index < trailingIndex + 1; index++) {
  199. if (indicesToMove.includes(index)) {
  200. targetElements.push(elements[index]);
  201. } else {
  202. displacedElements.push(elements[index]);
  203. }
  204. }
  205. const leadingElements = elements.slice(0, leadingIndex);
  206. const trailingElements = elements.slice(trailingIndex + 1);
  207. return direction === "left"
  208. ? [
  209. ...leadingElements,
  210. ...targetElements,
  211. ...displacedElements,
  212. ...trailingElements,
  213. ]
  214. : [
  215. ...leadingElements,
  216. ...displacedElements,
  217. ...targetElements,
  218. ...trailingElements,
  219. ];
  220. };
  221. // public API
  222. // -----------------------------------------------------------------------------
  223. export const moveOneLeft = (
  224. elements: readonly ExcalidrawElement[],
  225. appState: AppState,
  226. ) => {
  227. return shiftElements(appState, elements.slice(), "left");
  228. };
  229. export const moveOneRight = (
  230. elements: readonly ExcalidrawElement[],
  231. appState: AppState,
  232. ) => {
  233. return shiftElements(appState, elements.slice(), "right");
  234. };
  235. export const moveAllLeft = (
  236. elements: readonly ExcalidrawElement[],
  237. appState: AppState,
  238. ) => {
  239. return shiftElementsToEnd(elements, appState, "left");
  240. };
  241. export const moveAllRight = (
  242. elements: readonly ExcalidrawElement[],
  243. appState: AppState,
  244. ) => {
  245. return shiftElementsToEnd(elements, appState, "right");
  246. };