tldraw/state/commands/transform-single.ts

70 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-05-19 09:35:00 +00:00
import Command from "./command"
import history from "../history"
import { Data, Corner, Edge } 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"
import { getPage } from "utils/utils"
2021-05-19 09:35:00 +00:00
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
) {
const shape = getPage(data, after.currentPageId).shapes[after.id]
2021-05-19 21:24:41 +00:00
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, type, initialShape, initialShapeBounds } = after
const { shapes } = getPage(data, after.currentPageId)
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) {
shapes[id] = shape
2021-05-19 21:24:41 +00:00
} else {
getShapeUtils(shape).transformSingle(shape, initialShapeBounds, {
type,
initialShape,
scaleX,
scaleY,
transformOrigin: [0.5, 0.5],
2021-05-19 21:24:41 +00:00
})
}
2021-05-19 09:35:00 +00:00
},
undo(data) {
const { id, type, initialShapeBounds } = before
const { shapes } = getPage(data, before.currentPageId)
2021-05-19 21:24:41 +00:00
data.selectedIds.clear()
2021-05-19 09:35:00 +00:00
2021-05-19 21:24:41 +00:00
if (isCreating) {
delete shapes[id]
2021-05-19 21:24:41 +00:00
} else {
const shape = shapes[id]
2021-05-19 21:24:41 +00:00
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,
transformOrigin: [0.5, 0.5],
2021-05-19 21:24:41 +00:00
})
}
2021-05-19 09:35:00 +00:00
},
})
)
}