useDrag.hook.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { DragKeyEnum } from '@/enums/editPageEnum'
  2. import { createComponent } from '@/packages'
  3. import { ConfigType } from '@/packages/index.d'
  4. import {
  5. CreateComponentType,
  6. PickCreateComponentType
  7. } from '@/packages/index.d'
  8. import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
  9. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  10. import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
  11. import { loadingStart, loadingFinish, loadingError } from '@/utils'
  12. import throttle from 'lodash/throttle'
  13. const chartEditStore = useChartEditStore()
  14. const { onClickoutside } = useContextMenu()
  15. // * 拖拽到编辑区域里
  16. export const handleDrag = async (e: DragEvent) => {
  17. e.preventDefault()
  18. try {
  19. loadingStart()
  20. // 获取拖拽数据
  21. const drayDataString = e!.dataTransfer!.getData(DragKeyEnum.DROG_KEY)
  22. if (!drayDataString) {
  23. loadingFinish()
  24. return
  25. }
  26. const dropData: Exclude<ConfigType, ['image']> = JSON.parse(
  27. drayDataString
  28. )
  29. // 创建新图表组件
  30. let newComponent: CreateComponentType = await createComponent(dropData)
  31. newComponent.setPosition(
  32. e.offsetX - newComponent.attr.w / 2,
  33. e.offsetY - newComponent.attr.h / 2
  34. )
  35. chartEditStore.addComponentList(newComponent, false, true)
  36. chartEditStore.setTargetSelectChart(newComponent.id)
  37. loadingFinish()
  38. } catch (error) {
  39. loadingError()
  40. window['$message'].warning(`图表正在研发中, 敬请期待...`)
  41. }
  42. }
  43. // * 拖拽中
  44. export const handleDragOver = (e: DragEvent) => {
  45. e.preventDefault()
  46. e.stopPropagation()
  47. if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
  48. }
  49. // * 不拦截默认行为点击
  50. export const mousedownHandleUnStop = (
  51. e: MouseEvent,
  52. item?: CreateComponentType
  53. ) => {
  54. if (item) {
  55. chartEditStore.setTargetSelectChart(item.id)
  56. return
  57. }
  58. chartEditStore.setTargetSelectChart(undefined)
  59. }
  60. // * 移动图表
  61. export const useMouseHandle = () => {
  62. // 点击事件(包含移动事件)
  63. const mousedownHandle = (e: MouseEvent, item: CreateComponentType) => {
  64. e.preventDefault()
  65. e.stopPropagation()
  66. onClickoutside()
  67. chartEditStore.setTargetSelectChart(item.id)
  68. const scale = chartEditStore.getEditCanvas.scale
  69. const width = chartEditStore.getEditCanvasConfig.width
  70. const height = chartEditStore.getEditCanvasConfig.height
  71. // 记录图表初始位置和大小
  72. const itemAttrX = item.attr.x
  73. const itemAttrY = item.attr.y
  74. const itemAttrW = item.attr.w
  75. const itemAttrH = item.attr.h
  76. // 记录点击初始位置
  77. const startX = e.screenX
  78. const startY = e.screenY
  79. // 记录初始位置
  80. chartEditStore.setMousePosition(startX, startY)
  81. // 计算偏移量(处理 scale 比例问题)
  82. const mousemove = throttle((moveEvent: MouseEvent) => {
  83. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  84. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  85. let currX = Math.round(itemAttrX + (moveEvent.screenX - startX) / scale)
  86. let currY = Math.round(itemAttrY + (moveEvent.screenY - startY) / scale)
  87. // 要预留的距离
  88. const distance = 50
  89. // 基于左上角位置检测
  90. currX = currX < -itemAttrW + distance ? -itemAttrW + distance : currX
  91. currY = currY < -itemAttrH + distance ? -itemAttrH + distance : currY
  92. // 基于右下角位置检测
  93. currX = currX > width - distance ? width - distance : currX
  94. currY = currY > height - distance ? height - distance : currY
  95. item.attr.x = currX
  96. item.attr.y = currY
  97. }, 30)
  98. const mouseup = () => {
  99. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  100. chartEditStore.setMousePosition(0, 0)
  101. document.removeEventListener('mousemove', mousemove)
  102. document.removeEventListener('mouseup', mouseup)
  103. }
  104. document.addEventListener('mousemove', mousemove)
  105. document.addEventListener('mouseup', mouseup)
  106. }
  107. // * 进入事件
  108. const mouseenterHandle = (e: MouseEvent, item: CreateComponentType) => {
  109. e.preventDefault()
  110. e.stopPropagation()
  111. chartEditStore.setTargetHoverChart(item.id)
  112. }
  113. // * 移出事件
  114. const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType) => {
  115. e.preventDefault()
  116. e.stopPropagation()
  117. chartEditStore.setTargetHoverChart(undefined)
  118. }
  119. return { mousedownHandle, mouseenterHandle, mouseleaveHandle }
  120. }
  121. // * 移动锚点
  122. export const useMousePointHandle = (
  123. e: MouseEvent,
  124. point: string,
  125. attr: PickCreateComponentType<'attr'>
  126. ) => {
  127. e.stopPropagation()
  128. e.preventDefault()
  129. // 设置拖拽状态
  130. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  131. const scale = chartEditStore.getEditCanvas.scale
  132. const itemAttrX = attr.x
  133. const itemAttrY = attr.y
  134. const itemAttrW = attr.w
  135. const itemAttrH = attr.h
  136. // 记录点击初始位置
  137. const startX = e.screenX
  138. const startY = e.screenY
  139. // 记录初始位置
  140. chartEditStore.setMousePosition(startX, startY)
  141. const mousemove = throttle((moveEvent: MouseEvent) => {
  142. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  143. let currX = Math.round((moveEvent.screenX - startX) / scale)
  144. let currY = Math.round((moveEvent.screenY - startY) / scale)
  145. const isTop = /t/.test(point)
  146. const isBottom = /b/.test(point)
  147. const isLeft = /l/.test(point)
  148. const isRight = /r/.test(point)
  149. const newHeight = itemAttrH + (isTop ? -currY : (isBottom ? currY : 0))
  150. const newWidth = itemAttrW + (isLeft ? -currX : (isRight ? currX : 0))
  151. attr.h = newHeight > 0 ? newHeight : 0
  152. attr.w = newWidth > 0 ? newWidth : 0
  153. attr.x = itemAttrX + (isLeft ? currX : 0)
  154. attr.y = itemAttrY + (isTop ? currY : 0)
  155. }, 50)
  156. const mouseup = () => {
  157. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  158. chartEditStore.setMousePosition(0, 0)
  159. document.removeEventListener('mousemove', mousemove)
  160. document.removeEventListener('mouseup', mouseup)
  161. }
  162. document.addEventListener('mousemove', mousemove)
  163. document.addEventListener('mouseup', mouseup)
  164. }