tldraw/state/commands/toggle.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

import Command from './command'
import history from '../history'
import { Data, Shape } from 'types'
2021-06-24 08:18:14 +00:00
import { getPage, getSelectedShapes } from 'utils'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
import { PropsOfType } from 'types'
export default function toggleCommand(
data: Data,
2021-06-21 21:35:28 +00:00
prop: PropsOfType<Shape>
): void {
const { currentPageId } = data
const selectedShapes = getSelectedShapes(data)
const isAllToggled = selectedShapes.every((shape) => shape[prop])
const initialShapes = Object.fromEntries(
selectedShapes.map((shape) => [shape.id, shape[prop]])
)
history.execute(
data,
new Command({
2021-06-19 17:22:46 +00:00
name: 'toggle_prop',
category: 'canvas',
do(data) {
const { shapes } = getPage(data, currentPageId)
for (const id in initialShapes) {
const shape = shapes[id]
getShapeUtils(shape).setProperty(
shape,
prop,
isAllToggled ? false : true
)
}
},
undo(data) {
const { shapes } = getPage(data, currentPageId)
for (const id in initialShapes) {
const shape = shapes[id]
getShapeUtils(shape).setProperty(shape, prop, initialShapes[id])
}
},
})
)
}