2021-06-04 21:21:03 +00:00
|
|
|
import { Data, Edge, Corner, Bounds } from 'types'
|
2021-05-29 12:40:41 +00:00
|
|
|
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 12:27:01 +00:00
|
|
|
import {
|
2021-05-22 15:45:24 +00:00
|
|
|
getBoundsCenter,
|
|
|
|
getBoundsFromPoints,
|
2021-05-19 12:27:01 +00:00
|
|
|
getCommonBounds,
|
2021-06-04 21:21:03 +00:00
|
|
|
getDocumentBranch,
|
2021-05-22 15:45:24 +00:00
|
|
|
getPage,
|
2021-05-19 12:27:01 +00:00
|
|
|
getRelativeTransformedBoundingBox,
|
2021-05-29 12:40:41 +00:00
|
|
|
getSelectedShapes,
|
2021-05-22 15:45:24 +00:00
|
|
|
getShapes,
|
2021-05-19 12:27:01 +00:00
|
|
|
getTransformedBoundingBox,
|
2021-06-04 16:08:43 +00:00
|
|
|
updateParents,
|
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 {
|
2021-05-19 12:27:01 +00:00
|
|
|
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) {
|
2021-05-19 12:27:01 +00:00
|
|
|
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-05-22 15:45:24 +00:00
|
|
|
|
|
|
|
const { shapes } = getPage(data)
|
2021-05-14 12:44:23 +00:00
|
|
|
|
2021-05-19 12:27:01 +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-19 12:27:01 +00:00
|
|
|
)
|
2021-05-14 12:44:23 +00:00
|
|
|
|
2021-05-19 12:27:01 +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) {
|
2021-05-22 15:45:24 +00:00
|
|
|
const { initialShape, initialShapeBounds, transformOrigin } =
|
|
|
|
shapeBounds[id]
|
2021-05-14 12:44:23 +00:00
|
|
|
|
2021-05-19 12:27:01 +00:00
|
|
|
const newShapeBounds = getRelativeTransformedBoundingBox(
|
|
|
|
newBoundingBox,
|
|
|
|
initialBounds,
|
|
|
|
initialShapeBounds,
|
|
|
|
this.scaleX < 0,
|
|
|
|
this.scaleY < 0
|
|
|
|
)
|
2021-05-14 12:44:23 +00:00
|
|
|
|
2021-05-22 15:45:24 +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,
|
2021-05-19 12:27:01 +00:00
|
|
|
scaleX: this.scaleX,
|
|
|
|
scaleY: this.scaleY,
|
2021-05-22 15:45:24 +00:00
|
|
|
transformOrigin,
|
2021-05-15 13:02:13 +00:00
|
|
|
})
|
2021-05-29 12:40:41 +00:00
|
|
|
}
|
2021-06-04 16:08:43 +00:00
|
|
|
|
|
|
|
updateParents(data, Object.keys(shapeBounds))
|
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
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
const { shapes } = getPage(data, currentPageId)
|
2021-05-22 15:45:24 +00:00
|
|
|
|
2021-05-29 12:40:41 +00:00
|
|
|
for (let id in shapeBounds) {
|
2021-06-04 16:08:43 +00:00
|
|
|
const shape = shapes[id]
|
2021-05-17 21:27:18 +00:00
|
|
|
|
2021-05-22 15:45:24 +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,
|
2021-05-19 12:27:01 +00:00
|
|
|
scaleX: 1,
|
|
|
|
scaleY: 1,
|
2021-05-22 15:45:24 +00:00
|
|
|
transformOrigin,
|
2021-05-15 13:02:13 +00:00
|
|
|
})
|
2021-06-04 16:08:43 +00:00
|
|
|
|
|
|
|
updateParents(data, Object.keys(shapeBounds))
|
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-06-04 21:21:03 +00:00
|
|
|
const page = getPage(cData)
|
2021-05-14 12:44:23 +00:00
|
|
|
|
2021-06-04 21:21:03 +00:00
|
|
|
const initialShapes = Array.from(cData.selectedIds.values())
|
|
|
|
.flatMap((id) => getDocumentBranch(cData, id).map((id) => page.shapes[id]))
|
|
|
|
.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
|
|
|
)
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
const boundsArr = Object.values(shapesBounds)
|
|
|
|
|
2021-05-29 12:54:33 +00:00
|
|
|
const commonBounds = getCommonBounds(...boundsArr)
|
2021-05-22 15:45:24 +00:00
|
|
|
|
|
|
|
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,
|
2021-05-19 12:27:01 +00:00
|
|
|
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]
|
2021-05-22 15:45:24 +00:00
|
|
|
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,
|
2021-05-22 15:45:24 +00:00
|
|
|
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,
|
|
|
|
// })
|
|
|
|
// }
|