tldraw/state/commands/generate.ts
2021-06-29 13:00:59 +01:00

35 lines
977 B
TypeScript

import Command from './command'
import history from '../history'
import { Data, Shape } from 'types'
import { deepClone } from 'utils'
import tld from 'utils/tld'
export default function generateCommand(
data: Data,
generatedShapes: Shape[]
): void {
const initialShapes = tld
.getShapes(data)
.filter((shape) => shape.isGenerated)
.map(deepClone)
history.execute(
data,
new Command({
name: 'generate_shapes',
category: 'canvas',
do(data) {
const { shapes } = tld.getPage(data)
initialShapes.forEach((shape) => delete shapes[shape.id])
generatedShapes.forEach((shape) => (shapes[shape.id] = shape))
tld.setSelectedIds(data, [])
},
undo(data) {
const { shapes } = tld.getPage(data)
generatedShapes.forEach((shape) => delete shapes[shape.id])
initialShapes.forEach((shape) => (shapes[shape.id] = shape))
tld.setSelectedIds(data, [])
},
})
)
}