修复代码块复制附带 ` 围栏:纯内容复制(transformCopiedText=false + copy 拦截 + 复制按钮改纯内容)
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
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 });
|
||||
const clip = () => app.evaluate(({ clipboard }) => clipboard.readText());
|
||||
return { app, page, invoke, clip };
|
||||
}
|
||||
|
||||
async function openDocWithCode(a: any) {
|
||||
await a.invoke('vault:switch', { root: a.v });
|
||||
await a.page.getByRole('button', { name: /新建文档/ }).click();
|
||||
await a.page.getByPlaceholder(/输入文档标题/).fill('CodeCopy');
|
||||
await a.page.getByPlaceholder(/输入文档标题/).press('Enter');
|
||||
await expect(a.page.getByTestId('tabbar').getByText('CodeCopy', { exact: true })).toBeVisible({ timeout: 10000 });
|
||||
const ed = a.page.locator('.ProseMirror');
|
||||
await ed.click();
|
||||
await a.page.locator('button[title="代码块 Ctrl+Shift+K"]').click();
|
||||
await a.page.keyboard.type('sudo apt-get update');
|
||||
}
|
||||
|
||||
// 复制代码块内容不应附带 ``` 围栏
|
||||
test('代码块顶部“复制”按钮复制纯内容(无围栏)', async () => {
|
||||
const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-cc1-'));
|
||||
const ud = nodePath.join(tmp, 'ud'); const v = nodePath.join(tmp, 'v');
|
||||
await fs.mkdir(ud, { recursive: true }); await fs.mkdir(v, { recursive: true });
|
||||
const a = await launch(ud);
|
||||
a.v = v;
|
||||
await openDocWithCode(a);
|
||||
await a.page.locator('.ProseMirror button').filter({ hasText: /复制/ }).first().click();
|
||||
await a.page.waitForTimeout(200);
|
||||
const text = await a.clip();
|
||||
console.log('CLIP_BUTTON', JSON.stringify(text));
|
||||
expect(text.trim()).toBe('sudo apt-get update');
|
||||
await a.app.close();
|
||||
});
|
||||
|
||||
// Ctrl+C 选中代码块内容复制也不应带围栏
|
||||
test('代码块内 Ctrl+C 复制纯内容(无围栏)', async () => {
|
||||
const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-cc2-'));
|
||||
const ud = nodePath.join(tmp, 'ud'); const v = nodePath.join(tmp, 'v');
|
||||
await fs.mkdir(ud, { recursive: true }); await fs.mkdir(v, { recursive: true });
|
||||
const a = await launch(ud);
|
||||
a.v = v;
|
||||
await openDocWithCode(a);
|
||||
const code = a.page.locator('.ProseMirror pre code');
|
||||
await code.click();
|
||||
// 选中代码块内全部内容
|
||||
await a.page.keyboard.press('Control+a');
|
||||
await a.page.keyboard.press('Control+c');
|
||||
await a.page.waitForTimeout(200);
|
||||
const text = await a.clip();
|
||||
console.log('CLIP_CTRLC', JSON.stringify(text));
|
||||
expect(text.trim()).toBe('sudo apt-get update');
|
||||
await a.app.close();
|
||||
});
|
||||
@@ -54,8 +54,8 @@ test('回归:文件列表缩进递进且带图标', async () => {
|
||||
await a.app.close();
|
||||
});
|
||||
|
||||
// 回归:代码块复制携带 Markdown 围栏(FR-反馈3)
|
||||
test('回归:复制代码块携带 ```language 围栏', async () => {
|
||||
// 回归:代码块复制应为纯代码内容,不附带 ```language 围栏(用户反馈修正)
|
||||
test('回归:复制代码块为纯内容(无围栏)', async () => {
|
||||
const tmp = await fs.mkdtemp(nodePath.join(os.tmpdir(), 'hn-reg3-'));
|
||||
const userData = nodePath.join(tmp, 'ud'); const vault = nodePath.join(tmp, 'v');
|
||||
await fs.mkdir(userData, { recursive: true }); await fs.mkdir(vault, { recursive: true });
|
||||
@@ -75,7 +75,8 @@ test('回归:复制代码块携带 ```language 围栏', async () => {
|
||||
await copyBtn.click();
|
||||
await a.page.waitForTimeout(400);
|
||||
const text = await a.page.evaluate(() => (globalThis as any).__cap?.[0] ?? null);
|
||||
expect(text).toBe('```bash\necho hello\nls -la\n```');
|
||||
// 纯代码内容,不带 ``` 围栏
|
||||
expect(text).toBe('echo hello\nls -la');
|
||||
await a.app.close();
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user