tldraw/state/commands/style.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

import Command from './command'
import history from '../history'
import { Data, ShapeStyles } from 'types'
2021-06-04 17:56:46 +00:00
import { getDocumentBranch, getPage, getSelectedShapes } from 'utils/utils'
import { getShapeUtils } from 'lib/shape-utils'
import { current } from 'immer'
2021-05-26 10:34:10 +00:00
export default function styleCommand(data: Data, styles: Partial<ShapeStyles>) {
2021-06-04 17:56:46 +00:00
const cData = current(data)
const page = getPage(cData)
const { currentPageId } = cData
const shapesToStyle = Array.from(data.selectedIds.values())
.flatMap((id) => getDocumentBranch(data, id))
.map((id) => page.shapes[id])
2021-05-26 10:34:10 +00:00
history.execute(
data,
new Command({
name: 'changed_style',
category: 'canvas',
2021-05-26 10:34:10 +00:00
manualSelection: true,
do(data) {
const { shapes } = getPage(data, currentPageId)
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) {
const { shapes } = getPage(data, currentPageId)
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)
}
},
})
)
}