tldraw/state/sessions/transform-session.ts

144 lines
3.6 KiB
TypeScript
Raw Normal View History

import { Data, TransformEdge, TransformCorner } from "types"
2021-05-14 12:44:23 +00:00
import * as vec from "utils/vec"
import BaseSession from "./base-session"
import commands from "state/commands"
import { current } from "immer"
import { getShapeUtils } from "lib/shapes"
import {
getCommonBounds,
getRelativeTransformedBoundingBox,
getTransformedBoundingBox,
} from "utils/utils"
2021-05-14 12:44:23 +00:00
export default class TransformSession extends BaseSession {
scaleX = 1
scaleY = 1
2021-05-14 12:44:23 +00:00
transformType: TransformEdge | TransformCorner
origin: number[]
snapshot: TransformSnapshot
constructor(
data: Data,
2021-05-15 13:02:13 +00:00
transformType: TransformCorner | TransformEdge,
2021-05-14 12:44:23 +00:00
point: number[]
) {
super(data)
this.origin = point
2021-05-15 13:02:13 +00:00
this.transformType = transformType
this.snapshot = getTransformSnapshot(data, transformType)
2021-05-14 12:44:23 +00:00
}
update(data: Data, point: number[]) {
const { transformType } = this
2021-05-18 10:45:29 +00:00
const { currentPageId, selectedIds, shapeBounds, initialBounds } =
this.snapshot
2021-05-14 12:44:23 +00:00
const newBoundingBox = getTransformedBoundingBox(
initialBounds,
transformType,
vec.vec(this.origin, point),
data.boundsRotation
)
2021-05-14 12:44:23 +00:00
this.scaleX = newBoundingBox.scaleX
this.scaleY = newBoundingBox.scaleY
2021-05-14 12:44:23 +00:00
2021-05-14 21:05:21 +00:00
// Now work backward to calculate a new bounding box for each of the shapes.
2021-05-14 12:44:23 +00:00
selectedIds.forEach((id) => {
2021-05-14 21:05:21 +00:00
const { initialShape, initialShapeBounds } = shapeBounds[id]
2021-05-14 12:44:23 +00:00
const newShapeBounds = getRelativeTransformedBoundingBox(
newBoundingBox,
initialBounds,
initialShapeBounds,
this.scaleX < 0,
this.scaleY < 0
)
2021-05-14 12:44:23 +00:00
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, newShapeBounds, {
type: this.transformType,
2021-05-14 21:05:21 +00:00
initialShape,
scaleX: this.scaleX,
scaleY: this.scaleY,
2021-05-15 13:02:13 +00:00
})
2021-05-14 21:05:21 +00:00
})
2021-05-14 12:44:23 +00:00
}
cancel(data: Data) {
const { currentPageId, selectedIds, shapeBounds } = this.snapshot
2021-05-14 21:05:21 +00:00
selectedIds.forEach((id) => {
const shape = data.document.pages[currentPageId].shapes[id]
2021-05-17 21:27:18 +00:00
2021-05-14 21:05:21 +00:00
const { initialShape, initialShapeBounds } = shapeBounds[id]
2021-05-15 13:02:13 +00:00
getShapeUtils(shape).transform(shape, initialShapeBounds, {
type: this.transformType,
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
})
2021-05-14 12:44:23 +00:00
}
complete(data: Data) {
2021-05-15 13:02:13 +00:00
commands.transform(
data,
this.snapshot,
getTransformSnapshot(data, this.transformType),
this.scaleX,
this.scaleY
2021-05-15 13:02:13 +00:00
)
2021-05-14 12:44:23 +00:00
}
}
2021-05-15 13:02:13 +00:00
export function getTransformSnapshot(
data: Data,
transformType: TransformEdge | TransformCorner
) {
2021-05-14 12:44:23 +00:00
const {
document: { pages },
selectedIds,
currentPageId,
} = current(data)
2021-05-14 21:05:21 +00:00
const pageShapes = pages[currentPageId].shapes
2021-05-14 12:44:23 +00:00
// A mapping of selected shapes and their bounds
const shapesBounds = Object.fromEntries(
Array.from(selectedIds.values()).map((id) => {
2021-05-14 21:05:21 +00:00
const shape = pageShapes[id]
2021-05-14 12:44:23 +00:00
return [shape.id, getShapeUtils(shape).getBounds(shape)]
})
)
// The common (exterior) bounds of the selected shapes
2021-05-14 21:05:21 +00:00
const bounds = getCommonBounds(...Object.values(shapesBounds))
2021-05-14 12:44:23 +00:00
// Return a mapping of shapes to bounds together with the relative
// positions of the shape's bounds within the common bounds shape.
return {
2021-05-15 13:02:13 +00:00
type: transformType,
currentPageId,
2021-05-14 12:44:23 +00:00
selectedIds: new Set(selectedIds),
initialBounds: bounds,
2021-05-14 12:44:23 +00:00
shapeBounds: Object.fromEntries(
Array.from(selectedIds.values()).map((id) => {
return [
id,
{
2021-05-14 21:05:21 +00:00
initialShape: pageShapes[id],
initialShapeBounds: shapesBounds[id],
2021-05-14 12:44:23 +00:00
},
]
})
),
}
}
export type TransformSnapshot = ReturnType<typeof getTransformSnapshot>