mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Fix zindex
This commit is contained in:
parent
65d5595c79
commit
2569691cc6
3 changed files with 30 additions and 19 deletions
|
@ -6,8 +6,6 @@ import { getElementsInGroup } from "@excalidraw/element/groups";
|
||||||
|
|
||||||
import { syncMovedIndices } from "@excalidraw/element/fractionalIndex";
|
import { syncMovedIndices } from "@excalidraw/element/fractionalIndex";
|
||||||
|
|
||||||
import { getSelectedElements } from "@excalidraw/excalidraw/scene";
|
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
ExcalidrawElement,
|
ExcalidrawElement,
|
||||||
ExcalidrawFrameLikeElement,
|
ExcalidrawFrameLikeElement,
|
||||||
|
@ -15,7 +13,9 @@ import type {
|
||||||
|
|
||||||
import type { AppState } from "@excalidraw/excalidraw/types";
|
import type { AppState } from "@excalidraw/excalidraw/types";
|
||||||
|
|
||||||
import Scene from "../../excalidraw/scene/Scene";
|
import type Scene from "@excalidraw/excalidraw/scene/Scene";
|
||||||
|
|
||||||
|
import { getSelectedElements } from "./selection";
|
||||||
|
|
||||||
const isOfTargetFrame = (element: ExcalidrawElement, frameId: string) => {
|
const isOfTargetFrame = (element: ExcalidrawElement, frameId: string) => {
|
||||||
return element.frameId === frameId || element.id === frameId;
|
return element.frameId === frameId || element.id === frameId;
|
||||||
|
@ -85,11 +85,11 @@ const getTargetIndexAccountingForBinding = (
|
||||||
nextElement: ExcalidrawElement,
|
nextElement: ExcalidrawElement,
|
||||||
elements: readonly ExcalidrawElement[],
|
elements: readonly ExcalidrawElement[],
|
||||||
direction: "left" | "right",
|
direction: "left" | "right",
|
||||||
|
scene: Scene,
|
||||||
) => {
|
) => {
|
||||||
if ("containerId" in nextElement && nextElement.containerId) {
|
if ("containerId" in nextElement && nextElement.containerId) {
|
||||||
const containerElement = Scene.getScene(nextElement)!.getElement(
|
// TODO: why not to get the container from the nextElements?
|
||||||
nextElement.containerId,
|
const containerElement = scene.getElement(nextElement.containerId);
|
||||||
);
|
|
||||||
if (containerElement) {
|
if (containerElement) {
|
||||||
return direction === "left"
|
return direction === "left"
|
||||||
? Math.min(
|
? Math.min(
|
||||||
|
@ -106,8 +106,7 @@ const getTargetIndexAccountingForBinding = (
|
||||||
(binding) => binding.type !== "arrow",
|
(binding) => binding.type !== "arrow",
|
||||||
)?.id;
|
)?.id;
|
||||||
if (boundElementId) {
|
if (boundElementId) {
|
||||||
const boundTextElement =
|
const boundTextElement = scene.getElement(boundElementId);
|
||||||
Scene.getScene(nextElement)!.getElement(boundElementId);
|
|
||||||
if (boundTextElement) {
|
if (boundTextElement) {
|
||||||
return direction === "left"
|
return direction === "left"
|
||||||
? Math.min(
|
? Math.min(
|
||||||
|
@ -157,6 +156,7 @@ const getTargetIndex = (
|
||||||
* If whole frame (including all children) is being moved, supply `null`.
|
* If whole frame (including all children) is being moved, supply `null`.
|
||||||
*/
|
*/
|
||||||
containingFrame: ExcalidrawFrameLikeElement["id"] | null,
|
containingFrame: ExcalidrawFrameLikeElement["id"] | null,
|
||||||
|
scene: Scene,
|
||||||
) => {
|
) => {
|
||||||
const sourceElement = elements[boundaryIndex];
|
const sourceElement = elements[boundaryIndex];
|
||||||
|
|
||||||
|
@ -196,8 +196,12 @@ const getTargetIndex = (
|
||||||
sourceElement?.groupIds.join("") === nextElement?.groupIds.join("")
|
sourceElement?.groupIds.join("") === nextElement?.groupIds.join("")
|
||||||
) {
|
) {
|
||||||
return (
|
return (
|
||||||
getTargetIndexAccountingForBinding(nextElement, elements, direction) ??
|
getTargetIndexAccountingForBinding(
|
||||||
candidateIndex
|
nextElement,
|
||||||
|
elements,
|
||||||
|
direction,
|
||||||
|
scene,
|
||||||
|
) ?? candidateIndex
|
||||||
);
|
);
|
||||||
} else if (!nextElement?.groupIds.includes(appState.editingGroupId)) {
|
} else if (!nextElement?.groupIds.includes(appState.editingGroupId)) {
|
||||||
// candidate element is outside current editing group → prevent
|
// candidate element is outside current editing group → prevent
|
||||||
|
@ -220,8 +224,12 @@ const getTargetIndex = (
|
||||||
|
|
||||||
if (!nextElement.groupIds.length) {
|
if (!nextElement.groupIds.length) {
|
||||||
return (
|
return (
|
||||||
getTargetIndexAccountingForBinding(nextElement, elements, direction) ??
|
getTargetIndexAccountingForBinding(
|
||||||
candidateIndex
|
nextElement,
|
||||||
|
elements,
|
||||||
|
direction,
|
||||||
|
scene,
|
||||||
|
) ?? candidateIndex
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,6 +269,7 @@ const shiftElementsByOne = (
|
||||||
elements: readonly ExcalidrawElement[],
|
elements: readonly ExcalidrawElement[],
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
direction: "left" | "right",
|
direction: "left" | "right",
|
||||||
|
scene: Scene,
|
||||||
) => {
|
) => {
|
||||||
const indicesToMove = getIndicesToMove(elements, appState);
|
const indicesToMove = getIndicesToMove(elements, appState);
|
||||||
const targetElementsMap = getTargetElementsMap(elements, indicesToMove);
|
const targetElementsMap = getTargetElementsMap(elements, indicesToMove);
|
||||||
|
@ -295,6 +304,7 @@ const shiftElementsByOne = (
|
||||||
boundaryIndex,
|
boundaryIndex,
|
||||||
direction,
|
direction,
|
||||||
containingFrame,
|
containingFrame,
|
||||||
|
scene,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (targetIndex === -1 || boundaryIndex === targetIndex) {
|
if (targetIndex === -1 || boundaryIndex === targetIndex) {
|
||||||
|
@ -508,15 +518,17 @@ function shiftElementsAccountingForFrames(
|
||||||
export const moveOneLeft = (
|
export const moveOneLeft = (
|
||||||
allElements: readonly ExcalidrawElement[],
|
allElements: readonly ExcalidrawElement[],
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
|
scene: Scene,
|
||||||
) => {
|
) => {
|
||||||
return shiftElementsByOne(allElements, appState, "left");
|
return shiftElementsByOne(allElements, appState, "left", scene);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const moveOneRight = (
|
export const moveOneRight = (
|
||||||
allElements: readonly ExcalidrawElement[],
|
allElements: readonly ExcalidrawElement[],
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
|
scene: Scene,
|
||||||
) => {
|
) => {
|
||||||
return shiftElementsByOne(allElements, appState, "right");
|
return shiftElementsByOne(allElements, appState, "right", scene);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const moveAllLeft = (
|
export const moveAllLeft = (
|
||||||
|
|
|
@ -24,9 +24,9 @@ export const actionSendBackward = register({
|
||||||
keywords: ["move down", "zindex", "layer"],
|
keywords: ["move down", "zindex", "layer"],
|
||||||
icon: SendBackwardIcon,
|
icon: SendBackwardIcon,
|
||||||
trackEvent: { category: "element" },
|
trackEvent: { category: "element" },
|
||||||
perform: (elements, appState) => {
|
perform: (elements, appState, value, app) => {
|
||||||
return {
|
return {
|
||||||
elements: moveOneLeft(elements, appState),
|
elements: moveOneLeft(elements, appState, app.scene),
|
||||||
appState,
|
appState,
|
||||||
captureUpdate: CaptureUpdateAction.IMMEDIATELY,
|
captureUpdate: CaptureUpdateAction.IMMEDIATELY,
|
||||||
};
|
};
|
||||||
|
@ -54,9 +54,9 @@ export const actionBringForward = register({
|
||||||
keywords: ["move up", "zindex", "layer"],
|
keywords: ["move up", "zindex", "layer"],
|
||||||
icon: BringForwardIcon,
|
icon: BringForwardIcon,
|
||||||
trackEvent: { category: "element" },
|
trackEvent: { category: "element" },
|
||||||
perform: (elements, appState) => {
|
perform: (elements, appState, value, app) => {
|
||||||
return {
|
return {
|
||||||
elements: moveOneRight(elements, appState),
|
elements: moveOneRight(elements, appState, app.scene),
|
||||||
appState,
|
appState,
|
||||||
captureUpdate: CaptureUpdateAction.IMMEDIATELY,
|
captureUpdate: CaptureUpdateAction.IMMEDIATELY,
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,6 @@ import {
|
||||||
} from "@excalidraw/common";
|
} from "@excalidraw/common";
|
||||||
import { isNonDeletedElement } from "@excalidraw/element";
|
import { isNonDeletedElement } from "@excalidraw/element";
|
||||||
import { isFrameLikeElement } from "@excalidraw/element/typeChecks";
|
import { isFrameLikeElement } from "@excalidraw/element/typeChecks";
|
||||||
|
|
||||||
import { getElementsInGroup } from "@excalidraw/element/groups";
|
import { getElementsInGroup } from "@excalidraw/element/groups";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue