Skip to content

Commit 89112d8

Browse files
committed
优化标签页快捷键跳转逻辑
1 parent f1202ca commit 89112d8

File tree

1 file changed

+33
-3
lines changed
  • src/layouts/components/Topbar/Tabbar

1 file changed

+33
-3
lines changed

src/layouts/components/Topbar/Tabbar/index.vue

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,34 @@ function contextMenuItems(routeItem: Tabbar.recordRaw) {
103103
]
104104
}
105105
106+
const visibleTabIndex = ref<number[]>([])
107+
function getVisibleTabs() {
108+
const containerWidth = tabsRef.value?.ref?.$el.clientWidth ?? 0
109+
const scrollLeft = tabsRef.value?.ref?.el?.viewportElement?.scrollLeft ?? 0
110+
visibleTabIndex.value = []
111+
if (tabRef.value) {
112+
for (let i = 0; i < tabRef.value.length; i++) {
113+
const tab = tabRef.value[i]
114+
const tabLeft = tab.offsetLeft
115+
const tabRight = tabLeft + tab.offsetWidth
116+
// 检查标签页是否在可视区域内
117+
if (tabLeft < scrollLeft + containerWidth && tabRight > scrollLeft) {
118+
if (i >= 0 && i < tabbarStore.list.length) {
119+
visibleTabIndex.value.push(i)
120+
}
121+
}
122+
}
123+
}
124+
}
125+
function getVisibleTabIndex(arrayIndex: number) {
126+
return visibleTabIndex.value.findIndex(visibleTab => visibleTab === arrayIndex) ?? -1
127+
}
128+
watch(() => keys.alt, (val) => {
129+
if (val) {
130+
getVisibleTabs()
131+
}
132+
})
133+
106134
onMounted(() => {
107135
hotkeys('alt+left,alt+right,alt+w,alt+1,alt+2,alt+3,alt+4,alt+5,alt+6,alt+7,alt+8,alt+9,alt+0', (e, handle) => {
108136
if (settingsStore.settings.tabbar.enable && settingsStore.settings.tabbar.enableHotkeys) {
@@ -138,7 +166,9 @@ onMounted(() => {
138166
case 'alt+9':
139167
{
140168
const number = Number(handle.key.split('+')[1])
141-
tabbarStore.list[number - 1]?.fullPath && router.push(tabbarStore.list[number - 1].fullPath)
169+
if (visibleTabIndex.value[number - 1]) {
170+
router.push(tabbarStore.list[visibleTabIndex.value[number - 1]].fullPath)
171+
}
142172
break
143173
}
144174
// 切换到最后一个标签页
@@ -179,8 +209,8 @@ onUnmounted(() => {
179209
<div v-if="tabbarStore.list.length > 1" class="action-icon" @click.stop="tabbar.closeById(element.tabId)">
180210
<FaIcon name="i-ri:close-fill" />
181211
</div>
182-
<div v-show="keys.alt && index < 9" class="hotkey-number">
183-
{{ index + 1 }}
212+
<div v-show="keys.alt && getVisibleTabIndex(index) >= 0 && getVisibleTabIndex(index) < 9" class="hotkey-number">
213+
{{ getVisibleTabIndex(index) + 1 }}
184214
</div>
185215
</div>
186216
<template #content>

0 commit comments

Comments
 (0)