tldraw/state/commands/transform.ts

79 lines
2 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"
import { getShapeUtils } from "lib/shapes"
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,
anchor: TransformCorner | TransformEdge
2021-05-14 21:05:21 +00:00
) {
history.execute(
data,
new Command({
name: "translate_shapes",
category: "canvas",
do(data) {
2021-05-15 13:02:13 +00:00
const {
type,
shapeBounds,
initialBounds,
currentPageId,
selectedIds,
2021-05-18 10:45:29 +00:00
boundsRotation,
2021-05-15 13:02:13 +00:00
} = after
2021-05-14 21:05:21 +00:00
const { shapes } = data.document.pages[currentPageId]
selectedIds.forEach((id) => {
const { initialShape, initialShapeBounds } = shapeBounds[id]
const shape = shapes[id]
2021-05-15 13:02:13 +00:00
getShapeUtils(shape).transform(shape, initialShapeBounds, {
type,
2021-05-14 21:05:21 +00:00
initialShape,
initialShapeBounds,
2021-05-15 13:02:13 +00:00
initialBounds,
2021-05-18 10:45:29 +00:00
boundsRotation,
2021-05-15 13:02:13 +00:00
isFlippedX: false,
isFlippedY: false,
2021-05-19 09:35:00 +00:00
isSingle: false,
2021-05-15 13:02:13 +00:00
anchor,
})
2021-05-14 21:05:21 +00:00
})
},
undo(data) {
const {
2021-05-15 13:02:13 +00:00
type,
2021-05-14 21:05:21 +00:00
shapeBounds,
initialBounds,
currentPageId,
selectedIds,
2021-05-18 10:45:29 +00:00
boundsRotation,
2021-05-14 21:05:21 +00:00
} = before
const { shapes } = data.document.pages[currentPageId]
selectedIds.forEach((id) => {
const { initialShape, initialShapeBounds } = shapeBounds[id]
const shape = shapes[id]
2021-05-15 13:02:13 +00:00
getShapeUtils(shape).transform(shape, initialShapeBounds, {
type,
2021-05-14 21:05:21 +00:00
initialShape,
initialShapeBounds,
2021-05-15 13:02:13 +00:00
initialBounds,
2021-05-18 10:45:29 +00:00
boundsRotation,
2021-05-15 13:02:13 +00:00
isFlippedX: false,
isFlippedY: false,
2021-05-19 09:35:00 +00:00
isSingle: false,
2021-05-15 13:02:13 +00:00
anchor: type,
})
2021-05-14 21:05:21 +00:00
})
},
})
)
}