useDrag.hook.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { toRaw } from 'vue'
  2. import { DragKeyEnum, MouseEventButton, WinKeyboard, MacKeyboard } from '@/enums/editPageEnum'
  3. import { createComponent } from '@/packages'
  4. import { ConfigType } from '@/packages/index.d'
  5. import { CreateComponentType, CreateComponentGroupType, PickCreateComponentType } from '@/packages/index.d'
  6. import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
  7. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  8. import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
  9. import { loadingStart, loadingFinish, loadingError } from '@/utils'
  10. import throttle from 'lodash/throttle'
  11. const chartEditStore = useChartEditStore()
  12. const { onClickOutSide } = useContextMenu()
  13. // * 拖拽到编辑区域里
  14. export const dragHandle = async (e: DragEvent) => {
  15. e.preventDefault()
  16. try {
  17. loadingStart()
  18. // 获取拖拽数据
  19. const drayDataString = e!.dataTransfer!.getData(DragKeyEnum.DROG_KEY)
  20. if (!drayDataString) {
  21. loadingFinish()
  22. return
  23. }
  24. // 修改状态
  25. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_CREATE, false)
  26. const dropData: Exclude<ConfigType, ['image']> = JSON.parse(drayDataString)
  27. // 创建新图表组件
  28. let newComponent: CreateComponentType = await createComponent(dropData)
  29. newComponent.setPosition(e.offsetX - newComponent.attr.w / 2, e.offsetY - newComponent.attr.h / 2)
  30. chartEditStore.addComponentList(newComponent, false, true)
  31. chartEditStore.setTargetSelectChart(newComponent.id)
  32. loadingFinish()
  33. } catch (error) {
  34. loadingError()
  35. window['$message'].warning(`图表正在研发中, 敬请期待...`)
  36. }
  37. }
  38. // * 进入拖拽区域
  39. export const dragoverHandle = (e: DragEvent) => {
  40. e.preventDefault()
  41. e.stopPropagation()
  42. if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
  43. }
  44. // * 不拦截默认行为点击
  45. export const mousedownHandleUnStop = (e: MouseEvent, item?: CreateComponentType | CreateComponentGroupType) => {
  46. if (item) {
  47. chartEditStore.setTargetSelectChart(item.id)
  48. return
  49. }
  50. chartEditStore.setTargetSelectChart(undefined)
  51. }
  52. // * 鼠标事件
  53. export const useMouseHandle = () => {
  54. // * Click 事件, 松开鼠标触发
  55. const mouseClickHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  56. e.preventDefault()
  57. e.stopPropagation()
  58. // 若此时按下了 CTRL, 表示多选
  59. if (
  60. window.$KeyboardActive?.has(WinKeyboard.CTRL_SOURCE_KEY) ||
  61. window.$KeyboardActive?.has(MacKeyboard.CTRL_SOURCE_KEY)
  62. ) {
  63. // 若已选中,则去除
  64. if (chartEditStore.targetChart.selectId.includes(item.id)) {
  65. const exList = chartEditStore.targetChart.selectId.filter(e => e !== item.id)
  66. chartEditStore.setTargetSelectChart(exList)
  67. } else {
  68. chartEditStore.setTargetSelectChart(item.id, true)
  69. }
  70. }
  71. }
  72. // * 按下事件(包含移动事件)
  73. const mousedownHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  74. e.preventDefault()
  75. e.stopPropagation()
  76. onClickOutSide()
  77. // 按下左键 + CTRL
  78. if (
  79. e.buttons === MouseEventButton.LEFT &&
  80. (window.$KeyboardActive?.has(WinKeyboard.CTRL_SOURCE_KEY) ||
  81. window.$KeyboardActive?.has(MacKeyboard.CTRL_SOURCE_KEY))
  82. )
  83. return
  84. // 按下右键 + 选中多个 + 目标元素是多选子元素
  85. const selectId = chartEditStore.getTargetChart.selectId
  86. if (e.buttons === MouseEventButton.RIGHT && selectId.length > 1 && selectId.includes(item.id)) return
  87. // 选中当前目标组件
  88. chartEditStore.setTargetSelectChart(item.id)
  89. // 按下右键
  90. if (e.buttons === MouseEventButton.RIGHT) return
  91. const scale = chartEditStore.getEditCanvas.scale
  92. const canvasWidth = chartEditStore.getEditCanvasConfig.width
  93. const canvasHeight = chartEditStore.getEditCanvasConfig.height
  94. // 记录图表初始位置和大小
  95. const targetMap = new Map()
  96. chartEditStore.getTargetChart.selectId.forEach(id => {
  97. const index = chartEditStore.fetchTargetIndex(id)
  98. if (index !== -1) {
  99. const { x, y, w, h } = toRaw(chartEditStore.getComponentList[index]).attr
  100. targetMap.set(id, { x, y, w, h })
  101. }
  102. })
  103. // 记录点击初始位置
  104. const startX = e.screenX
  105. const startY = e.screenY
  106. // 记录初始位置
  107. chartEditStore.setMousePosition(startX, startY)
  108. // 移动-计算偏移量
  109. const mousemove = throttle((moveEvent: MouseEvent) => {
  110. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  111. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  112. // 当前偏移量,处理 scale 比例问题
  113. let offsetX = (moveEvent.screenX - startX) / scale
  114. let offsetY = (moveEvent.screenY - startY) / scale
  115. chartEditStore.getTargetChart.selectId.forEach(id => {
  116. if(!targetMap.has(id)) return
  117. const index = chartEditStore.fetchTargetIndex(id)
  118. // 拿到初始位置数据
  119. const { x, y, w, h } = targetMap.get(id)
  120. const componentInstance = chartEditStore.getComponentList[index]
  121. let currX = Math.round(x + offsetX)
  122. let currY = Math.round(y + offsetY)
  123. // 要预留的距离
  124. const distance = 50
  125. // 基于左上角位置检测
  126. currX = currX < -w + distance ? -w + distance : currX
  127. currY = currY < -h + distance ? -h + distance : currY
  128. // 基于右下角位置检测
  129. currX = currX > canvasWidth - distance ? canvasWidth - distance : currX
  130. currY = currY > canvasHeight - distance ? canvasHeight - distance : currY
  131. componentInstance.attr = Object.assign(componentInstance.attr, {
  132. x: currX,
  133. y: currY
  134. })
  135. })
  136. return
  137. }, 30)
  138. const mouseup = () => {
  139. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  140. chartEditStore.setMousePosition(0, 0)
  141. document.removeEventListener('mousemove', mousemove)
  142. document.removeEventListener('mouseup', mouseup)
  143. }
  144. document.addEventListener('mousemove', mousemove)
  145. document.addEventListener('mouseup', mouseup)
  146. }
  147. // * 进入事件
  148. const mouseenterHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  149. e.preventDefault()
  150. e.stopPropagation()
  151. chartEditStore.setTargetHoverChart(item.id)
  152. }
  153. // * 移出事件
  154. const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  155. e.preventDefault()
  156. e.stopPropagation()
  157. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  158. chartEditStore.setTargetHoverChart(undefined)
  159. }
  160. return { mouseClickHandle, mousedownHandle, mouseenterHandle, mouseleaveHandle }
  161. }
  162. // * 移动锚点
  163. export const useMousePointHandle = (e: MouseEvent, point: string, attr: PickCreateComponentType<'attr'>) => {
  164. e.stopPropagation()
  165. e.preventDefault()
  166. // 设置拖拽状态
  167. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  168. const scale = chartEditStore.getEditCanvas.scale
  169. const itemAttrX = attr.x
  170. const itemAttrY = attr.y
  171. const itemAttrW = attr.w
  172. const itemAttrH = attr.h
  173. // 记录点击初始位置
  174. const startX = e.screenX
  175. const startY = e.screenY
  176. // 记录初始位置
  177. chartEditStore.setMousePosition(startX, startY)
  178. const mousemove = throttle((moveEvent: MouseEvent) => {
  179. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  180. let currX = Math.round((moveEvent.screenX - startX) / scale)
  181. let currY = Math.round((moveEvent.screenY - startY) / scale)
  182. const isTop = /t/.test(point)
  183. const isBottom = /b/.test(point)
  184. const isLeft = /l/.test(point)
  185. const isRight = /r/.test(point)
  186. const newHeight = itemAttrH + (isTop ? -currY : isBottom ? currY : 0)
  187. const newWidth = itemAttrW + (isLeft ? -currX : isRight ? currX : 0)
  188. attr.h = newHeight > 0 ? newHeight : 0
  189. attr.w = newWidth > 0 ? newWidth : 0
  190. attr.x = itemAttrX + (isLeft ? currX : 0)
  191. attr.y = itemAttrY + (isTop ? currY : 0)
  192. }, 50)
  193. const mouseup = () => {
  194. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  195. chartEditStore.setMousePosition(0, 0)
  196. document.removeEventListener('mousemove', mousemove)
  197. document.removeEventListener('mouseup', mouseup)
  198. }
  199. document.addEventListener('mousemove', mousemove)
  200. document.addEventListener('mouseup', mouseup)
  201. }