diff --git a/packages/session-ui/component-tests/markdown.fixture.tsx b/packages/session-ui/component-tests/markdown.fixture.tsx index 0b6a2c13e8e..c550cb43e85 100644 --- a/packages/session-ui/component-tests/markdown.fixture.tsx +++ b/packages/session-ui/component-tests/markdown.fixture.tsx @@ -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 ( @@ -54,25 +57,40 @@ export async function mountMarkdown(options: { checked={streaming()} onChange={(event) => setStreaming(event.currentTarget.checked)} /> + setGerman(event.currentTarget.checked)} + /> - - 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))) - } + (german() ? "de" : "en"), + t: (key, params) => (german() && key === "ui.message.copy" ? "Kopieren" : english.t(key, params)), + plural: english.plural, + pluralForm: english.pluralForm, + }} > - - - - + + 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))) + } + > + + + + + ) }, host) diff --git a/packages/session-ui/component-tests/markdown.spec.ts b/packages/session-ui/component-tests/markdown.spec.ts index 5d990792d8c..f6c28096648 100644 --- a/packages/session-ui/component-tests/markdown.spec.ts +++ b/packages/session-ui/component-tests/markdown.spec.ts @@ -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) diff --git a/packages/session-ui/src/components/markdown.tsx b/packages/session-ui/src/components/markdown.tsx index 69d7b621f83..b14492f3e09 100644 --- a/packages/session-ui/src/components/markdown.tsx +++ b/packages/session-ui/src/components/markdown.tsx @@ -642,6 +642,7 @@ export function Markdown( const completedCode = new Map>() const settledMarkdown = new Map>() let renderedBlocks: RenderedBlock[] = [] + let appliedLabels: CopyLabels | undefined let streamed = false const [projection] = createResource( @@ -883,9 +884,15 @@ export function Markdown( container.querySelectorAll("img[data-markdown-favicon]").forEach((image) => { if (image.complete && image.naturalWidth > 0) image.dataset.loaded = "" }) - container - .querySelectorAll('[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('[data-slot="markdown-copy-button"]') + .forEach((button) => setCopyState(button, labels, button.dataset.copied === "true")) + + appliedLabels = labels if (!copyCleanup) copyCleanup = setupCodeCopy(container, () => ({