useDrag.hook.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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, cloneDeep } from 'lodash'
  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. let prevComponentInstance:CreateComponentType | CreateComponentGroupType
  199. chartEditStore.getTargetChart.selectId.forEach(id => {
  200. if (!targetMap.has(id)) return
  201. const index = chartEditStore.fetchTargetIndex(id)
  202. // 拿到初始位置数据
  203. prevComponentInstance = cloneDeep(chartEditStore.getComponentList[index])
  204. })
  205. // 记录初始位置
  206. chartEditStore.setMousePosition(undefined, undefined, startX, startY)
  207. // 移动-计算偏移量
  208. const mousemove = throttle((moveEvent: MouseEvent) => {
  209. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  210. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  211. // 当前偏移量,处理 scale 比例问题
  212. let offsetX = (moveEvent.screenX - startX) / scale
  213. let offsetY = (moveEvent.screenY - startY) / scale
  214. chartEditStore.getTargetChart.selectId.forEach(id => {
  215. if (!targetMap.has(id)) return
  216. const index = chartEditStore.fetchTargetIndex(id)
  217. // 拿到初始位置数据
  218. const { x, y, w, h } = targetMap.get(id)
  219. const componentInstance = chartEditStore.getComponentList[index]
  220. let currX = Math.round(x + offsetX)
  221. let currY = Math.round(y + offsetY)
  222. // 要预留的距离
  223. const distance = 50
  224. // 基于左上角位置检测
  225. currX = currX < -w + distance ? -w + distance : currX
  226. currY = currY < -h + distance ? -h + distance : currY
  227. // 基于右下角位置检测
  228. currX = currX > canvasWidth - distance ? canvasWidth - distance : currX
  229. currY = currY > canvasHeight - distance ? canvasHeight - distance : currY
  230. componentInstance.attr = Object.assign(componentInstance.attr, {
  231. x: currX,
  232. y: currY
  233. })
  234. })
  235. return
  236. }, 20)
  237. const mouseup = () => {
  238. chartEditStore.setMousePosition(0, 0, 0, 0)
  239. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  240. // 加入历史栈
  241. if(prevComponentInstance){
  242. chartEditStore.getTargetChart.selectId.forEach(id => {
  243. if (!targetMap.has(id)) return
  244. const index = chartEditStore.fetchTargetIndex(id)
  245. const curComponentInstance = chartEditStore.getComponentList[index]
  246. prevComponentInstance.attr = Object.assign(prevComponentInstance.attr, {
  247. offsetX: curComponentInstance.attr.x - prevComponentInstance.attr.x,
  248. offsetY: curComponentInstance.attr.y - prevComponentInstance.attr.y
  249. })
  250. })
  251. chartEditStore.moveComponentList(prevComponentInstance)
  252. }
  253. document.removeEventListener('mousemove', mousemove)
  254. document.removeEventListener('mouseup', mouseup)
  255. }
  256. document.addEventListener('mousemove', mousemove)
  257. document.addEventListener('mouseup', mouseup)
  258. }
  259. // * 进入事件
  260. const mouseenterHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  261. e.preventDefault()
  262. e.stopPropagation()
  263. if (!chartEditStore.getEditCanvas.isSelect) {
  264. chartEditStore.setTargetHoverChart(item.id)
  265. }
  266. }
  267. // * 移出事件
  268. const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  269. e.preventDefault()
  270. e.stopPropagation()
  271. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  272. chartEditStore.setTargetHoverChart(undefined)
  273. }
  274. return { mouseClickHandle, mousedownHandle, mouseenterHandle, mouseleaveHandle }
  275. }
  276. // * 移动锚点
  277. export const useMousePointHandle = (e: MouseEvent, point: string, attr: PickCreateComponentType<'attr'>) => {
  278. e.stopPropagation()
  279. e.preventDefault()
  280. // 设置拖拽状态
  281. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  282. const scale = chartEditStore.getEditCanvas.scale
  283. const itemAttrX = attr.x
  284. const itemAttrY = attr.y
  285. const itemAttrW = attr.w
  286. const itemAttrH = attr.h
  287. // 记录点击初始位置
  288. const startX = e.screenX
  289. const startY = e.screenY
  290. // 记录初始位置
  291. chartEditStore.setMousePosition(startX, startY)
  292. const mousemove = throttle((moveEvent: MouseEvent) => {
  293. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  294. let currX = Math.round((moveEvent.screenX - startX) / scale)
  295. let currY = Math.round((moveEvent.screenY - startY) / scale)
  296. const isTop = /t/.test(point)
  297. const isBottom = /b/.test(point)
  298. const isLeft = /l/.test(point)
  299. const isRight = /r/.test(point)
  300. const newHeight = itemAttrH + (isTop ? -currY : isBottom ? currY : 0)
  301. const newWidth = itemAttrW + (isLeft ? -currX : isRight ? currX : 0)
  302. attr.h = newHeight > 0 ? newHeight : 0
  303. attr.w = newWidth > 0 ? newWidth : 0
  304. attr.x = itemAttrX + (isLeft ? currX : 0)
  305. attr.y = itemAttrY + (isTop ? currY : 0)
  306. }, 50)
  307. const mouseup = () => {
  308. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  309. chartEditStore.setMousePosition(0, 0, 0, 0)
  310. document.removeEventListener('mousemove', mousemove)
  311. document.removeEventListener('mouseup', mouseup)
  312. }
  313. document.addEventListener('mousemove', mousemove)
  314. document.addEventListener('mouseup', mouseup)
  315. }