zindex.ts 8.3 KB

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