diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index b48485fc61212..4affaa94566e5 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -203,6 +203,7 @@ import { createMarkdownRenderer } from "./chat/markdownRenderer"; import { splitMarkdownBlocks } from "./chat/markdownStreamBlocks"; import { MarkdownMermaidCodeBlock } from "./chat/MarkdownMermaidCodeBlock"; import { MarkdownTable } from "./chat/MarkdownTable"; +import { markdownTableMinWidth } from "./chat/markdownTableLayout"; import { MarkdownDetails } from "./chat/MarkdownDetails"; import { authoredImageSizeStyle, @@ -1660,8 +1661,8 @@ const CHAT_MARKDOWN_COMPONENTS = { } return ; }, - table: function MarkdownTableRenderer({ node: _node, ...props }) { - return ; + table: function MarkdownTableRenderer({ node, ...props }) { + return ; }, details: function MarkdownDetailsRenderer({ node: _node, children, open: detailsOpen, dir }) { return ( diff --git a/apps/web/src/components/chat/MarkdownTable.tsx b/apps/web/src/components/chat/MarkdownTable.tsx index 6bb72ff5ca578..ffe33d866e9cb 100644 --- a/apps/web/src/components/chat/MarkdownTable.tsx +++ b/apps/web/src/components/chat/MarkdownTable.tsx @@ -1,5 +1,5 @@ -import { CheckIcon, CopyIcon } from "lucide-react"; -import { useRef, type ComponentProps } from "react"; +import { CheckIcon, CopyIcon, Maximize2Icon } from "lucide-react"; +import { useRef, useState, type ComponentProps, type CSSProperties } from "react"; import { useCopyToClipboard } from "../../hooks/useCopyToClipboard"; import { @@ -7,13 +7,25 @@ import { serializeTableElementToMarkdown, } from "../../markdown-clipboard"; import { Button } from "../ui/button"; +import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog"; import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; import { ScrollArea } from "../ui/scroll-area"; import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; import { reportMarkdownActionFailure } from "./markdownActionFailure"; +import type { markdownTableMinWidth } from "./markdownTableLayout"; -export function MarkdownTable({ children, ...props }: ComponentProps<"table">) { - const containerRef = useRef(null); +type MarkdownTableProps = ComponentProps<"table"> & { + minWidth?: ReturnType; +}; + +function TableCopyMenu({ + getTable, + labeled = false, +}: { + getTable: () => HTMLTableElement | null | undefined; + /** A text button instead of an icon. Inside the expanded dialog a tooltip would take one Escape press of its own. */ + labeled?: boolean; +}) { const { copyToClipboard, isCopied: copied } = useCopyToClipboard<"markdown" | "csv">({ timeout: 1200, onError: (cause, format) => @@ -22,7 +34,7 @@ export function MarkdownTable({ children, ...props }: ComponentProps<"table">) { const copyLabel = copied ? "Copied" : "Copy table"; const handleCopy = (format: "markdown" | "csv") => { - const table = containerRef.current?.querySelector("table"); + const table = getTable(); if (!table) return; copyToClipboard( format === "markdown" @@ -32,39 +44,103 @@ export function MarkdownTable({ children, ...props }: ComponentProps<"table">) { ); }; + const icon = copied ? : ; + return ( + + {labeled ? ( + }> + {icon} + {copied ? "Copied" : "Copy"} + + ) : ( + + + } + /> + } + > + {icon} + + {copyLabel} + + )} + + handleCopy("markdown")}>Copy as Markdown + handleCopy("csv")}>Copy as CSV + + + ); +} + +export function MarkdownTable({ children, minWidth, style, ...props }: MarkdownTableProps) { + const containerRef = useRef(null); + const expandedRef = useRef(null); + const [expanded, setExpanded] = useState(false); + const tableStyle = minWidth + ? ({ + ...style, + "--markdown-table-floor": `calc(${minWidth.ch}ch + ${minWidth.columns - 1} * 1.5rem)`, + } as CSSProperties) + : style; + return (
- - {children}
-
+
+ + + {children} +
+
+
{/* Stays within the row's px-1: the chat lane clips sideways overflow. */} -
- +
+ - } +
+ + containerRef.current?.querySelector("table")} />
+ + +
+ Table +
+ expandedRef.current?.querySelector("table")} /> +
+
+
+ + {children} +
+
+
+
); } diff --git a/apps/web/src/components/chat/TimelineAssistantRows.tsx b/apps/web/src/components/chat/TimelineAssistantRows.tsx index b62f6f8e4b10f..7a29cc7f4c924 100644 --- a/apps/web/src/components/chat/TimelineAssistantRows.tsx +++ b/apps/web/src/components/chat/TimelineAssistantRows.tsx @@ -53,7 +53,7 @@ export function AssistantTimelineRow({ row }: { row: Extract
T3 Code -
+
node.type === "element" && node.tagName === "table", + ); + if (!table) throw new Error("no table"); + return table; +} + +describe("markdownTableMinWidth", () => { + it("keeps short columns at their longest cell and caps long ones at the floor", () => { + const table = tableFrom( + [ + "| # | Approach | Notes |", + "| - | --- | --- |", + "| 1 | Breakout | Grows past the reading column into the free pane width |", + "| 12 | Scroll | **Keeps** the column |", + ].join("\n"), + ); + expect(markdownTableMinWidth(table)).toEqual({ + ch: 2 + "Breakout".length + MARKDOWN_TABLE_COLUMN_FLOOR_CH, + columns: 3, + }); + const spaced = tableFrom(["| A |", "| - |", "| **a** **b** **c** **d** |"].join("\n")); + expect(markdownTableMinWidth(spaced)).toEqual({ ch: "a b c d".length, columns: 1 }); + }); + + it("sums a floor per column, so many long columns outgrow a narrow lane", () => { + const cell = "a long explanation that would otherwise wrap into a tall block"; + const row = `| ${Array.from({ length: 7 }, () => cell).join(" | ")} |`; + const table = tableFrom( + [ + `| ${Array.from({ length: 7 }, (_, index) => `Column ${index}`).join(" | ")} |`, + `| ${Array.from({ length: 7 }, () => "---").join(" | ")} |`, + row, + ].join("\n"), + ); + expect(markdownTableMinWidth(table)).toEqual({ + ch: 7 * MARKDOWN_TABLE_COLUMN_FLOOR_CH, + columns: 7, + }); + }); + + it("returns null without a table", () => { + expect(markdownTableMinWidth(undefined)).toBeNull(); + }); +}); diff --git a/apps/web/src/components/chat/markdownTableLayout.ts b/apps/web/src/components/chat/markdownTableLayout.ts new file mode 100644 index 0000000000000..8a5e9776141c3 --- /dev/null +++ b/apps/web/src/components/chat/markdownTableLayout.ts @@ -0,0 +1,47 @@ +import type { Element, ElementContent } from "hast"; + +/** Widest a column's floor gets, in characters; longer cells wrap above it. */ +export const MARKDOWN_TABLE_COLUMN_FLOOR_CH = 16; + +function textContent(node: ElementContent): string { + if (node.type === "text") return node.value; + if (node.type !== "element") return ""; + let text = ""; + for (const child of node.children) text += textContent(child); + return text; +} + +function* tableRows(node: Element): Generator { + for (const child of node.children) { + if (child.type !== "element") continue; + if (child.tagName === "tr") yield child; + else if (child.tagName === "thead" || child.tagName === "tbody" || child.tagName === "tfoot") { + yield* tableRows(child); + } + } +} + +/** + * The narrowest a table can get before its cells turn into tall, hard-to-scan + * blocks: each column keeps room for its longest cell, up to the floor. Narrow + * columns such as numbers keep their natural width. Returns the summed floor in + * `ch` and the column count, or null when the table has no cells. + */ +export function markdownTableMinWidth( + table: Element | undefined, +): { readonly ch: number; readonly columns: number } | null { + if (!table) return null; + const longest: number[] = []; + for (const row of tableRows(table)) { + let column = 0; + for (const cell of row.children) { + if (cell.type !== "element" || (cell.tagName !== "th" && cell.tagName !== "td")) continue; + longest[column] = Math.max(longest[column] ?? 0, textContent(cell).trim().length); + column += 1; + } + } + if (longest.length === 0) return null; + let ch = 0; + for (const length of longest) ch += Math.min(length, MARKDOWN_TABLE_COLUMN_FLOOR_CH); + return { ch, columns: longest.length }; +} diff --git a/apps/web/src/index.css b/apps/web/src/index.css index 771c714b1ff8a..5b4655877e57d 100644 --- a/apps/web/src/index.css +++ b/apps/web/src/index.css @@ -2258,10 +2258,12 @@ code { /* Diagnostics-style tables: row separators only, and a scroll-fade container for horizontal overflow. Cells wrap and the table fits its lane. The root chat-markdown wrapping rules (overflow-wrap: anywhere) would let columns - shrink to single characters, so restore word-boundary wrapping: the min - column width is the longest word, and only wider content scrolls. */ + shrink to single characters, so restore word-boundary wrapping. Each column + also keeps a floor (MarkdownTable's --markdown-table-floor) so many long + columns scroll instead of collapsing into tall, narrow blocks. */ .chat-markdown table { width: 100%; + min-width: var(--markdown-table-floor, 0); border-collapse: collapse; overflow-wrap: normal; word-break: normal; @@ -2308,6 +2310,82 @@ code { max-width: none; } +/* Expand only helps a table that does not fit. */ +.chat-markdown-table-container:not(:has([data-has-overflow-x])) .chat-markdown-table-expand { + display: none; +} + +/* A scrolled table keeps its first column in view. The left edge fade would + dim that column, so only the right edge fades. */ +.chat-markdown-table-container [data-has-overflow-x] { + --tw-mask-left: linear-gradient(#000, #000); +} + +.chat-markdown-table-container [data-has-overflow-x] :is(th, td):first-child, +.chat-markdown-table-expanded :is(th, td):first-child { + position: sticky; + inset-inline-start: 0; + z-index: 1; + background: var(--background); + box-shadow: inset -1px 0 0 color-mix(in srgb, var(--contrast-border) 60%, transparent); +} + +.chat-markdown-table-expanded thead th { + position: sticky; + top: 0; + z-index: 2; + background: var(--background); + box-shadow: inset 0 -1px 0 color-mix(in srgb, var(--contrast-border) 60%, transparent); +} + +.chat-markdown-table-expanded thead th:first-child { + z-index: 3; + box-shadow: + inset -1px 0 0 color-mix(in srgb, var(--contrast-border) 60%, transparent), + inset 0 -1px 0 color-mix(in srgb, var(--contrast-border) 60%, transparent); +} + +/* Edge padding lives on the cells: sticky cells pin to the scroller's padding + edge, so scroller padding would let scrolled columns show beside them. */ +.chat-markdown-table-expanded :is(th, td) { + max-width: 32rem; +} + +.chat-markdown-table-expanded :is(th, td):first-child { + padding-inline-start: 1rem; +} + +.chat-markdown-table-expanded :is(th, td):last-child { + padding-inline-end: 1rem; +} + +/* Assistant replies let a wide table grow past the reading column, centred on + it, up to the timeline row (the pane minus its gutters). Prose stays at the + chat width. Past the row width the table wraps and then scrolls as before. */ +.messages-timeline-row-frame { + container: chat-row / inline-size; +} + +@container chat-row (width > 0) { + .chat-content-lane:has([data-chat-wide-content] .chat-markdown-table-breakout) { + overflow-x: visible; + } + + [data-chat-wide-content] .chat-markdown > .chat-markdown-table-container { + .chat-markdown-table-breakout { + display: flex; + justify-content: center; + } + + .chat-markdown-table-breakout > :first-child { + flex: none; + width: max-content; + min-width: 100%; + max-width: 100cqw; + } + } +} + /* Keep overflow discoverable when the edge fade falls between columns. */ .chat-markdown .chat-markdown-table-container