index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <content-box
  3. class="go-content-layers"
  4. :class="{ scoped: !chartLayoutStore.getLayers }"
  5. title="图层"
  6. :depth="2"
  7. @back="backHandle"
  8. @mousedown="boxMousedownHandle($event)"
  9. >
  10. <template #icon>
  11. <n-icon size="16" :depth="2" :component="LayersIcon" />
  12. </template>
  13. <template #top-right>
  14. <n-button-group style="display: flex">
  15. <n-button
  16. v-for="(item, index) in layerModeEnumList"
  17. :key="index"
  18. ghost
  19. size="tiny"
  20. :type="layerMode === item.value ? 'primary' : 'tertiary'"
  21. @click="layerMode = item.value as LayerModeEnum"
  22. >
  23. <n-tooltip :show-arrow="false" trigger="hover">
  24. <template #trigger>
  25. <n-icon size="14" :component="item.icon" />
  26. </template>
  27. {{ item.label }}
  28. </n-tooltip>
  29. </n-button>
  30. </n-button-group>
  31. </template>
  32. <!-- 图层内容 -->
  33. <n-space v-if="reverseList.length === 0" justify="center">
  34. <n-text class="not-layer-text">暂无图层~</n-text>
  35. </n-space>
  36. <!-- https://github.com/SortableJS/vue.draggable.next -->
  37. <draggable item-key="id" v-model="layerList" ghostClass="ghost" @change="onMoveCallback">
  38. <template #item="{ element }">
  39. <div class="go-content-layer-box">
  40. <!-- 组合 -->
  41. <layers-group-list-item
  42. v-if="element.isGroup"
  43. :componentGroupData="element"
  44. :layer-mode="layerMode"
  45. ></layers-group-list-item>
  46. <!-- 单组件 -->
  47. <layers-list-item
  48. v-else
  49. :componentData="element"
  50. :layer-mode="layerMode"
  51. @mousedown="mousedownHandle($event, element)"
  52. @mouseenter="mouseenterHandle(element)"
  53. @mouseleave="mouseleaveHandle(element)"
  54. @contextmenu="handleContextMenu($event, element, optionsHandle)"
  55. ></layers-list-item>
  56. </div>
  57. </template>
  58. </draggable>
  59. </content-box>
  60. </template>
  61. <script setup lang="ts">
  62. import { computed, ref, watch } from 'vue'
  63. import Draggable from 'vuedraggable'
  64. import cloneDeep from 'lodash/cloneDeep'
  65. import { ContentBox } from '../ContentBox/index'
  66. import { useChartLayoutStore } from '@/store/modules/chartLayoutStore/chartLayoutStore'
  67. import { ChartLayoutStoreEnum } from '@/store/modules/chartLayoutStore/chartLayoutStore.d'
  68. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  69. import { CreateComponentType, CreateComponentGroupType } from '@/packages/index.d'
  70. import { MenuOptionsItemType } from '@/views/chart/hooks/useContextMenu.hook.d'
  71. import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
  72. import { MenuEnum, MouseEventButton, WinKeyboard, MacKeyboard } from '@/enums/editPageEnum'
  73. import { LayersListItem } from './components/LayersListItem/index'
  74. import { LayersGroupListItem } from './components/LayersGroupListItem/index'
  75. import { LayerModeEnum } from './enums'
  76. import { icon } from '@/plugins'
  77. const { LayersIcon, ImageIcon, ImagesIcon, ListIcon } = icon.ionicons5
  78. const chartLayoutStore = useChartLayoutStore()
  79. const chartEditStore = useChartEditStore()
  80. const { handleContextMenu, onClickOutSide } = useContextMenu()
  81. const layerList = ref<any>([])
  82. const layerModeEnumList = [
  83. { label: '缩略图', icon: ImagesIcon, value: 'thumbnail' },
  84. { label: '文本列表', icon: ListIcon, value: 'text' }
  85. ]
  86. const layerMode = ref<LayerModeEnum>('thumbnail')
  87. // 逆序展示
  88. const reverseList = computed(() => {
  89. const list: Array<CreateComponentType | CreateComponentGroupType> = cloneDeep(chartEditStore.getComponentList)
  90. return list.reverse()
  91. })
  92. watch(
  93. () => reverseList.value,
  94. newValue => {
  95. layerList.value = newValue
  96. }
  97. )
  98. // 右键事件
  99. const optionsHandle = (
  100. targetList: MenuOptionsItemType[],
  101. allList: MenuOptionsItemType[],
  102. targetInstance: CreateComponentType
  103. ) => {
  104. // 多选处理
  105. if (chartEditStore.getTargetChart.selectId.length > 1) {
  106. const list: MenuOptionsItemType[] = []
  107. targetList.forEach(item => {
  108. // 成组
  109. if (item.key === MenuEnum.GROUP) {
  110. list.push(item)
  111. }
  112. })
  113. return list
  114. }
  115. return targetList
  116. }
  117. // 缩小
  118. const backHandle = () => {
  119. chartLayoutStore.setItem(ChartLayoutStoreEnum.LAYERS, false)
  120. }
  121. // 移动结束处理
  122. const onMoveCallback = (val: any) => {
  123. const { oldIndex, newIndex } = val.moved
  124. if (newIndex - oldIndex > 0) {
  125. // 从上往下
  126. const moveTarget = chartEditStore.getComponentList.splice(-(oldIndex + 1), 1)[0]
  127. chartEditStore.getComponentList.splice(-newIndex, 0, moveTarget)
  128. } else {
  129. // 从下往上
  130. const moveTarget = chartEditStore.getComponentList.splice(-(oldIndex + 1), 1)[0]
  131. if (newIndex === 0) {
  132. chartEditStore.getComponentList.push(moveTarget)
  133. } else {
  134. chartEditStore.getComponentList.splice(-newIndex, 0, moveTarget)
  135. }
  136. }
  137. }
  138. const boxMousedownHandle = (e: MouseEvent) => {
  139. const box = document.querySelector('.go-content-layer-box')
  140. if ((e.target as any).contains(box)) {
  141. chartEditStore.setTargetSelectChart()
  142. }
  143. }
  144. // 点击事件
  145. const mousedownHandle = (e: MouseEvent, item: CreateComponentType) => {
  146. onClickOutSide()
  147. // 若此时按下了 CTRL, 表示多选
  148. const id = item.id
  149. if (
  150. e.buttons === MouseEventButton.LEFT &&
  151. (window.$KeyboardActive?.has(WinKeyboard.CTRL_SOURCE_KEY) ||
  152. window.$KeyboardActive?.has(MacKeyboard.CTRL_SOURCE_KEY))
  153. ) {
  154. // 若已选中,则去除
  155. if (chartEditStore.targetChart.selectId.includes(id)) {
  156. const exList = chartEditStore.targetChart.selectId.filter(e => e !== id)
  157. chartEditStore.setTargetSelectChart(exList)
  158. } else {
  159. chartEditStore.setTargetSelectChart(id, true)
  160. }
  161. return
  162. }
  163. chartEditStore.setTargetSelectChart(id)
  164. }
  165. // 进入事件
  166. const mouseenterHandle = (item: CreateComponentType) => {
  167. chartEditStore.setTargetHoverChart(item.id)
  168. }
  169. // 移出事件
  170. const mouseleaveHandle = (item: CreateComponentType) => {
  171. chartEditStore.setTargetHoverChart(undefined)
  172. }
  173. </script>
  174. <style lang="scss" scoped>
  175. $wight: 200px;
  176. @include go(content-layers) {
  177. width: $wight;
  178. flex-shrink: 0;
  179. overflow: hidden;
  180. @extend .go-transition;
  181. .not-layer-text {
  182. position: relative;
  183. top: 10px;
  184. white-space: nowrap;
  185. opacity: 0.4;
  186. }
  187. &.scoped {
  188. width: 0;
  189. }
  190. .ghost {
  191. opacity: 0;
  192. }
  193. .go-layer-mode-active {
  194. color: #51d6a9;
  195. }
  196. }
  197. </style>