Spaces:
Paused
Paused
Download packages/element/src/align.ts from Leon4gr45/excalidraw: direct link, hf CLI and curl.
- Browser
- Download file 2.23 kB
-
https://huggingface.co/spaces/Leon4gr45/excalidraw/resolve/main/packages/element/src/align.ts
- Command line
-
hf download hf://spaces/Leon4gr45/excalidraw/packages/element/src/align.ts
-
curl -L -o align.ts https://huggingface.co/spaces/Leon4gr45/excalidraw/resolve/main/packages/element/src/align.ts
2.23 kB
| import type { AppState } from "@excalidraw/excalidraw/types"; | |
| import { updateBoundElements } from "./binding"; | |
| import { getCommonBoundingBox } from "./bounds"; | |
| import { getSelectedElementsByGroup } from "./groups"; | |
| import type { Scene } from "./Scene"; | |
| import type { BoundingBox } from "./bounds"; | |
| import type { ExcalidrawElement } from "./types"; | |
| export interface Alignment { | |
| position: "start" | "center" | "end"; | |
| axis: "x" | "y"; | |
| } | |
| export const alignElements = ( | |
| selectedElements: ExcalidrawElement[], | |
| alignment: Alignment, | |
| scene: Scene, | |
| appState: Readonly<AppState>, | |
| ): ExcalidrawElement[] => { | |
| const groups: ExcalidrawElement[][] = getSelectedElementsByGroup( | |
| selectedElements, | |
| scene.getNonDeletedElementsMap(), | |
| appState, | |
| ); | |
| const selectionBoundingBox = getCommonBoundingBox(selectedElements); | |
| return groups.flatMap((group) => { | |
| const translation = calculateTranslation( | |
| group, | |
| selectionBoundingBox, | |
| alignment, | |
| ); | |
| return group.map((element) => { | |
| // update element | |
| const updatedEle = scene.mutateElement(element, { | |
| x: element.x + translation.x, | |
| y: element.y + translation.y, | |
| }); | |
| // update bound elements | |
| updateBoundElements(element, scene, { | |
| simultaneouslyUpdated: group, | |
| }); | |
| return updatedEle; | |
| }); | |
| }); | |
| }; | |
| const calculateTranslation = ( | |
| group: ExcalidrawElement[], | |
| selectionBoundingBox: BoundingBox, | |
| { 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, | |
| }; | |
| }; | |