useDrag.hook.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import { toRaw } from 'vue'
  2. import { DragKeyEnum, MouseEventButton } 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, setComponentPosition } 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. setComponentPosition(newComponent, 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. !item.status.lock &&
  127. !item.status.hide
  128. ) {
  129. isSelect = true
  130. chartEditStore.setTargetSelectChart(item.id, true)
  131. }
  132. }
  133. })
  134. }, 20)
  135. // 鼠标抬起
  136. const mouseup = () => {
  137. // 鼠标抬起时,结束mousemove的节流函数,避免选框不消失问题
  138. mousemove.cancel()
  139. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_SELECT, false)
  140. chartEditStore.setMousePosition(0, 0, 0, 0)
  141. document.removeEventListener('mousemove', mousemove)
  142. document.removeEventListener('mouseup', mouseup)
  143. }
  144. document.addEventListener('mousemove', mousemove)
  145. document.addEventListener('mouseup', mouseup)
  146. }
  147. // * 鼠标事件
  148. export const useMouseHandle = () => {
  149. // * Click 事件, 松开鼠标触发
  150. const mouseClickHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  151. e.preventDefault()
  152. e.stopPropagation()
  153. if (item.status.lock) return
  154. // 若此时按下了 CTRL, 表示多选
  155. if (window.$KeyboardActive?.ctrl) {
  156. // 若已选中,则去除
  157. if (chartEditStore.targetChart.selectId.includes(item.id)) {
  158. const exList = chartEditStore.targetChart.selectId.filter(e => e !== item.id)
  159. chartEditStore.setTargetSelectChart(exList)
  160. } else {
  161. chartEditStore.setTargetSelectChart(item.id, true)
  162. }
  163. }
  164. }
  165. // * 按下事件(包含移动事件)
  166. const mousedownHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  167. e.preventDefault()
  168. e.stopPropagation()
  169. if (item.status.lock) return
  170. onClickOutSide()
  171. // 按下左键 + CTRL
  172. if (e.buttons === MouseEventButton.LEFT && window.$KeyboardActive?.ctrl) return
  173. // 按下右键 + 选中多个 + 目标元素是多选子元素
  174. const selectId = chartEditStore.getTargetChart.selectId
  175. if (e.buttons === MouseEventButton.RIGHT && selectId.length > 1 && selectId.includes(item.id)) return
  176. // 选中当前目标组件
  177. chartEditStore.setTargetSelectChart(item.id)
  178. // 按下右键
  179. if (e.buttons === MouseEventButton.RIGHT) return
  180. const scale = chartEditStore.getEditCanvas.scale
  181. const canvasWidth = chartEditStore.getEditCanvasConfig.width
  182. const canvasHeight = chartEditStore.getEditCanvasConfig.height
  183. // 记录图表初始位置和大小
  184. const targetMap = new Map()
  185. chartEditStore.getTargetChart.selectId.forEach(id => {
  186. const index = chartEditStore.fetchTargetIndex(id)
  187. if (index !== -1) {
  188. const { x, y, w, h } = toRaw(chartEditStore.getComponentList[index]).attr
  189. targetMap.set(id, { x, y, w, h })
  190. }
  191. })
  192. // 记录点击初始位置
  193. const startX = e.screenX
  194. const startY = e.screenY
  195. // 记录历史位置
  196. let prevComponentInstance: Array<CreateComponentType | CreateComponentGroupType> = []
  197. chartEditStore.getTargetChart.selectId.forEach(id => {
  198. if (!targetMap.has(id)) return
  199. const index = chartEditStore.fetchTargetIndex(id)
  200. // 拿到初始位置数据
  201. prevComponentInstance.push(cloneDeep(chartEditStore.getComponentList[index]))
  202. })
  203. // 记录初始位置
  204. chartEditStore.setMousePosition(undefined, undefined, startX, startY)
  205. // 移动-计算偏移量
  206. const mousemove = throttle((moveEvent: MouseEvent) => {
  207. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  208. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  209. // 当前偏移量,处理 scale 比例问题
  210. let offsetX = (moveEvent.screenX - startX) / scale
  211. let offsetY = (moveEvent.screenY - startY) / scale
  212. chartEditStore.getTargetChart.selectId.forEach(id => {
  213. if (!targetMap.has(id)) return
  214. const index = chartEditStore.fetchTargetIndex(id)
  215. // 拿到初始位置数据
  216. const { x, y, w, h } = targetMap.get(id)
  217. const componentInstance = chartEditStore.getComponentList[index]
  218. let currX = Math.round(x + offsetX)
  219. let currY = Math.round(y + offsetY)
  220. // 要预留的距离
  221. const distance = 50
  222. // 基于左上角位置检测
  223. currX = currX < -w + distance ? -w + distance : currX
  224. currY = currY < -h + distance ? -h + distance : currY
  225. // 基于右下角位置检测
  226. currX = currX > canvasWidth - distance ? canvasWidth - distance : currX
  227. currY = currY > canvasHeight - distance ? canvasHeight - distance : currY
  228. if (componentInstance) {
  229. componentInstance.attr = Object.assign(componentInstance.attr, {
  230. x: currX,
  231. y: currY
  232. })
  233. }
  234. })
  235. return
  236. }, 20)
  237. const mouseup = () => {
  238. try {
  239. chartEditStore.setMousePosition(0, 0, 0, 0)
  240. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  241. // 加入历史栈
  242. if (prevComponentInstance.length) {
  243. chartEditStore.getTargetChart.selectId.forEach(id => {
  244. if (!targetMap.has(id)) return
  245. const index = chartEditStore.fetchTargetIndex(id)
  246. const curComponentInstance = chartEditStore.getComponentList[index]
  247. // 找到记录的所选组件
  248. prevComponentInstance.forEach(preItem => {
  249. if (preItem.id === id) {
  250. preItem.attr = Object.assign(preItem.attr, {
  251. offsetX: curComponentInstance.attr.x - preItem.attr.x,
  252. offsetY: curComponentInstance.attr.y - preItem.attr.y
  253. })
  254. }
  255. })
  256. })
  257. chartEditStore.moveComponentList(prevComponentInstance)
  258. }
  259. document.removeEventListener('mousemove', mousemove)
  260. document.removeEventListener('mouseup', mouseup)
  261. } catch (err) {
  262. console.log(err)
  263. }
  264. }
  265. document.addEventListener('mousemove', mousemove)
  266. document.addEventListener('mouseup', mouseup)
  267. }
  268. // * 进入事件
  269. const mouseenterHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  270. e.preventDefault()
  271. e.stopPropagation()
  272. if (!chartEditStore.getEditCanvas.isSelect) {
  273. chartEditStore.setTargetHoverChart(item.id)
  274. }
  275. }
  276. // * 移出事件
  277. const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  278. e.preventDefault()
  279. e.stopPropagation()
  280. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  281. chartEditStore.setTargetHoverChart(undefined)
  282. }
  283. return { mouseClickHandle, mousedownHandle, mouseenterHandle, mouseleaveHandle }
  284. }
  285. // * 移动锚点
  286. export const useMousePointHandle = (e: MouseEvent, point: string, attr: PickCreateComponentType<'attr'>) => {
  287. e.stopPropagation()
  288. e.preventDefault()
  289. // 设置拖拽状态
  290. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  291. const scale = chartEditStore.getEditCanvas.scale
  292. const itemAttrX = attr.x
  293. const itemAttrY = attr.y
  294. const itemAttrW = attr.w
  295. const itemAttrH = attr.h
  296. // 记录点击初始位置
  297. const startX = e.screenX
  298. const startY = e.screenY
  299. // 记录初始位置
  300. chartEditStore.setMousePosition(startX, startY)
  301. const mousemove = throttle((moveEvent: MouseEvent) => {
  302. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  303. let currX = Math.round((moveEvent.screenX - startX) / scale)
  304. let currY = Math.round((moveEvent.screenY - startY) / scale)
  305. const isTop = /t/.test(point)
  306. const isBottom = /b/.test(point)
  307. const isLeft = /l/.test(point)
  308. const isRight = /r/.test(point)
  309. const newHeight = itemAttrH + (isTop ? -currY : isBottom ? currY : 0)
  310. const newWidth = itemAttrW + (isLeft ? -currX : isRight ? currX : 0)
  311. attr.h = newHeight > 0 ? newHeight : 0
  312. attr.w = newWidth > 0 ? newWidth : 0
  313. attr.x = itemAttrX + (isLeft ? currX : 0)
  314. attr.y = itemAttrY + (isTop ? currY : 0)
  315. }, 50)
  316. const mouseup = () => {
  317. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  318. chartEditStore.setMousePosition(0, 0, 0, 0)
  319. document.removeEventListener('mousemove', mousemove)
  320. document.removeEventListener('mouseup', mouseup)
  321. }
  322. document.addEventListener('mousemove', mousemove)
  323. document.addEventListener('mouseup', mouseup)
  324. }