2021-06-02 11:50:34 +00:00
|
|
|
import Command from './command'
|
|
|
|
import history from '../history'
|
|
|
|
import { Data, ShapeStyles } from 'types'
|
2021-06-29 12:00:59 +00:00
|
|
|
import tld from 'utils/tld'
|
2021-07-09 08:59:43 +00:00
|
|
|
import { deepClone } from 'utils'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { getShapeUtils } from 'state/shape-utils'
|
2021-05-26 10:34:10 +00:00
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
export default function styleCommand(
|
|
|
|
data: Data,
|
|
|
|
styles: Partial<ShapeStyles>
|
|
|
|
): void {
|
2021-06-29 14:54:46 +00:00
|
|
|
const page = tld.getPage(data)
|
2021-06-04 17:56:46 +00:00
|
|
|
|
2021-07-09 08:59:43 +00:00
|
|
|
const selectedIds = [...tld.getSelectedIds(data)]
|
2021-06-07 11:18:50 +00:00
|
|
|
|
|
|
|
const shapesToStyle = selectedIds
|
2021-06-29 12:00:59 +00:00
|
|
|
.flatMap((id) => tld.getDocumentBranch(data, id))
|
2021-06-29 14:54:46 +00:00
|
|
|
.map((id) => deepClone(page.shapes[id]))
|
2021-05-26 10:34:10 +00:00
|
|
|
|
|
|
|
history.execute(
|
|
|
|
data,
|
|
|
|
new Command({
|
2021-06-19 17:22:46 +00:00
|
|
|
name: 'style_shapes',
|
2021-06-02 11:50:34 +00:00
|
|
|
category: 'canvas',
|
2021-05-26 10:34:10 +00:00
|
|
|
manualSelection: true,
|
|
|
|
do(data) {
|
2021-06-29 12:00:59 +00:00
|
|
|
const { shapes } = tld.getPage(data)
|
2021-05-26 10:34:10 +00:00
|
|
|
|
2021-06-04 17:56:46 +00:00
|
|
|
for (const { id } of shapesToStyle) {
|
2021-05-26 10:34:10 +00:00
|
|
|
const shape = shapes[id]
|
|
|
|
getShapeUtils(shape).applyStyles(shape, styles)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
undo(data) {
|
2021-06-29 12:00:59 +00:00
|
|
|
const { shapes } = tld.getPage(data)
|
2021-05-26 10:34:10 +00:00
|
|
|
|
2021-06-04 17:56:46 +00:00
|
|
|
for (const { id, style } of shapesToStyle) {
|
2021-05-26 10:34:10 +00:00
|
|
|
const shape = shapes[id]
|
|
|
|
getShapeUtils(shape).applyStyles(shape, style)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|