110 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-01-29 23:58:56 +08:00
import { onUnmounted, onMounted } from 'vue'
2022-01-24 16:25:43 +08:00
import { getChartEditStore } from './useStore.hook'
import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
2022-01-29 23:58:56 +08:00
import { CreateComponentType } from '@/packages/index.d'
2022-01-30 14:20:28 +08:00
import throttle from 'lodash/throttle'
2022-01-24 16:25:43 +08:00
const chartEditStore = getChartEditStore()
2022-01-27 22:30:35 +08:00
// 布局处理
2022-01-24 16:25:43 +08:00
export const useLayout = () => {
onMounted(() => {
2022-01-24 21:12:18 +08:00
// 设置 Dom 值(ref 不生效先用 document)
2022-01-24 16:25:43 +08:00
chartEditStore.setEditCanvasItem(
2022-01-24 21:12:18 +08:00
EditCanvasTypeEnum.EDIT_LAYOUT_DOM,
2022-01-24 16:25:43 +08:00
document.getElementById('go-chart-edit-layout')
)
chartEditStore.setEditCanvasItem(
2022-01-24 21:12:18 +08:00
EditCanvasTypeEnum.EDIT_CONTENT_DOM,
2022-01-24 16:25:43 +08:00
document.getElementById('go-chart-edit-content')
)
// 大小初始化
chartEditStore.setPageSize()
// 监听初始化
const removeScale = chartEditStore.listenerScale()
onUnmounted(() => {
removeScale()
})
})
2022-01-29 23:58:56 +08:00
}
2022-01-30 14:20:28 +08:00
// 不拦截默认行为点击
export const mousedownHandleUnStop = (
e: MouseEvent,
item?: CreateComponentType
) => {
2022-01-29 23:58:56 +08:00
if (item) {
chartEditStore.setTargetSelectChart(item.id)
return
}
chartEditStore.setTargetSelectChart(item)
}
2022-01-30 14:20:28 +08:00
export const useMouseHandle = () => {
// 点击事件(包含移动事件)
const mousedownHandle = (e: MouseEvent, item: CreateComponentType) => {
e.preventDefault()
e.stopPropagation()
chartEditStore.setTargetSelectChart(item.id)
const scale = chartEditStore.getEditCanvas.scale
const width = chartEditStore.getEditCanvas.width
const height = chartEditStore.getEditCanvas.height
// 获取编辑区域 Dom
const editcontentDom = chartEditStore.getEditCanvas.editContentDom
// 记录图表初始位置
const itemAttrX = item.attr.x
const itemAttrY = item.attr.y
// 记录点击初始位置
const startX = e.screenX
const startY = e.screenY
// 计算偏移量(处理 scale 比例问题)
const mousemove = throttle((moveEvent: MouseEvent) => {
let currX = itemAttrX + (moveEvent.screenX - startX) / scale
let currY = itemAttrY + (moveEvent.screenY - startY) / scale
// 位置检测
currX = currX < 0 ? 0 : currX
currY = currY < 0 ? 0 : currY
// 预留 20px 边距
currX = currX > width - 20 ? width - 20 : currX
currY = currY > height - 20 ? height - 20 : currY
item.attr.x = currX
item.attr.y = currY
}, 30)
const mouseup = () => {
editcontentDom!.removeEventListener('mousemove', mousemove)
editcontentDom!.removeEventListener('mouseup', mouseup)
}
editcontentDom!.addEventListener('mousemove', mousemove)
editcontentDom!.addEventListener('mouseup', mouseup)
}
// 进入事件
const mouseenterHandle = (e: MouseEvent, item: CreateComponentType) => {
e.preventDefault()
e.stopPropagation()
chartEditStore.setTargetHoverChart(item.id)
}
// 移出事件
const mouseleaveHandle = (e: MouseEvent, item: CreateComponentType) => {
e.preventDefault()
e.stopPropagation()
chartEditStore.setTargetHoverChart(undefined)
}
2022-01-29 23:58:56 +08:00
2022-01-30 14:20:28 +08:00
return { mousedownHandle, mouseenterHandle, mouseleaveHandle }
2022-01-29 23:58:56 +08:00
}