fix(session-ui): relabel code copy buttons only when the language changes (#60538)

This commit is contained in:
Luke Parker
2026-10-30 18:30:46 +10:00
committed by GitHub
Unverified
parent 5ff0edecb8
commit 1e03c36bb6
3 changed files with 60 additions and 20 deletions
@@ -1,6 +1,7 @@
import { createSignal, Show } from "solid-js"
import { render } from "solid-js/web"
import { DialogProvider } from "@opencode/ui/context/dialog"
import { I18nProvider, useI18n } from "@opencode/ui/context/i18n"
import { Markdown } from "../src/components/markdown"
import { preloadMarkdown } from "../src/components/markdown-cache"
import { MarkdownProvider, type MarkdownLocalFileExists } from "../src/context/markdown"
@@ -44,6 +45,8 @@ export async function mountMarkdown(options: {
const [text, setText] = createSignal(options.text)
const [streaming, setStreaming] = createSignal(options.streaming ?? false)
const [visible, setVisible] = createSignal(true)
const [german, setGerman] = createSignal(false)
const english = useI18n()
return (
<DialogProvider>
@@ -54,25 +57,40 @@ export async function mountMarkdown(options: {
checked={streaming()}
onChange={(event) => setStreaming(event.currentTarget.checked)}
/>
<input
aria-label="German"
type="checkbox"
checked={german()}
onChange={(event) => setGerman(event.currentTarget.checked)}
/>
<button onClick={() => setVisible((value) => !value)}>Toggle Markdown</button>
<MarkdownProvider
readImage={(path, signal) =>
options.images ? readLocalImage(api, "C:/project", path, signal) : Promise.resolve(undefined)
}
localFileExists={(path) =>
options.localFileExists?.(path) ??
(options.synchronousFiles ? existingFiles.has(path) : Promise.resolve(existingFiles.has(path)))
}
<I18nProvider
value={{
locale: () => (german() ? "de" : "en"),
t: (key, params) => (german() && key === "ui.message.copy" ? "Kopieren" : english.t(key, params)),
plural: english.plural,
pluralForm: english.pluralForm,
}}
>
<Show when={visible()}>
<Markdown
text={text()}
streaming={streaming()}
cacheKey={options.cached ? "markdown-test" : undefined}
deferUntilReady
/>
</Show>
</MarkdownProvider>
<MarkdownProvider
readImage={(path, signal) =>
options.images ? readLocalImage(api, "C:/project", path, signal) : Promise.resolve(undefined)
}
localFileExists={(path) =>
options.localFileExists?.(path) ??
(options.synchronousFiles ? existingFiles.has(path) : Promise.resolve(existingFiles.has(path)))
}
>
<Show when={visible()}>
<Markdown
text={text()}
streaming={streaming()}
cacheKey={options.cached ? "markdown-test" : undefined}
deferUntilReady
/>
</Show>
</MarkdownProvider>
</I18nProvider>
</DialogProvider>
)
}, host)
@@ -763,6 +763,21 @@ story("replaces completed DOM before live rendering and retains streamed code co
await expect(markdown).toHaveCount(0)
})
story("relabels streamed code copy actions when the language changes", async ({ page }) => {
await page.evaluate(async (fixture) => {
const { mountMarkdown } = await import(fixture)
await mountMarkdown({ text: "```sh\necho hello\n", streaming: true })
}, fixture)
const harness = page.getByTestId("markdown-fixture")
const markdown = harness.locator('[data-component="markdown"]')
await expect(markdown.getByRole("button", { name: "Copy" })).toHaveCount(1)
await harness.getByLabel("Markdown text").fill("```sh\necho hello\n```\n\nStreaming continues")
await expect(markdown.locator("p")).toHaveText("Streaming continues")
await harness.getByLabel("German").check()
await expect(markdown.getByRole("button", { name: "Kopieren" })).toHaveCount(1)
await expect(markdown.getByRole("button", { name: "Copy" })).toHaveCount(0)
})
story("highlights streamed code across comment and string boundaries", async ({ page }) => {
await page.evaluate(async (fixture) => {
const { mountMarkdown } = await import(fixture)
@@ -642,6 +642,7 @@ export function Markdown(
const completedCode = new Map<string, Extract<RenderedBlock, { mode: "code" }>>()
const settledMarkdown = new Map<string, Exclude<RenderedBlock, { mode: "code" }>>()
let renderedBlocks: RenderedBlock[] = []
let appliedLabels: CopyLabels | undefined
let streamed = false
const [projection] = createResource(
@@ -883,9 +884,15 @@ export function Markdown(
container.querySelectorAll<HTMLImageElement>("img[data-markdown-favicon]").forEach((image) => {
if (image.complete && image.naturalWidth > 0) image.dataset.loaded = ""
})
container
.querySelectorAll<HTMLElement>('[data-slot="markdown-copy-button"]')
.forEach((button) => setCopyState(button, labels, button.dataset.copied === "true"))
// A button gets the labels of the run that creates it, so the others only need new ones when the language changes.
// Relabelling every button on each streamed delta re-rendered each one's tooltip and icon button.
if (appliedLabels?.copy !== labels.copy || appliedLabels?.copied !== labels.copied)
container
.querySelectorAll<HTMLElement>('[data-slot="markdown-copy-button"]')
.forEach((button) => setCopyState(button, labels, button.dataset.copied === "true"))
appliedLabels = labels
if (!copyCleanup)
copyCleanup = setupCodeCopy(container, () => ({