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

49 lines
1.1 KiB
TypeScript

import Command from './command'
import history from '../history'
import { Data, Shape } from 'types'
import { getShapeUtils } from 'state/shape-utils'
import tld from 'utils/tld'
// 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'
): void {
history.execute(
data,
new Command({
name,
category: 'canvas',
do(data) {
const { shapes } = tld.getPage(data)
after.forEach((shape) => {
shapes[shape.id] = shape
getShapeUtils(shape).onSessionComplete(shape)
})
tld.updateParents(
data,
after.map((shape) => shape.id)
)
},
undo(data) {
const { shapes } = tld.getPage(data)
before.forEach((shape) => {
shapes[shape.id] = shape
getShapeUtils(shape).onSessionComplete(shape)
})
tld.updateParents(
data,
before.map((shape) => shape.id)
)
},
})
)
}