2021-05-22 15:45:24 +00:00
|
|
|
import { Data, Edge, Corner } from "types"
|
2021-05-14 12:44:23 +00:00
|
|
|
import * as vec from "utils/vec"
|
|
|
|
import BaseSession from "./base-session"
|
|
|
|
import commands from "state/commands"
|
|
|
|
import { current } from "immer"
|
2021-05-20 09:49:40 +00:00
|
|
|
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-05-22 15:45:24 +00:00
|
|
|
getPage,
|
2021-05-19 12:27:01 +00:00
|
|
|
getRelativeTransformedBoundingBox,
|
2021-05-22 15:45:24 +00:00
|
|
|
getShapes,
|
2021-05-19 12:27:01 +00:00
|
|
|
getTransformedBoundingBox,
|
|
|
|
} 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-22 15:45:24 +00:00
|
|
|
transformType: Edge | Corner | "center"
|
2021-05-14 12:44:23 +00:00
|
|
|
origin: number[]
|
|
|
|
snapshot: TransformSnapshot
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
data: Data,
|
2021-05-22 15:45:24 +00:00
|
|
|
transformType: Corner | Edge | "center",
|
2021-05-14 12:44:23 +00:00
|
|
|
point: number[]
|
|
|
|
) {
|
|
|
|
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-22 15:45:24 +00:00
|
|
|
const { selectedIds, shapeBounds, initialBounds } = this.snapshot
|
|
|
|
|
|
|
|
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,
|
|
|
|
isAspectRatioLocked
|
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
|
|
|
|
|
|
|
selectedIds.forEach((id) => {
|
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]
|
|
|
|
|
|
|
|
// 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-05-14 21:05:21 +00:00
|
|
|
|
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-14 21:05:21 +00:00
|
|
|
})
|
2021-05-14 12:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel(data: Data) {
|
2021-05-19 12:27:01 +00:00
|
|
|
const { currentPageId, selectedIds, shapeBounds } = this.snapshot
|
2021-05-14 21:05:21 +00:00
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
const page = getPage(data, currentPageId)
|
|
|
|
|
2021-05-14 21:05:21 +00:00
|
|
|
selectedIds.forEach((id) => {
|
2021-05-22 15:45:24 +00:00
|
|
|
const shape = page.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-05-14 21:05:21 +00:00
|
|
|
})
|
2021-05-14 12:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
complete(data: Data) {
|
2021-05-15 13:02:13 +00:00
|
|
|
commands.transform(
|
|
|
|
data,
|
|
|
|
this.snapshot,
|
|
|
|
getTransformSnapshot(data, this.transformType),
|
2021-05-19 12:27:01 +00:00
|
|
|
this.scaleX,
|
|
|
|
this.scaleY
|
2021-05-15 13:02:13 +00:00
|
|
|
)
|
2021-05-14 12:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 13:02:13 +00:00
|
|
|
export function getTransformSnapshot(
|
|
|
|
data: Data,
|
2021-05-22 15:45:24 +00:00
|
|
|
transformType: Edge | Corner | "center"
|
2021-05-15 13:02:13 +00:00
|
|
|
) {
|
2021-05-14 12:44:23 +00:00
|
|
|
const {
|
|
|
|
document: { pages },
|
|
|
|
selectedIds,
|
|
|
|
currentPageId,
|
|
|
|
} = current(data)
|
|
|
|
|
2021-05-14 21:05:21 +00:00
|
|
|
const pageShapes = pages[currentPageId].shapes
|
|
|
|
|
2021-05-14 12:44:23 +00:00
|
|
|
// A mapping of selected shapes and their bounds
|
|
|
|
const shapesBounds = Object.fromEntries(
|
|
|
|
Array.from(selectedIds.values()).map((id) => {
|
2021-05-14 21:05:21 +00:00
|
|
|
const shape = pageShapes[id]
|
2021-05-14 12:44:23 +00:00
|
|
|
return [shape.id, getShapeUtils(shape).getBounds(shape)]
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
const boundsArr = Object.values(shapesBounds)
|
|
|
|
|
2021-05-14 12:44:23 +00:00
|
|
|
// The common (exterior) bounds of the selected shapes
|
2021-05-22 15:45:24 +00:00
|
|
|
const bounds = 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-19 12:27:01 +00:00
|
|
|
currentPageId,
|
2021-05-14 12:44:23 +00:00
|
|
|
selectedIds: new Set(selectedIds),
|
2021-05-19 12:27:01 +00:00
|
|
|
initialBounds: bounds,
|
2021-05-14 12:44:23 +00:00
|
|
|
shapeBounds: Object.fromEntries(
|
|
|
|
Array.from(selectedIds.values()).map((id) => {
|
2021-05-22 15:45:24 +00:00
|
|
|
const initialShapeBounds = shapesBounds[id]
|
|
|
|
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 [
|
|
|
|
id,
|
|
|
|
{
|
2021-05-14 21:05:21 +00:00
|
|
|
initialShape: pageShapes[id],
|
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>
|