useDrag.hook.ts 7.2 KB

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