tldraw/state/sessions/transform-session.ts

217 lines
5.7 KiB
TypeScript
Raw Normal View History

2021-06-21 21:35:28 +00:00
import { Data, Edge, Corner } from 'types'
import vec from 'utils/vec'
2021-05-29 12:40:41 +00:00
import BaseSession from './base-session'
import commands from 'state/commands'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
2021-06-29 12:00:59 +00:00
import tld from 'utils/tld'
import {
2021-06-21 13:13:16 +00:00
deepClone,
getBoundsCenter,
getBoundsFromPoints,
getCommonBounds,
getRelativeTransformedBoundingBox,
getTransformedBoundingBox,
setToArray,
2021-06-24 08:18:14 +00:00
} from '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-06-21 21:35:28 +00:00
update(data: Data, point: number[], isAspectRatioLocked = false): void {
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
2021-06-29 12:00:59 +00:00
const { shapes } = tld.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-06-21 21:35:28 +00:00
for (const 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
})
shapes[id] = { ...shape }
2021-05-29 12:40:41 +00:00
}
2021-06-04 16:08:43 +00:00
2021-06-29 12:00:59 +00:00
tld.updateParents(data, Object.keys(shapeBounds))
2021-05-14 12:44:23 +00:00
}
2021-06-21 21:35:28 +00:00
cancel(data: Data): void {
2021-06-24 12:34:43 +00:00
const { shapeBounds } = this.snapshot
2021-05-14 21:05:21 +00:00
2021-06-29 12:00:59 +00:00
const { shapes } = tld.getPage(data)
2021-06-21 21:35:28 +00:00
for (const id in shapeBounds) {
2021-06-04 16:08:43 +00:00
const shape = 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-06-04 16:08:43 +00:00
2021-06-29 12:00:59 +00:00
tld.updateParents(data, Object.keys(shapeBounds))
2021-05-29 12:40:41 +00:00
}
2021-05-14 12:44:23 +00:00
}
2021-06-21 21:35:28 +00:00
complete(data: Data): void {
2021-06-21 13:13:16 +00:00
const { initialShapes, hasUnlockedShapes } = this.snapshot
if (!hasUnlockedShapes) return
2021-06-29 12:00:59 +00:00
const page = tld.getPage(data)
2021-06-21 13:13:16 +00:00
const finalShapes = initialShapes.map((shape) =>
deepClone(page.shapes[shape.id])
2021-05-15 13:02:13 +00:00
)
2021-06-21 13:13:16 +00:00
commands.mutate(data, initialShapes, finalShapes)
2021-05-14 12:44:23 +00:00
}
}
2021-06-21 21:35:28 +00:00
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
2021-05-23 13:46:04 +00:00
export function getTransformSnapshot(data: Data, transformType: Edge | Corner) {
2021-06-21 13:13:16 +00:00
const { currentPageId } = data
2021-06-29 12:00:59 +00:00
const page = tld.getPage(data)
2021-05-14 12:44:23 +00:00
2021-06-29 12:00:59 +00:00
const initialShapes = setToArray(tld.getSelectedIds(data))
.flatMap((id) =>
tld.getDocumentBranch(data, id).map((id) => page.shapes[id])
)
2021-06-04 21:21:03 +00:00
.filter((shape) => !shape.isLocked)
2021-06-21 13:13:16 +00:00
.map((shape) => deepClone(shape))
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(
2021-06-17 10:43:55 +00:00
(shape) =>
shape.isAspectRatioLocked || !getShapeUtils(shape).canChangeAspectRatio
2021-05-29 13:59:11 +00:00
)
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-06-21 13:13:16 +00:00
initialShapes,
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)
2021-06-21 21:35:28 +00:00
const ix = (ic[0] - initialInnerBounds.minX) / initialInnerBounds.width
const 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]
2021-06-04 21:21:03 +00:00
// function resizeDescendants(data: Data, shapeId: string, bounds: Bounds) {
// const { initialShape, initialShapeBounds, transformOrigin } =
// shapeBounds[id]
// const newShapeBounds = getRelativeTransformedBoundingBox(
// newBoundingBox,
// initialBounds,
// initialShapeBounds,
// this.scaleX < 0,
// this.scaleY < 0
// )
// const shape = shapes[id]
// getShapeUtils(shape).transform(shape, newShapeBounds, {
// type: this.transformType,
// initialShape,
// scaleX: this.scaleX,
// scaleY: this.scaleY,
// transformOrigin,
// })
// }