2021-05-28 20:30:27 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
|
|
|
import { Data } from 'types'
|
2021-06-29 12:00:59 +00:00
|
|
|
import { deepClone } from 'utils'
|
|
|
|
import tld from 'utils/tld'
|
2021-06-24 08:18:14 +00:00
|
|
|
import { uniqueId } from 'utils'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-05-28 20:30:27 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export default function duplicateCommand(data: Data): void {
|
2021-06-29 12:00:59 +00:00
|
|
|
const selectedShapes = tld.getSelectedShapes(data).map(deepClone)
|
2021-06-24 12:34:43 +00:00
|
|
|
|
2021-05-28 20:30:27 +00:00
|
|
|
const duplicates = selectedShapes.map((shape) => ({
|
|
|
|
...shape,
|
2021-06-16 12:09:45 +00:00
|
|
|
id: uniqueId(),
|
2021-06-29 12:00:59 +00:00
|
|
|
point: vec.add(
|
|
|
|
shape.point,
|
|
|
|
vec.div([16, 16], tld.getCurrentCamera(data).zoom)
|
|
|
|
),
|
2021-06-19 17:22:46 +00:00
|
|
|
isGenerated: false,
|
2021-05-28 20:30:27 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
|
|
|
name: 'duplicate_shapes',
|
|
|
|
category: 'canvas',
|
|
|
|
manualSelection: true,
|
|
|
|
do(data) {
|
2021-06-29 12:00:59 +00:00
|
|
|
const { shapes } = tld.getPage(data)
|
2021-05-28 20:30:27 +00:00
|
|
|
|
|
|
|
for (const duplicate of duplicates) {
|
|
|
|
shapes[duplicate.id] = duplicate
|
|
|
|
}
|
2021-06-07 11:18:50 +00:00
|
|
|
|
2021-06-29 12:00:59 +00:00
|
|
|
tld.setSelectedIds(
|
2021-06-07 11:18:50 +00:00
|
|
|
data,
|
|
|
|
duplicates.map((d) => d.id)
|
|
|
|
)
|
2021-05-28 20:30:27 +00:00
|
|
|
},
|
|
|
|
undo(data) {
|
2021-06-29 12:00:59 +00:00
|
|
|
const { shapes } = tld.getPage(data)
|
2021-05-28 20:30:27 +00:00
|
|
|
|
|
|
|
for (const duplicate of duplicates) {
|
|
|
|
delete shapes[duplicate.id]
|
|
|
|
}
|
|
|
|
|
2021-06-29 12:00:59 +00:00
|
|
|
tld.setSelectedIds(
|
2021-06-07 11:18:50 +00:00
|
|
|
data,
|
|
|
|
selectedShapes.map((d) => d.id)
|
|
|
|
)
|
2021-05-28 20:30:27 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|