feat(app): find in side panel previews (#63663)

This commit is contained in:
Aiden Cline
2026-11-13 12:23:47 -06:00
committed by GitHub
Unverified
parent 0b7a1a04e6
commit 083e3114a5
5 changed files with 485 additions and 6 deletions
@@ -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",
"<!-- note -->\n<!doctype html><title>Page</title><p>First needle</p><p style='margin-top:2000px'>Last needle</p>",
],
])
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)
})
@@ -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);
}
@@ -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: {
<div
data-slot="artifact-document-stage"
data-kind="markdown"
data-file-find="document"
data-document-surface-inset
data-document-surface-scrollport
data-document-surface-column
@@ -716,7 +718,7 @@ function ArtifactAudio(props: MediaProps) {
}
function ArtifactFrame(props: { path: string; content: FileContent; kind: "pdf" | "html" }) {
const source = createMediaSource(() => props.content)
const source = createMediaSource(() => (props.kind === "html" ? findableHtml(props.content) : props.content))
// PDF Open Parameters: start with the thumbnail pane closed and the page fitted to the pane width.
const src = () => (props.kind === "pdf" ? `${source().src}#navpanes=0&view=FitH` : source().src)
@@ -724,6 +726,7 @@ function ArtifactFrame(props: { path: string; content: FileContent; kind: "pdf"
<iframe
class="block h-full w-full flex-1 border-0 bg-white"
title={getFilename(props.path)}
data-file-find={props.kind === "html" ? "frame" : undefined}
src={src()}
// The PDF viewer is Chromium's own and does not run in a sandboxed frame. HTML runs as an
// opaque origin: no app storage, cookies, or credentialed requests reach it.
@@ -798,7 +801,7 @@ function ArtifactMermaid(props: { text: string; cacheKey?: string }) {
return (
<div class="relative flex min-h-0 flex-1 flex-col">
<div ref={scroller} class="min-h-0 flex-1 overflow-auto" data-scrollable>
<div ref={scroller} class="min-h-0 flex-1 overflow-auto" data-scrollable data-file-find="document">
<div class="grid min-h-full w-max min-w-full place-items-center">
<div data-slot="artifact-diagram" style={{ width: `${Math.min(view.width, 896)}px`, zoom: view.zoom }}>
<Markdown text={`\`\`\`mermaid\n${props.text}\n\`\`\``} cacheKey={props.cacheKey} />
@@ -837,7 +840,7 @@ function ArtifactTable(props: { parsed: ReturnType<typeof parseDelimited> }) {
}
return (
<div class="min-h-0 flex-1 overflow-auto" data-scrollable>
<div class="min-h-0 flex-1 overflow-auto" data-scrollable data-file-find="document">
<table data-slot="artifact-table" class="min-w-full text-13-regular text-text-base">
<thead>
<tr>
+376
View File
@@ -0,0 +1,376 @@
import { Option, Schema } from "effect"
import { createSignal, onCleanup, Show, untrack } from "solid-js"
import { makeEventListener } from "@solid-primitives/event-listener"
import { createResizeObserver } from "@solid-primitives/resize-observer"
import { createKeyed, type FileContent } from "@opencode/plugin/gui"
import { FileSearchBar } from "@opencode/session-ui/file-search"
const HIT = "file-preview-find"
const ACTIVE = "file-preview-find-current"
/** Marks messages between the bar and the script it adds to an HTML preview. */
const CHANNEL = "opencode-file-find"
/** The open bar: what it searches, the query, and the active match out of `count`. */
type PreviewFindState = {
target: HTMLElement
query: string
index: number
count: number
pos: { top: number; right: number }
}
/** The marked ranges of a rendered preview, and the bar's input. */
type PreviewFindDom = { ranges: Range[]; input?: HTMLInputElement }
/** A frame the observer waits for, so a burst of mutations searches once. */
type PendingFrame = { frame?: number }
/**
* Find for previews the code view's find can't search: rendered Markdown, Mermaid and tables by their text, and HTML
* through a script added to the page, since the preview runs in a sandboxed frame. It uses the code view's bar and
* marks, so both look the same.
*/
export function createPreviewFind(input: { root: () => HTMLElement | undefined; active: () => boolean }) {
const [state, setState] = createSignal<PreviewFindState>()
const local: PreviewFindDom = { ranges: [] }
const update = (next: Partial<PreviewFindState>) => {
const current = untrack(state)
if (current) setState({ ...current, ...next })
}
const search = (query: string, index: number, scroll = true) => {
const current = untrack(state)
if (!current) return
if (current.target instanceof HTMLIFrameElement) {
update({ query, index })
current.target.contentWindow?.postMessage({ source: CHANNEL, op: "search", query, index, marks: marks() }, "*")
return
}
local.ranges = query ? documentRanges(current.target, query) : []
const at = local.ranges.length === 0 ? 0 : Math.min(index, local.ranges.length - 1)
update({ query, index: at, count: local.ranges.length })
paint(at, scroll)
}
const paint = (index: number, scroll: boolean) => {
if (!globalThis.CSS?.highlights) return
const active = local.ranges[index]
CSS.highlights.set(HIT, new Highlight(...local.ranges.filter((range) => range !== active)))
if (!active) return CSS.highlights.delete(ACTIVE)
CSS.highlights.set(ACTIVE, new Highlight(active))
if (scroll) active.startContainer.parentElement?.scrollIntoView({ block: "center", inline: "nearest" })
}
// Where the code view puts its bar, so it stays put when the preview switches to the source.
const position = (el: HTMLElement) => {
const rect = el.getBoundingClientRect()
return { top: Math.round(rect.top) - 4, right: Math.round(window.innerWidth - rect.right) + 8 }
}
/** Opens the bar for the shown preview; false when none is showing, so the code view's find runs instead. */
const open = () => {
const el = input.root()?.querySelector<HTMLElement>("[data-file-find]")
// A closed side panel keeps its preview mounted but hidden; the bar would open off screen.
if (!el?.checkVisibility()) return false
const current = untrack(state)
if (current?.target !== el) {
close()
setState({ target: el, query: current?.query ?? "", index: 0, count: 0, pos: position(el) })
}
search(untrack(state)?.query ?? "", 0)
requestAnimationFrame(() => {
local.input?.focus()
local.input?.select()
})
return true
}
const close = () => {
const current = untrack(state)
if (!current) return
local.ranges = []
CSS.highlights?.delete(HIT)
CSS.highlights?.delete(ACTIVE)
if (current.target instanceof HTMLIFrameElement)
current.target.contentWindow?.postMessage({ source: CHANNEL, op: "clear" }, "*")
setState(undefined)
}
const next = (dir: 1 | -1) => {
const current = untrack(state)
if (!current?.query || current.count === 0) return
const index = (current.index + dir + current.count) % current.count
if (current.target instanceof HTMLIFrameElement) return search(current.query, index)
update({ index })
paint(index, true)
}
// The HTML preview's script answers searches, says when its page loaded, and passes on Mod+F pressed inside it,
// which never reaches this window's key handlers.
makeEventListener(window, "message", (event: MessageEvent) => {
const frame = input.root()?.querySelector<HTMLIFrameElement>('iframe[data-file-find="frame"]')
if (!frame || event.source !== frame.contentWindow) return
const decoded = decodeFrameMessage(event.data)
if (Option.isNone(decoded)) return
const data = decoded.value
if (data.op === "shortcut") {
if (input.active()) open()
return
}
const current = untrack(state)
if (current?.target !== frame) return
if (data.op === "ready") return search(current.query, current.index)
if (data.op === "result" && data.query === current.query) update({ count: data.count, index: data.index })
})
// Keep the bar on its preview, search text that renders again, and close once the preview is gone, e.g. on a
// switch to the source, whose code view has its own find.
createKeyed(
() => state()?.target,
(el) => {
const root = input.root()
const pending: PendingFrame = {}
const observer = new MutationObserver(() => {
if (pending.frame !== undefined) return
pending.frame = requestAnimationFrame(() => {
pending.frame = undefined
if (!el.isConnected) return close()
if (el instanceof HTMLIFrameElement) return
const current = untrack(state)
// Text that renders again keeps the active match without scrolling to it.
if (current) search(current.query, current.index, false)
})
})
if (root) observer.observe(root, { childList: true, subtree: true, characterData: true })
const reposition = () => update({ pos: position(el) })
makeEventListener(window, "resize", reposition, { passive: true })
createResizeObserver(el, reposition)
onCleanup(() => {
observer.disconnect()
if (pending.frame !== undefined) cancelAnimationFrame(pending.frame)
})
},
)
createKeyed(() => !input.active(), close)
onCleanup(close)
return {
state,
open,
close,
next,
setInput: (el: HTMLInputElement) => {
local.input = el
},
setQuery: (query: string) => search(query, 0),
onKeyDown: (event: KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault()
close()
return
}
if (event.key !== "Enter") return
event.preventDefault()
next(event.shiftKey ? -1 : 1)
},
}
}
export function PreviewFindBar(props: { find: ReturnType<typeof createPreviewFind> }) {
return (
<Show when={props.find.state()}>
{(state) => (
<FileSearchBar
pos={state().pos}
query={state().query}
count={state().count}
index={state().index}
setInput={props.find.setInput}
onInput={props.find.setQuery}
onKeyDown={props.find.onKeyDown}
onClose={props.find.close}
onPrev={() => props.find.next(-1)}
onNext={() => props.find.next(1)}
/>
)}
</Show>
)
}
/**
* The HTML with the find script first, after any doctype so the page keeps its rendering mode. The script runs before
* the page's own, so a Content-Security-Policy the page declares later does not block it.
*/
export function findableHtml(content: FileContent): FileContent {
if (content.type !== "text" || content.url || content.bytes) return content
const script = `<script>(${frameFind.toString()})(${JSON.stringify(CHANNEL)})</script>`
const doctype = /^(?:\s|<!--[\s\S]*?-->)*<!doctype[^>]*>/i.exec(content.content)?.[0] ?? ""
return { ...content, content: doctype + script + content.content.slice(doctype.length) }
}
/** The page's colors can't resolve the app's theme, so the marks travel resolved with each search. */
function marks() {
const probe = document.createElement("span")
document.body.append(probe)
probe.style.backgroundColor = "rgb(from var(--surface-warning-base) r g b / 0.35)"
const hit = getComputedStyle(probe).backgroundColor
probe.style.backgroundColor = "rgb(from var(--surface-warning-strong) r g b / 0.55)"
const active = getComputedStyle(probe).backgroundColor
probe.remove()
return { hit, active }
}
/** What the HTML preview's script sends; the page's own scripts can post to this window too. */
const decodeFrameMessage = Schema.decodeUnknownOption(
Schema.Union([
Schema.Struct({ source: Schema.Literal(CHANNEL), op: Schema.Literals(["ready", "shortcut"]) }),
Schema.Struct({
source: Schema.Literal(CHANNEL),
op: Schema.Literal("result"),
query: Schema.String,
count: Schema.Number,
index: Schema.Number,
}),
]),
)
/** Every case-insensitive occurrence of `query` in the text under `root`, in reading order. */
function documentRanges(root: HTMLElement, query: string) {
const needle = query.toLowerCase()
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT)
const ranges: Range[] = []
for (let node = walker.nextNode(); node; node = walker.nextNode()) {
if (!(node instanceof Text)) continue
const hay = node.data.toLowerCase()
// Text the preview hides, such as a diagram's source, can't be shown as a match.
if (!hay.includes(needle) || !node.parentElement?.checkVisibility()) continue
for (let at = hay.indexOf(needle); at !== -1; at = hay.indexOf(needle, at + needle.length)) {
const range = document.createRange()
range.setStart(node, at)
range.setEnd(node, at + needle.length)
ranges.push(range)
}
}
return ranges
}
/**
* Runs inside the HTML preview, serialized into its page, so it can use nothing from this module. It finds and marks
* text the way the bar does for other previews, and hands Mod+F back to the app.
*/
function frameFind(channel: string) {
const post = (
message: { op: "ready" | "shortcut" } | { op: "result"; query: string; count: number; index: number },
) => parent.postMessage({ source: channel, ...message }, "*")
// An adopted sheet, since a style element would fall to the page's own Content-Security-Policy.
const sheet = new CSSStyleSheet()
window.addEventListener("message", (event) => {
const data = event.data
if (event.source !== parent || data?.source !== channel) return
const api = CSS.highlights
if (data.op === "clear") {
api?.delete("opencode-file-find")
api?.delete("opencode-file-find-current")
return
}
if (data.op !== "search") return
const query = String(data.query)
const needle = query.toLowerCase()
const ranges: Range[] = []
const walker = document.createTreeWalker(document.body ?? document.documentElement, NodeFilter.SHOW_TEXT)
for (let node = walker.nextNode(); node && needle; node = walker.nextNode()) {
if (!(node instanceof Text)) continue
const hay = node.data.toLowerCase()
if (!hay.includes(needle) || !node.parentElement?.checkVisibility()) continue
for (let at = hay.indexOf(needle); at !== -1; at = hay.indexOf(needle, at + needle.length)) {
const range = document.createRange()
range.setStart(node, at)
range.setEnd(node, at + needle.length)
ranges.push(range)
}
}
const index = ranges.length === 0 ? 0 : Math.min(Math.max(data.index, 0), ranges.length - 1)
const active = ranges[index]
sheet.replaceSync(
`::highlight(opencode-file-find){background-color:${data.marks?.hit}}::highlight(opencode-file-find-current){background-color:${data.marks?.active}}`,
)
if (!document.adoptedStyleSheets.includes(sheet))
document.adoptedStyleSheets = [...document.adoptedStyleSheets, sheet]
if (api) {
api.set("opencode-file-find", new Highlight(...ranges.filter((range) => range !== active)))
if (active) api.set("opencode-file-find-current", new Highlight(active))
else api.delete("opencode-file-find-current")
}
active?.startContainer.parentElement?.scrollIntoView({ block: "center", inline: "nearest" })
post({ op: "result", query, count: ranges.length, index })
})
window.addEventListener(
"keydown",
(event) => {
if (event.key.toLowerCase() !== "f" || !(event.metaKey || event.ctrlKey) || event.altKey || event.shiftKey) return
event.preventDefault()
post({ op: "shortcut" })
},
true,
)
window.addEventListener("DOMContentLoaded", () => post({ op: "ready" }))
}
+13 -3
View File
@@ -12,6 +12,7 @@ import { createKeyed, usePlugin, type LineRange, type MountedSession, type Sessi
import { artifactKind } from "@opencode/util/artifact"
import ArtifactView from "./artifact-view"
import { useShared } from "./context"
import { createPreviewFind, PreviewFindBar } from "./find"
import { fileTabPath } from "./path"
import {
FILE_SCROLL_INTERACTED,
@@ -71,7 +72,7 @@ function FileCommentMenu(props: {
/** The code view's scroll element and the animation frame that restores or stores its scroll. */
type ScrollTarget = { element?: HTMLDivElement; frame?: number }
type FindState = { find: FileSearchHandle | null }
type FindState = { find: FileSearchHandle | null; root?: HTMLDivElement }
/** The session the view shows and the one it showed before a switch, whose unsaved scroll a switch still stores. */
type ShownSessions = { current: MountedSession; previous?: MountedSession }
@@ -417,7 +418,9 @@ export function SessionFileView(props: { session: MountedSession; screen: Sessio
),
})
// Mod+F finds in the shown file.
const previewFind = createPreviewFind({ root: () => state.root, active })
// Mod+F finds in the shown file, or in its preview.
makeEventListener(
window,
"keydown",
@@ -430,6 +433,8 @@ export function SessionFileView(props: { session: MountedSession; screen: Sessio
event.preventDefault()
event.stopPropagation()
if (previewFind.open()) return
// Find scrolls to its matches; the stored scroll must not pull the view back.
scrollSync.interacted()
state.find?.focus()
@@ -523,7 +528,12 @@ export function SessionFileView(props: { session: MountedSession; screen: Sessio
)
return (
<div class="relative h-full min-h-0 flex flex-col" classList={{ "mt-3": !artifact() }}>
<div
ref={(el) => (state.root = el)}
class="relative h-full min-h-0 flex flex-col"
classList={{ "mt-3": !artifact() }}
>
<PreviewFindBar find={previewFind} />
<Switch>
<Match when={current()?.loaded ? current()?.content : undefined}>
{(value) => (