Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

feat(turnrewind): Escape plus focus trap on both modals via a shared a11y helper

yingtianlan355e79ba818a
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/register/dialog.ts b/packages/dsh-tauri-turnrewind-ts/src/client/register/dialog.ts
index d41aacb7..8f7b4702 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/client/register/dialog.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/register/dialog.ts
@@ -11,6 +11,7 @@
import type { LocaleKey } from '../locales'
import { TURNREWIND_CLASS_PREFIX, TURNREWIND_STYLE_ID } from '../constants'
+import { bindModalA11y } from '../utils/modal-a11y'
export interface UnsupportedNotice {
id: string
@@ -25,9 +26,11 @@ interface DialogElements {
reasonBox: HTMLDivElement
recoveryButton: HTMLButtonElement
button: HTMLButtonElement
+ card: HTMLDivElement
}
let dialog: DialogElements | undefined
+let a11y: ReturnType<typeof bindModalA11y> | undefined
/** 恢复面板入口(apply() 注入):reason 命中恢复围栏时展示。 */
let recoveryOpener: (() => void) | null = null
@@ -93,12 +96,22 @@ function ensureDialog(): DialogElements {
hide()
})
- dialog = { backdrop, title, intro, reasonLabel, reasonBox, recoveryButton, button }
+ const elements: DialogElements = { backdrop, title, intro, reasonLabel, reasonBox, recoveryButton, button, card }
+ a11y = bindModalA11y(
+ () => dialog?.card ?? null,
+ () => dialog?.backdrop.dataset.visible === 'true',
+ () => {
+ backdrop.dataset.visible = 'false'
+ },
+ )
+ dialog = elements
return dialog
}
/** 插件 stop/HMR 时整个 backdrop 子树移除——listener 全部随之释放。 */
export function disposeDialog(): void {
+ a11y?.release()
+ a11y = undefined
if (!dialog)
return
dialog.backdrop.remove()
@@ -127,6 +140,7 @@ export function showDialog(t: (key: LocaleKey) => string, notices: UnsupportedNo
if (underRecovery)
el.recoveryButton.textContent = t('recoveryOpen')
el.backdrop.dataset.visible = 'true'
+ a11y?.takeFocus()
}
/**
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/register/recovery.ts b/packages/dsh-tauri-turnrewind-ts/src/client/register/recovery.ts
index 770dbef4..6032d388 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/client/register/recovery.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/register/recovery.ts
@@ -10,9 +10,11 @@
import type { RecoveryWorkspaceInfo, Translate } from '../types'
import { TURNREWIND_CLASS_PREFIX, TURNREWIND_HTTP_BASE } from '../constants'
+import { bindModalA11y } from '../utils/modal-a11y'
interface RecoveryElements {
backdrop: HTMLDivElement
+ card: HTMLDivElement
title: HTMLDivElement
intro: HTMLDivElement
list: HTMLDivElement
@@ -21,7 +23,7 @@ interface RecoveryElements {
}
let recovery: RecoveryElements | undefined
-let escapeHandler: ((event: KeyboardEvent) => void) | undefined
+let a11y: ReturnType<typeof bindModalA11y> | undefined
function ensureRecoveryDialog(): RecoveryElements {
if (recovery)
@@ -71,23 +73,20 @@ function ensureRecoveryDialog(): RecoveryElements {
if (event.target === backdrop)
hide()
})
- // P2-6(第一步):Escape 关闭。listener 随 dispose 移除。
- escapeHandler = (event: KeyboardEvent) => {
- if (event.key === 'Escape' && backdrop.dataset.visible === 'true')
- hide()
- }
- document.addEventListener('keydown', escapeHandler)
- recovery = { backdrop, title, intro, list, error, closeButton }
+ recovery = { backdrop, card, title, intro, list, error, closeButton }
+ a11y = bindModalA11y(
+ () => recovery?.card ?? null,
+ () => recovery?.backdrop.dataset.visible === 'true',
+ hide,
+ )
return recovery
}
-/** 插件 stop/HMR 时整个子树与 Escape listener 一并移除。 */
+/** 插件 stop/HMR 时整个子树与 a11y listener 一并移除。 */
export function disposeRecoveryDialog(): void {
- if (escapeHandler) {
- document.removeEventListener('keydown', escapeHandler)
- escapeHandler = undefined
- }
+ a11y?.release()
+ a11y = undefined
if (!recovery)
return
recovery.backdrop.remove()
@@ -183,6 +182,7 @@ export function openRecoveryDialog(t: Translate): void {
el.closeButton.textContent = t('recoveryClose')
el.error.dataset.visible = 'false'
el.backdrop.dataset.visible = 'true'
+ a11y?.takeFocus()
void load(t, el.list).catch((error: unknown) => {
showActionError(t, String((error as Error)?.message ?? error))
})
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/utils/modal-a11y.ts b/packages/dsh-tauri-turnrewind-ts/src/client/utils/modal-a11y.ts
new file mode 100644
index 00000000..585f803c
--- /dev/null
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/utils/modal-a11y.ts
@@ -0,0 +1,73 @@
+/**
+ * client/utils/modal-a11y.ts — 模态弹窗的最小可访问性绑定(P2-6)。
+ *
+ * Escape 关闭 + Tab 焦点陷阱 + 显示时聚焦卡内首个可交互元素。不可用弹窗与
+ * 恢复面板两个 DOM 模态共用;document 级 listener 由 release() 移除,
+ * 插件 dispose 时调用。
+ */
+
+const FOCUSABLE_SELECTOR = 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
+
+export interface ModalA11yBinding {
+ /** 挂到 document keydown 上(bindModalA11y 内部已挂,暴露仅为测试/调试)。 */
+ onKeydown: (event: KeyboardEvent) => void
+ /** 弹窗显示时调用:记住先前焦点并聚焦卡内首个可交互元素。 */
+ takeFocus: () => void
+ /** 永久释放(插件 dispose):移除 listener 并尝试还原先前焦点。 */
+ release: () => void
+}
+
+export function bindModalA11y(getCard: () => HTMLElement | null, isVisible: () => boolean, hide: () => void): ModalA11yBinding {
+ let previous: HTMLElement | null = null
+
+ const onKeydown = (event: KeyboardEvent): void => {
+ if (!isVisible())
+ return
+ if (event.key === 'Escape') {
+ event.preventDefault()
+ hide()
+ return
+ }
+ if (event.key !== 'Tab')
+ return
+ const card = getCard()
+ if (!card)
+ return
+ const focusables = [...card.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR)]
+ if (focusables.length === 0)
+ return
+ const active = document.activeElement
+ if (!(active instanceof HTMLElement) || !card.contains(active)) {
+ // 焦点落在卡外(例如刚点击了 backdrop 里的空白处):拉回第一个。
+ event.preventDefault()
+ focusables[0]!.focus()
+ return
+ }
+ const first = focusables[0]!
+ const last = focusables[focusables.length - 1]!
+ if (event.shiftKey && active === first) {
+ event.preventDefault()
+ last.focus()
+ }
+ else if (!event.shiftKey && active === last) {
+ event.preventDefault()
+ first.focus()
+ }
+ }
+ document.addEventListener('keydown', onKeydown)
+
+ return {
+ onKeydown,
+ takeFocus() {
+ previous = document.activeElement instanceof HTMLElement ? document.activeElement : null
+ const card = getCard()
+ const target = card?.querySelector<HTMLElement>(FOCUSABLE_SELECTOR) ?? card
+ target?.focus()
+ },
+ release() {
+ document.removeEventListener('keydown', onKeydown)
+ previous?.focus()
+ previous = null
+ },
+ }
+}