fix(app): keep an open tab's file watch when an HTML preview loads the file

This commit is contained in:
Dan Smolsky 2026-11-17 21:40:39 -05:00
parent 25ac6afea1
commit 9e0786cdcc
2 changed files with 17 additions and 4 deletions

View file

@ -25,7 +25,14 @@ import { Markdown } from "@opencode/session-ui/markdown"
import { MarkdownProvider, useMarkdown } from "@opencode/session-ui/context/markdown"
import { artifactKind } from "@opencode/util/artifact"
import { getDirectory, getFilename } from "@opencode/util/path"
import { createKeyed, usePlugin, type FileContent, type MountedSession, type SessionScreen } from "@opencode/plugin/gui"
import {
createKeyed,
usePanel,
usePlugin,
type FileContent,
type MountedSession,
type SessionScreen,
} from "@opencode/plugin/gui"
import {
blobUrlFromContent,
bytesFromContent,
@ -726,11 +733,13 @@ function ArtifactAudio(props: MediaProps) {
function ArtifactFrame(props: { screen: SessionScreen; path: string; content: FileContent; kind: "pdf" | "html" }) {
let frame: HTMLIFrameElement | undefined
const panel = usePanel()
const source = createMediaSource(() =>
props.kind === "html" ? findableHtml(mediaHtml(props.content)) : props.content,
)
createFrameMedia({ frame: () => frame, path: () => props.path, file: props.screen.file })
createFrameMedia({ frame: () => frame, path: () => props.path, file: props.screen.file, open: panel.open })
// 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)

View file

@ -2,6 +2,7 @@ import { Option, Schema } from "effect"
import { makeEventListener } from "@solid-primitives/event-listener"
import type { FileContent, Files } from "@opencode/plugin/gui"
import { bytesFromContent, frameMediaPath, htmlWithScript } from "./artifact"
import { fileTabId } from "./path"
/** Marks messages between the app and the media script it adds to an HTML preview. */
const CHANNEL = "opencode-file-media"
@ -20,6 +21,8 @@ export function createFrameMedia(input: {
frame: () => HTMLIFrameElement | undefined
path: () => string
file: Files
/** The open file tabs' ids. */
open: () => readonly string[]
}) {
makeEventListener(window, "message", async (event: MessageEvent) => {
const target = input.frame()?.contentWindow
@ -32,10 +35,11 @@ export function createFrameMedia(input: {
if (!path) return
// An open tab may already hold the file. Otherwise load it and stop the watch `sync` starts, as nothing shows it.
if (!input.file.get(path)?.content) {
await input.file.sync(path, { silent: true })
input.file.unwatch(path)
// `sync` starts a server watch that only an open tab of the file needs.
if (!input.open().includes(fileTabId(input.file, path))) input.file.unwatch(path)
}
const content = input.file.get(path)?.content