Files
RuneStone/tests/smoke/editor/imageNavigation.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

156 lines
9.3 KiB
JavaScript

// Images dragged from the file tree: hidden source, selection, image menu actions, cancel, and source mode.
import assert from "node:assert/strict";
import { mockTauri, outputDir, runSmoke, smokeUrl } from "../_harness/index.mjs";
await runSmoke("editor/imageNavigation", async ({ page }) => {
await mockTauri(page, {
commands: test => {
// Image files hold base64 SVG.
const svg = btoa('<svg xmlns="http://www.w3.org/2000/svg" width="640" height="360"><rect width="640" height="360" fill="#486077"/><circle cx="320" cy="180" r="110" fill="#93cab5"/></svg>');
const workspace = test.workspace({
root: "C:\\smoke-workspace",
files: {
"notes/welcome.md": "Above.\n\nBelow.\n",
"notes/second.md": "# Second note\n",
"src/hello.py": "print('hello')\n",
"images/picture (#1).svg": svg,
"images/replacement.svg": svg,
},
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);
},
workspace_read_data_url: ({ rel }) => {
if (!workspace.files.has(rel)) throw new Error("missing image");
return "data:image/svg+xml;base64," + workspace.files.get(rel);
},
workspace_reveal: () => null,
};
},
});
const output = await outputDir("editor/imageNavigation");
async function writer() {
await page.locator(".cm-content").waitFor();
await page.evaluate(async () => {
const { EditorView } = await import("/node_modules/@codemirror/view/dist/index.js");
window.__CM__ = EditorView.findFromDOM(document.querySelector(".cm-content"));
});
}
async function source() { return page.evaluate(() => window.__CM__.state.doc.toString()); }
async function dragFromTree(selector = ".cm-content", cancel = false) {
const row = page.locator('[data-tree-rel="images/picture (#1).svg"]');
const from = await row.boundingBox(), to = await page.locator(selector).boundingBox();
await page.mouse.move(from.x + 60, from.y + from.height / 2);
await page.mouse.down();
await page.mouse.move(to.x + 40, to.y + 10, { steps: 12 });
await page.getByText("Insert image", { exact: true }).waitFor();
assert.equal(await page.locator(".image-drop-target").count(), 1);
if (cancel) await page.keyboard.press("Escape");
await page.mouse.up();
}
async function imageMenu() {
await page.locator(".md-image-frame").first().click({ button: "right" });
await page.getByRole("menu", { name: "Image actions" }).waitFor();
}
await page.context().grantPermissions(["clipboard-read", "clipboard-write"]);
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" }).click();
await writer();
await page.locator('[data-tree-rel="images"]').click();
await dragFromTree();
await page.locator(".md-image-frame img").waitFor();
const linked = await source();
assert.ok(linked.startsWith("![](../images/picture%20%28%231%29.svg)\n\nAbove."), linked);
assert.equal(await page.evaluate(() => window.__TEST__.calls.filter(c => ["workspace_write_bytes", "workspace_rename"].includes(c.command)).length), 0);
assert.equal(await page.locator(".cm-line").filter({ hasText: "picture%20" }).count(), 0);
// Neither caret movement nor selection exposes an image's Markdown.
await page.evaluate(() => window.__CM__.dispatch({ selection: { anchor: 0, head: 38 } }));
assert.equal(await page.locator(".cm-line").filter({ hasText: "picture%20" }).count(), 0);
await page.locator(".md-image-frame img").click();
assert.equal(await page.locator(".md-image-frame.is-selected").count(), 1);
await page.keyboard.press("Shift+F10");
await page.getByRole("menu", { name: "Image actions" }).waitFor();
assert.equal(await page.locator(".md-image-frame.is-selected").count(), 1);
await page.screenshot({ path: `${output}/image-context-menu.png` });
await page.getByRole("menuitem", { name: "Copy Markdown", exact: true }).click();
assert.equal(await page.evaluate(() => navigator.clipboard.readText()), "![](../images/picture%20%28%231%29.svg)");
await imageMenu();
await page.getByRole("menuitem", { name: "Copy image", exact: true }).click();
await page.waitForFunction(async () => (await navigator.clipboard.read()).some(item => item.types.includes("image/png")));
await imageMenu();
await page.getByRole("menuitem", { name: "Show in Explorer", exact: true }).click();
await page.waitForFunction(() => window.__TEST__.calls.some(c => c.command === "workspace_reveal" && c.args.rel === "images/picture (#1).svg"));
await imageMenu();
await page.getByRole("menuitem", { name: "Remove image", exact: true }).click();
assert.ok(!(await source()).includes("picture%20"));
assert.ok(await page.evaluate(() => window.__TEST__.files.has("images/picture (#1).svg")));
await page.keyboard.press("Control+z");
assert.equal(await source(), linked);
await page.locator(".md-image-frame").hover();
await page.getByRole("button", { name: "Edit image source", exact: true }).click();
await page.locator(".cm-line").filter({ hasText: "picture%20" }).waitFor();
assert.equal(await page.locator(".md-image-frame img").count(), 1);
await page.getByRole("button", { name: "Hide image source", exact: true }).click();
assert.equal(await page.locator(".cm-line").filter({ hasText: "picture%20" }).count(), 0);
await imageMenu();
await page.getByRole("menuitem", { name: "Swap image…", exact: true }).click();
await page.getByRole("textbox", { name: "Image path relative to this note, or https URL" }).fill("../images/replacement.svg");
await page.getByRole("button", { name: "Continue", exact: true }).click();
await page.waitForFunction(() => window.__CM__.state.doc.toString().includes("replacement.svg"));
assert.equal(await page.locator(".cm-line").filter({ hasText: "replacement.svg" }).count(), 0);
await imageMenu();
await page.getByRole("menuitem", { name: "Open in new tab", exact: true }).click();
await page.getByRole("tab", { name: "replacement.svg", exact: true }).waitFor();
await page.getByRole("tab", { name: /welcome.md/ }).click();
await writer();
const beforeCancel = await source();
await dragFromTree(".cm-content", true);
assert.equal(await source(), beforeCancel);
assert.equal(await page.locator(".image-drop-target").count(), 0);
// Inline images remain independently selectable without hiding adjacent prose.
await page.evaluate(() => {
const view = window.__CM__;
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: "Before ![](../images/replacement.svg) between ![[../images/replacement.svg]] after.\n" }, selection: { anchor: 0 } });
});
await page.waitForFunction(() => document.querySelectorAll(".md-image-frame").length === 2);
assert.ok((await page.locator(".cm-content").innerText()).includes("Before"));
assert.ok(!(await page.locator(".cm-content").innerText()).includes("![]"));
await page.locator(".md-image-frame img").nth(1).click();
await page.keyboard.press("Delete");
assert.equal(await source(), "Before ![](../images/replacement.svg) between after.\n");
await page.keyboard.press("Control+z");
assert.ok((await source()).includes("![[../images/replacement.svg]]"));
// Reference-image editing affects only the selected embed, not its definition.
await page.evaluate(() => {
const view = window.__CM__;
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: "![Reference][pic]\n\nBelow.\n\n[pic]: ../images/replacement.svg\n" }, selection: { anchor: 0 } });
});
await page.locator(".md-image-frame").hover();
assert.equal(await page.locator(".cm-line").filter({ hasText: "![Reference]" }).count(), 0);
await page.getByRole("button", { name: "Edit image source", exact: true }).click();
await page.locator(".cm-line").filter({ hasText: "![Reference]" }).waitFor();
await page.keyboard.press("Escape");
assert.equal(await page.locator(".cm-line").filter({ hasText: "![Reference]" }).count(), 0);
// The same navigator drag works in the source editor.
// The layout switcher names its buttons by mode (src/lib/layout/EditorPane.svelte:200-208, src/lib/ui/ModeSwitcher.svelte:96).
await page.getByRole("group", { name: "Editor layout", exact: true }).hover();
await page.getByRole("button", { name: "IDE", exact: true }).click();
await page.locator(".source-editor .cm-content").waitFor();
await dragFromTree(".source-editor .cm-content");
await page.keyboard.press("Control+s");
await page.waitForFunction(() => window.__TEST__.files.get("notes/welcome.md").includes("picture%20"));
assert.equal(await page.evaluate(() => window.__TEST__.calls.filter(c => c.command === "workspace_write_bytes").length), 0);
console.log("Navigator image drag, hidden source, selection, copy, swap, remove, open, reveal, cancel and source mode passed.");
});