useDrop.hook.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { toRefs } from 'vue'
  2. import { getChartEditStore } from './useStore.hook'
  3. import { DragKeyEnum } from '@/enums/editPageEnum'
  4. import { createComponent } from '@/packages'
  5. import { ConfigType } from '@/packages/index.d'
  6. import { loadingStart, loadingFinish, loadingError } from '@/utils'
  7. import { CreateComponentType } from '@/packages/index.d'
  8. import throttle from 'lodash/throttle'
  9. import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
  10. const { onClickoutside } = useContextMenu()
  11. const chartEditStore = getChartEditStore()
  12. const { scale } = toRefs(chartEditStore.getEditCanvas)
  13. // * 拖拽中
  14. export const handleDrop = async (e: DragEvent) => {
  15. e.preventDefault()
  16. try {
  17. loadingStart()
  18. // 获取拖拽数据
  19. const drayDataString = e!.dataTransfer!.getData(DragKeyEnum.DROG_KEY)
  20. const dropData: Exclude<ConfigType, ['node', 'image']> = JSON.parse(
  21. drayDataString
  22. )
  23. // 创建新图表组件
  24. let newComponent:CreateComponentType = await createComponent(dropData)
  25. newComponent.setPosition(e.offsetX - newComponent.attr.w / 2, e.offsetY - newComponent.attr.h / 2)
  26. chartEditStore.addComponentList(newComponent, false, true)
  27. chartEditStore.setTargetSelectChart(newComponent.id)
  28. loadingFinish()
  29. } catch (error) {
  30. loadingError()
  31. window['$message'].warning(`图表正在研发中, 敬请期待...`)
  32. }
  33. }
  34. // * 拖拽结束
  35. export const handleDragOver = (e: DragEvent) => {
  36. e.preventDefault()
  37. e.stopPropagation()
  38. if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
  39. }
  40. // * 不拦截默认行为点击
  41. export const mousedownHandleUnStop = (
  42. e: MouseEvent,
  43. item?: CreateComponentType
  44. ) => {
  45. if (item) {
  46. chartEditStore.setTargetSelectChart(item.id)
  47. return
  48. }
  49. chartEditStore.setTargetSelectChart(undefined)
  50. }
  51. // * 移动图表
  52. export const useMouseHandle = () => {
  53. // 点击事件(包含移动事件)
  54. const mousedownHandle = (e: MouseEvent, item: CreateComponentType) => {
  55. e.preventDefault()
  56. e.stopPropagation()
  57. onClickoutside()
  58. chartEditStore.setTargetSelectChart(item.id)
  59. const scale = chartEditStore.getEditCanvas.scale
  60. const width = chartEditStore.getEditCanvas.width
  61. const height = chartEditStore.getEditCanvas.height
  62. // 获取编辑区域 Dom
  63. const editcontentDom = chartEditStore.getEditCanvas.editContentDom
  64. // 记录图表初始位置和大小
  65. const itemAttrX = item.attr.x
  66. const itemAttrY = item.attr.y
  67. const itemAttrW = item.attr.w
  68. const itemAttrH = item.attr.h
  69. // 记录点击初始位置
  70. const startX = e.screenX
  71. const startY = e.screenY
  72. // 计算偏移量(处理 scale 比例问题)
  73. const mousemove = throttle((moveEvent: MouseEvent) => {
  74. let currX = Math.round(itemAttrX + (moveEvent.screenX - startX) / scale)
  75. let currY = Math.round(itemAttrY + (moveEvent.screenY - startY) / scale)
  76. // 要预留的距离
  77. const distance = 50
  78. // 基于左上角位置检测
  79. currX = currX < -itemAttrW + distance ? -itemAttrW + distance : currX
  80. currY = currY < -itemAttrH + distance ? -itemAttrH + distance : currY
  81. // 基于右下角位置检测
  82. currX = currX > width - distance ? width - distance : currX
  83. currY = currY > height - distance ? height - distance : currY
  84. item.attr.x = currX
  85. item.attr.y = currY
  86. }, 30)
  87. const mouseup = () => {
  88. editcontentDom!.removeEventListener('mousemove', mousemove)
  89. editcontentDom!.removeEventListener('mouseup', mouseup)
  90. }
  91. editcontentDom!.addEventListener('mousemove', mousemove)
  92. editcontentDom!.addEventListener('mouseup', mouseup)
  93. }
  94. // * 进入事件
  95. const mouseenterHandle = (e: MouseEvent, item: CreateComponentType) => {
  96. e.preventDefault()
  97. e.stopPropagation()
  98. chartEditStore.setTargetHoverChart(item.id)
  99. }
  100. // * 移出事件
  101. const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType) => {
  102. e.preventDefault()
  103. e.stopPropagation()
  104. chartEditStore.setTargetHoverChart(undefined)
  105. }
  106. return { mousedownHandle, mouseenterHandle, mouseleaveHandle }
  107. }