From df339a06117a2fe7b029de8657e49c66dccb830b Mon Sep 17 00:00:00 2001 From: YuleBest Date: Wed, 16 Sep 2026 15:20:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(webui):=20=E5=AD=97=E4=BD=93=E9=80=89?= =?UTF-8?q?=E6=8B=A9=E5=99=A8=E8=BF=94=E5=9B=9E=E8=B7=B3=E4=B8=A4=E5=B1=82?= =?UTF-8?q?=E3=80=81=E5=9C=A8=E9=9D=9E=20Download=20=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E9=80=89=E5=AD=97=E4=BD=93=E6=97=A0=E6=95=88=20(close=20#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 选择器的对话框 DOM 是单例 (按 id 复用), 但配置页与字体编辑页各自 new 了 一个 FontFilePicker。第二个实例命中已有 DOM 时 initDialog 直接返回, 因此 返回/取消按钮的监听仍属于第一个实例: 返回键按第一个实例的过期路径导航 (从 Download 返回会退到 /storage, 看起来跳了两级), 重新渲染后列表项的 回调也变成第一个实例的, 于是选中文件只更新配置页槽位、编辑页收不到。 改为全局唯一实例, 选中回调改由 show() 每次传入 (每次打开的意图本就不同), 列表点击改用事件委托只绑定一次, 不再逐次渲染重复挂监听。 --- web/src/fontPicker.ts | 109 +++++++++++++++++++++++------------------- web/src/slots.ts | 24 +++++----- web/src/tools.ts | 6 +-- 3 files changed, 75 insertions(+), 64 deletions(-) diff --git a/web/src/fontPicker.ts b/web/src/fontPicker.ts index bd6b61f..81e7fa3 100644 --- a/web/src/fontPicker.ts +++ b/web/src/fontPicker.ts @@ -12,25 +12,42 @@ export interface FileItem { size?: string; } -export class FontFilePicker { - private currentPath = '/storage/emulated/0'; - private dialogEl!: HTMLElement; - private listEl!: HTMLElement; - private pathEl!: HTMLElement; - private onSelectCallback: (path: string, fileName: string) => void; +/** 选中文件时的回调: 完整路径与文件名 */ +export type FontSelectHandler = (path: string, fileName: string) => void; - constructor(onSelect: (path: string, fileName: string) => void) { - this.onSelectCallback = onSelect; +const DEFAULT_PATH = '/storage/emulated/0'; + +/** + * 文件选择器。全局只应存在一个实例 (下方导出 fontPicker) —— 对话框 DOM 是 + * 单例 (按 id 复用), 若允许第二个实例, 它会复用这份 DOM 却拿不到返回/取消 + * 按钮的监听 (initDialog 命中已有元素时直接返回), 于是返回键按上一个实例的 + * 过期路径导航、选中文件也回调给上一个实例, 表现为「返回跳两层」「在别的 + * 目录选文件没反应」(issue #13)。因此选中回调改为每次 show 时传入。 + */ +class FontFilePicker { + private currentPath = DEFAULT_PATH; + private dialogEl: any = null; + private listEl: HTMLElement | null = null; + private pathEl: HTMLElement | null = null; + private onSelect: FontSelectHandler | null = null; + + /** 打开选择器: initialPath 为起始目录, onSelect 为本次选择的处理函数 */ + public show(initialPath: string, onSelect: FontSelectHandler): void { this.initDialog(); + this.onSelect = onSelect; + this.currentPath = initialPath || DEFAULT_PATH; + this.pathEl!.textContent = this.currentPath; + this.dialogEl.open = true; + void this.loadDirectory(this.currentPath); + } + + public hide(): void { + if (this.dialogEl) this.dialogEl.open = false; + this.onSelect = null; } private initDialog(): void { - if (document.getElementById('picker-dialog')) { - this.dialogEl = document.getElementById('picker-dialog')!; - this.listEl = document.getElementById('picker-list')!; - this.pathEl = document.getElementById('picker-path')!; - return; - } + if (this.dialogEl) return; const dialogHTML = ` @@ -41,7 +58,7 @@ export class FontFilePicker {
选择字体文件
-
/storage/emulated/0
+
${DEFAULT_PATH}
@@ -57,22 +74,29 @@ export class FontFilePicker { `; document.body.insertAdjacentHTML('beforeend', dialogHTML); - this.dialogEl = document.getElementById('picker-dialog')!; - this.listEl = document.getElementById('picker-list')!; - this.pathEl = document.getElementById('picker-path')!; + this.dialogEl = document.getElementById('picker-dialog'); + this.listEl = document.getElementById('picker-list'); + this.pathEl = document.getElementById('picker-path'); document.getElementById('picker-back-btn')?.addEventListener('click', () => this.navigateUp()); document.getElementById('picker-cancel-btn')?.addEventListener('click', () => this.hide()); - } - public show(initialPath = '/storage/emulated/0'): void { - this.currentPath = initialPath; - (this.dialogEl as any).open = true; - this.loadDirectory(this.currentPath); - } + // 列表项用事件委托: 只绑定一次, 渲染只替换内容, 不重复挂监听 + this.listEl!.addEventListener('click', (e) => { + const item = (e.target as HTMLElement).closest('.picker-item') as HTMLElement | null; + if (!item) return; + const name = item.dataset.name!; - public hide(): void { - (this.dialogEl as any).open = false; + if (item.dataset.isdir === 'true') { + this.currentPath = this.currentPath === '/' ? `/${name}` : `${this.currentPath}/${name}`; + void this.loadDirectory(this.currentPath); + } else { + const fullPath = `${this.currentPath}/${name}`; + const handler = this.onSelect; + this.hide(); + handler?.(fullPath, name); + } + }); } private navigateUp(): void { @@ -80,22 +104,22 @@ export class FontFilePicker { const parts = this.currentPath.split('/').filter(Boolean); parts.pop(); this.currentPath = parts.length === 0 ? '/' : '/' + parts.join('/'); - this.loadDirectory(this.currentPath); + void this.loadDirectory(this.currentPath); } private async loadDirectory(path: string): Promise { - this.pathEl.textContent = path; - this.listEl.innerHTML = `
正在读取目录...
`; + this.pathEl!.textContent = path; + this.listEl!.innerHTML = `
正在读取目录...
`; try { const { items, failed } = await this.readDir(path); if (failed) { - this.listEl.innerHTML = `
无法读取该目录
`; + this.listEl!.innerHTML = `
无法读取该目录
`; return; } this.renderList(items); } catch (e) { - this.listEl.innerHTML = `
读取目录异常: ${String(e)}
`; + this.listEl!.innerHTML = `
读取目录异常: ${String(e)}
`; } } @@ -140,7 +164,7 @@ export class FontFilePicker { private renderList(items: FileItem[]): void { if (items.length === 0) { - this.listEl.innerHTML = ` + this.listEl!.innerHTML = `
find_in_page 此目录下没有 .ttf 字体文件 @@ -148,7 +172,7 @@ export class FontFilePicker { return; } - this.listEl.innerHTML = items + this.listEl!.innerHTML = items .map( (item) => ` @@ -165,21 +189,8 @@ export class FontFilePicker { `, ) .join(''); - - this.listEl.querySelectorAll('.picker-item').forEach((el) => { - el.addEventListener('click', () => { - const name = (el as HTMLElement).dataset.name!; - const isDir = (el as HTMLElement).dataset.isdir === 'true'; - - if (isDir) { - this.currentPath = this.currentPath === '/' ? `/${name}` : `${this.currentPath}/${name}`; - this.loadDirectory(this.currentPath); - } else { - const fullPath = `${this.currentPath}/${name}`; - this.onSelectCallback(fullPath, name); - this.hide(); - } - }); - }); } } + +/** 全局唯一的文件选择器实例 (见 FontFilePicker 的说明) */ +export const fontPicker = new FontFilePicker(); diff --git a/web/src/slots.ts b/web/src/slots.ts index e91a77b..00668d1 100644 --- a/web/src/slots.ts +++ b/web/src/slots.ts @@ -1,4 +1,4 @@ -import { FontFilePicker } from './fontPicker'; +import { fontPicker } from './fontPicker'; import { exec, shellQuote } from './ksu'; import { readFontInfo } from './fontInfo'; import { ensureFontsCopy } from './fontFiles'; @@ -55,13 +55,16 @@ export function setApplying(v: boolean): void { applying = v; } -const picker = new FontFilePicker((path, name) => { - const slot = slots[pickingKey]; - slot.path = path; - slot.fileName = name; - renderSlots(); - void refreshSlotInfo(slot); -}); +function pickFontFor(key: SlotKey): void { + pickingKey = key; + fontPicker.show('/storage/emulated/0', (path, name) => { + const slot = slots[pickingKey]; + slot.path = path; + slot.fileName = name; + renderSlots(); + void refreshSlotInfo(slot); + }); +} // 未选择时的占位文案: 回退可视化 (WYSIWYG) function slotPlaceholder(slot: FontSlot): string { @@ -128,11 +131,10 @@ export function renderSlots() { }) .join(''); - // 绑定事件: 整卡点击选择字体 + // 绑定事件: 整卡点击选择字体 (回调按点击的槽位处理) slotsEl.querySelectorAll('.slot-card').forEach((card) => { card.addEventListener('click', () => { - pickingKey = card.dataset.key as SlotKey; - picker.show(); + pickFontFor(card.dataset.key as SlotKey); }); }); slotsEl.querySelectorAll('.slot-clear').forEach((btn) => { diff --git a/web/src/tools.ts b/web/src/tools.ts index 8d541a3..e58ee56 100644 --- a/web/src/tools.ts +++ b/web/src/tools.ts @@ -1,6 +1,6 @@ import '@material/web/button/filled-button.js'; import { exec, shellQuote } from './ksu'; -import { FontFilePicker } from './fontPicker'; +import { fontPicker } from './fontPicker'; import { renderPreview, type PreviewData } from './previewRenderer'; // ---------------- 工具框架 ---------------- @@ -377,7 +377,6 @@ class FontEditorPage { private originalBytes: Uint8Array | null = null; private fileName = ''; private hasNameChanges = false; - private picker: FontFilePicker | null = null; private previewText = DEFAULT_PREVIEW_TEXT; private editTimer: number | null = null; @@ -502,10 +501,9 @@ class FontEditorPage { void this.loadFont('demo-font.ttf', 'demo-font.ttf'); return; } - this.picker ??= new FontFilePicker((path, name) => { + fontPicker.show('/storage/emulated/0/Download', (path, name) => { void this.copyAndLoad(path, name); }); - this.picker.show('/storage/emulated/0/Download'); } private async copyAndLoad(path: string, name: string): Promise {