219 lines
7.4 KiB
TypeScript
Raw Normal View History

import { DragKeyEnum, MouseEventButton, WinKeyboard, MacKeyboard } from '@/enums/editPageEnum'
2022-01-25 18:19:44 +08:00
import { createComponent } from '@/packages'
import { ConfigType } from '@/packages/index.d'
2022-08-07 17:24:05 +08:00
import { CreateComponentType, CreateComponentGroupType, PickCreateComponentType } from '@/packages/index.d'
2022-02-01 17:12:16 +08:00
import { useContextMenu } from '@/views/chart/hooks/useContextMenu.hook'
2022-03-04 20:57:36 +08:00
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
import { loadingStart, loadingFinish, loadingError } from '@/utils'
import throttle from 'lodash/throttle'
2022-01-24 21:12:18 +08:00
2022-03-04 20:57:36 +08:00
const chartEditStore = useChartEditStore()
2022-03-28 17:24:56 +08:00
const { onClickOutSide } = useContextMenu()
2022-01-24 21:12:18 +08:00
// * 拖拽到编辑区域里
2022-04-11 18:17:09 +08:00
export const dragHandle = async (e: DragEvent) => {
2022-01-24 21:12:18 +08:00
e.preventDefault()
2022-01-27 23:16:51 +08:00
2022-01-24 21:12:18 +08:00
try {
2022-01-27 23:16:51 +08:00
loadingStart()
2022-04-11 18:17:09 +08:00
2022-01-27 23:16:51 +08:00
// 获取拖拽数据
2022-01-25 18:19:44 +08:00
const drayDataString = e!.dataTransfer!.getData(DragKeyEnum.DROG_KEY)
2022-02-06 21:35:38 +08:00
if (!drayDataString) {
loadingFinish()
return
}
2022-04-11 18:17:09 +08:00
// 修改状态
chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_CREATE, false)
const dropData: Exclude<ConfigType, ['image']> = JSON.parse(drayDataString)
2022-01-27 23:16:51 +08:00
// 创建新图表组件
2022-03-03 10:19:41 +08:00
let newComponent: CreateComponentType = await createComponent(dropData)
2022-01-25 18:19:44 +08:00
newComponent.setPosition(e.offsetX - newComponent.attr.w / 2, e.offsetY - newComponent.attr.h / 2)
2022-02-03 22:54:31 +08:00
chartEditStore.addComponentList(newComponent, false, true)
2022-03-03 10:19:41 +08:00
chartEditStore.setTargetSelectChart(newComponent.id)
2022-01-27 23:16:51 +08:00
loadingFinish()
2022-01-24 21:12:18 +08:00
} catch (error) {
2022-01-27 23:16:51 +08:00
loadingError()
window['$message'].warning(`图表正在研发中, 敬请期待...`)
2022-01-24 21:12:18 +08:00
}
}
2022-04-11 18:17:09 +08:00
// * 进入拖拽区域
export const dragoverHandle = (e: DragEvent) => {
2022-01-24 21:12:18 +08:00
e.preventDefault()
e.stopPropagation()
2022-01-24 21:12:18 +08:00
if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
}
2022-01-30 19:38:12 +08:00
2022-02-01 17:12:16 +08:00
// * 不拦截默认行为点击
2022-08-07 17:24:05 +08:00
export const mousedownHandleUnStop = (e: MouseEvent, item?: CreateComponentType | CreateComponentGroupType) => {
2022-01-30 19:38:12 +08:00
if (item) {
chartEditStore.setTargetSelectChart(item.id)
return
}
2022-02-01 01:20:00 +08:00
chartEditStore.setTargetSelectChart(undefined)
2022-01-30 19:38:12 +08:00
}
2022-02-01 17:12:16 +08:00
// * 移动图表
2022-01-30 19:38:12 +08:00
export const useMouseHandle = () => {
// * Click 事件, 松开鼠标触发
2022-08-07 17:24:05 +08:00
const mouseClickHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
2022-01-30 19:38:12 +08:00
e.preventDefault()
e.stopPropagation()
// 若此时按下了 CTRL, 表示多选
if (
window.$KeyboardActive?.has(WinKeyboard.CTRL_SOURCE_KEY) ||
window.$KeyboardActive?.has(MacKeyboard.CTRL_SOURCE_KEY)
) {
chartEditStore.setTargetSelectChart(item.id, true)
}
}
2022-01-30 19:38:12 +08:00
// * 按下事件(包含移动事件)
2022-08-07 17:24:05 +08:00
const mousedownHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
e.preventDefault()
e.stopPropagation()
2022-03-28 17:24:56 +08:00
onClickOutSide()
// 按下左键 + CTRL
if (
e.buttons === MouseEventButton.LEFT &&
(window.$KeyboardActive?.has(WinKeyboard.CTRL_SOURCE_KEY) ||
window.$KeyboardActive?.has(MacKeyboard.CTRL_SOURCE_KEY))
)
return
2022-08-05 10:58:46 +08:00
// 按下右键 + 选中多个 + 目标元素是多选子元素
const selectId = chartEditStore.getTargetChart.selectId
if (e.buttons === MouseEventButton.RIGHT && selectId.length > 1 && selectId.includes(item.id)) return
// 选中当前目标组件
2022-01-30 19:38:12 +08:00
chartEditStore.setTargetSelectChart(item.id)
// 按下右键
if (e.buttons === MouseEventButton.RIGHT) return
2022-01-30 19:38:12 +08:00
const scale = chartEditStore.getEditCanvas.scale
2022-02-06 21:35:38 +08:00
const width = chartEditStore.getEditCanvasConfig.width
const height = chartEditStore.getEditCanvasConfig.height
2022-01-30 19:38:12 +08:00
// 记录图表初始位置和大小
2022-01-30 19:38:12 +08:00
const itemAttrX = item.attr.x
const itemAttrY = item.attr.y
const itemAttrW = item.attr.w
const itemAttrH = item.attr.h
2022-01-30 19:38:12 +08:00
// 记录点击初始位置
const startX = e.screenX
const startY = e.screenY
2022-03-04 20:57:36 +08:00
// 记录初始位置
chartEditStore.setMousePosition(startX, startY)
2022-01-30 19:38:12 +08:00
// 计算偏移量(处理 scale 比例问题)
const mousemove = throttle((moveEvent: MouseEvent) => {
2022-03-04 20:57:36 +08:00
chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
2022-02-01 17:12:16 +08:00
let currX = Math.round(itemAttrX + (moveEvent.screenX - startX) / scale)
let currY = Math.round(itemAttrY + (moveEvent.screenY - startY) / scale)
2022-01-30 19:38:12 +08:00
// 要预留的距离
const distance = 50
// 基于左上角位置检测
currX = currX < -itemAttrW + distance ? -itemAttrW + distance : currX
currY = currY < -itemAttrH + distance ? -itemAttrH + distance : currY
2022-01-30 19:38:12 +08:00
// 基于右下角位置检测
currX = currX > width - distance ? width - distance : currX
currY = currY > height - distance ? height - distance : currY
2022-01-30 19:38:12 +08:00
item.attr.x = currX
item.attr.y = currY
}, 30)
const mouseup = () => {
2022-03-04 20:57:36 +08:00
chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
chartEditStore.setMousePosition(0, 0)
2022-03-03 10:19:41 +08:00
document.removeEventListener('mousemove', mousemove)
document.removeEventListener('mouseup', mouseup)
2022-01-30 19:38:12 +08:00
}
2022-03-03 10:19:41 +08:00
document.addEventListener('mousemove', mousemove)
document.addEventListener('mouseup', mouseup)
2022-01-30 19:38:12 +08:00
}
2022-02-01 17:12:16 +08:00
// * 进入事件
2022-08-07 17:24:05 +08:00
const mouseenterHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
2022-01-30 19:38:12 +08:00
e.preventDefault()
e.stopPropagation()
chartEditStore.setTargetHoverChart(item.id)
}
2022-02-01 17:12:16 +08:00
// * 移出事件
2022-08-07 17:24:05 +08:00
const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType | CreateComponentGroupType) => {
2022-01-30 19:38:12 +08:00
e.preventDefault()
e.stopPropagation()
chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
2022-01-30 19:38:12 +08:00
chartEditStore.setTargetHoverChart(undefined)
}
return { mouseClickHandle, mousedownHandle, mouseenterHandle, mouseleaveHandle }
2022-03-03 10:19:41 +08:00
}
// * 移动锚点
export const useMousePointHandle = (e: MouseEvent, point: string, attr: PickCreateComponentType<'attr'>) => {
2022-03-03 10:19:41 +08:00
e.stopPropagation()
e.preventDefault()
2022-03-04 20:57:36 +08:00
// 设置拖拽状态
chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, true)
2022-03-03 10:19:41 +08:00
const scale = chartEditStore.getEditCanvas.scale
const itemAttrX = attr.x
const itemAttrY = attr.y
const itemAttrW = attr.w
const itemAttrH = attr.h
// 记录点击初始位置
const startX = e.screenX
const startY = e.screenY
2022-03-04 20:57:36 +08:00
// 记录初始位置
chartEditStore.setMousePosition(startX, startY)
const mousemove = throttle((moveEvent: MouseEvent) => {
chartEditStore.setMousePosition(moveEvent.screenX, moveEvent.screenY)
2022-03-03 10:19:41 +08:00
let currX = Math.round((moveEvent.screenX - startX) / scale)
let currY = Math.round((moveEvent.screenY - startY) / scale)
2022-03-04 20:57:36 +08:00
2022-03-03 10:19:41 +08:00
const isTop = /t/.test(point)
const isBottom = /b/.test(point)
const isLeft = /l/.test(point)
const isRight = /r/.test(point)
2022-04-11 18:17:09 +08:00
const newHeight = itemAttrH + (isTop ? -currY : isBottom ? currY : 0)
const newWidth = itemAttrW + (isLeft ? -currX : isRight ? currX : 0)
2022-03-04 20:57:36 +08:00
2022-03-03 10:19:41 +08:00
attr.h = newHeight > 0 ? newHeight : 0
attr.w = newWidth > 0 ? newWidth : 0
attr.x = itemAttrX + (isLeft ? currX : 0)
attr.y = itemAttrY + (isTop ? currY : 0)
2022-03-06 15:19:18 +08:00
}, 50)
2022-03-03 10:19:41 +08:00
2022-03-04 20:57:36 +08:00
const mouseup = () => {
chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_DRAG, false)
chartEditStore.setMousePosition(0, 0)
document.removeEventListener('mousemove', mousemove)
document.removeEventListener('mouseup', mouseup)
2022-03-03 10:19:41 +08:00
}
2022-03-04 20:57:36 +08:00
document.addEventListener('mousemove', mousemove)
document.addEventListener('mouseup', mouseup)
2022-03-03 10:19:41 +08:00
}