2021-06-19 17:22:46 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { StretchType, Data, Corner } from 'types'
|
2021-06-24 08:18:14 +00:00
|
|
|
import { deepClone, getCommonBounds, getPage, getSelectedShapes } from 'utils'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { getShapeUtils } from 'state/shape-utils'
|
2021-05-26 19:20:52 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export default function stretchCommand(data: Data, type: StretchType): void {
|
2021-06-22 18:13:16 +00:00
|
|
|
const initialShapes = getSelectedShapes(data).map((shape) => deepClone(shape))
|
|
|
|
|
|
|
|
const snapshot = Object.fromEntries(
|
|
|
|
initialShapes.map((shape) => [
|
|
|
|
shape.id,
|
|
|
|
{
|
|
|
|
initialShape: shape,
|
|
|
|
initialBounds: getShapeUtils(shape).getBounds(shape),
|
|
|
|
},
|
|
|
|
])
|
|
|
|
)
|
|
|
|
|
|
|
|
const commonBounds = getCommonBounds(
|
|
|
|
...initialShapes.map((shape) => getShapeUtils(shape).getBounds(shape))
|
2021-05-26 19:20:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
2021-06-19 17:22:46 +00:00
|
|
|
name: 'stretched_shapes',
|
|
|
|
category: 'canvas',
|
2021-05-26 19:20:52 +00:00
|
|
|
do(data) {
|
2021-06-24 12:34:43 +00:00
|
|
|
const { shapes } = getPage(data)
|
2021-05-26 19:20:52 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case StretchType.Horizontal: {
|
2021-06-22 18:13:16 +00:00
|
|
|
Object.values(snapshot).forEach(
|
|
|
|
({ initialShape, initialBounds }) => {
|
|
|
|
const newBounds = { ...initialBounds }
|
|
|
|
newBounds.minX = commonBounds.minX
|
|
|
|
newBounds.width = commonBounds.width
|
|
|
|
newBounds.maxX = commonBounds.maxX
|
|
|
|
|
|
|
|
const shape = shapes[initialShape.id]
|
|
|
|
|
|
|
|
getShapeUtils(shape).transform(shape, newBounds, {
|
|
|
|
type: Corner.TopLeft,
|
|
|
|
scaleX: newBounds.width / initialBounds.width,
|
|
|
|
scaleY: 1,
|
|
|
|
initialShape,
|
|
|
|
transformOrigin: [0.5, 0.5],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2021-05-27 06:32:55 +00:00
|
|
|
|
|
|
|
break
|
2021-05-26 19:20:52 +00:00
|
|
|
}
|
|
|
|
case StretchType.Vertical: {
|
2021-06-22 18:13:16 +00:00
|
|
|
Object.values(snapshot).forEach(
|
|
|
|
({ initialShape, initialBounds }) => {
|
|
|
|
const newBounds = { ...initialBounds }
|
|
|
|
newBounds.minY = commonBounds.minY
|
|
|
|
newBounds.height = commonBounds.height
|
|
|
|
newBounds.maxY = commonBounds.maxY
|
2021-05-27 06:32:55 +00:00
|
|
|
|
2021-06-22 18:13:16 +00:00
|
|
|
const shape = shapes[initialShape.id]
|
|
|
|
|
|
|
|
getShapeUtils(shape).transform(shape, newBounds, {
|
|
|
|
type: Corner.TopLeft,
|
|
|
|
scaleX: 1,
|
|
|
|
scaleY: newBounds.height / initialBounds.height,
|
|
|
|
initialShape,
|
|
|
|
transformOrigin: [0.5, 0.5],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2021-05-26 19:20:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
undo(data) {
|
2021-06-24 12:34:43 +00:00
|
|
|
const { shapes } = getPage(data)
|
2021-06-22 18:13:16 +00:00
|
|
|
initialShapes.forEach((shape) => (shapes[shape.id] = shape))
|
2021-05-26 19:20:52 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|