feat: 切换标签异常问题修复
This commit is contained in:
@@ -26,22 +26,37 @@ import { menus, type MenuItem } from '@constant/menus'
|
||||
|
||||
const currentId = ref(1)
|
||||
const tabMap = new Map<number, string>()
|
||||
let cleanupListener: (() => void) | undefined
|
||||
const cleanupListeners: (() => void)[] = []
|
||||
|
||||
const getHtmlPath = (menuUrl: string) => {
|
||||
const cleanUrl = menuUrl.startsWith('/') ? menuUrl.slice(1) : menuUrl
|
||||
let filename = ''
|
||||
switch (cleanUrl) {
|
||||
case 'home': return 'home.html'
|
||||
case 'knowledge': return 'knowledge.html'
|
||||
case 'task': return 'task.html'
|
||||
case 'setting': return 'setting.html'
|
||||
default: return 'home.html'
|
||||
case 'home':
|
||||
filename = 'home.html'
|
||||
break
|
||||
case 'knowledge':
|
||||
filename = 'knowledge.html'
|
||||
break
|
||||
case 'task':
|
||||
filename = 'task.html'
|
||||
break
|
||||
case 'setting':
|
||||
filename = 'setting.html'
|
||||
break
|
||||
default:
|
||||
filename = 'home.html'
|
||||
}
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
return `/html/${filename}`
|
||||
}
|
||||
return filename
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
if (window.api && window.api.tabs) {
|
||||
cleanupListener = window.api.tabs.on('tab-closed', (payload: any) => {
|
||||
const cleanupClosed = window.api.tabs.on('tab-closed', (payload: any) => {
|
||||
const { tabId } = payload
|
||||
for (const [menuId, id] of tabMap.entries()) {
|
||||
if (id === tabId) {
|
||||
@@ -50,6 +65,18 @@ onMounted(async () => {
|
||||
}
|
||||
}
|
||||
})
|
||||
if (cleanupClosed) cleanupListeners.push(cleanupClosed)
|
||||
|
||||
const cleanupCreated = window.api.tabs.on('tab-created', (tab: any) => {
|
||||
for (const menu of menus) {
|
||||
const targetHtml = getHtmlPath(menu.url)
|
||||
if (tab.url.includes(targetHtml)) {
|
||||
tabMap.set(menu.id, tab.id)
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
if (cleanupCreated) cleanupListeners.push(cleanupCreated)
|
||||
|
||||
try {
|
||||
const tabs = await window.api.tabs.list()
|
||||
@@ -71,7 +98,7 @@ onMounted(async () => {
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
if (cleanupListener) cleanupListener()
|
||||
cleanupListeners.forEach(fn => fn())
|
||||
})
|
||||
|
||||
const handleClick = async (item: MenuItem) => {
|
||||
|
||||
Reference in New Issue
Block a user