2021-06-18 10:14:23 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
|
|
|
import { Data, Shape } from 'types'
|
|
|
|
import {
|
|
|
|
getCommonBounds,
|
|
|
|
getPage,
|
|
|
|
getSelectedIds,
|
|
|
|
screenToWorld,
|
|
|
|
setSelectedIds,
|
|
|
|
setToArray,
|
2021-06-24 08:18:14 +00:00
|
|
|
} from 'utils'
|
|
|
|
import { uniqueId } from 'utils'
|
2021-06-18 10:14:23 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { getShapeUtils } from 'state/shape-utils'
|
2021-06-18 10:14:23 +00:00
|
|
|
import state from 'state/state'
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export default function pasteCommand(data: Data, initialShapes: Shape[]): void {
|
2021-06-18 10:14:23 +00:00
|
|
|
const center = screenToWorld(
|
|
|
|
[window.innerWidth / 2, window.innerHeight / 2],
|
|
|
|
data
|
|
|
|
)
|
|
|
|
|
|
|
|
const bounds = getCommonBounds(
|
|
|
|
...initialShapes.map((shape) =>
|
|
|
|
getShapeUtils(shape).getRotatedBounds(shape)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
const topLeft = vec.sub(center, [bounds.width / 2, bounds.height / 2])
|
|
|
|
|
|
|
|
const newIdMap = Object.fromEntries(
|
|
|
|
initialShapes.map((shape) => [shape.id, uniqueId()])
|
|
|
|
)
|
|
|
|
|
|
|
|
const oldSelectedIds = setToArray(getSelectedIds(data))
|
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
2021-06-19 17:22:46 +00:00
|
|
|
name: 'paste_new_shapes',
|
2021-06-18 10:14:23 +00:00
|
|
|
category: 'canvas',
|
|
|
|
manualSelection: true,
|
|
|
|
do(data) {
|
2021-06-24 12:34:43 +00:00
|
|
|
const { shapes } = getPage(data)
|
2021-06-18 10:14:23 +00:00
|
|
|
|
|
|
|
let childIndex =
|
|
|
|
(state.values.currentShapes[state.values.currentShapes.length - 1]
|
|
|
|
?.childIndex || 0) + 1
|
|
|
|
|
|
|
|
for (const shape of initialShapes) {
|
|
|
|
const topLeftOffset = vec.sub(shape.point, [bounds.minX, bounds.minY])
|
|
|
|
|
|
|
|
const newId = newIdMap[shape.id]
|
|
|
|
|
|
|
|
shapes[newId] = {
|
|
|
|
...shape,
|
|
|
|
id: newId,
|
|
|
|
parentId: oldSelectedIds[shape.parentId] || data.currentPageId,
|
|
|
|
childIndex: childIndex++,
|
|
|
|
point: vec.add(topLeft, topLeftOffset),
|
2021-06-23 22:32:21 +00:00
|
|
|
isGenerated: false,
|
2021-06-18 10:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setSelectedIds(data, Object.values(newIdMap))
|
|
|
|
},
|
|
|
|
undo(data) {
|
2021-06-24 12:34:43 +00:00
|
|
|
const { shapes } = getPage(data)
|
2021-06-18 10:14:23 +00:00
|
|
|
|
|
|
|
Object.values(newIdMap).forEach((id) => delete shapes[id])
|
|
|
|
|
|
|
|
setSelectedIds(data, oldSelectedIds)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|