tldraw/state/commands/duplicate.ts
Steve Ruiz 48c659a68a Trims default false boolean props
This should reduce the size of files / saves, for example.
2021-07-04 09:47:37 +01:00

53 lines
1.2 KiB
TypeScript

import Command from './command'
import history from '../history'
import { Data } from 'types'
import { deepClone } from 'utils'
import tld from 'utils/tld'
import { uniqueId } from 'utils/utils'
import vec from 'utils/vec'
export default function duplicateCommand(data: Data): void {
const selectedShapes = tld.getSelectedShapes(data).map(deepClone)
const duplicates = selectedShapes.map((shape) => ({
...shape,
id: uniqueId(),
point: vec.add(
shape.point,
vec.div([16, 16], tld.getCurrentCamera(data).zoom)
),
}))
history.execute(
data,
new Command({
name: 'duplicate_shapes',
category: 'canvas',
manualSelection: true,
do(data) {
const { shapes } = tld.getPage(data)
for (const duplicate of duplicates) {
shapes[duplicate.id] = duplicate
}
tld.setSelectedIds(
data,
duplicates.map((d) => d.id)
)
},
undo(data) {
const { shapes } = tld.getPage(data)
for (const duplicate of duplicates) {
delete shapes[duplicate.id]
}
tld.setSelectedIds(
data,
selectedShapes.map((d) => d.id)
)
},
})
)
}