From 9e0786cdcc90cf39ec6582586e27cb53115a341d Mon Sep 17 00:00:00 2001 From: Dan Smolsky Date: Tue, 17 Nov 2026 21:40:39 -0500 Subject: [PATCH] fix(app): keep an open tab's file watch when an HTML preview loads the file --- packages/gui-plugins/src/file/artifact-view.tsx | 13 +++++++++++-- packages/gui-plugins/src/file/frame-media.ts | 8 ++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/gui-plugins/src/file/artifact-view.tsx b/packages/gui-plugins/src/file/artifact-view.tsx index 70b95d76238..e3f73431435 100644 --- a/packages/gui-plugins/src/file/artifact-view.tsx +++ b/packages/gui-plugins/src/file/artifact-view.tsx @@ -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) diff --git a/packages/gui-plugins/src/file/frame-media.ts b/packages/gui-plugins/src/file/frame-media.ts index be47acad7fa..d3e876d2b29 100644 --- a/packages/gui-plugins/src/file/frame-media.ts +++ b/packages/gui-plugins/src/file/frame-media.ts @@ -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