// 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(''); 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."); });