You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
4 years ago
|
import { ExcalidrawElement } from "./element/types";
|
||
|
import { newElementWith } from "./element/mutateElement";
|
||
2 years ago
|
import { BoundingBox, getCommonBoundingBox } from "./element/bounds";
|
||
3 years ago
|
import { getMaximumGroups } from "./groups";
|
||
4 years ago
|
|
||
|
export interface Alignment {
|
||
|
position: "start" | "center" | "end";
|
||
|
axis: "x" | "y";
|
||
|
}
|
||
|
|
||
|
export const alignElements = (
|
||
|
selectedElements: ExcalidrawElement[],
|
||
|
alignment: Alignment,
|
||
|
): ExcalidrawElement[] => {
|
||
|
const groups: ExcalidrawElement[][] = getMaximumGroups(selectedElements);
|
||
|
|
||
|
const selectionBoundingBox = getCommonBoundingBox(selectedElements);
|
||
|
|
||
|
return groups.flatMap((group) => {
|
||
|
const translation = calculateTranslation(
|
||
|
group,
|
||
|
selectionBoundingBox,
|
||
|
alignment,
|
||
|
);
|
||
|
return group.map((element) =>
|
||
|
newElementWith(element, {
|
||
|
x: element.x + translation.x,
|
||
|
y: element.y + translation.y,
|
||
|
}),
|
||
|
);
|
||
|
});
|
||
|
};
|
||
|
|
||
|
const calculateTranslation = (
|
||
|
group: ExcalidrawElement[],
|
||
2 years ago
|
selectionBoundingBox: BoundingBox,
|
||
4 years ago
|
{ axis, position }: Alignment,
|
||
|
): { x: number; y: number } => {
|
||
|
const groupBoundingBox = getCommonBoundingBox(group);
|
||
|
|
||
|
const [min, max]: ["minX" | "minY", "maxX" | "maxY"] =
|
||
|
axis === "x" ? ["minX", "maxX"] : ["minY", "maxY"];
|
||
|
|
||
|
const noTranslation = { x: 0, y: 0 };
|
||
|
if (position === "start") {
|
||
|
return {
|
||
|
...noTranslation,
|
||
|
[axis]: selectionBoundingBox[min] - groupBoundingBox[min],
|
||
|
};
|
||
|
} else if (position === "end") {
|
||
|
return {
|
||
|
...noTranslation,
|
||
|
[axis]: selectionBoundingBox[max] - groupBoundingBox[max],
|
||
|
};
|
||
|
} // else if (position === "center") {
|
||
|
return {
|
||
|
...noTranslation,
|
||
|
[axis]:
|
||
|
(selectionBoundingBox[min] + selectionBoundingBox[max]) / 2 -
|
||
|
(groupBoundingBox[min] + groupBoundingBox[max]) / 2,
|
||
|
};
|
||
|
};
|