tldraw/state/commands/rotate.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-05-29 22:27:19 +00:00
import Command from './command'
import history from '../history'
import { Data } from 'types'
import { RotateSnapshot } from 'state/sessions/rotate-session'
2021-06-24 08:18:14 +00:00
import { getPage } from 'utils'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
2021-05-17 21:27:18 +00:00
2021-05-19 09:35:00 +00:00
export default function rotateCommand(
2021-05-17 21:27:18 +00:00
data: Data,
before: RotateSnapshot,
after: RotateSnapshot
2021-06-21 21:35:28 +00:00
): void {
2021-05-17 21:27:18 +00:00
history.execute(
data,
new Command({
2021-06-19 17:22:46 +00:00
name: 'rotate_shapes',
2021-05-29 22:27:19 +00:00
category: 'canvas',
2021-05-17 21:27:18 +00:00
do(data) {
const { shapes } = getPage(data)
2021-05-17 21:27:18 +00:00
2021-06-21 21:35:28 +00:00
for (const { id, point, rotation } of after.initialShapes) {
2021-05-18 08:32:20 +00:00
const shape = shapes[id]
2021-06-05 14:29:49 +00:00
getShapeUtils(shape)
.rotateBy(shape, rotation - shape.rotation)
2021-06-05 07:42:17 +00:00
.translateTo(shape, point)
2021-06-05 14:29:49 +00:00
.onSessionComplete(shape)
2021-05-17 21:27:18 +00:00
}
2021-05-18 08:32:20 +00:00
data.boundsRotation = after.boundsRotation
2021-05-17 21:27:18 +00:00
},
undo(data) {
2021-06-24 12:34:43 +00:00
const { shapes } = getPage(data)
2021-05-17 21:27:18 +00:00
2021-06-21 21:35:28 +00:00
for (const { id, point, rotation } of before.initialShapes) {
2021-05-18 08:32:20 +00:00
const shape = shapes[id]
2021-06-05 14:29:49 +00:00
getShapeUtils(shape)
.rotateBy(shape, rotation - shape.rotation)
2021-06-05 07:42:17 +00:00
.translateTo(shape, point)
2021-06-05 14:29:49 +00:00
.onSessionComplete(shape)
2021-05-17 21:27:18 +00:00
}
2021-05-18 08:32:20 +00:00
data.boundsRotation = before.boundsRotation
2021-05-17 21:27:18 +00:00
},
})
)
}