Files
RuneStone/tests/smoke/editor/markdownMenu.smoke.mjs
T
Knotty Wyvern 724402729e Replace Monaco with one CodeMirror editor and nest list items with Tab
Phase 4b. DocumentEditor.svelte replaces LiveWriter.svelte and the Monaco
EditorHost.svelte. Each open document has one CodeMirror state. Write and
Source are two compartment configurations of it, so they share the undo
history. editorStates.ts keeps the state while the tab stays open. The
Monaco model, the diff bridge, its timers, and sessions.ts are gone.

- Source: syntax colors from the theme tokens, lazy language packages,
  line numbers, folding, and the current line in IDE, wrapped lines in
  Writing, several carets, and bracket matching and closing.
- Lists (card 088e778b): Tab nests a list item and Shift+Tab moves it
  up, in Write and Source. Both renumber the list. The source keeps
  "1.". Write and Read show numbers, letters, then Roman numerals.
- Indent (card 696c30ce): a fixed 4 spaces in Write and Source.
- The code menu uses CodeMirror commands. The command palette item and
  the Monaco styles, search, theme, and workers are removed.
- @codemirror/legacy-modes is pinned to 6.5.4. Version 6.5.5 uses
  parser types that do not match @codemirror/language 6.12.
- 26 smoke tests use CodeMirror instead of Monaco. New tests:
  editor/listNesting and the markdownLists unit tests.
- Docs: development, UI design, plugin host, user guide, and UI-040.

pnpm check, pnpm test (341 of 341), and pnpm build pass. The full smoke
suite passes, except one template test that passed on retry.
2026-10-28 10:48:44 -07:00

154 lines
9.8 KiB
JavaScript

// Markdown text menu in source and writing modes: formatting, paragraphs, tables, links, extraction, clipboard, and stale selections.
import assert from "node:assert/strict";
import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("editor/markdownMenu", async ({ page }) => {
await mockTauri(page, {
commands: test => {
const workspace = 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",
"notes/second.md": "# Second note\n",
"src/hello.py": "print('hello')\n",
},
writeEvents: true,
});
test.files = workspace.files;
return {
...workspace,
// Saves send the last saved text. Reject a save when the file changed, as the backend does.
workspace_write: args => {
if (args.expected !== undefined && workspace.files.get(args.rel) !== args.expected) throw new Error("Save blocked: file changed on disk");
return workspace.workspace_write(args);
},
};
},
});
const output = await outputDir("editor/markdownMenu");
async function quickOpen(name) {
await page.keyboard.press("Control+p");
await page.getByRole("textbox", { name: "Find a file" }).fill(name);
await page.getByRole("dialog").getByRole("button").filter({ hasText: name }).first().click();
await page.getByRole("tab", { selected: true }).click({ button: "right" });
await page.getByRole("menuitem", { name: "Source mode", exact: true }).click();
await page.locator(".source-editor .cm-content").waitFor();
}
async function waitForText(text) {
await page.evaluate(async () => { window.__EditorView = (await import("/node_modules/@codemirror/view/dist/index.js")).EditorView; });
await page.waitForFunction(expected => [...document.querySelectorAll(".cm-content")].some(node => window.__EditorView.findFromDOM(node)?.state.doc.toString() === expected), text);
}
await page.goto(smokeUrl);
await page.getByText("smoke-workspace", { exact: true }).waitFor();
await quickOpen("welcome.md");
const source = "First paragraph.\n\nSecond paragraph.\n";
await page.evaluate(async value => {
const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js");
const view = EditorView.findFromDOM(document.querySelector(".source-editor .cm-content"));
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: value } });
view.dispatch({ selection: { anchor: 0, head: 5 } }); view.focus();
}, source);
const menu = page.getByRole("menu", { name: "Markdown text actions", exact: true });
await page.keyboard.press("Shift+F10");
await menu.waitFor();
await menu.getByRole("menuitem", { name: "Format", exact: true }).hover();
await menu.getByRole("menuitem", { name: "Bold", exact: true }).click();
await waitForText("**First** paragraph.\n\nSecond paragraph.\n");
await page.keyboard.press("Control+z"); await waitForText(source);
await page.getByRole("tab", { selected: true }).click({ button: "right" });
await page.getByRole("menuitem", { name: "Writing mode", exact: true }).click();
const writer = page.getByRole("textbox", { name: "Writing editor", exact: true });
await writer.click(); await page.keyboard.press("Control+Home");
await page.keyboard.press("Control+Shift+ArrowRight");
await page.keyboard.press("Shift+F10"); await menu.waitFor();
await menu.getByRole("menuitem", { name: "Format", exact: true }).hover();
await menu.getByRole("menuitem", { name: "Italic", exact: true }).click();
await page.keyboard.press("Control+s");
await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md") === "*First* paragraph.\n\nSecond paragraph.\n");
await page.keyboard.press("Control+z");
await page.keyboard.press("Control+Home");
await page.keyboard.press("Shift+F10");
await menu.getByRole("menuitem", { name: "Paragraph", exact: true }).hover();
await menu.getByRole("menuitem", { name: "Heading 2", exact: true }).click();
await page.keyboard.press("Control+s");
await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("## First paragraph."));
await page.keyboard.press("Control+z");
await page.keyboard.press("Control+End");
await page.keyboard.press("Shift+F10");
await menu.getByRole("menuitem", { name: "Insert", exact: true }).hover();
await menu.getByRole("menuitem", { name: "Table", exact: true }).click();
await page.locator(".live-writer table").waitFor();
await page.keyboard.press("Control+z");
await page.keyboard.press("Control+Home");
await page.keyboard.press("Control+Shift+ArrowRight");
// Mouse invocation must preserve the selection, too.
await page.locator(".cm-line").filter({ hasText: "First paragraph." }).click({ button: "right", position: { x: 12, y: 10 } });
await menu.waitFor();
await menu.getByRole("menuitem", { name: "Format", exact: true }).hover();
await menu.getByRole("menuitem", { name: "Highlight", exact: true }).click();
await page.keyboard.press("Control+s");
await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("==First== paragraph."));
await page.keyboard.press("Control+End");
await page.locator(".writer-highlight").waitFor();
assert.equal(await page.locator(".writer-highlight").textContent(), "First");
await page.keyboard.press("Control+z");
await page.keyboard.press("Control+End");
await page.keyboard.press("Shift+F10");
await menu.getByRole("menuitem", { name: "Insert", exact: true }).hover();
await page.screenshot({ path: `${output}/markdown-insert-menu.png` });
await page.keyboard.press("Escape");
await page.setViewportSize({ width: 760, height: 600 });
await page.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve))));
await writer.focus();
await page.keyboard.press("Shift+F10");
await menu.getByRole("menuitem", { name: "Paragraph", exact: true }).focus();
await page.keyboard.press("ArrowRight");
assert.equal(await page.evaluate(() => document.activeElement.textContent), "Bullet list");
const rect = await menu.getByRole("menu", { name: "Paragraph", exact: true }).boundingBox();
assert.ok(rect.x >= 0 && rect.y >= 0 && rect.x + rect.width <= 760 && rect.y + rect.height <= 600);
await page.screenshot({ path: `${output}/markdown-paragraph-menu.png` });
await page.keyboard.press("ArrowLeft");
assert.equal(await page.evaluate(() => document.activeElement.textContent), "Paragraph");
await page.keyboard.press("Escape");
await page.keyboard.press("Control+Home");
await page.keyboard.press("Control+Shift+ArrowRight");
await page.keyboard.press("Shift+F10");
await menu.getByRole("menuitem", { name: "Add external link", exact: true }).click();
// External links insert `[label]()` with the caret in the URL, without a dialog (src/lib/editor/markdownMenu.ts:57, src/lib/editor/linkCommands.ts:3-8).
assert.equal(await page.getByRole("dialog").count(), 0);
await page.keyboard.type("https://example.com/a");
await page.keyboard.press("Control+s");
await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("[First](https://example.com/a) paragraph."));
// Undo the typed URL, then the link, which restores the selected label.
await page.keyboard.press("Control+z");
await page.keyboard.press("Control+z");
await page.evaluate(async () => {
const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js");
window.__CM__ = EditorView.findFromDOM(document.querySelector(".cm-content"));
});
await page.waitForFunction(() => window.__CM__.state.sliceDoc().startsWith("First paragraph."));
await page.keyboard.press("Shift+F10");
await menu.getByRole("menuitem", { name: "Extract current selection…", exact: true }).click();
await page.getByRole("textbox", { name: "Extract selection to a new note (workspace relative path)" }).fill("notes/extracted.md");
await page.getByRole("button", { name: "Continue", exact: true }).click();
await page.keyboard.press("Control+s");
await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("[[/notes/extracted.md|First]] paragraph."));
assert.equal(await page.evaluate(() => window.__TEST__.files.get("notes/extracted.md")), "First\n");
await page.keyboard.press("Control+z");
await page.context().grantPermissions(["clipboard-read", "clipboard-write"]);
await page.evaluate(() => navigator.clipboard.writeText("Pasted"));
await page.keyboard.press("Shift+F10");
await menu.getByRole("menuitem", { name: "Paste as plain text", exact: true }).click();
await page.waitForFunction(() => document.querySelector(".cm-content")?.textContent.includes("Pasted paragraph."));
await page.keyboard.press("Control+s");
await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").startsWith("Pasted paragraph."));
await page.evaluate(() => { navigator.clipboard.readText = () => new Promise(resolve => { window.__finishPaste = resolve; }); });
await page.keyboard.press("Shift+F10");
await menu.getByRole("menuitem", { name: "Paste as plain text", exact: true }).click();
await writer.focus(); await page.keyboard.press("End"); await page.keyboard.type(" kept");
await page.evaluate(async () => { window.__finishPaste("STALE"); await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve))); });
assert.ok(!(await writer.textContent()).includes("STALE"));
assert.ok((await writer.textContent()).includes("kept"));
console.log("PASS: Markdown menus in source/writing modes, selection retention, formatting, paragraph conversion, table insertion, undo, keyboard navigation, narrow viewport, links, extraction, clipboard and stale-selection guards.");
});