You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

style.ts 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data, ShapeStyles } from 'types'
  4. import { getDocumentBranch, getPage, getSelectedShapes } from 'utils/utils'
  5. import { getShapeUtils } from 'lib/shape-utils'
  6. import { current } from 'immer'
  7. export default function styleCommand(data: Data, styles: Partial<ShapeStyles>) {
  8. const cData = current(data)
  9. const page = getPage(cData)
  10. const { currentPageId } = cData
  11. const shapesToStyle = Array.from(data.selectedIds.values())
  12. .flatMap((id) => getDocumentBranch(data, id))
  13. .map((id) => page.shapes[id])
  14. history.execute(
  15. data,
  16. new Command({
  17. name: 'changed_style',
  18. category: 'canvas',
  19. manualSelection: true,
  20. do(data) {
  21. const { shapes } = getPage(data, currentPageId)
  22. for (const { id } of shapesToStyle) {
  23. const shape = shapes[id]
  24. getShapeUtils(shape).applyStyles(shape, styles)
  25. }
  26. },
  27. undo(data) {
  28. const { shapes } = getPage(data, currentPageId)
  29. for (const { id, style } of shapesToStyle) {
  30. const shape = shapes[id]
  31. getShapeUtils(shape).applyStyles(shape, style)
  32. }
  33. },
  34. })
  35. )
  36. }