tldraw/state/commands/transform.ts

52 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-05-14 21:05:21 +00:00
import Command from "./command"
import history from "../history"
2021-05-15 13:02:13 +00:00
import { Data, TransformCorner, TransformEdge } 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"
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, currentPageId, selectedIds } = after
2021-05-14 21:05:21 +00:00
selectedIds.forEach((id) => {
const { initialShape, initialShapeBounds } = after.shapeBounds[id]
const shape = data.document.pages[currentPageId].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,
2021-05-15 13:02:13 +00:00
})
2021-05-14 21:05:21 +00:00
})
},
undo(data) {
const { type, currentPageId, selectedIds } = before
2021-05-14 21:05:21 +00:00
selectedIds.forEach((id) => {
const { initialShape, initialShapeBounds } = before.shapeBounds[id]
const shape = data.document.pages[currentPageId].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,
2021-05-15 13:02:13 +00:00
})
2021-05-14 21:05:21 +00:00
})
},
})
)
}