Files
2026-11-16 04:21:21 -07:00

93 lines
5.3 KiB
JavaScript

// Table Enter and Shift+Enter, table clipboard with line breaks, task marker editing, and title Tab, in both themes and a narrow window.
import assert from "node:assert/strict";
import path from "node:path";
import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("tables/enterKey", async ({ page }) => {
const output = await outputDir("tables/enterKey");
await mockTauri(page, {
commands: test => ({
...test.workspace({ root: "C:\\smoke-workspace", files: { "notes/welcome.md": "---\ntitle: Welcome\n---\n\n# Working notes\n\nFirst paragraph.\n\n- [ ] A task\n\n[[second]]\n" }, writeEvents: true }),
spelling_menu_install: () => null,
}),
});
await page.goto(smokeUrl);
await page.getByText('smoke-workspace', { exact: true }).waitFor();
await page.keyboard.press('Control+p');
await page.getByRole('textbox', { name: 'Find a file' }).fill('welcome.md');
await page.getByRole('dialog').getByRole('button').filter({ hasText: 'welcome.md' }).first().click();
await page.getByRole('textbox', { name: 'Writing editor', exact: true }).waitFor();
await page.evaluate(async () => {
const { EditorView } = await import('/node_modules/@codemirror/view/dist/index.js');
window.__CM__ = EditorView.findFromDOM(document.querySelector('.cm-content'));
});
const source = '# Example\n\n| Name | Notes |\n| --- | --- |\n| Alpha | First<br>Second |\n| Beta | Last |\n\nAfter.\n\n- [ ] A task\n';
const reset = async () => {
await page.evaluate(source => window.__CM__.dispatch({ changes: { from: 0, to: window.__CM__.state.doc.length, insert: source }, selection: { anchor: 0 } }), source);
await page.locator('.live-writer table').waitFor();
};
const cell = (r, c) => page.locator('.live-writer table tr').nth(r).locator('th,td').nth(c);
const shot = name => page.screenshot({ path: path.join(output, `${name}.png`) });
for (const mode of ['dark', 'light']) {
await page.evaluate(async mode => (await import('/src/lib/theme/appearance.ts')).saveAppearance({ theme: 'default', mode }), mode);
await reset();
await cell(1, 1).click();
await page.keyboard.press('Enter');
await page.waitForFunction(() => document.activeElement?.closest('td')?.cellIndex === 1 && document.activeElement.closest('tr').rowIndex === 2);
await page.keyboard.press('Enter');
await page.waitForFunction(() => document.activeElement?.closest('td')?.cellIndex === 1 && document.activeElement.closest('tr').rowIndex === 3);
assert.equal(await page.locator('.live-writer table tr').count(), 4);
await page.keyboard.type('New');
await page.keyboard.press('Shift+Enter');
await page.keyboard.type('Line');
assert.match(await page.evaluate(() => window.__CM__.state.sliceDoc()), /New<br>Line/);
await shot(`${mode}-table-enter`);
await reset();
await cell(0, 0).click();
await page.keyboard.press('Shift+F10');
await page.getByRole('menuitem', { name: 'Select cell', exact: true }).click();
await page.keyboard.press('Shift+ArrowRight');
await page.keyboard.press('Shift+ArrowDown');
const copied = await page.locator('.writer-table').evaluate(node => {
const data = new DataTransfer();
node.dispatchEvent(new ClipboardEvent('copy', { clipboardData: data, bubbles: true, cancelable: true }));
return { html: data.getData('text/html'), text: data.getData('text/plain') };
});
assert.match(copied.html, /<table>/);
assert.match(copied.text, /Name\tNotes/);
await page.evaluate(() => {
const view = window.__CM__;
const anchor = view.state.sliceDoc().indexOf('After.');
view.dispatch({ selection: { anchor } }); view.focus();
});
await page.getByRole('textbox', { name: 'Writing editor', exact: true }).evaluate((node, copied) => {
const data = new DataTransfer(); data.setData('text/html', copied.html); data.setData('text/plain', copied.text);
node.dispatchEvent(new ClipboardEvent('paste', { clipboardData: data, bubbles: true, cancelable: true }));
}, copied);
assert.equal(await page.locator('.live-writer table').count(), 2);
assert.match(await page.evaluate(() => window.__CM__.state.sliceDoc()), /First<br>Second/);
await shot(`${mode}-table-paste`);
await reset();
await page.evaluate(() => {
const view = window.__CM__;
view.dispatch({ selection: { anchor: view.state.sliceDoc().indexOf('A task') + 3 } }); view.focus();
});
assert.equal(await page.locator('.writer-checkbox').count(), 1);
await shot(`${mode}-checklist-text`);
await page.evaluate(() => {
const view = window.__CM__;
view.dispatch({ selection: { anchor: view.state.sliceDoc().indexOf('[ ]') + 1 } }); view.focus();
});
assert.equal(await page.locator('.writer-checkbox').count(), 0);
await shot(`${mode}-checklist-source`);
await page.getByRole('button', { name: 'Rename note', exact: true }).click();
await page.getByRole('textbox', { name: 'Note filename', exact: true }).press('Tab');
await page.waitForFunction(() => document.activeElement?.classList.contains('cm-content'));
await shot(`${mode}-title-tab`);
await page.setViewportSize({ width: 600, height: 760 });
await reset();
await shot(`${mode}-narrow`);
await page.setViewportSize({ width: 1440, height: 1000 });
}
});