tldraw/state/commands/transform.ts

61 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-05-14 21:05:21 +00:00
import Command from "./command"
import history from "../history"
import { Data, Corner, Edge } from "types"
2021-05-14 21:05:21 +00:00
import { TransformSnapshot } from "state/sessions/transform-session"
2021-05-20 09:49:40 +00:00
import { getShapeUtils } from "lib/shape-utils"
import { getPage } 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-15 13:02:13 +00:00
after: TransformSnapshot,
scaleX: number,
scaleY: number
2021-05-14 21:05:21 +00:00
) {
history.execute(
data,
new Command({
name: "translate_shapes",
category: "canvas",
do(data) {
const { type, selectedIds } = after
const { shapes } = getPage(data)
2021-05-14 21:05:21 +00:00
selectedIds.forEach((id) => {
const { initialShape, initialShapeBounds, transformOrigin } =
after.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,
scaleX: 1,
scaleY: 1,
transformOrigin,
2021-05-15 13:02:13 +00:00
})
2021-05-14 21:05:21 +00:00
})
},
undo(data) {
const { type, selectedIds } = before
const { shapes } = getPage(data)
2021-05-14 21:05:21 +00:00
selectedIds.forEach((id) => {
const { initialShape, initialShapeBounds, transformOrigin } =
before.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,
scaleX: 1,
scaleY: 1,
transformOrigin,
2021-05-15 13:02:13 +00:00
})
2021-05-14 21:05:21 +00:00
})
},
})
)
}