tldraw/state/commands/nudge.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-05-28 20:30:27 +00:00
import Command from './command'
import history from '../history'
import { Data } from 'types'
2021-06-24 08:18:14 +00:00
import { getPage, getSelectedShapes } from 'utils'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
import vec from 'utils/vec'
2021-05-28 20:30:27 +00:00
2021-06-21 21:35:28 +00:00
export default function nudgeCommand(data: Data, delta: number[]): void {
2021-05-28 20:30:27 +00:00
const selectedShapes = getSelectedShapes(data)
const shapeBounds = Object.fromEntries(
selectedShapes.map(
(shape) => [shape.id, getShapeUtils(shape).getBounds(shape)] as const
)
)
history.execute(
data,
new Command({
2021-06-19 17:22:46 +00:00
name: 'nudge_shapes',
2021-05-28 20:30:27 +00:00
category: 'canvas',
do(data) {
2021-06-24 12:34:43 +00:00
const { shapes } = getPage(data)
2021-05-28 20:30:27 +00:00
2021-06-21 21:35:28 +00:00
for (const id in shapeBounds) {
2021-05-28 20:30:27 +00:00
const shape = shapes[id]
2021-06-02 15:05:44 +00:00
getShapeUtils(shape).setProperty(
shape,
'point',
vec.add(shape.point, delta)
)
2021-05-28 20:30:27 +00:00
}
},
undo(data) {
2021-06-24 12:34:43 +00:00
const { shapes } = getPage(data)
2021-05-28 20:30:27 +00:00
2021-06-21 21:35:28 +00:00
for (const id in shapeBounds) {
2021-05-28 20:30:27 +00:00
const shape = shapes[id]
2021-06-02 15:05:44 +00:00
getShapeUtils(shape).setProperty(
shape,
'point',
vec.sub(shape.point, delta)
)
2021-05-28 20:30:27 +00:00
}
},
})
)
}