tldraw/state/commands/style.ts

37 lines
1 KiB
TypeScript
Raw Normal View History

import Command from './command'
import history from '../history'
import { Data, ShapeStyles } from 'types'
import { 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-05-26 10:34:10 +00:00
const { currentPageId } = data
const initialShapes = getSelectedShapes(current(data))
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)
for (const { id } of initialShapes) {
const shape = shapes[id]
getShapeUtils(shape).applyStyles(shape, styles)
}
},
undo(data) {
const { shapes } = getPage(data, currentPageId)
for (const { id, style } of initialShapes) {
const shape = shapes[id]
getShapeUtils(shape).applyStyles(shape, style)
}
},
})
)
}