useDrag.hook.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. !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 (
  156. window.$KeyboardActive?.has(WinKeyboard.CTRL_SOURCE_KEY) ||
  157. window.$KeyboardActive?.has(MacKeyboard.CTRL_SOURCE_KEY)
  158. ) {
  159. // 若已选中,则去除
  160. if (chartEditStore.targetChart.selectId.includes(item.id)) {
  161. const exList = chartEditStore.targetChart.selectId.filter(e => e !== item.id)
  162. chartEditStore.setTargetSelectChart(exList)
  163. } else {
  164. chartEditStore.setTargetSelectChart(item.id, true)
  165. }
  166. }
  167. }
  168. // * 按下事件(包含移动事件)
  169. const mousedownHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
  170. e.preventDefault()
  171. e.stopPropagation()
  172. if (item.status.lock) return
  173. onClickOutSide()
  174. // 按下左键 + CTRL
  175. if (
  176. e.buttons === MouseEventButton.LEFT &&
  177. (window.$KeyboardActive?.has(WinKeyboard.CTRL_SOURCE_KEY) ||
  178. window.$KeyboardActive?.has(MacKeyboard.CTRL_SOURCE_KEY))
  179. )
  180. return
  181. // 按下右键 + 选中多个 + 目标元素是多选子元素
  182. const selectId = chartEditStore.getTargetChart.selectId
  183. if (e.buttons === MouseEventButton.RIGHT && selectId.length > 1 && selectId.includes(item.id)) return
  184. // 选中当前目标组件
  185. chartEditStore.setTargetSelectChart(item.id)
  186. // 按下右键
  187. if (e.buttons === MouseEventButton.RIGHT) return
  188. const scale = chartEditStore.getEditCanvas.scale
  189. const canvasWidth = chartEditStore.getEditCanvasConfig.width
  190. const canvasHeight = chartEditStore.getEditCanvasConfig.height
  191. // 记录图表初始位置和大小
  192. const targetMap = new Map()
  193. chartEditStore.getTargetChart.selectId.forEach(id => {
  194. const index = chartEditStore.fetchTargetIndex(id)
  195. if (index !== -1) {
  196. const { x, y, w, h } = toRaw(chartEditStore.getComponentList[index]).attr
  197. targetMap.set(id, { x, y, w, h })
  198. }
  199. })
  200. // 记录点击初始位置
  201. const startX = e.screenX
  202. const startY = e.screenY
  203. // 记录历史位置
  204. let prevComponentInstance: Array<CreateComponentType | CreateComponentGroupType> = []
  205. chartEditStore.getTargetChart.selectId.forEach(id => {
  206. if (!targetMap.has(id)) return
  207. const index = chartEditStore.fetchTargetIndex(id)
  208. // 拿到初始位置数据
  209. prevComponentInstance.push(cloneDeep(chartEditStore.getComponentList[index]))
  210. })
  211. // 记录初始位置
  212. chartEditStore.setMousePosition(undefined, undefined, startX, startY)
  213. // 移动-计算偏移量
  214. const mousemove = throttle((moveEvent: MouseEvent) => {
  215. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
  216. chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
  217. // 当前偏移量,处理 scale 比例问题
  218. let offsetX = (moveEvent.screenX - startX) / scale
  219. let offsetY = (moveEvent.screenY - startY) / scale
  220. chartEditStore.getTargetChart.selectId.forEach(id => {
  221. if (!targetMap.has(id)) return
  222. const index = chartEditStore.fetchTargetIndex(id)
  223. // 拿到初始位置数据
  224. const { x, y, w, h } = targetMap.get(id)
  225. const componentInstance = chartEditStore.getComponentList[index]
  226. let currX = Math.round(x + offsetX)
  227. let currY = Math.round(y + offsetY)
  228. // 要预留的距离
  229. const distance = 50
  230. // 基于左上角位置检测
  231. currX = currX < -w + distance ? -w + distance : currX
  232. currY = currY < -h + distance ? -h + distance : currY
  233. // 基于右下角位置检测
  234. currX = currX > canvasWidth - distance ? canvasWidth - distance : currX
  235. currY = currY > canvasHeight - distance ? canvasHeight - distance : currY
  236. componentInstance.attr = Object.assign(componentInstance.attr, {
  237. x: currX,
  238. y: currY
  239. })
  240. })
  241. return
  242. }, 20)
  243. const mouseup = () => {
  244. chartEditStore.setMousePosition(0, 0, 0, 0)
  245. chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
  246. // 加入历史栈
  247. if (prevComponentInstance.length) {
  248. chartEditStore.getTargetChart.selectId.forEach(id => {
  249. if (!targetMap.has(id)) return
  250. const index = chartEditStore.fetchTargetIndex(id)
  251. const curComponentInstance = chartEditStore.getComponentList[index]
  252. // 找到记录的所选组件
  253. prevComponentInstance.forEach(preItem => {
  254. if (preItem.id === id) {
  255. preItem.attr = Object.assign(preItem.attr, {
  256. offsetX: curComponentInstance.attr.x - preItem.attr.x,
  257. offsetY: curComponentInstance.attr.y - preItem.attr.y
  258. })
  259. }
  260. })
  261. })
  262. chartEditStore.moveComponentList(prevComponentInstance)
  263. }
  264. document.removeEventListener('mousemove', mousemove)
  265. document.removeEventListener('mouseup', mouseup)
  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. }