tldraw/state/commands/mutate.ts

50 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-06-21 13:13:16 +00:00
import Command from './command'
import history from '../history'
import { Data, Shape } from 'types'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
2021-06-21 13:13:16 +00:00
import { getPage, updateParents } from 'utils/utils'
// Used when changing the properties of one or more shapes,
// without changing selection or deleting any shapes.
export default function mutateShapesCommand(
data: Data,
before: Shape[],
after: Shape[],
name = 'mutate_shapes'
2021-06-21 21:35:28 +00:00
): void {
2021-06-21 13:13:16 +00:00
history.execute(
data,
new Command({
name,
category: 'canvas',
do(data) {
const { shapes } = getPage(data)
after.forEach((shape) => {
shapes[shape.id] = shape
getShapeUtils(shape).onSessionComplete(shape)
})
// updateParents(
// data,
// after.map((shape) => shape.id)
// )
},
undo(data) {
const { shapes } = getPage(data)
before.forEach((shape) => {
shapes[shape.id] = shape
getShapeUtils(shape).onSessionComplete(shape)
})
updateParents(
data,
before.map((shape) => shape.id)
)
},
})
)
}