tldraw/state/commands/align.ts

103 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-05-26 19:20:52 +00:00
import Command from "./command"
import history from "../history"
import { AlignType, Data } from "types"
2021-05-26 19:34:57 +00:00
import { getCommonBounds, getPage, getSelectedShapes } from "utils/utils"
2021-05-26 19:20:52 +00:00
import { getShapeUtils } from "lib/shape-utils"
export default function alignCommand(data: Data, type: AlignType) {
const { currentPageId } = data
2021-05-26 19:34:57 +00:00
const selectedShapes = getSelectedShapes(data)
const entries = selectedShapes.map(
(shape) => [shape.id, getShapeUtils(shape).getBounds(shape)] as const
2021-05-26 19:20:52 +00:00
)
2021-05-26 19:34:57 +00:00
const boundsForShapes = Object.fromEntries(entries)
const commonBounds = getCommonBounds(...entries.map((entry) => entry[1]))
const midX = commonBounds.minX + commonBounds.width / 2
const midY = commonBounds.minY + commonBounds.height / 2
2021-05-26 19:20:52 +00:00
history.execute(
data,
new Command({
name: "aligned",
category: "canvas",
do(data) {
const { shapes } = getPage(data, currentPageId)
switch (type) {
case AlignType.Top: {
2021-05-26 19:34:57 +00:00
for (let id in boundsForShapes) {
const shape = shapes[id]
getShapeUtils(shape).translateTo(shape, [
shape.point[0],
commonBounds.minY,
])
}
2021-05-26 19:20:52 +00:00
break
}
case AlignType.CenterVertical: {
2021-05-26 19:34:57 +00:00
for (let id in boundsForShapes) {
const shape = shapes[id]
getShapeUtils(shape).translateTo(shape, [
shape.point[0],
midY - boundsForShapes[id].height / 2,
])
}
2021-05-26 19:20:52 +00:00
break
}
case AlignType.Bottom: {
2021-05-26 19:34:57 +00:00
for (let id in boundsForShapes) {
const shape = shapes[id]
getShapeUtils(shape).translateTo(shape, [
shape.point[0],
commonBounds.maxY - boundsForShapes[id].height,
])
}
2021-05-26 19:20:52 +00:00
break
}
case AlignType.Left: {
2021-05-26 19:34:57 +00:00
for (let id in boundsForShapes) {
const shape = shapes[id]
getShapeUtils(shape).translateTo(shape, [
commonBounds.minX,
shape.point[1],
])
}
2021-05-26 19:20:52 +00:00
break
}
case AlignType.CenterHorizontal: {
2021-05-26 19:34:57 +00:00
for (let id in boundsForShapes) {
const shape = shapes[id]
getShapeUtils(shape).translateTo(shape, [
midX - boundsForShapes[id].width / 2,
shape.point[1],
])
}
2021-05-26 19:20:52 +00:00
break
}
case AlignType.Right: {
2021-05-26 19:34:57 +00:00
for (let id in boundsForShapes) {
const shape = shapes[id]
getShapeUtils(shape).translateTo(shape, [
commonBounds.maxX - boundsForShapes[id].width,
shape.point[1],
])
}
2021-05-26 19:20:52 +00:00
break
}
}
},
undo(data) {
const { shapes } = getPage(data, currentPageId)
2021-05-26 19:34:57 +00:00
for (let id in boundsForShapes) {
2021-05-26 19:20:52 +00:00
const shape = shapes[id]
2021-05-26 19:34:57 +00:00
const initialBounds = boundsForShapes[id]
getShapeUtils(shape).translateTo(shape, [
initialBounds.minX,
initialBounds.minY,
])
2021-05-26 19:20:52 +00:00
}
},
})
)
}