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

110 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-05-29 12:54:33 +00:00
import { Data, Edge, Corner } from 'types'
import vec from 'utils/vec'
2021-05-29 12:54:33 +00:00
import BaseSession from './base-session'
import commands from 'state/commands'
import { current } from 'immer'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
2021-05-19 09:35:00 +00:00
import {
getTransformedBoundingBox,
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-06-21 21:35:28 +00:00
update(data: Data, point: number[], isAspectRatioLocked = false): void {
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
data.document.pages[data.currentPageId].shapes[shape.id] = { ...shape }
2021-06-04 16:08:43 +00:00
updateParents(data, [id])
2021-05-19 09:35:00 +00:00
}
2021-06-21 21:35:28 +00:00
cancel(data: Data): void {
2021-06-05 19:36:46 +00:00
const { id, initialShape } = this.snapshot
2021-06-05 19:36:46 +00:00
const page = getPage(data)
page.shapes[id] = initialShape
2021-06-04 16:08:43 +00:00
updateParents(data, [id])
2021-05-19 09:35:00 +00:00
}
2021-06-21 21:35:28 +00:00
complete(data: Data): void {
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
)
}
}
2021-06-21 21:35:28 +00:00
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
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
>