Tldraw/state/commands/transform.ts

67 wiersze
1.7 KiB
TypeScript
Czysty Zwykły widok Historia

2021-05-29 12:40:41 +00:00
import Command from './command'
import history from '../history'
import { Data } from 'types'
import { TransformSnapshot } from 'state/sessions/transform-session'
import { getShapeUtils } from 'lib/shape-utils'
2021-06-04 16:08:43 +00:00
import { getPage, updateParents } from 'utils/utils'
2021-05-14 21:05:21 +00:00
2021-05-19 09:35:00 +00:00
export default function transformCommand(
2021-05-14 21:05:21 +00:00
data: Data,
before: TransformSnapshot,
2021-05-31 19:13:43 +00:00
after: TransformSnapshot,
scaleX: number,
scaleY: number
2021-05-14 21:05:21 +00:00
) {
history.execute(
data,
new Command({
2021-05-29 12:40:41 +00:00
name: 'translate_shapes',
category: 'canvas',
2021-05-31 20:44:21 +00:00
do(data, isInitial) {
if (isInitial) return
2021-05-29 12:40:41 +00:00
const { type, shapeBounds } = after
const { shapes } = getPage(data)
2021-05-14 21:05:21 +00:00
2021-05-29 12:40:41 +00:00
for (let id in shapeBounds) {
const { initialShape, initialShapeBounds, transformOrigin } =
2021-05-29 12:40:41 +00:00
shapeBounds[id]
const shape = shapes[id]
2021-05-14 21:05:21 +00:00
2021-05-15 13:02:13 +00:00
getShapeUtils(shape).transform(shape, initialShapeBounds, {
type,
2021-05-14 21:05:21 +00:00
initialShape,
2021-05-29 12:40:41 +00:00
transformOrigin,
2021-05-31 19:13:43 +00:00
scaleX,
scaleY,
2021-05-15 13:02:13 +00:00
})
2021-05-29 12:40:41 +00:00
}
2021-06-04 16:08:43 +00:00
updateParents(data, Object.keys(shapeBounds))
2021-05-14 21:05:21 +00:00
},
undo(data) {
2021-05-29 12:40:41 +00:00
const { type, shapeBounds } = before
const { shapes } = getPage(data)
2021-05-14 21:05:21 +00:00
2021-05-29 12:40:41 +00:00
for (let id in shapeBounds) {
const { initialShape, initialShapeBounds, transformOrigin } =
2021-05-29 12:40:41 +00:00
shapeBounds[id]
const shape = shapes[id]
2021-05-14 21:05:21 +00:00
2021-05-15 13:02:13 +00:00
getShapeUtils(shape).transform(shape, initialShapeBounds, {
type,
2021-05-14 21:05:21 +00:00
initialShape,
2021-05-29 12:40:41 +00:00
transformOrigin,
2021-05-31 20:44:21 +00:00
scaleX: scaleX < 0 ? scaleX * -1 : scaleX,
scaleY: scaleX < 0 ? scaleX * -1 : scaleX,
2021-05-15 13:02:13 +00:00
})
2021-05-29 12:40:41 +00:00
}
2021-06-04 16:08:43 +00:00
updateParents(data, Object.keys(shapeBounds))
2021-05-14 21:05:21 +00:00
},
})
)
}