42 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-01-24 21:12:18 +08:00
import { toRefs } from 'vue'
import { getChartEditStore } from './useStore.hook'
import { DragKeyEnum } from '@/enums/editPageEnum'
2022-01-25 18:19:44 +08:00
import { createComponent } from '@/packages'
import { ConfigType } from '@/packages/index.d'
2022-01-27 23:16:51 +08:00
import { loadingStart, loadingFinish, loadingError } from '@/utils'
2022-01-24 21:12:18 +08:00
const chartEditStore = getChartEditStore()
const { scale } = toRefs(chartEditStore.getEditCanvas)
// * 拖拽中
2022-01-25 18:19:44 +08:00
export const handleDrop = 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-01-24 21:12:18 +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)
const dropData: Exclude<ConfigType, ['node', 'image']> = JSON.parse(
drayDataString
)
2022-01-27 23:16:51 +08:00
// 创建新图表组件
let newComponent = await createComponent(dropData)
2022-01-25 18:19:44 +08:00
2022-01-29 11:44:51 +08:00
newComponent.setPosition(e.offsetX - newComponent.attr.w / 2, e.offsetY - newComponent.attr.h / 2)
2022-01-25 18:19:44 +08:00
chartEditStore.addComponentList(newComponent)
2022-01-24 21:12:18 +08:00
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
}
}
// * 拖拽结束
export const handleDragOver = (e: DragEvent) => {
e.preventDefault()
e.stopPropagation()
if (e.dataTransfer) e.dataTransfer.dropEffect = 'copy'
}