组件易用性优化,所有组件打开后支持ESC关闭

This commit is contained in:
expdsn 2024-11-15 18:29:48 +08:00
parent 5fbe526e2e
commit dffe6bb85c
1 changed files with 32 additions and 20 deletions

View File

@ -1,5 +1,5 @@
import useRouterStore, { type RouteStr } from '@/useRouterStore'
import { computed, defineComponent, ref, Transition, watch } from 'vue'
import { computed, defineComponent, onMounted, onUnmounted, ref, Transition, watch } from 'vue'
import { OhVueIcon, addIcons } from 'oh-vue-icons'
import { MdClose, MdOpeninfull, MdClosefullscreen } from 'oh-vue-icons/icons'
import asyncLoader from './utils/asyncLoader'
@ -24,12 +24,25 @@ export default defineComponent(() => {
watch(router, () => {
full.value = false
})
onMounted(() => {
window.addEventListener('keydown', handleKeydown)
})
onUnmounted(() => {
// 清理事件监听
window.removeEventListener('keydown', handleKeydown)
})
function handleKeydown(e: any) {
if (e.key === 'Escape') {
router.back()
}
}
return () => (
<div
class="fixed left-0 top-0 z-50 w-full"
onContextmenu={(e) => e.stopPropagation()}
onKeydown={(e) => e.stopPropagation()}
onKeydown={(e) => {
e.stopPropagation()
}}
>
{/* 背景遮罩 */}
<Transition>
@ -93,8 +106,7 @@ export default defineComponent(() => {
<AdderPage />
) : router.path === 'global-background' ? (
<BackgroundSwtich />
) :
router.path.startsWith('widget-') ? (
) : router.path.startsWith('widget-') ? (
(() => {
const name = router.path.split('-')[1]
const selected = widgetList.find((el) => el.name === name)