diff --git a/packages/app/e2e/regression/side-panels.spec.ts b/packages/app/e2e/regression/side-panels.spec.ts index 25de3813077..b58082aa29c 100644 --- a/packages/app/e2e/regression/side-panels.spec.ts +++ b/packages/app/e2e/regression/side-panels.spec.ts @@ -387,3 +387,84 @@ test("the new tab view lists recently closed tabs, expands to the rest, and keep await expect(rows.first()).toContainText("closed-00.ts") await expect(recent).not.toContainText("README.md") }) + +test("Mod+F finds in Markdown and HTML previews with the code view's find bar", async ({ page }) => { + await page.setViewportSize({ width: 1440, height: 900 }) + const directory = "C:/OpenCode/PreviewFind" + const key = `local\u0000${base64Encode(directory)}/ses_previewfind` + + const files = new Map([ + ["notes.md", "# Needle notes\n\nOne needle here.\n\nAnother **needle** there."], + ["diagram.mmd", "flowchart TD\n A[First needle] --> B[Last needle]"], + [ + "page.html", + "\n
First needle
Last needle
", + ], + ]) + + await openSession(page, { + name: "PreviewFind", + fileList: () => [], + fileContent: (path) => ({ type: "text", content: files.get(path.split("/").at(-1) ?? "") ?? "" }), + seed: { + panes: { ses_previewfind: { review: true } }, + storage: { + "opencode.global.dat:layout": { + sessionTabs: { + [key]: { all: ["file://notes.md", "file://diagram.mmd", "file://page.html"], active: "file://notes.md" }, + }, + }, + }, + }, + }) + + const side = page.locator('[data-pane-content="side"]') + const find = page.getByRole("textbox", { name: "Find" }) + const mod = await page.evaluate(() => (/(Mac|iPod|iPhone|iPad)/.test(navigator.platform) ? "Meta" : "Control")) + await side.getByText("One needle here.").click() + await page.keyboard.press(`${mod}+f`) + await expect(find).toBeFocused() + await find.fill("needle") + await expect(page.getByText("1/3", { exact: true })).toBeVisible() + await find.press("Enter") + await expect(page.getByText("2/3", { exact: true })).toBeVisible() + await find.press("Shift+Enter") + await find.press("Shift+Enter") + await expect(page.getByText("3/3", { exact: true })).toBeVisible() + await find.press("Escape") + await expect(find).toHaveCount(0) + + // A closed side panel keeps its preview mounted out of sight, so Mod+F opens no bar there. + const toggle = page.getByRole("button", { name: "Toggle review" }) + await toggle.click() + await expect(side).toBeHidden() + await page.keyboard.press(`${mod}+f`) + await expect(find).toHaveCount(0) + await toggle.click() + + // Mermaid keeps its source in a hidden code block; only the drawn labels count. + await side.getByRole("tab", { name: "diagram.mmd", exact: true }).click() + await side.locator("svg").getByText("First needle").click() + await page.keyboard.press(`${mod}+f`) + await expect(find).toBeFocused() + await find.fill("needle") + await expect(page.getByText("1/2", { exact: true })).toBeVisible() + await find.press("Escape") + await expect(find).toHaveCount(0) + + // The HTML runs in a sandboxed frame; Mod+F pressed inside it still opens the bar. + await side.getByRole("tab", { name: "page.html", exact: true }).click() + const frame = side.frameLocator('iframe[data-file-find="frame"]') + await frame.getByText("First needle").click() + // The find script goes after the doctype, even behind a comment, so the page keeps standards mode. + expect(await frame.locator("html").evaluate(() => document.compatMode)).toBe("CSS1Compat") + await page.keyboard.press(`${mod}+f`) + await expect(find).toBeFocused() + await find.fill("needle") + await expect(page.getByText("1/2", { exact: true })).toBeVisible() + await find.press("Enter") + await expect(page.getByText("2/2", { exact: true })).toBeVisible() + await expect(frame.getByText("Last needle")).toBeInViewport() + await find.press("Escape") + await expect(find).toHaveCount(0) +}) diff --git a/packages/gui-plugins/src/file/artifact-view.css b/packages/gui-plugins/src/file/artifact-view.css index f2625006c07..d60610bbb5b 100644 --- a/packages/gui-plugins/src/file/artifact-view.css +++ b/packages/gui-plugins/src/file/artifact-view.css @@ -315,3 +315,12 @@ img[data-slot="artifact-media"] { padding: 20px 12px; } } + +/* The same marks as the code view's find. */ +::highlight(file-preview-find) { + background-color: rgb(from var(--surface-warning-base) r g b / 0.35); +} + +::highlight(file-preview-find-current) { + background-color: rgb(from var(--surface-warning-strong) r g b / 0.55); +} diff --git a/packages/gui-plugins/src/file/artifact-view.tsx b/packages/gui-plugins/src/file/artifact-view.tsx index 2f3746c44ce..cf1c725fd74 100644 --- a/packages/gui-plugins/src/file/artifact-view.tsx +++ b/packages/gui-plugins/src/file/artifact-view.tsx @@ -38,6 +38,7 @@ import { import { FileViewer } from "@opencode/plugin/gui/file" import { uuid } from "@opencode/util/uuid" import { ArtifactToolbar } from "./artifact-toolbar" +import { findableHtml } from "./find" import { createArtifactViewerState, createVideoOverlayState, @@ -148,6 +149,7 @@ export default function ArtifactView(props: {