2021-04-25 14:09:38 +02:00
|
|
|
import { bumpVersion } from "./element/mutateElement";
|
2023-06-15 00:42:01 +08:00
|
|
|
import { isFrameElement } from "./element/typeChecks";
|
2023-10-25 23:16:02 +02:00
|
|
|
import { ExcalidrawElement, ExcalidrawFrameElement } from "./element/types";
|
2020-09-11 17:06:07 +02:00
|
|
|
import { getElementsInGroup } from "./groups";
|
2021-12-17 18:40:37 +05:30
|
|
|
import { getSelectedElements } from "./scene";
|
|
|
|
import Scene from "./scene/Scene";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { AppState } from "./types";
|
2021-12-17 18:40:37 +05:30
|
|
|
import { arrayToMap, findIndex, findLastIndex } from "./utils";
|
2020-01-03 21:03:25 -08:00
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
const isOfTargetFrame = (element: ExcalidrawElement, frameId: string) => {
|
|
|
|
return element.frameId === frameId || element.id === frameId;
|
2023-06-15 00:42:01 +08:00
|
|
|
};
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
/**
|
|
|
|
* Returns indices of elements to move based on selected elements.
|
|
|
|
* Includes contiguous deleted elements that are between two selected elements,
|
|
|
|
* e.g.: [0 (selected), 1 (deleted), 2 (deleted), 3 (selected)]
|
2023-06-15 00:42:01 +08:00
|
|
|
*
|
|
|
|
* Specified elements (elementsToBeMoved) take precedence over
|
|
|
|
* appState.selectedElementsIds
|
2020-09-11 17:06:07 +02:00
|
|
|
*/
|
|
|
|
const getIndicesToMove = (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
2023-06-15 00:42:01 +08:00
|
|
|
elementsToBeMoved?: readonly ExcalidrawElement[],
|
2020-09-11 17:06:07 +02:00
|
|
|
) => {
|
|
|
|
let selectedIndices: number[] = [];
|
|
|
|
let deletedIndices: number[] = [];
|
|
|
|
let includeDeletedIndex = null;
|
2020-11-06 22:06:30 +02:00
|
|
|
let index = -1;
|
2021-12-17 18:40:37 +05:30
|
|
|
const selectedElementIds = arrayToMap(
|
2023-06-15 00:42:01 +08:00
|
|
|
elementsToBeMoved
|
|
|
|
? elementsToBeMoved
|
|
|
|
: getSelectedElements(elements, appState, {
|
|
|
|
includeBoundTextElement: true,
|
2023-10-25 23:16:02 +02:00
|
|
|
includeElementsInFrames: true,
|
2023-06-15 00:42:01 +08:00
|
|
|
}),
|
2021-12-17 18:40:37 +05:30
|
|
|
);
|
2020-11-06 22:06:30 +02:00
|
|
|
while (++index < elements.length) {
|
2023-06-15 00:42:01 +08:00
|
|
|
const element = elements[index];
|
|
|
|
if (selectedElementIds.get(element.id)) {
|
2020-09-11 17:06:07 +02:00
|
|
|
if (deletedIndices.length) {
|
|
|
|
selectedIndices = selectedIndices.concat(deletedIndices);
|
|
|
|
deletedIndices = [];
|
|
|
|
}
|
2020-11-06 22:06:30 +02:00
|
|
|
selectedIndices.push(index);
|
|
|
|
includeDeletedIndex = index + 1;
|
2023-06-15 00:42:01 +08:00
|
|
|
} else if (element.isDeleted && includeDeletedIndex === index) {
|
2020-11-06 22:06:30 +02:00
|
|
|
includeDeletedIndex = index + 1;
|
|
|
|
deletedIndices.push(index);
|
2020-09-11 17:06:07 +02:00
|
|
|
} else {
|
|
|
|
deletedIndices = [];
|
2020-01-03 21:03:25 -08:00
|
|
|
}
|
2020-09-11 17:06:07 +02:00
|
|
|
}
|
|
|
|
return selectedIndices;
|
|
|
|
};
|
2020-01-09 19:22:04 +04:00
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
const toContiguousGroups = (array: number[]) => {
|
|
|
|
let cursor = 0;
|
|
|
|
return array.reduce((acc, value, index) => {
|
|
|
|
if (index > 0 && array[index - 1] !== value - 1) {
|
|
|
|
cursor = ++cursor;
|
|
|
|
}
|
|
|
|
(acc[cursor] || (acc[cursor] = [])).push(value);
|
|
|
|
return acc;
|
|
|
|
}, [] as number[][]);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-03 21:03:25 -08:00
|
|
|
|
2021-12-17 18:40:37 +05:30
|
|
|
/**
|
|
|
|
* @returns index of target element, consindering tightly-bound elements
|
|
|
|
* (currently non-linear elements bound to a container) as a one unit.
|
|
|
|
* If no binding present, returns `undefined`.
|
|
|
|
*/
|
|
|
|
const getTargetIndexAccountingForBinding = (
|
|
|
|
nextElement: ExcalidrawElement,
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
direction: "left" | "right",
|
|
|
|
) => {
|
|
|
|
if ("containerId" in nextElement && nextElement.containerId) {
|
|
|
|
if (direction === "left") {
|
|
|
|
const containerElement = Scene.getScene(nextElement)!.getElement(
|
|
|
|
nextElement.containerId,
|
|
|
|
);
|
|
|
|
if (containerElement) {
|
|
|
|
return elements.indexOf(containerElement);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return elements.indexOf(nextElement);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const boundElementId = nextElement.boundElements?.find(
|
|
|
|
(binding) => binding.type !== "arrow",
|
|
|
|
)?.id;
|
|
|
|
if (boundElementId) {
|
|
|
|
if (direction === "left") {
|
|
|
|
return elements.indexOf(nextElement);
|
|
|
|
}
|
2023-09-21 09:28:48 +05:30
|
|
|
|
2021-12-17 18:40:37 +05:30
|
|
|
const boundTextElement =
|
|
|
|
Scene.getScene(nextElement)!.getElement(boundElementId);
|
|
|
|
if (boundTextElement) {
|
|
|
|
return elements.indexOf(boundTextElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
const getContiguousFrameRangeElements = (
|
|
|
|
allElements: readonly ExcalidrawElement[],
|
|
|
|
frameId: ExcalidrawFrameElement["id"],
|
|
|
|
) => {
|
|
|
|
let rangeStart = -1;
|
|
|
|
let rangeEnd = -1;
|
|
|
|
allElements.forEach((element, index) => {
|
|
|
|
if (isOfTargetFrame(element, frameId)) {
|
|
|
|
if (rangeStart === -1) {
|
|
|
|
rangeStart = index;
|
|
|
|
}
|
|
|
|
rangeEnd = index;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (rangeStart === -1) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return allElements.slice(rangeStart, rangeEnd + 1);
|
|
|
|
};
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
/**
|
|
|
|
* Returns next candidate index that's available to be moved to. Currently that
|
|
|
|
* is a non-deleted element, and not inside a group (unless we're editing it).
|
|
|
|
*/
|
|
|
|
const getTargetIndex = (
|
|
|
|
appState: AppState,
|
2021-04-25 14:09:38 +02:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-09-11 17:06:07 +02:00
|
|
|
boundaryIndex: number,
|
|
|
|
direction: "left" | "right",
|
2023-10-25 23:16:02 +02:00
|
|
|
/**
|
|
|
|
* Frame id if moving frame children.
|
|
|
|
* If whole frame (including all children) is being moved, supply `null`.
|
|
|
|
*/
|
|
|
|
containingFrame: ExcalidrawFrameElement["id"] | null,
|
2020-09-11 17:06:07 +02:00
|
|
|
) => {
|
|
|
|
const sourceElement = elements[boundaryIndex];
|
|
|
|
|
|
|
|
const indexFilter = (element: ExcalidrawElement) => {
|
|
|
|
if (element.isDeleted) {
|
|
|
|
return false;
|
2020-01-03 21:38:48 -08:00
|
|
|
}
|
2023-10-25 23:16:02 +02:00
|
|
|
if (containingFrame) {
|
|
|
|
return element.frameId === containingFrame;
|
|
|
|
}
|
2020-09-11 17:06:07 +02:00
|
|
|
// if we're editing group, find closest sibling irrespective of whether
|
2020-11-05 19:06:18 +02:00
|
|
|
// there's a different-group element between them (for legacy reasons)
|
2020-09-11 17:06:07 +02:00
|
|
|
if (appState.editingGroupId) {
|
|
|
|
return element.groupIds.includes(appState.editingGroupId);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const candidateIndex =
|
|
|
|
direction === "left"
|
2023-10-25 23:16:02 +02:00
|
|
|
? findLastIndex(
|
|
|
|
elements,
|
|
|
|
(el) => indexFilter(el),
|
|
|
|
Math.max(0, boundaryIndex - 1),
|
|
|
|
)
|
|
|
|
: findIndex(elements, (el) => indexFilter(el), boundaryIndex + 1);
|
2020-09-11 17:06:07 +02:00
|
|
|
|
|
|
|
const nextElement = elements[candidateIndex];
|
|
|
|
|
|
|
|
if (!nextElement) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (appState.editingGroupId) {
|
|
|
|
if (
|
|
|
|
// candidate element is a sibling in current editing group → return
|
|
|
|
sourceElement?.groupIds.join("") === nextElement?.groupIds.join("")
|
|
|
|
) {
|
2021-12-17 18:40:37 +05:30
|
|
|
return (
|
|
|
|
getTargetIndexAccountingForBinding(nextElement, elements, direction) ??
|
|
|
|
candidateIndex
|
|
|
|
);
|
2020-09-11 17:06:07 +02:00
|
|
|
} else if (!nextElement?.groupIds.includes(appState.editingGroupId)) {
|
|
|
|
// candidate element is outside current editing group → prevent
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
if (
|
|
|
|
!containingFrame &&
|
|
|
|
(nextElement.frameId || nextElement.type === "frame")
|
|
|
|
) {
|
|
|
|
const frameElements = getContiguousFrameRangeElements(
|
|
|
|
elements,
|
|
|
|
nextElement.frameId || nextElement.id,
|
|
|
|
);
|
|
|
|
return direction === "left"
|
|
|
|
? elements.indexOf(frameElements[0])
|
|
|
|
: elements.indexOf(frameElements[frameElements.length - 1]);
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
if (!nextElement.groupIds.length) {
|
2021-12-17 18:40:37 +05:30
|
|
|
return (
|
|
|
|
getTargetIndexAccountingForBinding(nextElement, elements, direction) ??
|
|
|
|
candidateIndex
|
|
|
|
);
|
2020-09-11 17:06:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const siblingGroupId = appState.editingGroupId
|
|
|
|
? nextElement.groupIds[
|
|
|
|
nextElement.groupIds.indexOf(appState.editingGroupId) - 1
|
|
|
|
]
|
|
|
|
: nextElement.groupIds[nextElement.groupIds.length - 1];
|
|
|
|
|
|
|
|
const elementsInSiblingGroup = getElementsInGroup(elements, siblingGroupId);
|
|
|
|
|
|
|
|
if (elementsInSiblingGroup.length) {
|
|
|
|
// assumes getElementsInGroup() returned elements are sorted
|
2020-11-05 19:06:18 +02:00
|
|
|
// by zIndex (ascending)
|
2020-09-11 17:06:07 +02:00
|
|
|
return direction === "left"
|
|
|
|
? elements.indexOf(elementsInSiblingGroup[0])
|
|
|
|
: elements.indexOf(
|
|
|
|
elementsInSiblingGroup[elementsInSiblingGroup.length - 1],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return candidateIndex;
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-03 21:38:48 -08:00
|
|
|
|
2023-06-15 00:42:01 +08:00
|
|
|
const getTargetElementsMap = <T extends ExcalidrawElement>(
|
|
|
|
elements: readonly T[],
|
2021-04-25 14:09:38 +02:00
|
|
|
indices: number[],
|
|
|
|
) => {
|
|
|
|
return indices.reduce((acc, index) => {
|
|
|
|
const element = elements[index];
|
|
|
|
acc[element.id] = element;
|
|
|
|
return acc;
|
|
|
|
}, {} as Record<string, ExcalidrawElement>);
|
|
|
|
};
|
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
const shiftElementsByOne = (
|
2021-04-25 14:09:38 +02:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2023-06-15 00:42:01 +08:00
|
|
|
appState: AppState,
|
2020-09-11 17:06:07 +02:00
|
|
|
direction: "left" | "right",
|
|
|
|
) => {
|
2023-10-25 23:16:02 +02:00
|
|
|
const indicesToMove = getIndicesToMove(elements, appState);
|
2021-04-25 14:09:38 +02:00
|
|
|
const targetElementsMap = getTargetElementsMap(elements, indicesToMove);
|
2020-09-11 17:06:07 +02:00
|
|
|
let groupedIndices = toContiguousGroups(indicesToMove);
|
|
|
|
|
|
|
|
if (direction === "right") {
|
|
|
|
groupedIndices = groupedIndices.reverse();
|
|
|
|
}
|
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
const selectedFrames = new Set<ExcalidrawFrameElement["id"]>(
|
|
|
|
indicesToMove
|
|
|
|
.filter((idx) => elements[idx].type === "frame")
|
|
|
|
.map((idx) => elements[idx].id),
|
|
|
|
);
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
groupedIndices.forEach((indices, i) => {
|
|
|
|
const leadingIndex = indices[0];
|
|
|
|
const trailingIndex = indices[indices.length - 1];
|
|
|
|
const boundaryIndex = direction === "left" ? leadingIndex : trailingIndex;
|
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
const containingFrame = indices.some((idx) => {
|
|
|
|
const el = elements[idx];
|
|
|
|
return el.frameId && selectedFrames.has(el.frameId);
|
|
|
|
})
|
|
|
|
? null
|
|
|
|
: elements[boundaryIndex]?.frameId;
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
const targetIndex = getTargetIndex(
|
|
|
|
appState,
|
|
|
|
elements,
|
|
|
|
boundaryIndex,
|
|
|
|
direction,
|
2023-10-25 23:16:02 +02:00
|
|
|
containingFrame,
|
2020-09-11 17:06:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if (targetIndex === -1 || boundaryIndex === targetIndex) {
|
2020-01-03 21:03:25 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
const leadingElements =
|
|
|
|
direction === "left"
|
|
|
|
? elements.slice(0, targetIndex)
|
|
|
|
: elements.slice(0, leadingIndex);
|
|
|
|
const targetElements = elements.slice(leadingIndex, trailingIndex + 1);
|
|
|
|
const displacedElements =
|
|
|
|
direction === "left"
|
|
|
|
? elements.slice(targetIndex, leadingIndex)
|
|
|
|
: elements.slice(trailingIndex + 1, targetIndex + 1);
|
|
|
|
const trailingElements =
|
|
|
|
direction === "left"
|
|
|
|
? elements.slice(trailingIndex + 1)
|
|
|
|
: elements.slice(targetIndex + 1);
|
2020-01-03 21:03:25 -08:00
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
elements =
|
|
|
|
direction === "left"
|
|
|
|
? [
|
|
|
|
...leadingElements,
|
|
|
|
...targetElements,
|
|
|
|
...displacedElements,
|
|
|
|
...trailingElements,
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
...leadingElements,
|
|
|
|
...displacedElements,
|
|
|
|
...targetElements,
|
|
|
|
...trailingElements,
|
|
|
|
];
|
2020-01-03 21:03:25 -08:00
|
|
|
});
|
2020-01-09 19:22:04 +04:00
|
|
|
|
2021-04-25 14:09:38 +02:00
|
|
|
return elements.map((element) => {
|
|
|
|
if (targetElementsMap[element.id]) {
|
|
|
|
return bumpVersion(element);
|
|
|
|
}
|
|
|
|
return element;
|
|
|
|
});
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-03 21:38:48 -08:00
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
const shiftElementsToEnd = (
|
2020-09-11 17:06:07 +02:00
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
direction: "left" | "right",
|
2023-10-25 23:16:02 +02:00
|
|
|
containingFrame: ExcalidrawFrameElement["id"] | null,
|
|
|
|
elementsToBeMoved?: readonly ExcalidrawElement[],
|
2020-09-11 17:06:07 +02:00
|
|
|
) => {
|
2023-10-25 23:16:02 +02:00
|
|
|
const indicesToMove = getIndicesToMove(elements, appState, elementsToBeMoved);
|
2021-04-25 14:09:38 +02:00
|
|
|
const targetElementsMap = getTargetElementsMap(elements, indicesToMove);
|
2020-09-11 17:06:07 +02:00
|
|
|
const displacedElements: ExcalidrawElement[] = [];
|
|
|
|
|
2020-11-06 22:06:30 +02:00
|
|
|
let leadingIndex: number;
|
|
|
|
let trailingIndex: number;
|
2020-09-11 17:06:07 +02:00
|
|
|
if (direction === "left") {
|
2023-10-25 23:16:02 +02:00
|
|
|
if (containingFrame) {
|
|
|
|
leadingIndex = findIndex(elements, (el) =>
|
|
|
|
isOfTargetFrame(el, containingFrame),
|
|
|
|
);
|
|
|
|
} else if (appState.editingGroupId) {
|
2020-09-11 17:06:07 +02:00
|
|
|
const groupElements = getElementsInGroup(
|
|
|
|
elements,
|
|
|
|
appState.editingGroupId,
|
|
|
|
);
|
|
|
|
if (!groupElements.length) {
|
|
|
|
return elements;
|
|
|
|
}
|
|
|
|
leadingIndex = elements.indexOf(groupElements[0]);
|
|
|
|
} else {
|
|
|
|
leadingIndex = 0;
|
2020-01-03 21:38:48 -08:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
trailingIndex = indicesToMove[indicesToMove.length - 1];
|
|
|
|
} else {
|
2023-10-25 23:16:02 +02:00
|
|
|
if (containingFrame) {
|
|
|
|
trailingIndex = findLastIndex(elements, (el) =>
|
|
|
|
isOfTargetFrame(el, containingFrame),
|
|
|
|
);
|
|
|
|
} else if (appState.editingGroupId) {
|
2020-09-11 17:06:07 +02:00
|
|
|
const groupElements = getElementsInGroup(
|
|
|
|
elements,
|
|
|
|
appState.editingGroupId,
|
|
|
|
);
|
|
|
|
if (!groupElements.length) {
|
|
|
|
return elements;
|
|
|
|
}
|
|
|
|
trailingIndex = elements.indexOf(groupElements[groupElements.length - 1]);
|
|
|
|
} else {
|
|
|
|
trailingIndex = elements.length - 1;
|
2020-01-03 21:38:48 -08:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
leadingIndex = indicesToMove[0];
|
|
|
|
}
|
2020-01-09 19:22:04 +04:00
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
if (leadingIndex === -1) {
|
|
|
|
leadingIndex = 0;
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
for (let index = leadingIndex; index < trailingIndex + 1; index++) {
|
2021-04-25 14:09:38 +02:00
|
|
|
if (!indicesToMove.includes(index)) {
|
2020-09-11 17:06:07 +02:00
|
|
|
displacedElements.push(elements[index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 14:09:38 +02:00
|
|
|
const targetElements = Object.values(targetElementsMap).map((element) => {
|
|
|
|
return bumpVersion(element);
|
|
|
|
});
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
const leadingElements = elements.slice(0, leadingIndex);
|
|
|
|
const trailingElements = elements.slice(trailingIndex + 1);
|
|
|
|
|
|
|
|
return direction === "left"
|
|
|
|
? [
|
|
|
|
...leadingElements,
|
|
|
|
...targetElements,
|
|
|
|
...displacedElements,
|
|
|
|
...trailingElements,
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
...leadingElements,
|
|
|
|
...displacedElements,
|
|
|
|
...targetElements,
|
|
|
|
...trailingElements,
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
function shiftElementsAccountingForFrames(
|
|
|
|
allElements: readonly ExcalidrawElement[],
|
2023-06-15 00:42:01 +08:00
|
|
|
appState: AppState,
|
|
|
|
direction: "left" | "right",
|
|
|
|
shiftFunction: (
|
2023-10-25 23:16:02 +02:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2023-06-15 00:42:01 +08:00
|
|
|
appState: AppState,
|
|
|
|
direction: "left" | "right",
|
2023-10-25 23:16:02 +02:00
|
|
|
containingFrame: ExcalidrawFrameElement["id"] | null,
|
2023-06-15 00:42:01 +08:00
|
|
|
elementsToBeMoved?: readonly ExcalidrawElement[],
|
|
|
|
) => ExcalidrawElement[] | readonly ExcalidrawElement[],
|
|
|
|
) {
|
2023-10-25 23:16:02 +02:00
|
|
|
const elementsToMove = arrayToMap(
|
|
|
|
getSelectedElements(allElements, appState, {
|
|
|
|
includeBoundTextElement: true,
|
|
|
|
includeElementsInFrames: true,
|
|
|
|
}),
|
2023-06-23 06:10:08 +08:00
|
|
|
);
|
2023-10-25 23:16:02 +02:00
|
|
|
|
|
|
|
const frameAwareContiguousElementsToMove: {
|
|
|
|
regularElements: ExcalidrawElement[];
|
|
|
|
frameChildren: Map<ExcalidrawFrameElement["id"], ExcalidrawElement[]>;
|
|
|
|
} = { regularElements: [], frameChildren: new Map() };
|
|
|
|
|
|
|
|
const fullySelectedFrames = new Set<ExcalidrawFrameElement["id"]>();
|
|
|
|
|
|
|
|
for (const element of allElements) {
|
|
|
|
if (elementsToMove.has(element.id) && isFrameElement(element)) {
|
|
|
|
fullySelectedFrames.add(element.id);
|
2023-06-23 06:10:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
for (const element of allElements) {
|
|
|
|
if (elementsToMove.has(element.id)) {
|
|
|
|
if (
|
|
|
|
isFrameElement(element) ||
|
|
|
|
(element.frameId && fullySelectedFrames.has(element.frameId))
|
|
|
|
) {
|
|
|
|
frameAwareContiguousElementsToMove.regularElements.push(element);
|
|
|
|
} else if (!element.frameId) {
|
|
|
|
frameAwareContiguousElementsToMove.regularElements.push(element);
|
|
|
|
} else {
|
|
|
|
const frameChildren =
|
|
|
|
frameAwareContiguousElementsToMove.frameChildren.get(
|
|
|
|
element.frameId,
|
|
|
|
) || [];
|
|
|
|
frameChildren.push(element);
|
|
|
|
frameAwareContiguousElementsToMove.frameChildren.set(
|
|
|
|
element.frameId,
|
|
|
|
frameChildren,
|
|
|
|
);
|
|
|
|
}
|
2023-06-15 00:42:01 +08:00
|
|
|
}
|
2023-10-25 23:16:02 +02:00
|
|
|
}
|
2023-06-15 00:42:01 +08:00
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
let nextElements = allElements;
|
2023-06-15 00:42:01 +08:00
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
const frameChildrenSets = Array.from(
|
|
|
|
frameAwareContiguousElementsToMove.frameChildren.entries(),
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const [frameId, children] of frameChildrenSets) {
|
|
|
|
nextElements = shiftFunction(
|
|
|
|
allElements,
|
|
|
|
appState,
|
|
|
|
direction,
|
|
|
|
frameId,
|
|
|
|
children,
|
|
|
|
);
|
|
|
|
}
|
2023-06-15 00:42:01 +08:00
|
|
|
|
2023-10-25 23:16:02 +02:00
|
|
|
return shiftFunction(
|
|
|
|
nextElements,
|
|
|
|
appState,
|
|
|
|
direction,
|
|
|
|
null,
|
|
|
|
frameAwareContiguousElementsToMove.regularElements,
|
|
|
|
);
|
2023-06-15 00:42:01 +08:00
|
|
|
}
|
|
|
|
|
2020-09-11 17:06:07 +02:00
|
|
|
// public API
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export const moveOneLeft = (
|
2023-10-25 23:16:02 +02:00
|
|
|
allElements: readonly ExcalidrawElement[],
|
2020-09-11 17:06:07 +02:00
|
|
|
appState: AppState,
|
|
|
|
) => {
|
2023-10-25 23:16:02 +02:00
|
|
|
return shiftElementsByOne(allElements, appState, "left");
|
2020-09-11 17:06:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const moveOneRight = (
|
2023-10-25 23:16:02 +02:00
|
|
|
allElements: readonly ExcalidrawElement[],
|
2020-09-11 17:06:07 +02:00
|
|
|
appState: AppState,
|
|
|
|
) => {
|
2023-10-25 23:16:02 +02:00
|
|
|
return shiftElementsByOne(allElements, appState, "right");
|
2020-09-11 17:06:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const moveAllLeft = (
|
2023-10-25 23:16:02 +02:00
|
|
|
allElements: readonly ExcalidrawElement[],
|
2020-09-11 17:06:07 +02:00
|
|
|
appState: AppState,
|
|
|
|
) => {
|
2023-10-25 23:16:02 +02:00
|
|
|
return shiftElementsAccountingForFrames(
|
|
|
|
allElements,
|
|
|
|
appState,
|
|
|
|
"left",
|
|
|
|
shiftElementsToEnd,
|
|
|
|
);
|
2020-09-11 17:06:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const moveAllRight = (
|
2023-10-25 23:16:02 +02:00
|
|
|
allElements: readonly ExcalidrawElement[],
|
2020-09-11 17:06:07 +02:00
|
|
|
appState: AppState,
|
|
|
|
) => {
|
2023-10-25 23:16:02 +02:00
|
|
|
return shiftElementsAccountingForFrames(
|
|
|
|
allElements,
|
|
|
|
appState,
|
|
|
|
"right",
|
|
|
|
shiftElementsToEnd,
|
|
|
|
);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|