Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion packages/workshop-frontend/src/ChatInterface.markdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, describe, expect, it } from "vitest";
import { afterEach, describe, expect, it, vi } from "vitest";
import { MarkdownMessage } from "./ChatInterface";

(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
Expand All @@ -14,6 +14,7 @@ describe("MarkdownMessage line breaks", () => {
afterEach(async () => {
if (root) await act(async () => root.unmount());
container?.remove();
vi.unstubAllGlobals();
});

async function render(message: string) {
Expand Down Expand Up @@ -43,4 +44,19 @@ describe("MarkdownMessage line breaks", () => {
expect(paragraphs[0].textContent).toBe("para one");
expect(paragraphs[1].textContent).toBe("para two");
});

it("copies fenced code without the Markdown trailing newline", async () => {
const writeText = vi.fn<(text: string) => Promise<void>>(async () => undefined);
vi.stubGlobal("navigator", { clipboard: { writeText } });
await render("```ts\nconst answer = 42;\n```");

const button = container.querySelector<HTMLButtonElement>(
'button[aria-label="Copy code"]',
);
expect(button?.title).toBe("Copy code");

await act(async () => button?.click());

expect(writeText).toHaveBeenCalledWith("const answer = 42;");
});
});
46 changes: 44 additions & 2 deletions packages/workshop-frontend/src/ChatInterface.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@
font-size: 0.85em;
}

.codeBlock {
position: relative;
margin: 0.5em 0;
}

.markdownContent pre {
background-color: var(--color-kumo-elevated);
border: 1px solid var(--color-kumo-line);
border-radius: 8px;
padding: 10px 12px;
padding: 10px 44px 10px 12px;
overflow-x: auto;
margin: 0.5em 0;
margin: 0;
}
.markdownContent pre code {
background: none;
Expand All @@ -43,6 +48,43 @@
font-size: 0.8em;
}

.codeCopyButton {
position: absolute;
top: 8px;
right: 8px;
z-index: 1;
display: flex;
padding: 4px;
border: 1px solid var(--color-kumo-line);
border-radius: 5px;
background-color: var(--color-kumo-base);
box-shadow: 0 1px 2px color-mix(in srgb, var(--text-color-kumo-default) 20%, transparent);
color: var(--text-color-kumo-subtle);
cursor: pointer;
opacity: 0.6;
transition: background-color 150ms ease-out, border-color 150ms ease-out,
box-shadow 150ms ease-out, color 150ms ease-out, opacity 150ms ease-out,
transform 100ms ease-out;
}

.codeCopyButton:hover,
.codeCopyButton:focus-visible {
background-color: var(--color-kumo-tint);
border-color: var(--color-kumo-fill);
color: var(--text-color-kumo-default);
opacity: 1;
}

.codeCopyButton:active {
box-shadow: none;
transform: translateY(1px);
}

.codeCopyButton:focus-visible {
outline: 2px solid var(--color-kumo-brand);
outline-offset: 1px;
}

.markdownContent ul,
.markdownContent ol {
margin: 0.4em 0;
Expand Down
26 changes: 26 additions & 0 deletions packages/workshop-frontend/src/ChatInterface.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { isTransientRpcError, logRpcFailure } from "./rpcErrors";
import {
Fragment,
isValidElement,
memo,
useState,
useEffect,
useLayoutEffect,
useRef,
useMemo,
useCallback,
type ComponentPropsWithoutRef,
type Dispatch,
type ReactNode,
type SetStateAction,
Expand Down Expand Up @@ -40,6 +42,7 @@ import {
ArrowsClockwise,
Lightning,
Copy,
Clipboard as ClipboardIcon,
WarningCircle,
Code,
File as FileIcon,
Expand Down Expand Up @@ -1237,10 +1240,33 @@ function FormatMention({ format }: { format: MessageFormatRef }) {
);
}

function CodeBlock({ children, ...props }: ComponentPropsWithoutRef<"pre">) {
const code = isValidElement<{ children?: ReactNode }>(children) &&
typeof children.props.children === "string"
? children.props.children.replace(/\n$/, "")
: "";

return (
<div className={styles.codeBlock}>
<pre {...props}>{children}</pre>
<button
type="button"
className={styles.codeCopyButton}
onClick={() => void copyToClipboard(code)}
aria-label="Copy code"
title="Copy code"
>
<ClipboardIcon size={16} />
</button>
</div>
);
}

function getMarkdownComponents(
mentionsByToken?: Map<string, Mention>,
): Components {
return {
pre: ({ node: _node, ...props }) => <CodeBlock {...props} />,
table: ({ node: _node, children, ...props }) => (
<div className={styles.markdownTableWrapper}>
<table {...props}>{children}</table>
Expand Down
Loading