useDrag.hook.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import { toRaw } from 'vue'
  2. import { DragKeyEnum, MouseEventButton, WinKeyboard, MacKeyboard } from '@/enums/editPageEnum'
  3. import { createComponent } from '@/packages'
  4. import { ConfigType } from '@/packages/index.d'
  5. import { CreateComponentType, CreateComponentGroupType, PickCreateComponentType } from '@/packages/index.d'
  6. import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
  7. import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  8. import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
  9. import { loadingStart, loadingFinish, loadingError } from '@/utils'
  10. import throttle from 'lodash/throttle'
  11. const chartEditStore = useChartEditStore()
  12. const { onClickOutSide } = useContextMenu()
  13. // * 拖拽到编辑区域里
  14. export const dragHandle = async (e: DragEvent) => {
  15. e.preventDefault()
  16. try {
  17. loadingStart()
  18. // 获取拖拽数据
  19. const drayDataString = e!.dataTransfer!.getData(DragKeyEnum.DRAG_KEY)
  20. if (!drayDataString) {
  21. loadingFinish()
  22. return
  23. }
  24. // 修改状态
  25. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_CREATE, false)
  26. const dropData: Exclude<ConfigType, ['image']> = JSON.parse(drayDataString)
  27. // 创建新图表组件
  28. let newComponent: CreateComponentType = await createComponent(dropData)
  29. newComponent.setPosition(e.offsetX - newComponent.attr.w / 2, e.offsetY - newComponent.attr.h / 2)
  30. chartEditStore.addComponentList(newComponent, false, true)
  31. chartEditStore.setTargetSelectChart(newComponent.id)
  32. loadingFinish()
  33. } catch (error) {
  34. loadingError()
  35. window['$message'].warning(`图表正在研发中, 敬请期待...`)
  36. }
  37. }
  38. // * 进入拖拽区域
  39. export const dragoverHandle = (e: DragEvent) => {
  40. e.preventDefault()
  41. e.stopPropagation()
  42. if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
  43. }
  44. // * 不拦截默认行为点击
  45. export const mousedownHandleUnStop = (e: MouseEvent, item?: CreateComponentType | CreateComponentGroupType) => {
  46. if (item) {
  47. chartEditStore.setTargetSelectChart(item.id)
  48. return
  49. }
  50. chartEditStore.setTargetSelectChart(undefined)
  51. }
  52. // * 框选
  53. export const mousedownBoxSelect = (e: MouseEvent, item?: CreateComponentType | CreateComponentGroupType) => {
  54. mousedownHandleUnStop(e)
  55. // 记录点击初始位置
  56. const startOffsetX = e.offsetX
  57. const startOffsetY = e.offsetY
  58. const startScreenX = e.screenX
  59. const startScreenY = e.screenY
  60. // 记录缩放
  61. const scale = chartEditStore.getEditCanvas.scale
  62. chartEditStore.setMousePosition(undefined, undefined, startOffsetX, startOffsetY)
  63. // 移动框选
  64. const mousemove = throttle((moveEvent: MouseEvent) => {
  65. // 取消当前选中
  66. chartEditStore.setTargetSelectChart()
  67. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_SELECT, true)
  68. // 这里先把相对值算好,不然组件无法获取 startScreenX 和 startScreenY 的值
  69. const currX = startOffsetX + moveEvent.screenX - startScreenX
  70. const currY = startOffsetY + moveEvent.screenY - startScreenY
  71. chartEditStore.setMousePosition(currX, currY)
  72. // 计算框选的左上角和右下角
  73. const selectAttr = {
  74. // 左上角
  75. x1: 0,
  76. y1: 0,
  77. // 右下角
  78. x2: 0,
  79. y2: 0
  80. }
  81. if (currX > startOffsetX && currY > startOffsetY) {
  82. // 右下方向
  83. selectAttr.x1 = startOffsetX
  84. selectAttr.y1 = startOffsetY
  85. selectAttr.x2 = Math.round(startOffsetX + (moveEvent.screenX - startScreenX) / scale)
  86. selectAttr.y2 = Math.round(startOffsetY + (moveEvent.screenY - startScreenY) / scale)
  87. } else if (currX > startOffsetX && currY < startOffsetY) {
  88. // 右上方向
  89. selectAttr.x1 = startOffsetX
  90. selectAttr.y1 = Math.round(startOffsetY - (startScreenY - moveEvent.screenY) / scale)
  91. selectAttr.x2 = Math.round(startOffsetX + (moveEvent.screenX - startScreenX) / scale)
  92. selectAttr.y2 = startOffsetY
  93. } else if (currX < startOffsetX && currY > startOffsetY) {
  94. selectAttr.x1 = Math.round(startOffsetX - (startScreenX - moveEvent.screenX) / scale)
  95. selectAttr.y1 = startOffsetY
  96. selectAttr.x2 = startOffsetX
  97. selectAttr.y2 = Math.round(startOffsetY + (moveEvent.screenY - startScreenY ) / scale)
  98. // 左下方向
  99. } else {
  100. // 左上方向
  101. selectAttr.x1 = Math.round(startOffsetX - (startScreenX - moveEvent.screenX) / scale)
  102. selectAttr.y1 = Math.round(startOffsetY - (startScreenY - moveEvent.screenY) / scale)
  103. selectAttr.x2 = startOffsetX
  104. selectAttr.y2 = startOffsetY
  105. }
  106. // 遍历组件
  107. chartEditStore.getComponentList.forEach(item => {
  108. if (!chartEditStore.getTargetChart.selectId.includes(item.id)) {
  109. // 处理左上角
  110. let isSelect = false
  111. const { x, y, w, h } = item.attr
  112. const targetAttr = {
  113. // 左上角
  114. x1: x,
  115. y1: y,
  116. // 右下角
  117. x2: x + w,
  118. y2: y + h
  119. }
  120. // 全包含则选中
  121. if (
  122. targetAttr.x1 - selectAttr.x1 >= 0 &&
  123. targetAttr.y1 - selectAttr.y1 >= 0 &&
  124. targetAttr.x2 - selectAttr.x2 <= 0 &&
  125. targetAttr.y2 - selectAttr.y2 <= 0
  126. ) {
  127. isSelect = true
  128. chartEditStore.setTargetSelectChart(item.id, true)
  129. }
  130. }
  131. })
  132. }, 20)
  133. // 鼠标抬起
  134. const mouseup = () => {
  135. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_SELECT, false)
  136. chartEditStore.setMousePosition(0, 0, 0, 0)
  137. document.removeEventListener('mousemove', mousemove)
  138. document.removeEventListener('mouseup', mouseup)
  139. }
  140. document.addEventListener('mousemove', mousemove)
  141. document.addEventListener('mouseup', mouseup)
  142. }
  143. // * 鼠标事件
  144. export const useMouseHandle = () => {
  145. // * Click 事件, 松开鼠标触发
  146. const mouseClickHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  147. e.preventDefault()
  148. e.stopPropagation()
  149. // 若此时按下了 CTRL, 表示多选
  150. if (
  151. window.$KeyboardActive?.has(WinKeyboard.CTRL_SOURCE_KEY) ||
  152. window.$KeyboardActive?.has(MacKeyboard.CTRL_SOURCE_KEY)
  153. ) {
  154. // 若已选中,则去除
  155. if (chartEditStore.targetChart.selectId.includes(item.id)) {
  156. const exList = chartEditStore.targetChart.selectId.filter(e => e !== item.id)
  157. chartEditStore.setTargetSelectChart(exList)
  158. } else {
  159. chartEditStore.setTargetSelectChart(item.id, true)
  160. }
  161. }
  162. }
  163. // * 按下事件(包含移动事件)
  164. const mousedownHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  165. e.preventDefault()
  166. e.stopPropagation()
  167. onClickOutSide()
  168. // 按下左键 + CTRL
  169. if (
  170. e.buttons === MouseEventButton.LEFT &&
  171. (window.$KeyboardActive?.has(WinKeyboard.CTRL_SOURCE_KEY) ||
  172. window.$KeyboardActive?.has(MacKeyboard.CTRL_SOURCE_KEY))
  173. )
  174. return
  175. // 按下右键 + 选中多个 + 目标元素是多选子元素
  176. const selectId = chartEditStore.getTargetChart.selectId
  177. if (e.buttons === MouseEventButton.RIGHT && selectId.length > 1 && selectId.includes(item.id)) return
  178. // 选中当前目标组件
  179. chartEditStore.setTargetSelectChart(item.id)
  180. // 按下右键
  181. if (e.buttons === MouseEventButton.RIGHT) return
  182. const scale = chartEditStore.getEditCanvas.scale
  183. const canvasWidth = chartEditStore.getEditCanvasConfig.width
  184. const canvasHeight = chartEditStore.getEditCanvasConfig.height
  185. // 记录图表初始位置和大小
  186. const targetMap = new Map()
  187. chartEditStore.getTargetChart.selectId.forEach(id => {
  188. const index = chartEditStore.fetchTargetIndex(id)
  189. if (index !== -1) {
  190. const { x, y, w, h } = toRaw(chartEditStore.getComponentList[index]).attr
  191. targetMap.set(id, { x, y, w, h })
  192. }
  193. })
  194. // 记录点击初始位置
  195. const startX = e.screenX
  196. const startY = e.screenY
  197. // 记录初始位置
  198. chartEditStore.setMousePosition(undefined, undefined, startX, startY)
  199. // 移动-计算偏移量
  200. const mousemove = throttle((moveEvent: MouseEvent) => {
  201. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  202. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  203. // 当前偏移量,处理 scale 比例问题
  204. let offsetX = (moveEvent.screenX - startX) / scale
  205. let offsetY = (moveEvent.screenY - startY) / scale
  206. chartEditStore.getTargetChart.selectId.forEach(id => {
  207. if (!targetMap.has(id)) return
  208. const index = chartEditStore.fetchTargetIndex(id)
  209. // 拿到初始位置数据
  210. const { x, y, w, h } = targetMap.get(id)
  211. const componentInstance = chartEditStore.getComponentList[index]
  212. let currX = Math.round(x + offsetX)
  213. let currY = Math.round(y + offsetY)
  214. // 要预留的距离
  215. const distance = 50
  216. // 基于左上角位置检测
  217. currX = currX < -w + distance ? -w + distance : currX
  218. currY = currY < -h + distance ? -h + distance : currY
  219. // 基于右下角位置检测
  220. currX = currX > canvasWidth - distance ? canvasWidth - distance : currX
  221. currY = currY > canvasHeight - distance ? canvasHeight - distance : currY
  222. componentInstance.attr = Object.assign(componentInstance.attr, {
  223. x: currX,
  224. y: currY
  225. })
  226. })
  227. return
  228. }, 20)
  229. const mouseup = () => {
  230. chartEditStore.setMousePosition(0, 0, 0, 0)
  231. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  232. document.removeEventListener('mousemove', mousemove)
  233. document.removeEventListener('mouseup', mouseup)
  234. }
  235. document.addEventListener('mousemove', mousemove)
  236. document.addEventListener('mouseup', mouseup)
  237. }
  238. // * 进入事件
  239. const mouseenterHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  240. e.preventDefault()
  241. e.stopPropagation()
  242. if (!chartEditStore.getEditCanvas.isSelect) {
  243. chartEditStore.setTargetHoverChart(item.id)
  244. }
  245. }
  246. // * 移出事件
  247. const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  248. e.preventDefault()
  249. e.stopPropagation()
  250. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  251. chartEditStore.setTargetHoverChart(undefined)
  252. }
  253. return { mouseClickHandle, mousedownHandle, mouseenterHandle, mouseleaveHandle }
  254. }
  255. // * 移动锚点
  256. export const useMousePointHandle = (e: MouseEvent, point: string, attr: PickCreateComponentType<'attr'>) => {
  257. e.stopPropagation()
  258. e.preventDefault()
  259. // 设置拖拽状态
  260. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  261. const scale = chartEditStore.getEditCanvas.scale
  262. const itemAttrX = attr.x
  263. const itemAttrY = attr.y
  264. const itemAttrW = attr.w
  265. const itemAttrH = attr.h
  266. // 记录点击初始位置
  267. const startX = e.screenX
  268. const startY = e.screenY
  269. // 记录初始位置
  270. chartEditStore.setMousePosition(startX, startY)
  271. const mousemove = throttle((moveEvent: MouseEvent) => {
  272. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  273. let currX = Math.round((moveEvent.screenX - startX) / scale)
  274. let currY = Math.round((moveEvent.screenY - startY) / scale)
  275. const isTop = /t/.test(point)
  276. const isBottom = /b/.test(point)
  277. const isLeft = /l/.test(point)
  278. const isRight = /r/.test(point)
  279. const newHeight = itemAttrH + (isTop ? -currY : isBottom ? currY : 0)
  280. const newWidth = itemAttrW + (isLeft ? -currX : isRight ? currX : 0)
  281. attr.h = newHeight > 0 ? newHeight : 0
  282. attr.w = newWidth > 0 ? newWidth : 0
  283. attr.x = itemAttrX + (isLeft ? currX : 0)
  284. attr.y = itemAttrY + (isTop ? currY : 0)
  285. }, 50)
  286. const mouseup = () => {
  287. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  288. chartEditStore.setMousePosition(0, 0, 0, 0)
  289. document.removeEventListener('mousemove', mousemove)
  290. document.removeEventListener('mouseup', mouseup)
  291. }
  292. document.addEventListener('mousemove', mousemove)
  293. document.addEventListener('mouseup', mouseup)
  294. }