Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

style.ts 1.2KB

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