2021-05-28 20:30:27 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
|
|
|
import { Data } from 'types'
|
2021-06-03 12:06:39 +00:00
|
|
|
import { getCurrentCamera, getPage, getSelectedShapes } from 'utils/utils'
|
2021-05-28 20:30:27 +00:00
|
|
|
import { v4 as uuid } from 'uuid'
|
|
|
|
import { current } from 'immer'
|
|
|
|
import * as vec from 'utils/vec'
|
|
|
|
|
|
|
|
export default function duplicateCommand(data: Data) {
|
|
|
|
const { currentPageId } = data
|
|
|
|
const selectedShapes = getSelectedShapes(current(data))
|
|
|
|
const duplicates = selectedShapes.map((shape) => ({
|
|
|
|
...shape,
|
|
|
|
id: uuid(),
|
2021-06-03 12:06:39 +00:00
|
|
|
point: vec.add(shape.point, vec.div([16, 16], getCurrentCamera(data).zoom)),
|
2021-05-28 20:30:27 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
|
|
|
name: 'duplicate_shapes',
|
|
|
|
category: 'canvas',
|
|
|
|
manualSelection: true,
|
|
|
|
do(data) {
|
|
|
|
const { shapes } = getPage(data, currentPageId)
|
|
|
|
|
|
|
|
data.selectedIds.clear()
|
|
|
|
|
|
|
|
for (const duplicate of duplicates) {
|
|
|
|
shapes[duplicate.id] = duplicate
|
|
|
|
data.selectedIds.add(duplicate.id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
undo(data) {
|
|
|
|
const { shapes } = getPage(data, currentPageId)
|
|
|
|
data.selectedIds.clear()
|
|
|
|
|
|
|
|
for (const duplicate of duplicates) {
|
|
|
|
delete shapes[duplicate.id]
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let id in selectedShapes) {
|
|
|
|
data.selectedIds.add(id)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|