feat(M9): FR-K1/K3/K4/K6/K7 快捷键体系——多作用域(global/app/editor)+Scope.resolveActive 裁决(K1); 编辑器类绑定 find/bold/italic/strike/inlineCode/clearFormat(K3); 冲突数量汇总 conflictCount+SettingsPanel 顶部警示(K4); CheatSheet 从 store 动态渲染+formatCombo Mac 适配(K6); Ctrl+F 捕获阶段 keydown 拦截宿主查找+FindReplace 切换(K7); keymap-enhanced E2E 2 项

This commit is contained in:
hunter-dev
2026-08-26 14:43:57 +08:00
parent fcd822b9f4
commit bfb1f4b014
8 changed files with 217 additions and 29 deletions
@@ -0,0 +1,89 @@
import { test, expect } from '@playwright/test';
import { _electron as electron } from 'playwright';
import { promises as fs } from 'node:fs';
import nodePath from 'node:path';
import os from 'node:os';
const MAIN_JS = nodePath.join(process.cwd(), 'dist-electron/main/index.js');
async function launch(userData: string) {
const app = await electron.launch({ args: ['--user-data-dir=' + userData, MAIN_JS] });
const page = await app.firstWindow();
await page.waitForLoadState('domcontentloaded');
const invoke = (ch: string, args: any) => page.evaluate(({ ch, args }) => (globalThis as any).dsh.invoke(ch, args), { ch, args });
return { app, page, invoke };
}
async function newDoc(a: { page: any; invoke: any }, title: string) {
await a.page.getByRole('button', { name: /新建文档/ }).click();
await a.page.getByPlaceholder(/输入文档标题/).fill(title);
await a.page.getByPlaceholder(/输入文档标题/).press('Enter');
await expect(a.page.getByTestId('tabbar').getByText(title, { exact: true })).toBeVisible({ timeout: 10000 });
}
// FR-K7:Ctrl+F 呼出查找替换面板(拦截宿主查找)
test('FR-K7:Ctrl+F 呼出查找替换', async () => {
const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-k7-'));
const userData = nodePath.join(tmp, 'userdata');
const vault = nodePath.join(tmp, 'vault');
await fs.mkdir(userData, { recursive: true });
await fs.mkdir(vault, { recursive: true });
const a = await launch(userData);
await a.invoke('vault:switch', { root: vault });
await newDoc(a, 'K7测试');
// 查找替换面板初始不可见
await expect(a.page.getByPlaceholder('查找')).not.toBeVisible();
// Ctrl+F 呼出
await a.page.locator('.ProseMirror').click();
await a.page.keyboard.press('Control+f');
await expect(a.page.getByPlaceholder('查找')).toBeVisible({ timeout: 5000 });
// 再次 Ctrl+F 关闭
await a.page.keyboard.press('Control+f');
await expect(a.page.getByPlaceholder('查找')).not.toBeVisible({ timeout: 5000 });
await a.app.close();
});
// FR-K6:快捷键速查面板从 store 动态渲染(验证多作用域绑定 + Mac 适配 formatCombo)
test('FR-K6:快捷键速查面板显示绑定', async () => {
const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-k6-'));
const userData = nodePath.join(tmp, 'userdata');
const vault = nodePath.join(tmp, 'vault');
await fs.mkdir(userData, { recursive: true });
await fs.mkdir(vault, { recursive: true });
const a = await launch(userData);
await a.invoke('vault:switch', { root: vault });
// 验证 keymap store 包含多作用域绑定(FR-K1/K3)
const result = await a.page.evaluate(() => {
const store = (window as any).__KEYMAP_STORE__;
if (!store) return { error: 'no keymap store' };
const bindings = store.getState().bindings;
const scopes = [...new Set(bindings.map((b: any) => b.scope))];
return {
scopes,
hasEditor: bindings.some((b: any) => b.scope === 'editor'),
hasGlobal: bindings.some((b: any) => b.scope === 'global'),
hasApp: bindings.some((b: any) => b.scope === 'app'),
bindingCount: bindings.length,
conflictCount: store.getState().conflictCount(),
hasFind: bindings.some((b: any) => b.id === 'find'),
hasBold: bindings.some((b: any) => b.id === 'bold'),
};
});
console.log('KEYMAP_STORE', JSON.stringify(result));
expect(result.hasEditor).toBe(true);
expect(result.hasGlobal).toBe(true);
expect(result.hasApp).toBe(true);
expect(result.bindingCount).toBeGreaterThanOrEqual(10);
expect(result.conflictCount).toBe(0);
expect(result.hasFind).toBe(true);
expect(result.hasBold).toBe(true);
await a.app.close();
});