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.1KB

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