useDrag.hook.ts 5.5 KB

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