useDrag.hook.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. if (e.which == 2) return
  55. if (window.$KeyboardActive?.space) return
  56. mousedownHandleUnStop(e)
  57. // 记录点击初始位置
  58. const startOffsetX = e.offsetX
  59. const startOffsetY = e.offsetY
  60. const startScreenX = e.screenX
  61. const startScreenY = e.screenY
  62. // 记录缩放
  63. const scale = chartEditStore.getEditCanvas.scale
  64. chartEditStore.setMousePosition(undefined, undefined, startOffsetX, startOffsetY)
  65. // 移动框选
  66. const mousemove = throttle((moveEvent: MouseEvent) => {
  67. // 取消当前选中
  68. chartEditStore.setTargetSelectChart()
  69. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_SELECT, true)
  70. // 这里先把相对值算好,不然组件无法获取 startScreenX 和 startScreenY 的值
  71. const currX = startOffsetX + moveEvent.screenX - startScreenX
  72. const currY = startOffsetY + moveEvent.screenY - startScreenY
  73. chartEditStore.setMousePosition(currX, currY)
  74. // 计算框选的左上角和右下角
  75. const selectAttr = {
  76. // 左上角
  77. x1: 0,
  78. y1: 0,
  79. // 右下角
  80. x2: 0,
  81. y2: 0
  82. }
  83. if (currX > startOffsetX && currY > startOffsetY) {
  84. // 右下方向
  85. selectAttr.x1 = startOffsetX
  86. selectAttr.y1 = startOffsetY
  87. selectAttr.x2 = Math.round(startOffsetX + (moveEvent.screenX - startScreenX) / scale)
  88. selectAttr.y2 = Math.round(startOffsetY + (moveEvent.screenY - startScreenY) / scale)
  89. } else if (currX > startOffsetX && currY < startOffsetY) {
  90. // 右上方向
  91. selectAttr.x1 = startOffsetX
  92. selectAttr.y1 = Math.round(startOffsetY - (startScreenY - moveEvent.screenY) / scale)
  93. selectAttr.x2 = Math.round(startOffsetX + (moveEvent.screenX - startScreenX) / scale)
  94. selectAttr.y2 = startOffsetY
  95. } else if (currX < startOffsetX && currY > startOffsetY) {
  96. selectAttr.x1 = Math.round(startOffsetX - (startScreenX - moveEvent.screenX) / scale)
  97. selectAttr.y1 = startOffsetY
  98. selectAttr.x2 = startOffsetX
  99. selectAttr.y2 = Math.round(startOffsetY + (moveEvent.screenY - startScreenY) / scale)
  100. // 左下方向
  101. } else {
  102. // 左上方向
  103. selectAttr.x1 = Math.round(startOffsetX - (startScreenX - moveEvent.screenX) / scale)
  104. selectAttr.y1 = Math.round(startOffsetY - (startScreenY - moveEvent.screenY) / scale)
  105. selectAttr.x2 = startOffsetX
  106. selectAttr.y2 = startOffsetY
  107. }
  108. // 遍历组件
  109. chartEditStore.getComponentList.forEach(item => {
  110. if (!chartEditStore.getTargetChart.selectId.includes(item.id)) {
  111. // 处理左上角
  112. let isSelect = false
  113. const { x, y, w, h } = item.attr
  114. const targetAttr = {
  115. // 左上角
  116. x1: x,
  117. y1: y,
  118. // 右下角
  119. x2: x + w,
  120. y2: y + h
  121. }
  122. // 全包含则选中
  123. if (
  124. targetAttr.x1 - selectAttr.x1 >= 0 &&
  125. targetAttr.y1 - selectAttr.y1 >= 0 &&
  126. targetAttr.x2 - selectAttr.x2 <= 0 &&
  127. targetAttr.y2 - selectAttr.y2 <= 0 &&
  128. !item.status.lock &&
  129. !item.status.hide
  130. ) {
  131. isSelect = true
  132. chartEditStore.setTargetSelectChart(item.id, true)
  133. }
  134. }
  135. })
  136. }, 20)
  137. // 鼠标抬起
  138. const mouseup = () => {
  139. // 鼠标抬起时,结束mousemove的节流函数,避免选框不消失问题
  140. mousemove.cancel()
  141. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_SELECT, false)
  142. chartEditStore.setMousePosition(0, 0, 0, 0)
  143. document.removeEventListener('mousemove', mousemove)
  144. document.removeEventListener('mouseup', mouseup)
  145. }
  146. document.addEventListener('mousemove', mousemove)
  147. document.addEventListener('mouseup', mouseup)
  148. }
  149. // * 鼠标事件
  150. export const useMouseHandle = () => {
  151. // * Click 事件, 松开鼠标触发
  152. const mouseClickHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  153. e.preventDefault()
  154. e.stopPropagation()
  155. if (item.status.lock) return
  156. // 若此时按下了 CTRL, 表示多选
  157. if (window.$KeyboardActive?.ctrl) {
  158. // 若已选中,则去除
  159. if (chartEditStore.targetChart.selectId.includes(item.id)) {
  160. const exList = chartEditStore.targetChart.selectId.filter(e => e !== item.id)
  161. chartEditStore.setTargetSelectChart(exList)
  162. } else {
  163. chartEditStore.setTargetSelectChart(item.id, true)
  164. }
  165. }
  166. }
  167. // * 按下事件(包含移动事件)
  168. const mousedownHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  169. e.preventDefault()
  170. e.stopPropagation()
  171. if (item.status.lock) return
  172. onClickOutSide()
  173. // 按下左键 + CTRL
  174. if (e.buttons === MouseEventButton.LEFT && window.$KeyboardActive?.ctrl) 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: Array<CreateComponentType | CreateComponentGroupType> = []
  199. chartEditStore.getTargetChart.selectId.forEach(id => {
  200. if (!targetMap.has(id)) return
  201. const index = chartEditStore.fetchTargetIndex(id)
  202. // 拿到初始位置数据
  203. prevComponentInstance.push(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. if (componentInstance) {
  231. componentInstance.attr = Object.assign(componentInstance.attr, {
  232. x: currX,
  233. y: currY
  234. })
  235. }
  236. })
  237. return
  238. }, 20)
  239. const mouseup = () => {
  240. try {
  241. chartEditStore.setMousePosition(0, 0, 0, 0)
  242. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  243. // 加入历史栈
  244. if (prevComponentInstance.length) {
  245. chartEditStore.getTargetChart.selectId.forEach(id => {
  246. if (!targetMap.has(id)) return
  247. const index = chartEditStore.fetchTargetIndex(id)
  248. const curComponentInstance = chartEditStore.getComponentList[index]
  249. // 找到记录的所选组件
  250. prevComponentInstance.forEach(preItem => {
  251. if (preItem.id === id) {
  252. preItem.attr = Object.assign(preItem.attr, {
  253. offsetX: curComponentInstance.attr.x - preItem.attr.x,
  254. offsetY: curComponentInstance.attr.y - preItem.attr.y
  255. })
  256. }
  257. })
  258. })
  259. chartEditStore.moveComponentList(prevComponentInstance)
  260. }
  261. document.removeEventListener('mousemove', mousemove)
  262. document.removeEventListener('mouseup', mouseup)
  263. } catch (err) {
  264. console.log(err)
  265. }
  266. }
  267. document.addEventListener('mousemove', mousemove)
  268. document.addEventListener('mouseup', mouseup)
  269. }
  270. // * 进入事件
  271. const mouseenterHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  272. e.preventDefault()
  273. e.stopPropagation()
  274. if (!chartEditStore.getEditCanvas.isSelect) {
  275. chartEditStore.setTargetHoverChart(item.id)
  276. }
  277. }
  278. // * 移出事件
  279. const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  280. e.preventDefault()
  281. e.stopPropagation()
  282. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  283. chartEditStore.setTargetHoverChart(undefined)
  284. }
  285. return { mouseClickHandle, mousedownHandle, mouseenterHandle, mouseleaveHandle }
  286. }
  287. // * 移动锚点
  288. export const useMousePointHandle = (e: MouseEvent, point: string, attr: PickCreateComponentType<'attr'>) => {
  289. e.stopPropagation()
  290. e.preventDefault()
  291. // 设置拖拽状态
  292. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  293. const scale = chartEditStore.getEditCanvas.scale
  294. const itemAttrX = attr.x
  295. const itemAttrY = attr.y
  296. const itemAttrW = attr.w
  297. const itemAttrH = attr.h
  298. // 记录点击初始位置
  299. const startX = e.screenX
  300. const startY = e.screenY
  301. // 记录初始位置
  302. chartEditStore.setMousePosition(startX, startY)
  303. const mousemove = throttle((moveEvent: MouseEvent) => {
  304. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  305. let currX = Math.round((moveEvent.screenX - startX) / scale)
  306. let currY = Math.round((moveEvent.screenY - startY) / scale)
  307. const isTop = /t/.test(point)
  308. const isBottom = /b/.test(point)
  309. const isLeft = /l/.test(point)
  310. const isRight = /r/.test(point)
  311. const newHeight = itemAttrH + (isTop ? -currY : isBottom ? currY : 0)
  312. const newWidth = itemAttrW + (isLeft ? -currX : isRight ? currX : 0)
  313. attr.h = newHeight > 0 ? newHeight : 0
  314. attr.w = newWidth > 0 ? newWidth : 0
  315. attr.x = itemAttrX + (isLeft ? currX : 0)
  316. attr.y = itemAttrY + (isTop ? currY : 0)
  317. }, 50)
  318. const mouseup = () => {
  319. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  320. chartEditStore.setMousePosition(0, 0, 0, 0)
  321. document.removeEventListener('mousemove', mousemove)
  322. document.removeEventListener('mouseup', mouseup)
  323. }
  324. document.addEventListener('mousemove', mousemove)
  325. document.addEventListener('mouseup', mouseup)
  326. }