fix: 优化在线编辑JSON的代码和交互

This commit is contained in:
奔跑的面条 2022-10-27 21:10:58 +08:00
parent e4e77cd141
commit 1d73f71aaf
5 changed files with 135 additions and 123 deletions

View File

@ -27,6 +27,7 @@ import {
LockClosedOutline as LockClosedOutlineIcon, LockClosedOutline as LockClosedOutlineIcon,
HelpCircleOutline as HelpOutlineIcon, HelpCircleOutline as HelpOutlineIcon,
CodeSlash as CodeSlashIcon, CodeSlash as CodeSlashIcon,
Create as CreateIcon,
Rocket as RocketIcon, Rocket as RocketIcon,
Duplicate as DuplicateIcon, Duplicate as DuplicateIcon,
DuplicateOutline as DuplicateOutlineIcon, DuplicateOutline as DuplicateOutlineIcon,
@ -106,6 +107,8 @@ const ionicons5 = {
DuplicateOutlineIcon, DuplicateOutlineIcon,
// 代码 // 代码
CodeSlashIcon, CodeSlashIcon,
// 修改代码
CreateIcon,
// 事件(火箭) // 事件(火箭)
RocketIcon, RocketIcon,
// 退出 // 退出

View File

@ -55,6 +55,9 @@ export const backgroundImageSize = 5
// 预览展示方式 // 预览展示方式
export const previewScaleType = PreviewScaleEnum.FIT export const previewScaleType = PreviewScaleEnum.FIT
// 编辑工作台同步到 JSON 的轮询间隔5S
export const editToJsonInterval = 5000
// 数据请求间隔 // 数据请求间隔
export const requestInterval = 30 export const requestInterval = 30

View File

@ -135,9 +135,11 @@ watchEffect(() => {
<style lang="scss" scoped> <style lang="scss" scoped>
$min-width: 500px; $min-width: 500px;
$max-width: 670px;
@include go('edit-bottom') { @include go('edit-bottom') {
width: 100%; width: 100%;
min-width: $min-width; min-width: $min-width;
min-width: $max-width;
padding: 0 10px; padding: 0 10px;
display: flex; display: flex;
align-items: center; align-items: center;

View File

@ -7,7 +7,11 @@
@mouseleave="toolsMouseoutHandle" @mouseleave="toolsMouseoutHandle"
> >
<!-- PawIcon --> <!-- PawIcon -->
<n-icon v-show="settingStore.getChartToolsStatus === ToolsStatusEnum.ASIDE && isMiniComputed " class="asideLogo" size="22"> <n-icon
v-show="settingStore.getChartToolsStatus === ToolsStatusEnum.ASIDE && isMiniComputed"
class="asideLogo"
size="22"
>
<PawIcon></PawIcon> <PawIcon></PawIcon>
</n-icon> </n-icon>
@ -63,6 +67,7 @@ import { useSettingStore } from '@/store/modules/settingStore/settingStore'
import { ToolsStatusEnum } from '@/store/modules/settingStore/settingStore.d' import { ToolsStatusEnum } from '@/store/modules/settingStore/settingStore.d'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { fetchPathByName, routerTurnByPath, setSessionStorage, getLocalStorage } from '@/utils' import { fetchPathByName, routerTurnByPath, setSessionStorage, getLocalStorage } from '@/utils'
import { editToJsonInterval } from '@/settings/designSetting'
import { EditEnum, ChartEnum } from '@/enums/pageEnum' import { EditEnum, ChartEnum } from '@/enums/pageEnum'
import { StorageEnum } from '@/enums/storageEnum' import { StorageEnum } from '@/enums/storageEnum'
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
@ -74,92 +79,12 @@ import { useFile } from './hooks/useFile.hooks'
import { BtnListType, TypeEnum } from './index.d' import { BtnListType, TypeEnum } from './index.d'
import { icon } from '@/plugins' import { icon } from '@/plugins'
const { DownloadIcon, ShareIcon, PawIcon, SettingsSharpIcon, CodeSlashIcon } = icon.ionicons5 const { DownloadIcon, ShareIcon, PawIcon, SettingsSharpIcon, CreateIcon } = icon.ionicons5
const settingStore = useSettingStore() const settingStore = useSettingStore()
const chartEditStore = useChartEditStore() const chartEditStore = useChartEditStore()
const routerParamsInfo = useRoute() const routerParamsInfo = useRoute()
const { updateComponent } = useSync() const { updateComponent } = useSync()
//
const editHandle = () => {
// id
const path = fetchPathByName(EditEnum.CHART_EDIT_NAME, 'href')
if (!path) return
let { id } = routerParamsInfo.params as any
id = typeof id === 'string' ? id : id[0]
updateToSession(id)
routerTurnByPath(path, [id], undefined, true)
}
// SessionStorage 便
function updateToSession(id: string) {
const storageInfo = chartEditStore.getStorageInfo
const sessionStorageInfo = getLocalStorage(StorageEnum.GO_CHART_STORAGE_LIST) || []
if (sessionStorageInfo?.length) {
const repeateIndex = sessionStorageInfo.findIndex((e: { id: string }) => e.id === id)
//
if (repeateIndex !== -1) {
sessionStorageInfo.splice(repeateIndex, 1, { ...storageInfo, id })
setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, sessionStorageInfo)
} else {
sessionStorageInfo.push({ ...storageInfo, id})
setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, sessionStorageInfo)
}
} else {
setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, [{ ...storageInfo, id }])
}
}
function useSyncUpdate() {
//
let timer: any;
const updateFn = (e: any) => updateComponent(e!.detail, true, false)
const syncData = () => {
if (routerParamsInfo.name == ChartEnum.CHART_HOME_NAME) {
dispatchEvent(new CustomEvent(SavePageEnum.CHART, { detail: chartEditStore.getStorageInfo }))
}
}
//
const use = () => {
// 1
timer = setInterval(() => {
//
document.hasFocus() && syncData()
}, 5000)
// 2
addEventListener('blur', syncData)
// JSON
addEventListener(SavePageEnum.JSON, updateFn)
}
//
const unUse = () => {
clearInterval(timer)
removeEventListener(SavePageEnum.JSON, updateFn)
removeEventListener('blur', syncData)
}
//
const watchHandler = (toName: any, fromName: any) => {
if (fromName == ChartEnum.CHART_HOME_NAME) {
unUse()
}
if (toName == ChartEnum.CHART_HOME_NAME) {
use()
}
}
return watchHandler
}
watch(
() => routerParamsInfo.name,
useSyncUpdate(),
{ immediate: true }
)
// //
let mouseTime: any = null let mouseTime: any = null
// model // model
@ -170,45 +95,16 @@ const isMini = ref<boolean>(true)
const asideTootipDis = ref(true) const asideTootipDis = ref(true)
// //
const { importUploadFileListRef, importCustomRequest, importBeforeUpload } = useFile() const { importUploadFileListRef, importCustomRequest, importBeforeUpload } = useFile()
//
const btnList: BtnListType[] = [
{
key: 'export',
type: TypeEnum.BUTTON,
name: '导出',
icon: ShareIcon,
handle: exportHandle
},
{
key: 'import',
type: TypeEnum.IMPORTUPLOAD,
name: '导入',
icon: DownloadIcon
},
{
key: 'edit',
type: TypeEnum.BUTTON,
name: '编辑JSON',
icon: CodeSlashIcon,
handle: editHandle
},
{
key: 'setting',
type: TypeEnum.BUTTON,
name: '设置',
icon: SettingsSharpIcon,
handle: () => {
globalSettingModel.value = true
}
}
]
// //
const isAside = computed(() => settingStore.getChartToolsStatus === ToolsStatusEnum.ASIDE) const isAside = computed(() => settingStore.getChartToolsStatus === ToolsStatusEnum.ASIDE)
// //
const isHide = computed(() => settingStore.getChartToolsStatusHide) const isHide = computed(() => settingStore.getChartToolsStatusHide)
// //
const isMiniComputed = computed(() => isMini.value && isHide.value) const isMiniComputed = computed(() => isMini.value && isHide.value)
// //
const btnListComputed = computed(() => { const btnListComputed = computed(() => {
if (!isAside.value) return btnList if (!isAside.value) return btnList
@ -239,6 +135,119 @@ const toolsMouseoutHandle = () => {
isMini.value = true isMini.value = true
} }
} }
//
const editHandle = () => {
window['$message'].warning('将开启失焦更新与 5 秒同步更新!')
setTimeout(() => {
// id
const path = fetchPathByName(EditEnum.CHART_EDIT_NAME, 'href')
if (!path) return
let { id } = routerParamsInfo.params as any
id = typeof id === 'string' ? id : id[0]
updateToSession(id)
routerTurnByPath(path, [id], undefined, true)
}, 1000)
}
// SessionStorage 便
const updateToSession = (id: string) => {
const storageInfo = chartEditStore.getStorageInfo
const sessionStorageInfo = getLocalStorage(StorageEnum.GO_CHART_STORAGE_LIST) || []
if (sessionStorageInfo?.length) {
const repeateIndex = sessionStorageInfo.findIndex((e: { id: string }) => e.id === id)
//
if (repeateIndex !== -1) {
sessionStorageInfo.splice(repeateIndex, 1, { ...storageInfo, id })
setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, sessionStorageInfo)
} else {
sessionStorageInfo.push({ ...storageInfo, id })
setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, sessionStorageInfo)
}
} else {
setSessionStorage(StorageEnum.GO_CHART_STORAGE_LIST, [{ ...storageInfo, id }])
}
}
//
const useSyncUpdate = () => {
//
let timer: any
const updateFn = (e: any) => updateComponent(e!.detail, true, false)
const syncData = () => {
if (routerParamsInfo.name == ChartEnum.CHART_HOME_NAME) {
dispatchEvent(new CustomEvent(SavePageEnum.CHART, { detail: chartEditStore.getStorageInfo }))
}
}
//
const use = () => {
// 1
timer = setInterval(() => {
//
document.hasFocus() && syncData()
}, editToJsonInterval)
// 2
addEventListener('blur', syncData)
// JSON
addEventListener(SavePageEnum.JSON, updateFn)
}
//
const unUse = () => {
clearInterval(timer)
removeEventListener(SavePageEnum.JSON, updateFn)
removeEventListener('blur', syncData)
}
//
const watchHandler = (toName: any, fromName: any) => {
if (fromName == ChartEnum.CHART_HOME_NAME) {
unUse()
}
if (toName == ChartEnum.CHART_HOME_NAME) {
use()
}
}
return watchHandler
}
watch(() => routerParamsInfo.name, useSyncUpdate(), { immediate: true })
//
const btnList: BtnListType[] = [
{
key: 'export',
type: TypeEnum.BUTTON,
name: '导出',
icon: ShareIcon,
handle: exportHandle
},
{
key: 'import',
type: TypeEnum.IMPORTUPLOAD,
name: '导入',
icon: DownloadIcon
},
{
key: 'edit',
type: TypeEnum.BUTTON,
name: '编辑JSON',
icon: CreateIcon,
handle: editHandle
},
{
key: 'setting',
type: TypeEnum.BUTTON,
name: '设置',
icon: SettingsSharpIcon,
handle: () => {
globalSettingModel.value = true
}
}
]
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -12,16 +12,11 @@
</template> </template>
导入 导入
</n-button> </n-button>
<n-button class="go-mr-4" size="medium" @click="back">
<template #icon>
<n-icon>
<chevron-back-outline-icon></chevron-back-outline-icon>
</n-icon>
</template>
返回工作台
</n-button>
</div> </div>
<n-tag :bordered="false" type="warning"> ctrl + s 保存/更新 </n-tag> <n-space>
<n-tag :bordered="false" type="warning"> 页面失焦保存 </n-tag>
<n-tag :bordered="false" type="warning"> ctrl + s 保存 </n-tag>
</n-space>
</n-layout-header> </n-layout-header>
<n-layout-content> <n-layout-content>
<monaco-editor <monaco-editor