fix(webui): 字体选择器返回跳两层、在非 Download 目录选字体无效 (close #13)

选择器的对话框 DOM 是单例 (按 id 复用), 但配置页与字体编辑页各自 new 了
一个 FontFilePicker。第二个实例命中已有 DOM 时 initDialog 直接返回, 因此
返回/取消按钮的监听仍属于第一个实例: 返回键按第一个实例的过期路径导航
(从 Download 返回会退到 /storage, 看起来跳了两级), 重新渲染后列表项的
回调也变成第一个实例的, 于是选中文件只更新配置页槽位、编辑页收不到。

改为全局唯一实例, 选中回调改由 show() 每次传入 (每次打开的意图本就不同),
列表点击改用事件委托只绑定一次, 不再逐次渲染重复挂监听。
This commit is contained in:
YuleBest committed 2026-09-16 15:20:19 +08:00
1 parent 54de5d09b3
commit df339a0611
3 files changed
+75 -64

No files matched your search

+60 -49
View File
@@ -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 = `
<md-dialog id="picker-dialog" class="font-picker-dialog">
@@ -41,7 +58,7 @@ export class FontFilePicker {
</md-icon-button>
<div class="picker-path-group">
<div class="picker-subtitle">选择字体文件</div>
<div id="picker-path" class="picker-path-text">/storage/emulated/0</div>
<div id="picker-path" class="picker-path-text">${DEFAULT_PATH}</div>
</div>
</div>
</div>
@@ -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<void> {
this.pathEl.textContent = path;
this.listEl.innerHTML = `<div class="picker-loading">正在读取目录...</div>`;
this.pathEl!.textContent = path;
this.listEl!.innerHTML = `<div class="picker-loading">正在读取目录...</div>`;
try {
const { items, failed } = await this.readDir(path);
if (failed) {
this.listEl.innerHTML = `<div class="picker-error">无法读取该目录</div>`;
this.listEl!.innerHTML = `<div class="picker-error">无法读取该目录</div>`;
return;
}
this.renderList(items);
} catch (e) {
this.listEl.innerHTML = `<div class="picker-error">读取目录异常: ${String(e)}</div>`;
this.listEl!.innerHTML = `<div class="picker-error">读取目录异常: ${String(e)}</div>`;
}
}
@@ -140,7 +164,7 @@ export class FontFilePicker {
private renderList(items: FileItem[]): void {
if (items.length === 0) {
this.listEl.innerHTML = `
this.listEl!.innerHTML = `
<div class="picker-empty">
<md-icon>find_in_page</md-icon>
<span>此目录下没有 .ttf 字体文件</span>
@@ -148,7 +172,7 @@ export class FontFilePicker {
return;
}
this.listEl.innerHTML = items
this.listEl!.innerHTML = items
.map(
(item) => `
<md-list-item class="picker-item ${item.isDir ? 'is-dir' : 'is-file'}" data-name="${item.name}" data-isdir="${item.isDir}">
@@ -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();
+13 -11
View File
@@ -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<HTMLElement>('.slot-card').forEach((card) => {
card.addEventListener('click', () => {
pickingKey = card.dataset.key as SlotKey;
picker.show();
pickFontFor(card.dataset.key as SlotKey);
});
});
slotsEl.querySelectorAll<HTMLElement>('.slot-clear').forEach((btn) => {
+2 -4
View File
@@ -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<void> {