2021-05-29 10:12:28 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
|
|
|
import { Data, Shape } from 'types'
|
2021-06-29 12:00:59 +00:00
|
|
|
import tld from 'utils/tld'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { getShapeUtils } from 'state/shape-utils'
|
2021-05-29 10:12:28 +00:00
|
|
|
import { PropsOfType } from 'types'
|
|
|
|
|
2021-07-10 14:00:36 +00:00
|
|
|
/**
|
|
|
|
* The toggle command toggles a boolean property on all selected shapes.
|
|
|
|
* If the value is true for all selected shapes, then the property is
|
|
|
|
* set to false; the value is false for one or more of the selected
|
|
|
|
* shapes, then the value for all shapes is set to true.
|
|
|
|
*/
|
2021-05-29 10:12:28 +00:00
|
|
|
export default function toggleCommand(
|
|
|
|
data: Data,
|
2021-06-21 21:35:28 +00:00
|
|
|
prop: PropsOfType<Shape>
|
|
|
|
): void {
|
2021-06-29 12:00:59 +00:00
|
|
|
const selectedShapes = tld.getSelectedShapes(data)
|
2021-05-29 10:12:28 +00:00
|
|
|
const isAllToggled = selectedShapes.every((shape) => shape[prop])
|
2021-07-10 14:00:36 +00:00
|
|
|
const initialShapes = selectedShapes.map((shape) => ({
|
|
|
|
id: shape.id,
|
|
|
|
value: shape[prop],
|
|
|
|
}))
|
2021-05-29 10:12:28 +00:00
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
2021-06-19 17:22:46 +00:00
|
|
|
name: 'toggle_prop',
|
2021-05-29 10:12:28 +00:00
|
|
|
category: 'canvas',
|
|
|
|
do(data) {
|
2021-06-29 12:00:59 +00:00
|
|
|
const { shapes } = tld.getPage(data)
|
2021-05-29 10:12:28 +00:00
|
|
|
|
2021-07-10 14:00:36 +00:00
|
|
|
initialShapes.forEach(({ id }) => {
|
2021-05-29 10:12:28 +00:00
|
|
|
const shape = shapes[id]
|
|
|
|
getShapeUtils(shape).setProperty(
|
|
|
|
shape,
|
|
|
|
prop,
|
|
|
|
isAllToggled ? false : true
|
|
|
|
)
|
2021-07-10 14:00:36 +00:00
|
|
|
})
|
2021-05-29 10:12:28 +00:00
|
|
|
},
|
|
|
|
undo(data) {
|
2021-06-29 12:00:59 +00:00
|
|
|
const { shapes } = tld.getPage(data)
|
2021-05-29 10:12:28 +00:00
|
|
|
|
2021-07-10 14:00:36 +00:00
|
|
|
initialShapes.forEach(({ id, value }) => {
|
2021-05-29 10:12:28 +00:00
|
|
|
const shape = shapes[id]
|
2021-07-10 14:00:36 +00:00
|
|
|
getShapeUtils(shape).setProperty(shape, prop, value)
|
|
|
|
})
|
2021-05-29 10:12:28 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|