|
@@ -1,15 +1,18 @@
|
|
|
-import { isFrameLikeElement } from "./element/typeChecks";
|
|
|
-import { syncMovedIndices } from "./fractionalIndex";
|
|
|
+import { arrayToMap, findIndex, findLastIndex } from "@excalidraw/common";
|
|
|
+
|
|
|
+import type { AppState } from "@excalidraw/excalidraw/types";
|
|
|
+
|
|
|
+import type Scene from "@excalidraw/excalidraw/scene/Scene";
|
|
|
+
|
|
|
+import { isFrameLikeElement } from "./typeChecks";
|
|
|
+
|
|
|
import { getElementsInGroup } from "./groups";
|
|
|
-import { getSelectedElements } from "./scene";
|
|
|
-import Scene from "./scene/Scene";
|
|
|
-import { arrayToMap, findIndex, findLastIndex } from "./utils";
|
|
|
|
|
|
-import type {
|
|
|
- ExcalidrawElement,
|
|
|
- ExcalidrawFrameLikeElement,
|
|
|
-} from "./element/types";
|
|
|
-import type { AppState } from "./types";
|
|
|
+import { syncMovedIndices } from "./fractionalIndex";
|
|
|
+
|
|
|
+import { getSelectedElements } from "./selection";
|
|
|
+
|
|
|
+import type { ExcalidrawElement, ExcalidrawFrameLikeElement } from "./types";
|
|
|
|
|
|
const isOfTargetFrame = (element: ExcalidrawElement, frameId: string) => {
|
|
|
return element.frameId === frameId || element.id === frameId;
|
|
@@ -79,11 +82,11 @@ const getTargetIndexAccountingForBinding = (
|
|
|
nextElement: ExcalidrawElement,
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
direction: "left" | "right",
|
|
|
+ scene: Scene,
|
|
|
) => {
|
|
|
if ("containerId" in nextElement && nextElement.containerId) {
|
|
|
- const containerElement = Scene.getScene(nextElement)!.getElement(
|
|
|
- nextElement.containerId,
|
|
|
- );
|
|
|
+ // TODO: why not to get the container from the nextElements?
|
|
|
+ const containerElement = scene.getElement(nextElement.containerId);
|
|
|
if (containerElement) {
|
|
|
return direction === "left"
|
|
|
? Math.min(
|
|
@@ -100,8 +103,7 @@ const getTargetIndexAccountingForBinding = (
|
|
|
(binding) => binding.type !== "arrow",
|
|
|
)?.id;
|
|
|
if (boundElementId) {
|
|
|
- const boundTextElement =
|
|
|
- Scene.getScene(nextElement)!.getElement(boundElementId);
|
|
|
+ const boundTextElement = scene.getElement(boundElementId);
|
|
|
if (boundTextElement) {
|
|
|
return direction === "left"
|
|
|
? Math.min(
|
|
@@ -151,6 +153,7 @@ const getTargetIndex = (
|
|
|
* If whole frame (including all children) is being moved, supply `null`.
|
|
|
*/
|
|
|
containingFrame: ExcalidrawFrameLikeElement["id"] | null,
|
|
|
+ scene: Scene,
|
|
|
) => {
|
|
|
const sourceElement = elements[boundaryIndex];
|
|
|
|
|
@@ -190,8 +193,12 @@ const getTargetIndex = (
|
|
|
sourceElement?.groupIds.join("") === nextElement?.groupIds.join("")
|
|
|
) {
|
|
|
return (
|
|
|
- getTargetIndexAccountingForBinding(nextElement, elements, direction) ??
|
|
|
- candidateIndex
|
|
|
+ getTargetIndexAccountingForBinding(
|
|
|
+ nextElement,
|
|
|
+ elements,
|
|
|
+ direction,
|
|
|
+ scene,
|
|
|
+ ) ?? candidateIndex
|
|
|
);
|
|
|
} else if (!nextElement?.groupIds.includes(appState.editingGroupId)) {
|
|
|
// candidate element is outside current editing group → prevent
|
|
@@ -214,8 +221,12 @@ const getTargetIndex = (
|
|
|
|
|
|
if (!nextElement.groupIds.length) {
|
|
|
return (
|
|
|
- getTargetIndexAccountingForBinding(nextElement, elements, direction) ??
|
|
|
- candidateIndex
|
|
|
+ getTargetIndexAccountingForBinding(
|
|
|
+ nextElement,
|
|
|
+ elements,
|
|
|
+ direction,
|
|
|
+ scene,
|
|
|
+ ) ?? candidateIndex
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -255,6 +266,7 @@ const shiftElementsByOne = (
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
appState: AppState,
|
|
|
direction: "left" | "right",
|
|
|
+ scene: Scene,
|
|
|
) => {
|
|
|
const indicesToMove = getIndicesToMove(elements, appState);
|
|
|
const targetElementsMap = getTargetElementsMap(elements, indicesToMove);
|
|
@@ -289,6 +301,7 @@ const shiftElementsByOne = (
|
|
|
boundaryIndex,
|
|
|
direction,
|
|
|
containingFrame,
|
|
|
+ scene,
|
|
|
);
|
|
|
|
|
|
if (targetIndex === -1 || boundaryIndex === targetIndex) {
|
|
@@ -502,15 +515,17 @@ function shiftElementsAccountingForFrames(
|
|
|
export const moveOneLeft = (
|
|
|
allElements: readonly ExcalidrawElement[],
|
|
|
appState: AppState,
|
|
|
+ scene: Scene,
|
|
|
) => {
|
|
|
- return shiftElementsByOne(allElements, appState, "left");
|
|
|
+ return shiftElementsByOne(allElements, appState, "left", scene);
|
|
|
};
|
|
|
|
|
|
export const moveOneRight = (
|
|
|
allElements: readonly ExcalidrawElement[],
|
|
|
appState: AppState,
|
|
|
+ scene: Scene,
|
|
|
) => {
|
|
|
- return shiftElementsByOne(allElements, appState, "right");
|
|
|
+ return shiftElementsByOne(allElements, appState, "right", scene);
|
|
|
};
|
|
|
|
|
|
export const moveAllLeft = (
|