Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ungroup.ts 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Command from './command'
  2. import history from '../history'
  3. import { Data, GroupShape, Shape, ShapeType } from 'types'
  4. import {
  5. getCommonBounds,
  6. getPage,
  7. getSelectedShapes,
  8. getShape,
  9. } from 'utils/utils'
  10. import { current } from 'immer'
  11. import { createShape, getShapeUtils } from 'lib/shape-utils'
  12. import { PropsOfType } from 'types'
  13. import { v4 as uuid } from 'uuid'
  14. export default function ungroupCommand(data: Data) {
  15. const cData = current(data)
  16. const { currentPageId, selectedIds } = cData
  17. const selectedGroups = getSelectedShapes(cData)
  18. .filter((shape) => shape.type === ShapeType.Group)
  19. .sort((a, b) => a.childIndex - b.childIndex)
  20. // Are all of the shapes already in the same group?
  21. // - ungroup the shapes
  22. // Otherwise...
  23. // - remove the shapes from any existing group and add them to a new one
  24. history.execute(
  25. data,
  26. new Command({
  27. name: 'ungroup_shapes',
  28. category: 'canvas',
  29. do(data) {
  30. const { shapes } = getPage(data)
  31. // Remove shapes from old parents
  32. for (const oldGroupShape of selectedGroups) {
  33. const siblings = (
  34. oldGroupShape.parentId === currentPageId
  35. ? Object.values(shapes).filter(
  36. (shape) => shape.parentId === currentPageId
  37. )
  38. : shapes[oldGroupShape.parentId].children.map((id) => shapes[id])
  39. ).sort((a, b) => a.childIndex - b.childIndex)
  40. const trueIndex = siblings.findIndex((s) => s.id === oldGroupShape.id)
  41. let step: number
  42. if (trueIndex === siblings.length - 1) {
  43. step = 1
  44. } else {
  45. step =
  46. (siblings[trueIndex + 1].childIndex - oldGroupShape.childIndex) /
  47. (oldGroupShape.children.length + 1)
  48. }
  49. data.selectedIds.clear()
  50. // Move shapes to page
  51. oldGroupShape.children
  52. .map((id) => shapes[id])
  53. .forEach(({ id }, i) => {
  54. const shape = shapes[id]
  55. data.selectedIds.add(id)
  56. getShapeUtils(shape)
  57. .setProperty(shape, 'parentId', oldGroupShape.parentId)
  58. .setProperty(
  59. shape,
  60. 'childIndex',
  61. oldGroupShape.childIndex + step * i
  62. )
  63. })
  64. delete shapes[oldGroupShape.id]
  65. }
  66. },
  67. undo(data) {
  68. const { shapes } = getPage(data, currentPageId)
  69. selectedIds.clear()
  70. selectedGroups.forEach((group) => {
  71. selectedIds.add(group.id)
  72. shapes[group.id] = group
  73. group.children.forEach((id, i) => {
  74. const shape = shapes[id]
  75. getShapeUtils(shape)
  76. .setProperty(shape, 'parentId', group.id)
  77. .setProperty(shape, 'childIndex', i)
  78. })
  79. })
  80. },
  81. })
  82. )
  83. }
  84. function getShapeDepth(data: Data, id: string, depth = 0) {
  85. if (id === data.currentPageId) {
  86. return depth
  87. }
  88. return getShapeDepth(data, getShape(data, id).parentId, depth + 1)
  89. }