2021-05-29 10:12:28 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
|
|
|
import { Data, Shape } from 'types'
|
|
|
|
import { getPage, getSelectedShapes } from 'utils/utils'
|
|
|
|
import { getShapeUtils } from 'lib/shape-utils'
|
|
|
|
import { PropsOfType } from 'types'
|
|
|
|
|
|
|
|
export default function toggleCommand(
|
|
|
|
data: Data,
|
|
|
|
prop: PropsOfType<Shape, boolean>
|
|
|
|
) {
|
|
|
|
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-18 15:31:46 +00:00
|
|
|
name: 'toggle_shape_prop',
|
2021-05-29 10:12:28 +00:00
|
|
|
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])
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|