tldraw/state/commands/reset-bounds.ts

38 lines
1 KiB
TypeScript
Raw Normal View History

import Command from './command'
import history from '../history'
import { Data } from 'types'
2021-06-24 08:18:14 +00:00
import { getPage, getSelectedShapes, updateParents } from 'utils'
import { current } from 'immer'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
2021-06-21 21:35:28 +00:00
export default function resetBoundsCommand(data: Data): void {
const initialShapes = Object.fromEntries(
getSelectedShapes(current(data)).map((shape) => [shape.id, shape])
)
history.execute(
data,
new Command({
name: 'reset_bounds',
category: 'canvas',
do(data) {
getSelectedShapes(data).forEach((shape) => {
2021-06-18 15:31:46 +00:00
if (shape.isLocked) return
getShapeUtils(shape).onBoundsReset(shape)
})
updateParents(data, Object.keys(initialShapes))
},
undo(data) {
const page = getPage(data)
getSelectedShapes(data).forEach((shape) => {
2021-06-18 15:31:46 +00:00
if (shape.isLocked) return
page.shapes[shape.id] = initialShapes[shape.id]
})
updateParents(data, Object.keys(initialShapes))
},
})
)
}