2021-05-28 20:30:27 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
|
|
|
import { Data } from 'types'
|
|
|
|
import { getPage, getSelectedShapes } from 'utils/utils'
|
|
|
|
import { getShapeUtils } from 'lib/shape-utils'
|
|
|
|
import * as vec from 'utils/vec'
|
|
|
|
|
|
|
|
export default function nudgeCommand(data: Data, delta: number[]) {
|
|
|
|
const { currentPageId } = data
|
|
|
|
const selectedShapes = getSelectedShapes(data)
|
|
|
|
const shapeBounds = Object.fromEntries(
|
|
|
|
selectedShapes.map(
|
|
|
|
(shape) => [shape.id, getShapeUtils(shape).getBounds(shape)] as const
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
|
|
|
name: 'set_direction',
|
|
|
|
category: 'canvas',
|
|
|
|
do(data) {
|
|
|
|
const { shapes } = getPage(data, currentPageId)
|
|
|
|
|
|
|
|
for (let id in shapeBounds) {
|
|
|
|
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) {
|
|
|
|
const { shapes } = getPage(data, currentPageId)
|
|
|
|
|
|
|
|
for (let id in shapeBounds) {
|
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|