tldraw/state/commands/align.ts

103 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-06-02 15:05:44 +00:00
import Command from './command'
import history from '../history'
import { AlignType, Data } from 'types'
import { getCommonBounds, getPage, getSelectedShapes } from 'utils/utils'
import { getShapeUtils } from 'lib/shape-utils'
2021-05-26 19:20:52 +00:00
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({
2021-06-02 15:05:44 +00:00
name: 'aligned',
category: 'canvas',
2021-05-26 19:20:52 +00:00
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]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).translateTo(shape, [
2021-05-26 19:34:57 +00:00
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]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).translateTo(shape, [
2021-05-26 19:34:57 +00:00
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]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).translateTo(shape, [
2021-05-26 19:34:57 +00:00
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]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).translateTo(shape, [
2021-05-26 19:34:57 +00:00
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]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).translateTo(shape, [
2021-05-26 19:34:57 +00:00
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]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).translateTo(shape, [
2021-05-26 19:34:57 +00:00
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]
2021-06-04 16:08:43 +00:00
getShapeUtils(shape).translateTo(shape, [
2021-05-26 19:34:57 +00:00
initialBounds.minX,
initialBounds.minY,
])
2021-05-26 19:20:52 +00:00
}
},
})
)
}