tldraw/state/sessions/transform-session.ts

180 lines
4.6 KiB
TypeScript
Raw Normal View History

2021-05-29 12:40:41 +00:00
import { Data, Edge, Corner } from 'types'
import * as vec from 'utils/vec'
import BaseSession from './base-session'
import commands from 'state/commands'
import { current } from 'immer'
import { getShapeUtils } from 'lib/shape-utils'
import {
getBoundsCenter,
getBoundsFromPoints,
getCommonBounds,
getPage,
getRelativeTransformedBoundingBox,
2021-05-29 12:40:41 +00:00
getSelectedShapes,
getShapes,
getTransformedBoundingBox,
2021-05-29 12:40:41 +00:00
} from 'utils/utils'
2021-05-14 12:44:23 +00:00
export default class TransformSession extends BaseSession {
scaleX = 1
scaleY = 1
2021-05-23 13:46:04 +00:00
transformType: Edge | Corner
2021-05-14 12:44:23 +00:00
origin: number[]
snapshot: TransformSnapshot
2021-05-23 13:46:04 +00:00
constructor(data: Data, transformType: Corner | Edge, point: number[]) {
2021-05-14 12:44:23 +00:00
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
}
2021-05-21 07:42:56 +00:00
update(data: Data, point: number[], isAspectRatioLocked = false) {
const { transformType } = this
2021-05-18 10:45:29 +00:00
2021-05-29 13:59:11 +00:00
const { shapeBounds, initialBounds, isAllAspectRatioLocked } = this.snapshot
const { shapes } = getPage(data)
2021-05-14 12:44:23 +00:00
const newBoundingBox = getTransformedBoundingBox(
initialBounds,
transformType,
vec.vec(this.origin, point),
2021-05-21 07:42:56 +00:00
data.boundsRotation,
2021-05-29 13:59:11 +00:00
isAspectRatioLocked || isAllAspectRatioLocked
)
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
2021-05-29 12:40:41 +00:00
for (let id in shapeBounds) {
const { initialShape, initialShapeBounds, transformOrigin } =
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 = shapes[id]
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,
transformOrigin,
2021-05-15 13:02:13 +00:00
})
2021-05-29 12:40:41 +00:00
}
2021-05-14 12:44:23 +00:00
}
cancel(data: Data) {
2021-05-29 12:40:41 +00:00
const { currentPageId, shapeBounds } = this.snapshot
2021-05-14 21:05:21 +00:00
const page = getPage(data, currentPageId)
2021-05-29 12:40:41 +00:00
for (let id in shapeBounds) {
const shape = page.shapes[id]
2021-05-17 21:27:18 +00:00
const { initialShape, initialShapeBounds, transformOrigin } =
shapeBounds[id]
2021-05-14 21:05:21 +00:00
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,
transformOrigin,
2021-05-15 13:02:13 +00:00
})
2021-05-29 12:40:41 +00:00
}
2021-05-14 12:44:23 +00:00
}
complete(data: Data) {
2021-05-29 12:54:33 +00:00
if (!this.snapshot.hasUnlockedShapes) return
2021-05-15 13:02:13 +00:00
commands.transform(
data,
this.snapshot,
2021-05-31 19:13:43 +00:00
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-23 13:46:04 +00:00
export function getTransformSnapshot(data: Data, transformType: Edge | Corner) {
2021-05-29 12:40:41 +00:00
const cData = current(data)
const { currentPageId } = cData
2021-05-14 12:44:23 +00:00
2021-05-29 12:40:41 +00:00
const initialShapes = getSelectedShapes(cData).filter(
(shape) => !shape.isLocked
)
2021-05-14 21:05:21 +00:00
2021-05-29 12:54:33 +00:00
const hasUnlockedShapes = initialShapes.length > 0
2021-05-29 13:59:11 +00:00
const isAllAspectRatioLocked = initialShapes.every(
(shape) => shape.isAspectRatioLocked
)
2021-05-14 12:44:23 +00:00
const shapesBounds = Object.fromEntries(
2021-05-29 12:40:41 +00:00
initialShapes.map((shape) => [
shape.id,
getShapeUtils(shape).getBounds(shape),
])
2021-05-14 12:44:23 +00:00
)
const boundsArr = Object.values(shapesBounds)
2021-05-29 12:54:33 +00:00
const commonBounds = getCommonBounds(...boundsArr)
const initialInnerBounds = getBoundsFromPoints(boundsArr.map(getBoundsCenter))
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,
2021-05-29 12:54:33 +00:00
hasUnlockedShapes,
2021-05-29 13:59:11 +00:00
isAllAspectRatioLocked,
currentPageId,
2021-05-29 12:54:33 +00:00
initialBounds: commonBounds,
2021-05-14 12:44:23 +00:00
shapeBounds: Object.fromEntries(
2021-05-29 12:40:41 +00:00
initialShapes.map((shape) => {
const initialShapeBounds = shapesBounds[shape.id]
const ic = getBoundsCenter(initialShapeBounds)
let ix = (ic[0] - initialInnerBounds.minX) / initialInnerBounds.width
let iy = (ic[1] - initialInnerBounds.minY) / initialInnerBounds.height
2021-05-14 12:44:23 +00:00
return [
2021-05-29 12:40:41 +00:00
shape.id,
2021-05-14 12:44:23 +00:00
{
2021-05-23 13:46:04 +00:00
initialShape: shape,
initialShapeBounds,
transformOrigin: [ix, iy],
2021-05-14 12:44:23 +00:00
},
]
})
),
}
}
export type TransformSnapshot = ReturnType<typeof getTransformSnapshot>
2021-05-22 20:35:01 +00:00
// const transformOrigins = {
// [Edge.Top]: [0.5, 1],
// [Edge.Right]: [0, 0.5],
// [Edge.Bottom]: [0.5, 0],
// [Edge.Left]: [1, 0.5],
// [Corner.TopLeft]: [1, 1],
// [Corner.TopRight]: [0, 1],
// [Corner.BottomLeft]: [1, 0],
// [Corner.BottomRight]: [0, 0],
// }
// const origin = transformOrigins[this.transformType]