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,
|
2021-05-22 15:45:24 +00:00
|
|
|
getPage,
|
|
|
|
getShape,
|
|
|
|
getSelectedShapes,
|
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 {
|
2021-05-22 15:45:24 +00:00
|
|
|
transformType: Edge | Corner
|
2021-05-19 21:24:41 +00:00
|
|
|
origin: number[]
|
2021-05-19 12:27:01 +00:00
|
|
|
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,
|
2021-05-22 15:45:24 +00:00
|
|
|
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) {
|
2021-05-19 12:27:01 +00:00
|
|
|
const { transformType } = this
|
2021-05-19 09:35:00 +00:00
|
|
|
|
2021-05-19 12:27:01 +00:00
|
|
|
const { initialShapeBounds, currentPageId, initialShape, id } =
|
|
|
|
this.snapshot
|
2021-05-19 09:35:00 +00:00
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
const shape = getShape(data, id, currentPageId)
|
2021-05-19 09:35:00 +00:00
|
|
|
|
|
|
|
const newBoundingBox = getTransformedBoundingBox(
|
|
|
|
initialShapeBounds,
|
|
|
|
transformType,
|
2021-05-19 12:27:01 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
|
2021-05-19 12:27:01 +00:00
|
|
|
this.scaleX = newBoundingBox.scaleX
|
|
|
|
this.scaleY = newBoundingBox.scaleY
|
2021-05-19 09:35:00 +00:00
|
|
|
|
|
|
|
getShapeUtils(shape).transformSingle(shape, newBoundingBox, {
|
|
|
|
initialShape,
|
2021-05-19 12:27:01 +00:00
|
|
|
type: this.transformType,
|
|
|
|
scaleX: this.scaleX,
|
|
|
|
scaleY: this.scaleY,
|
2021-05-22 15:45:24 +00:00
|
|
|
transformOrigin: [0.5, 0.5],
|
2021-05-19 09:35:00 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel(data: Data) {
|
2021-05-19 12:27:01 +00:00
|
|
|
const { id, initialShape, initialShapeBounds, currentPageId } =
|
|
|
|
this.snapshot
|
2021-05-19 09:35:00 +00:00
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
const shape = getShape(data, id, currentPageId)
|
2021-05-19 12:27:01 +00:00
|
|
|
|
|
|
|
getShapeUtils(shape).transform(shape, initialShapeBounds, {
|
|
|
|
initialShape,
|
|
|
|
type: this.transformType,
|
|
|
|
scaleX: this.scaleX,
|
|
|
|
scaleY: this.scaleY,
|
2021-05-22 15:45:24 +00:00
|
|
|
transformOrigin: [0.5, 0.5],
|
2021-05-19 12:27:01 +00:00
|
|
|
})
|
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 12:27:01 +00:00
|
|
|
this.scaleX,
|
2021-05-19 21:24:41 +00:00
|
|
|
this.scaleY,
|
|
|
|
this.isCreating
|
2021-05-19 09:35:00 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTransformSingleSnapshot(
|
|
|
|
data: Data,
|
2021-05-22 15:45:24 +00:00
|
|
|
transformType: Edge | Corner
|
2021-05-19 09:35:00 +00:00
|
|
|
) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const shape = getSelectedShapes(current(data))[0]
|
2021-05-19 09:35:00 +00:00
|
|
|
const bounds = getShapeUtils(shape).getBounds(shape)
|
|
|
|
|
|
|
|
return {
|
2021-05-22 15:45:24 +00:00
|
|
|
id: shape.id,
|
2021-05-29 12:54:33 +00:00
|
|
|
hasUnlockedShape: !shape.isLocked,
|
2021-05-22 15:45:24 +00:00
|
|
|
currentPageId: data.currentPageId,
|
2021-05-19 09:35:00 +00:00
|
|
|
type: transformType,
|
|
|
|
initialShape: shape,
|
2021-05-19 12:27:01 +00:00
|
|
|
initialShapeBounds: bounds,
|
2021-05-19 09:35:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type TransformSingleSnapshot = ReturnType<
|
|
|
|
typeof getTransformSingleSnapshot
|
|
|
|
>
|