Group.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { v4 as uuidv4 } from 'uuid'
  2. import MultiSelectElement from './elements/MultiSelectElement'
  3. // 编组/取消编组类
  4. export default class Group {
  5. constructor(app) {
  6. this.app = app
  7. this.groupIdToElementList = {}
  8. this.newGroupIdMap = {}
  9. }
  10. // 多选时渲染编组元素的多选框
  11. render() {
  12. Object.keys(this.groupIdToElementList).forEach(groupId => {
  13. let group = this.groupIdToElementList[groupId]
  14. let selected = group[0].isSelected
  15. if (selected) {
  16. let mElement = new MultiSelectElement(
  17. {
  18. type: 'multiSelectElement'
  19. },
  20. this.app
  21. )
  22. mElement.setSelectedElementList(group)
  23. mElement.updateRect()
  24. mElement.dragElement.onlyShowBody()
  25. mElement.render()
  26. }
  27. })
  28. }
  29. // 存储到映射列表
  30. setToMap(element) {
  31. let groupId = element.getGroupId()
  32. if (groupId) {
  33. if (!this.groupIdToElementList[groupId]) {
  34. this.groupIdToElementList[groupId] = []
  35. }
  36. this.groupIdToElementList[groupId].push(element)
  37. }
  38. }
  39. // 初始化映射列表
  40. initIdToElementList(elementList) {
  41. this.groupIdToElementList = {}
  42. elementList.forEach(element => {
  43. this.setToMap(element)
  44. })
  45. }
  46. // 处理元素数据的复制
  47. handleCopyElementData(data) {
  48. if (data.groupId) {
  49. if (this.newGroupIdMap[data.groupId]) {
  50. data.groupId = this.newGroupIdMap[data.groupId]
  51. } else {
  52. data.groupId = this.newGroupIdMap[data.groupId] = uuidv4()
  53. }
  54. }
  55. return data
  56. }
  57. // 复位用于元素数据复制的存储对象
  58. clearCopyMap() {
  59. this.newGroupIdMap = {}
  60. }
  61. // 处理元素对象的复制
  62. handleCopyElement(element) {
  63. this.setToMap(element)
  64. }
  65. // 编组
  66. dogroup() {
  67. if (
  68. !this.app.selection.hasSelection ||
  69. this.app.selection.multiSelectElement.selectedElementList.length <= 1
  70. ) {
  71. return
  72. }
  73. let groupElement = this.app.selection.multiSelectElement.selectedElementList
  74. let groupId = uuidv4()
  75. this.groupIdToElementList[groupId] = groupElement
  76. groupElement.forEach(element => {
  77. element.setGroupId(groupId)
  78. })
  79. this.app.render.render()
  80. this.app.emitChange()
  81. }
  82. // 取消编组
  83. ungroup() {
  84. if (
  85. !this.app.selection.hasSelection ||
  86. this.app.selection.multiSelectElement.selectedElementList.length <= 1
  87. ) {
  88. return
  89. }
  90. let groupElement = this.app.selection.multiSelectElement.selectedElementList
  91. let groupId = groupElement[0].getGroupId()
  92. this.groupIdToElementList[groupId] = []
  93. delete this.groupIdToElementList[groupId]
  94. groupElement.forEach(element => {
  95. element.removeGroupId(groupId)
  96. })
  97. this.app.render.render()
  98. this.app.emitChange()
  99. }
  100. // 根据元素激活元素所在的组
  101. setSelection(element) {
  102. let groupId = element.getGroupId()
  103. if (this.groupIdToElementList[groupId]) {
  104. this.app.selection.selectElements(this.groupIdToElementList[groupId])
  105. }
  106. }
  107. // 获取和指定元素同一个组的所有元素
  108. getGroupElements(element) {
  109. let groupId = element.getGroupId()
  110. return this.groupIdToElementList[groupId] || []
  111. }
  112. }