tldraw/state/commands/generate.ts

34 lines
960 B
TypeScript
Raw Normal View History

import Command from './command'
import history from '../history'
2021-06-21 21:35:28 +00:00
import { Data, Shape } from 'types'
2021-06-24 12:34:43 +00:00
import { deepClone, getPage, getShapes, setSelectedIds } from 'utils'
2021-05-15 13:02:13 +00:00
2021-05-19 09:35:00 +00:00
export default function generateCommand(
2021-05-15 13:02:13 +00:00
data: Data,
generatedShapes: Shape[]
2021-06-21 21:35:28 +00:00
): void {
2021-06-24 12:34:43 +00:00
const initialShapes = getShapes(data)
.filter((shape) => shape.isGenerated)
.map(deepClone)
2021-05-15 13:02:13 +00:00
history.execute(
data,
new Command({
2021-06-19 17:22:46 +00:00
name: 'generate_shapes',
category: 'canvas',
2021-05-15 13:02:13 +00:00
do(data) {
const { shapes } = getPage(data)
2021-06-24 12:34:43 +00:00
initialShapes.forEach((shape) => delete shapes[shape.id])
generatedShapes.forEach((shape) => (shapes[shape.id] = shape))
setSelectedIds(data, [])
2021-05-15 13:02:13 +00:00
},
undo(data) {
const { shapes } = getPage(data)
2021-06-24 12:34:43 +00:00
generatedShapes.forEach((shape) => delete shapes[shape.id])
initialShapes.forEach((shape) => (shapes[shape.id] = shape))
setSelectedIds(data, [])
2021-05-15 13:02:13 +00:00
},
})
)
}