🐛 修复详情披露与滚动锁所有权
修复记录详情中的渐进式二维码与 wget 展示,并确保详情切换时重置披露状态。为滚动锁引入按所有者释放机制,避免重复或错误释放解锁仍打开的抽屉。 - 二维码和 wget 命令默认折叠,改为可操作的显示控件 - 详情关闭按钮保持 44px 触控目标,嵌套对话框 Escape 不穿透 - 增加 scrollLock mocked document 回归测试 - 已通过类型检查、架构检查、生产构建和 Node 测试
This commit is contained in:
1 parent
5ffee48d80
commit
cd06a5580e
4 files changed
+65
-17
No files matched your search
@@ -30,7 +30,7 @@
|
|||||||
type="button"
|
type="button"
|
||||||
aria-label="关闭文件详情"
|
aria-label="关闭文件详情"
|
||||||
@click="emit('close')"
|
@click="emit('close')"
|
||||||
class="rounded-[30px] p-1.5 transition-colors hover:bg-gray-100 dark:hover:bg-gray-800 sm:p-2"
|
class="min-h-11 min-w-11 rounded-[30px] p-1.5 transition-colors hover:bg-gray-100 dark:hover:bg-gray-800 sm:p-2"
|
||||||
>
|
>
|
||||||
<XIcon
|
<XIcon
|
||||||
class="w-4 h-4 sm:w-5 sm:h-5"
|
class="w-4 h-4 sm:w-5 sm:h-5"
|
||||||
@@ -140,11 +140,13 @@
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<p
|
<p
|
||||||
|
v-if="!showWget"
|
||||||
class="text-xs sm:text-sm font-mono break-all line-clamp-2"
|
class="text-xs sm:text-sm font-mono break-all line-clamp-2"
|
||||||
:class="[isDarkMode ? 'text-gray-300' : 'text-gray-600']"
|
:class="[isDarkMode ? 'text-gray-300' : 'text-gray-600']"
|
||||||
>
|
>
|
||||||
点击复制 wget 命令
|
<button type="button" class="min-h-11 rounded-[30px] px-3 text-left hover:bg-gray-200 dark:hover:bg-gray-700" @click="showWget = true">显示 wget 命令</button>
|
||||||
</p>
|
</p>
|
||||||
|
<p v-else class="text-xs sm:text-sm font-mono break-all" :class="[isDarkMode ? 'text-gray-300' : 'text-gray-600']">{{ wgetCommand }}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -156,8 +158,10 @@
|
|||||||
type="button"
|
type="button"
|
||||||
class="bg-white p-3 sm:p-4 rounded-lg shadow-sm mb-3 sm:mb-4"
|
class="bg-white p-3 sm:p-4 rounded-lg shadow-sm mb-3 sm:mb-4"
|
||||||
aria-label="取件二维码"
|
aria-label="取件二维码"
|
||||||
|
@click="showQr = !showQr"
|
||||||
>
|
>
|
||||||
<QRCode :value="qrValue" :size="140" level="M" class="sm:w-[160px] sm:h-[160px]" />
|
<span v-if="!showQr" class="flex h-[140px] w-[140px] items-center justify-center text-sm text-gray-500 sm:h-[160px] sm:w-[160px]">显示二维码</span>
|
||||||
|
<QRCode v-else :value="qrValue" :size="140" level="M" class="sm:w-[160px] sm:h-[160px]" />
|
||||||
</button>
|
</button>
|
||||||
<p
|
<p
|
||||||
class="text-xs sm:text-sm truncate max-w-full"
|
class="text-xs sm:text-sm truncate max-w-full"
|
||||||
@@ -199,6 +203,7 @@ import {
|
|||||||
|
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
import type { SentFileRecord } from '@/types'
|
import type { SentFileRecord } from '@/types'
|
||||||
|
import { buildAbsoluteUrl } from '@/utils/share-url'
|
||||||
import { lockBodyScroll, unlockBodyScroll } from './scrollLock'
|
import { lockBodyScroll, unlockBodyScroll } from './scrollLock'
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
@@ -221,6 +226,10 @@ const modalRef = ref<HTMLElement>()
|
|||||||
|
|
||||||
const modalTitleId = 'sent-record-detail-title'
|
const modalTitleId = 'sent-record-detail-title'
|
||||||
let returnFocusElement: HTMLElement | null = null
|
let returnFocusElement: HTMLElement | null = null
|
||||||
|
let scrollLockOwner: ReturnType<typeof lockBodyScroll> | null = null
|
||||||
|
const showQr = ref(false)
|
||||||
|
const showWget = ref(false)
|
||||||
|
const wgetCommand = computed(() => `wget ${buildAbsoluteUrl(`/share/select?code=${props.record?.retrieveCode ?? ''}`)} -O "${props.record?.filename ?? ''}"`)
|
||||||
const focusableSelector = 'button:not([disabled]), [href], [tabindex]:not([tabindex="-1"])'
|
const focusableSelector = 'button:not([disabled]), [href], [tabindex]:not([tabindex="-1"])'
|
||||||
const handleKeydown = (event: KeyboardEvent) => {
|
const handleKeydown = (event: KeyboardEvent) => {
|
||||||
if (event.key === 'Escape') { event.preventDefault(); emit('close'); return }
|
if (event.key === 'Escape') { event.preventDefault(); emit('close'); return }
|
||||||
@@ -232,14 +241,20 @@ const handleKeydown = (event: KeyboardEvent) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
watch(() => props.record, async (record) => {
|
watch(() => props.record, async (record) => {
|
||||||
|
if (scrollLockOwner) {
|
||||||
|
unlockBodyScroll(scrollLockOwner)
|
||||||
|
scrollLockOwner = null
|
||||||
|
}
|
||||||
|
showQr.value = false
|
||||||
|
showWget.value = false
|
||||||
if (record) {
|
if (record) {
|
||||||
returnFocusElement = document.activeElement instanceof HTMLElement ? document.activeElement : null
|
returnFocusElement = document.activeElement instanceof HTMLElement ? document.activeElement : null
|
||||||
lockBodyScroll(); await nextTick(); modalRef.value?.focus()
|
scrollLockOwner = lockBodyScroll(); await nextTick(); modalRef.value?.focus()
|
||||||
} else {
|
} else {
|
||||||
unlockBodyScroll(); await nextTick(); returnFocusElement?.focus(); returnFocusElement = null
|
await nextTick(); returnFocusElement?.focus(); returnFocusElement = null
|
||||||
}
|
}
|
||||||
}, { immediate: true })
|
}, { immediate: true })
|
||||||
onBeforeUnmount(() => { unlockBodyScroll(); returnFocusElement = null })
|
onBeforeUnmount(() => { unlockBodyScroll(scrollLockOwner); scrollLockOwner = null; returnFocusElement = null })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -57,11 +57,13 @@ const { t } = useI18n()
|
|||||||
const isDarkMode = inject('isDarkMode')
|
const isDarkMode = inject('isDarkMode')
|
||||||
const drawerRef = ref<HTMLElement>()
|
const drawerRef = ref<HTMLElement>()
|
||||||
let returnFocusElement: HTMLElement | null = null
|
let returnFocusElement: HTMLElement | null = null
|
||||||
|
let scrollLockOwner: ReturnType<typeof lockBodyScroll> | null = null
|
||||||
const focusableSelector =
|
const focusableSelector =
|
||||||
'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
|
'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
|
||||||
|
|
||||||
const handleKeydown = (event: KeyboardEvent) => {
|
const handleKeydown = (event: KeyboardEvent) => {
|
||||||
if (event.key === 'Escape') {
|
if (event.key === 'Escape') {
|
||||||
|
if (event.target !== event.currentTarget && (event.target as HTMLElement).closest('[role="dialog"]') !== event.currentTarget) return
|
||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
emit('close')
|
emit('close')
|
||||||
return
|
return
|
||||||
@@ -84,20 +86,21 @@ watch(
|
|||||||
() => props.visible,
|
() => props.visible,
|
||||||
async (visible) => {
|
async (visible) => {
|
||||||
if (visible) {
|
if (visible) {
|
||||||
lockBodyScroll()
|
scrollLockOwner = lockBodyScroll()
|
||||||
returnFocusElement = document.activeElement instanceof HTMLElement ? document.activeElement : null
|
returnFocusElement = document.activeElement instanceof HTMLElement ? document.activeElement : null
|
||||||
await nextTick()
|
await nextTick()
|
||||||
const first = drawerRef.value?.querySelector<HTMLElement>(focusableSelector)
|
const first = drawerRef.value?.querySelector<HTMLElement>(focusableSelector)
|
||||||
;(first || drawerRef.value)?.focus()
|
;(first || drawerRef.value)?.focus()
|
||||||
} else {
|
} else {
|
||||||
unlockBodyScroll()
|
unlockBodyScroll(scrollLockOwner)
|
||||||
|
scrollLockOwner = null
|
||||||
await nextTick()
|
await nextTick()
|
||||||
returnFocusElement?.focus()
|
returnFocusElement?.focus()
|
||||||
returnFocusElement = null
|
returnFocusElement = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
onBeforeUnmount(() => unlockBodyScroll())
|
onBeforeUnmount(() => { unlockBodyScroll(scrollLockOwner); scrollLockOwner = null })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -1,18 +1,21 @@
|
|||||||
let lockCount = 0
|
type ScrollLockOwner = symbol
|
||||||
|
|
||||||
|
let owners = new Set<ScrollLockOwner>()
|
||||||
let previousOverflow = ''
|
let previousOverflow = ''
|
||||||
|
|
||||||
export function lockBodyScroll(): void {
|
export function lockBodyScroll(): ScrollLockOwner {
|
||||||
if (lockCount === 0) {
|
const owner = Symbol('scroll-lock-owner')
|
||||||
|
if (owners.size === 0) {
|
||||||
previousOverflow = document.body.style.overflow
|
previousOverflow = document.body.style.overflow
|
||||||
document.body.style.overflow = 'hidden'
|
document.body.style.overflow = 'hidden'
|
||||||
}
|
}
|
||||||
lockCount += 1
|
owners.add(owner)
|
||||||
|
return owner
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unlockBodyScroll(): void {
|
export function unlockBodyScroll(owner: ScrollLockOwner | null | undefined): void {
|
||||||
if (lockCount === 0) return
|
if (!owner || !owners.delete(owner)) return
|
||||||
lockCount -= 1
|
if (owners.size === 0) {
|
||||||
if (lockCount === 0) {
|
|
||||||
document.body.style.overflow = previousOverflow
|
document.body.style.overflow = previousOverflow
|
||||||
previousOverflow = ''
|
previousOverflow = ''
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import assert from 'node:assert/strict'
|
||||||
|
import test from 'node:test'
|
||||||
|
|
||||||
|
const body = { style: { overflow: 'scroll' } }
|
||||||
|
globalThis.document = { body }
|
||||||
|
const { lockBodyScroll, unlockBodyScroll } = await import('../src/components/common/scrollLock.ts')
|
||||||
|
|
||||||
|
test('restores overflow only after the owning lock is released', () => {
|
||||||
|
const first = lockBodyScroll()
|
||||||
|
const second = lockBodyScroll()
|
||||||
|
unlockBodyScroll(Symbol('unrelated'))
|
||||||
|
assert.equal(body.style.overflow, 'hidden')
|
||||||
|
unlockBodyScroll(first)
|
||||||
|
assert.equal(body.style.overflow, 'hidden')
|
||||||
|
unlockBodyScroll(second)
|
||||||
|
assert.equal(body.style.overflow, 'scroll')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('releasing an owner twice cannot unlock another owner', () => {
|
||||||
|
const first = lockBodyScroll()
|
||||||
|
const second = lockBodyScroll()
|
||||||
|
unlockBodyScroll(first)
|
||||||
|
unlockBodyScroll(first)
|
||||||
|
assert.equal(body.style.overflow, 'hidden')
|
||||||
|
unlockBodyScroll(second)
|
||||||
|
assert.equal(body.style.overflow, 'scroll')
|
||||||
|
})
|
||||||
Reference in new issue
Block a user