tldraw/state/sessions/transform-single-session.ts

118 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-05-29 12:54:33 +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'
2021-05-19 09:35:00 +00:00
import {
getTransformedBoundingBox,
getCommonBounds,
getRotatedCorners,
getTransformAnchor,
getPage,
getShape,
getSelectedShapes,
2021-06-04 16:08:43 +00:00
updateParents,
2021-05-29 12:54:33 +00:00
} from 'utils/utils'
2021-05-19 09:35:00 +00:00
export default class TransformSingleSession extends BaseSession {
transformType: Edge | Corner
2021-05-19 21:24:41 +00:00
origin: number[]
scaleX = 1
scaleY = 1
2021-05-19 09:35:00 +00:00
snapshot: TransformSingleSnapshot
2021-05-19 21:24:41 +00:00
isCreating: boolean
2021-05-19 09:35:00 +00:00
constructor(
data: Data,
transformType: Corner | Edge,
2021-05-19 21:24:41 +00:00
point: number[],
isCreating = false
2021-05-19 09:35:00 +00:00
) {
super(data)
this.origin = point
this.transformType = transformType
this.snapshot = getTransformSingleSnapshot(data, transformType)
2021-05-19 21:24:41 +00:00
this.isCreating = isCreating
2021-05-19 09:35:00 +00:00
}
2021-05-21 07:42:56 +00:00
update(data: Data, point: number[], isAspectRatioLocked = false) {
const { transformType } = this
2021-05-19 09:35:00 +00:00
const { initialShapeBounds, currentPageId, initialShape, id } =
this.snapshot
2021-05-19 09:35:00 +00:00
const shape = getShape(data, id, currentPageId)
2021-05-19 09:35:00 +00:00
const newBoundingBox = getTransformedBoundingBox(
initialShapeBounds,
transformType,
vec.vec(this.origin, point),
2021-05-21 07:42:56 +00:00
shape.rotation,
2021-05-23 13:46:04 +00:00
isAspectRatioLocked || !getShapeUtils(initialShape).canChangeAspectRatio
2021-05-19 09:35:00 +00:00
)
this.scaleX = newBoundingBox.scaleX
this.scaleY = newBoundingBox.scaleY
2021-05-19 09:35:00 +00:00
getShapeUtils(shape).transformSingle(shape, newBoundingBox, {
initialShape,
type: this.transformType,
scaleX: this.scaleX,
scaleY: this.scaleY,
transformOrigin: [0.5, 0.5],
2021-05-19 09:35:00 +00:00
})
2021-06-04 16:08:43 +00:00
updateParents(data, [id])
2021-05-19 09:35:00 +00:00
}
cancel(data: Data) {
const { id, initialShape, initialShapeBounds, currentPageId } =
this.snapshot
2021-05-19 09:35:00 +00:00
const shape = getShape(data, id, currentPageId)
getShapeUtils(shape).transform(shape, initialShapeBounds, {
initialShape,
type: this.transformType,
scaleX: this.scaleX,
scaleY: this.scaleY,
transformOrigin: [0.5, 0.5],
})
2021-06-04 16:08:43 +00:00
updateParents(data, [id])
2021-05-19 09:35:00 +00:00
}
complete(data: Data) {
2021-05-29 12:54:33 +00:00
if (!this.snapshot.hasUnlockedShape) return
2021-05-19 09:35:00 +00:00
commands.transformSingle(
data,
this.snapshot,
getTransformSingleSnapshot(data, this.transformType),
2021-05-19 21:24:41 +00:00
this.isCreating
2021-05-19 09:35:00 +00:00
)
}
}
export function getTransformSingleSnapshot(
data: Data,
transformType: Edge | Corner
2021-05-19 09:35:00 +00:00
) {
const shape = getSelectedShapes(current(data))[0]
2021-05-19 09:35:00 +00:00
const bounds = getShapeUtils(shape).getBounds(shape)
return {
id: shape.id,
2021-05-29 12:54:33 +00:00
hasUnlockedShape: !shape.isLocked,
currentPageId: data.currentPageId,
2021-05-19 09:35:00 +00:00
type: transformType,
initialShape: shape,
initialShapeBounds: bounds,
2021-05-19 09:35:00 +00:00
}
}
export type TransformSingleSnapshot = ReturnType<
typeof getTransformSingleSnapshot
>