tldraw/state/commands/generate.ts

36 lines
977 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-29 12:00:59 +00:00
import { deepClone } from 'utils'
import tld from 'utils/tld'
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-29 12:00:59 +00:00
const initialShapes = tld
.getShapes(data)
2021-06-24 12:34:43 +00:00
.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) {
2021-06-29 12:00:59 +00:00
const { shapes } = tld.getPage(data)
2021-06-24 12:34:43 +00:00
initialShapes.forEach((shape) => delete shapes[shape.id])
generatedShapes.forEach((shape) => (shapes[shape.id] = shape))
2021-06-29 12:00:59 +00:00
tld.setSelectedIds(data, [])
2021-05-15 13:02:13 +00:00
},
undo(data) {
2021-06-29 12:00:59 +00:00
const { shapes } = tld.getPage(data)
2021-06-24 12:34:43 +00:00
generatedShapes.forEach((shape) => delete shapes[shape.id])
initialShapes.forEach((shape) => (shapes[shape.id] = shape))
2021-06-29 12:00:59 +00:00
tld.setSelectedIds(data, [])
2021-05-15 13:02:13 +00:00
},
})
)
}