useDrop.hook.ts 3.5 KB

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