tldraw/state/commands/transform-single.ts

65 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-05-19 09:35:00 +00:00
import Command from "./command"
import history from "../history"
import { Data, TransformCorner, TransformEdge } from "types"
2021-05-20 09:49:40 +00:00
import { getShapeUtils } from "lib/shape-utils"
2021-05-19 21:24:41 +00:00
import { current } from "immer"
2021-05-19 09:35:00 +00:00
import { TransformSingleSnapshot } from "state/sessions/transform-single-session"
export default function transformSingleCommand(
data: Data,
before: TransformSingleSnapshot,
after: TransformSingleSnapshot,
scaleX: number,
2021-05-19 21:24:41 +00:00
scaleY: number,
isCreating: boolean
2021-05-19 09:35:00 +00:00
) {
2021-05-19 21:24:41 +00:00
const shape =
current(data).document.pages[after.currentPageId].shapes[after.id]
2021-05-19 09:35:00 +00:00
history.execute(
data,
new Command({
name: "transform_single_shape",
2021-05-19 09:35:00 +00:00
category: "canvas",
2021-05-19 21:24:41 +00:00
manualSelection: true,
2021-05-19 09:35:00 +00:00
do(data) {
const { id, currentPageId, type, initialShape, initialShapeBounds } =
after
2021-05-19 09:35:00 +00:00
2021-05-19 21:24:41 +00:00
data.selectedIds.clear()
data.selectedIds.add(id)
2021-05-19 09:35:00 +00:00
2021-05-19 21:24:41 +00:00
if (isCreating) {
data.document.pages[currentPageId].shapes[id] = shape
} else {
getShapeUtils(shape).transformSingle(shape, initialShapeBounds, {
type,
initialShape,
scaleX,
scaleY,
})
}
2021-05-19 09:35:00 +00:00
},
undo(data) {
2021-05-19 21:24:41 +00:00
const { id, currentPageId, type, initialShapeBounds } = before
data.selectedIds.clear()
2021-05-19 09:35:00 +00:00
2021-05-19 21:24:41 +00:00
if (isCreating) {
delete data.document.pages[currentPageId].shapes[id]
} else {
const shape = data.document.pages[currentPageId].shapes[id]
data.selectedIds.add(id)
2021-05-19 09:35:00 +00:00
2021-05-19 21:24:41 +00:00
getShapeUtils(shape).transform(shape, initialShapeBounds, {
type,
initialShape: after.initialShape,
scaleX: 1,
scaleY: 1,
})
}
2021-05-19 09:35:00 +00:00
},
})
)
}