2021-05-14 21:05:21 +00:00
|
|
|
import {
|
|
|
|
Data,
|
|
|
|
TransformEdge,
|
|
|
|
TransformCorner,
|
|
|
|
Bounds,
|
|
|
|
BoundsSnapshot,
|
|
|
|
} 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"
|
|
|
|
import { getShapeUtils } from "lib/shapes"
|
|
|
|
import { getCommonBounds } from "utils/utils"
|
|
|
|
|
|
|
|
export default class TransformSession extends BaseSession {
|
|
|
|
delta = [0, 0]
|
|
|
|
transformType: TransformEdge | TransformCorner
|
|
|
|
origin: number[]
|
|
|
|
snapshot: TransformSnapshot
|
|
|
|
corners: {
|
|
|
|
a: number[]
|
|
|
|
b: number[]
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
data: Data,
|
|
|
|
type: TransformCorner | TransformEdge,
|
|
|
|
point: number[]
|
|
|
|
) {
|
|
|
|
super(data)
|
|
|
|
this.origin = point
|
|
|
|
this.transformType = type
|
|
|
|
this.snapshot = getTransformSnapshot(data)
|
|
|
|
|
|
|
|
const { minX, minY, maxX, maxY } = this.snapshot.initialBounds
|
|
|
|
|
|
|
|
this.corners = {
|
|
|
|
a: [minX, minY],
|
|
|
|
b: [maxX, maxY],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update(data: Data, point: number[]) {
|
|
|
|
const {
|
2021-05-14 21:05:21 +00:00
|
|
|
shapeBounds,
|
|
|
|
initialBounds,
|
|
|
|
currentPageId,
|
|
|
|
selectedIds,
|
|
|
|
} = this.snapshot
|
|
|
|
|
|
|
|
const { shapes } = data.document.pages[currentPageId]
|
2021-05-14 12:44:23 +00:00
|
|
|
|
|
|
|
let [x, y] = point
|
2021-05-14 21:05:21 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
corners: { a, b },
|
|
|
|
transformType,
|
|
|
|
} = this
|
2021-05-14 12:44:23 +00:00
|
|
|
|
|
|
|
// Edge Transform
|
|
|
|
|
|
|
|
switch (transformType) {
|
|
|
|
case TransformEdge.Top: {
|
2021-05-14 21:05:21 +00:00
|
|
|
a[1] = y
|
2021-05-14 12:44:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case TransformEdge.Right: {
|
2021-05-14 21:05:21 +00:00
|
|
|
b[0] = x
|
2021-05-14 12:44:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case TransformEdge.Bottom: {
|
2021-05-14 21:05:21 +00:00
|
|
|
b[1] = y
|
2021-05-14 12:44:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case TransformEdge.Left: {
|
2021-05-14 21:05:21 +00:00
|
|
|
a[0] = x
|
2021-05-14 12:44:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case TransformCorner.TopLeft: {
|
2021-05-14 21:05:21 +00:00
|
|
|
a[1] = y
|
|
|
|
a[0] = x
|
2021-05-14 12:44:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case TransformCorner.TopRight: {
|
2021-05-14 21:05:21 +00:00
|
|
|
b[0] = x
|
|
|
|
a[1] = y
|
2021-05-14 12:44:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case TransformCorner.BottomRight: {
|
2021-05-14 21:05:21 +00:00
|
|
|
b[1] = y
|
|
|
|
b[0] = x
|
2021-05-14 12:44:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
case TransformCorner.BottomLeft: {
|
2021-05-14 21:05:21 +00:00
|
|
|
a[0] = x
|
|
|
|
b[1] = y
|
2021-05-14 12:44:23 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 21:05:21 +00:00
|
|
|
// Calculate new common (externior) bounding box
|
2021-05-14 12:44:23 +00:00
|
|
|
const newBounds = {
|
2021-05-14 21:05:21 +00:00
|
|
|
minX: Math.min(a[0], b[0]),
|
|
|
|
minY: Math.min(a[1], b[1]),
|
|
|
|
maxX: Math.max(a[0], b[0]),
|
|
|
|
maxY: Math.max(a[1], b[1]),
|
|
|
|
width: Math.abs(b[0] - a[0]),
|
|
|
|
height: Math.abs(b[1] - a[1]),
|
2021-05-14 12:44:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 21:05:21 +00:00
|
|
|
const isFlippedX = b[0] < a[0]
|
|
|
|
const isFlippedY = b[1] < a[1]
|
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-14 21:05:21 +00:00
|
|
|
const { initialShape, initialShapeBounds } = shapeBounds[id]
|
|
|
|
const { nx, nmx, nw, ny, nmy, nh } = initialShapeBounds
|
|
|
|
const shape = shapes[id]
|
2021-05-14 12:44:23 +00:00
|
|
|
|
|
|
|
const minX = newBounds.minX + (isFlippedX ? nmx : nx) * newBounds.width
|
|
|
|
const minY = newBounds.minY + (isFlippedY ? nmy : ny) * newBounds.height
|
|
|
|
const width = nw * newBounds.width
|
|
|
|
const height = nh * newBounds.height
|
|
|
|
|
2021-05-14 21:05:21 +00:00
|
|
|
const newShapeBounds = {
|
2021-05-14 12:44:23 +00:00
|
|
|
minX,
|
|
|
|
minY,
|
|
|
|
maxX: minX + width,
|
|
|
|
maxY: minY + height,
|
|
|
|
width,
|
|
|
|
height,
|
2021-05-14 21:05:21 +00:00
|
|
|
isFlippedX,
|
|
|
|
isFlippedY,
|
|
|
|
}
|
2021-05-14 12:44:23 +00:00
|
|
|
|
2021-05-14 21:05:21 +00:00
|
|
|
// Pass the new data to the shape's transform utility for mutation.
|
|
|
|
// Most shapes should be able to transform using only the bounding box,
|
|
|
|
// however some shapes (e.g. those with internal points) will need more
|
|
|
|
// data here too.
|
|
|
|
|
|
|
|
getShapeUtils(shape).transform(
|
|
|
|
shape,
|
|
|
|
newShapeBounds,
|
|
|
|
initialShape,
|
|
|
|
initialShapeBounds,
|
|
|
|
initialBounds
|
|
|
|
)
|
|
|
|
})
|
2021-05-14 12:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel(data: Data) {
|
2021-05-14 21:05:21 +00:00
|
|
|
const {
|
|
|
|
shapeBounds,
|
|
|
|
initialBounds,
|
|
|
|
currentPageId,
|
|
|
|
selectedIds,
|
|
|
|
} = this.snapshot
|
2021-05-14 12:44:23 +00:00
|
|
|
|
2021-05-14 21:05:21 +00:00
|
|
|
const { shapes } = data.document.pages[currentPageId]
|
|
|
|
|
|
|
|
selectedIds.forEach((id) => {
|
|
|
|
const shape = shapes.shapes[id]
|
|
|
|
const { initialShape, initialShapeBounds } = shapeBounds[id]
|
|
|
|
|
|
|
|
getShapeUtils(shape).transform(
|
|
|
|
shape,
|
|
|
|
{
|
|
|
|
...initialShapeBounds,
|
|
|
|
isFlippedX: false,
|
|
|
|
isFlippedY: false,
|
|
|
|
},
|
|
|
|
initialShape,
|
|
|
|
initialShapeBounds,
|
|
|
|
initialBounds
|
|
|
|
)
|
|
|
|
})
|
2021-05-14 12:44:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
complete(data: Data) {
|
2021-05-14 21:05:21 +00:00
|
|
|
commands.transform(data, this.snapshot, getTransformSnapshot(data))
|
2021-05-14 12:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTransformSnapshot(data: Data) {
|
|
|
|
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)]
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
// The common (exterior) bounds of the selected shapes
|
2021-05-14 21:05:21 +00:00
|
|
|
const bounds = getCommonBounds(...Object.values(shapesBounds))
|
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 {
|
|
|
|
currentPageId,
|
|
|
|
initialBounds: bounds,
|
|
|
|
selectedIds: new Set(selectedIds),
|
|
|
|
shapeBounds: Object.fromEntries(
|
|
|
|
Array.from(selectedIds.values()).map((id) => {
|
|
|
|
const { minX, minY, width, height } = shapesBounds[id]
|
|
|
|
return [
|
|
|
|
id,
|
|
|
|
{
|
2021-05-14 21:05:21 +00:00
|
|
|
initialShape: pageShapes[id],
|
|
|
|
initialShapeBounds: {
|
|
|
|
...shapesBounds[id],
|
|
|
|
nx: (minX - bounds.minX) / bounds.width,
|
|
|
|
ny: (minY - bounds.minY) / bounds.height,
|
|
|
|
nmx: 1 - (minX + width - bounds.minX) / bounds.width,
|
|
|
|
nmy: 1 - (minY + height - bounds.minY) / bounds.height,
|
|
|
|
nw: width / bounds.width,
|
|
|
|
nh: height / bounds.height,
|
|
|
|
},
|
2021-05-14 12:44:23 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
})
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type TransformSnapshot = ReturnType<typeof getTransformSnapshot>
|