mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
Further remove refactor
This commit is contained in:
parent
b3946aa3b4
commit
272b418445
1 changed files with 86 additions and 3 deletions
|
@ -1,6 +1,8 @@
|
||||||
import { pointFrom, pointRotateRads } from "@excalidraw/math";
|
import { pointFrom, pointRotateRads } from "@excalidraw/math";
|
||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
|
|
||||||
|
import { isTextElement } from "@excalidraw/element/typeChecks";
|
||||||
|
|
||||||
import { getCommonBounds } from "@excalidraw/element/bounds";
|
import { getCommonBounds } from "@excalidraw/element/bounds";
|
||||||
|
|
||||||
import type { ElementsMap, ExcalidrawElement } from "@excalidraw/element/types";
|
import type { ElementsMap, ExcalidrawElement } from "@excalidraw/element/types";
|
||||||
|
@ -12,14 +14,12 @@ import {
|
||||||
getAtomicUnits,
|
getAtomicUnits,
|
||||||
getStepSizedValue,
|
getStepSizedValue,
|
||||||
isPropertyEditable,
|
isPropertyEditable,
|
||||||
moveElements,
|
|
||||||
moveGroupTo,
|
|
||||||
STEP_SIZE,
|
STEP_SIZE,
|
||||||
type AtomicUnit,
|
|
||||||
} from "./utils";
|
} from "./utils";
|
||||||
import { getElementsInAtomicUnit, moveElement } from "./utils";
|
import { getElementsInAtomicUnit, moveElement } from "./utils";
|
||||||
|
|
||||||
import type { DragInputCallbackType } from "./DragInput";
|
import type { DragInputCallbackType } from "./DragInput";
|
||||||
|
import type { AtomicUnit } from "./utils";
|
||||||
import type { AppState } from "../../types";
|
import type { AppState } from "../../types";
|
||||||
|
|
||||||
interface MultiPositionProps {
|
interface MultiPositionProps {
|
||||||
|
@ -31,6 +31,89 @@ interface MultiPositionProps {
|
||||||
appState: AppState;
|
appState: AppState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const moveElements = (
|
||||||
|
property: MultiPositionProps["property"],
|
||||||
|
changeInTopX: number,
|
||||||
|
changeInTopY: number,
|
||||||
|
originalElements: readonly ExcalidrawElement[],
|
||||||
|
originalElementsMap: ElementsMap,
|
||||||
|
scene: Scene,
|
||||||
|
) => {
|
||||||
|
for (let i = 0; i < originalElements.length; i++) {
|
||||||
|
const origElement = originalElements[i];
|
||||||
|
|
||||||
|
const [cx, cy] = [
|
||||||
|
origElement.x + origElement.width / 2,
|
||||||
|
origElement.y + origElement.height / 2,
|
||||||
|
];
|
||||||
|
const [topLeftX, topLeftY] = pointRotateRads(
|
||||||
|
pointFrom(origElement.x, origElement.y),
|
||||||
|
pointFrom(cx, cy),
|
||||||
|
origElement.angle,
|
||||||
|
);
|
||||||
|
|
||||||
|
const newTopLeftX =
|
||||||
|
property === "x" ? Math.round(topLeftX + changeInTopX) : topLeftX;
|
||||||
|
|
||||||
|
const newTopLeftY =
|
||||||
|
property === "y" ? Math.round(topLeftY + changeInTopY) : topLeftY;
|
||||||
|
|
||||||
|
moveElement(
|
||||||
|
newTopLeftX,
|
||||||
|
newTopLeftY,
|
||||||
|
origElement,
|
||||||
|
scene,
|
||||||
|
originalElementsMap,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const moveGroupTo = (
|
||||||
|
nextX: number,
|
||||||
|
nextY: number,
|
||||||
|
originalElements: ExcalidrawElement[],
|
||||||
|
originalElementsMap: ElementsMap,
|
||||||
|
scene: Scene,
|
||||||
|
) => {
|
||||||
|
const elementsMap = scene.getNonDeletedElementsMap();
|
||||||
|
const [x1, y1, ,] = getCommonBounds(originalElements);
|
||||||
|
const offsetX = nextX - x1;
|
||||||
|
const offsetY = nextY - y1;
|
||||||
|
|
||||||
|
for (let i = 0; i < originalElements.length; i++) {
|
||||||
|
const origElement = originalElements[i];
|
||||||
|
|
||||||
|
const latestElement = elementsMap.get(origElement.id);
|
||||||
|
if (!latestElement) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// bound texts are moved with their containers
|
||||||
|
if (!isTextElement(latestElement) || !latestElement.containerId) {
|
||||||
|
const [cx, cy] = [
|
||||||
|
latestElement.x + latestElement.width / 2,
|
||||||
|
latestElement.y + latestElement.height / 2,
|
||||||
|
];
|
||||||
|
|
||||||
|
const [topLeftX, topLeftY] = pointRotateRads(
|
||||||
|
pointFrom(latestElement.x, latestElement.y),
|
||||||
|
pointFrom(cx, cy),
|
||||||
|
latestElement.angle,
|
||||||
|
);
|
||||||
|
|
||||||
|
moveElement(
|
||||||
|
topLeftX + offsetX,
|
||||||
|
topLeftY + offsetY,
|
||||||
|
origElement,
|
||||||
|
scene,
|
||||||
|
originalElementsMap,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handlePositionChange: DragInputCallbackType<
|
const handlePositionChange: DragInputCallbackType<
|
||||||
MultiPositionProps["property"]
|
MultiPositionProps["property"]
|
||||||
> = ({
|
> = ({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue