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-29 12:00:59 +00:00
import tld from 'utils/tld'
2021-06-21 13:13:16 +00:00
// 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) {
2021-06-29 12:00:59 +00:00
const { shapes } = tld.getPage(data)
2021-06-21 13:13:16 +00:00
after.forEach((shape) => {
shapes[shape.id] = shape
getShapeUtils(shape).onSessionComplete(shape)
})
2021-06-29 12:00:59 +00:00
tld.updateParents(
data,
after.map((shape) => shape.id)
)
2021-06-21 13:13:16 +00:00
},
undo(data) {
2021-06-29 12:00:59 +00:00
const { shapes } = tld.getPage(data)
2021-06-21 13:13:16 +00:00
before.forEach((shape) => {
shapes[shape.id] = shape
getShapeUtils(shape).onSessionComplete(shape)
})
2021-06-29 12:00:59 +00:00
tld.updateParents(
2021-06-21 13:13:16 +00:00
data,
before.map((shape) => shape.id)
)
},
})
)
}