90 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-11-12 17:20:11 +08:00
import { watch } from 'vue'
import { useRoute } from 'vue-router'
2023-03-03 22:25:36 +08:00
import throttle from 'lodash/throttle'
2022-11-12 17:20:11 +08:00
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
2023-03-03 22:25:36 +08:00
import { EditCanvasTypeEnum } from '@/store/modules/chartEditStore/chartEditStore.d'
2022-11-12 17:20:11 +08:00
import { useSync } from '@/views/chart/hooks/useSync.hook'
import { ChartEnum } from '@/enums/pageEnum'
import { SavePageEnum } from '@/enums/editPageEnum'
import { editToJsonInterval } from '@/settings/designSetting'
2023-03-03 22:25:36 +08:00
import { goDialog } from '@/utils'
2022-11-12 17:20:11 +08:00
const { updateComponent } = useSync()
const chartEditStore = useChartEditStore()
2022-11-15 16:21:35 +08:00
2023-03-03 22:25:36 +08:00
export const syncData = () => {
goDialog({
message: '是否覆盖源视图内容,此操作不可撤回?',
isMaskClosable: true,
transformOrigin: 'center',
onPositiveCallback: () => {
window['$message'].success('正在同步编辑器...')
dispatchEvent(new CustomEvent(SavePageEnum.CHART, { detail: chartEditStore.getStorageInfo }))
}
})
}
// 同步数据到预览页
export const syncDataToPreview = () => {
dispatchEvent(new CustomEvent(SavePageEnum.CHART_TO_PREVIEW, { detail: chartEditStore.getStorageInfo }))
}
2022-11-12 17:20:11 +08:00
// 侦听器更新
const useSyncUpdateHandle = () => {
// 定义侦听器变量
let timer: any
2023-03-03 22:25:36 +08:00
// 更新处理
const updateFn = (e: any) => {
window['$message'].success('正在进行更新...')
updateComponent(e!.detail, true)
}
// 页面关闭处理
const closeFn = () => {
chartEditStore.setEditCanvas(EditCanvasTypeEnum.IS_CODE_EDIT, false)
2022-11-12 17:20:11 +08:00
}
// 开启侦听
const use = () => {
2023-03-03 22:25:36 +08:00
// 定时同步数据(暂不开启)
2022-11-13 23:29:43 +08:00
// timer = setInterval(() => {
// // 窗口激活并且处于工作台
// document.hasFocus() && syncData()
// }, editToJsonInterval)
2022-11-12 17:20:11 +08:00
// 失焦同步数据
addEventListener('blur', syncDataToPreview)
2023-03-03 22:25:36 +08:00
// 监听编辑器保存事件 刷新工作台图表
2022-11-12 17:20:11 +08:00
addEventListener(SavePageEnum.JSON, updateFn)
2023-03-03 22:25:36 +08:00
// 监听编辑页关闭
addEventListener(SavePageEnum.CLOSE, throttle(closeFn, 1000))
2022-11-12 17:20:11 +08:00
}
// 关闭侦听
const unUse = () => {
2022-11-13 23:29:43 +08:00
// clearInterval(timer)
removeEventListener('blur', syncDataToPreview)
2022-11-12 17:20:11 +08:00
removeEventListener(SavePageEnum.JSON, updateFn)
}
// 路由变更时处理
const watchHandler = (toName: any, fromName: any) => {
if (fromName == ChartEnum.CHART_HOME_NAME) {
unUse()
}
if (toName == ChartEnum.CHART_HOME_NAME) {
use()
}
}
2023-03-03 22:25:36 +08:00
2022-11-12 17:20:11 +08:00
return watchHandler
}
export const useSyncUpdate = () => {
const routerParamsInfo = useRoute()
watch(() => routerParamsInfo.name, useSyncUpdateHandle(), { immediate: true })
2023-03-03 22:25:36 +08:00
}