feat(M2): FR-E12 公式块——MathBlock 节点(KaTeX 块级渲染+可编辑源码区)+InlineMath 行内公式; SlashMenu 加 公式 项; addStorage markdown 序列化(../$..$); katex 依赖; editor-math E2E 1 项

This commit is contained in:
hunter-dev
2026-08-26 13:47:59 +08:00
parent 56735f53e6
commit 0764818ee1
6 changed files with 151 additions and 0 deletions
@@ -0,0 +1,54 @@
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-E12:斜杠菜单插入公式块 + KaTeX 渲染
test('FR-E12:斜杠菜单插入公式块 + KaTeX 渲染', async () => {
const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-e12-'));
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, '公式测试');
await a.page.locator('.ProseMirror').click();
await a.page.keyboard.type('/');
await a.page.waitForTimeout(300);
// 斜杠菜单应出现
await a.page.getByText('公式', { exact: true }).click();
// 应插入 mathBlock 节点
await expect(a.page.locator('.ProseMirror .math-block')).toHaveCount(1, { timeout: 5000 });
// 在公式源码区输入 LaTeX
const source = a.page.locator('.ProseMirror .math-source');
await source.click();
await a.page.keyboard.type('c = \\sqrt{a^2 + b^2}');
// KaTeX 预览区应渲染出 .katex 元素
await expect(a.page.locator('.ProseMirror .math-preview .katex')).toHaveCount(1, { timeout: 8000 });
await a.app.close();
});