Skip to content

Commit cbe3116

Browse files
feat(ui): add Markdown table interactions (#125245)
* feat(ui): add Markdown table interactions * fix(ui): scope table interactions to transcripts * fix(ui): complete table interaction contracts * chore: refresh PR head * fix(ui): satisfy table interaction gates --------- Co-authored-by: RoboClaw <[email protected]>
1 parent e97b42a commit cbe3116

11 files changed

Lines changed: 657 additions & 6 deletions

File tree

‎ui/src/components/markdown-parser.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from "./markdown-file-links.ts";
2323
import type { MarkdownRenderEnv } from "./markdown-render-options.ts";
2424
import { installMarkdownSessionLinks, SESSION_LINK_SCAN_RE } from "./markdown-session-links.ts";
25+
import { installMarkdownTables } from "./markdown-tables.ts";
2526
import { escapeMarkdownHtml } from "./markdown-text.ts";
2627

2728
const INLINE_DATA_IMAGE_RE = /^data:image\/[a-z0-9.+-]+;base64,/i;
@@ -134,6 +135,7 @@ export function createMarkdownParser(): MarkdownIt {
134135
markdownParser.enable("strikethrough");
135136
installAssistantTranscriptRoleMarkdown(markdownParser, escapeMarkdownHtml);
136137
installMarkdownDetails(markdownParser);
138+
installMarkdownTables(markdownParser);
137139

138140
// Disable fuzzy link detection to prevent bare filenames like "README.md"
139141
// from being auto-linked as "http://README.md". URLs with explicit protocol

‎ui/src/components/markdown-render-options.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
type MarkdownCodeBlockChrome = "copy" | "none";
2+
type MarkdownTableInteractions = "enabled" | "none";
23
type MarkdownRenderMode = "document" | "message";
34

45
export type MarkdownRenderOptions = {
@@ -9,6 +10,7 @@ export type MarkdownRenderOptions = {
910
progressBars?: boolean;
1011
mode?: MarkdownRenderMode;
1112
sessionLinks?: boolean;
13+
tableInteractions?: MarkdownTableInteractions;
1214
};
1315

1416
export type MarkdownRenderEnv = Required<MarkdownRenderOptions>;
@@ -24,5 +26,6 @@ export function normalizeMarkdownRenderOptions(
2426
progressBars: options.progressBars ?? false,
2527
mode: options.mode ?? "message",
2628
sessionLinks: options.sessionLinks ?? false,
29+
tableInteractions: options.tableInteractions ?? "none",
2730
};
2831
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/* @vitest-environment jsdom */
2+
3+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4+
import {
5+
enhanceMarkdownTables,
6+
handleMarkdownTableInteraction,
7+
releaseMarkdownTables,
8+
} from "./markdown-tables.ts";
9+
import { toSanitizedMarkdownHtml } from "./markdown.ts";
10+
11+
const { copyToClipboard } = vi.hoisted(() => ({
12+
copyToClipboard: vi.fn(async () => true),
13+
}));
14+
15+
vi.mock("../lib/clipboard.ts", () => ({ copyToClipboard }));
16+
17+
const markdown = `Open agent:main:dashboard:table
18+
19+
<progress value="3" max="7"></progress>
20+
21+
| Name | Value |
22+
| --- | --- |
23+
| Alpha | One |`;
24+
25+
class TestMutationObserver {
26+
static instances: TestMutationObserver[] = [];
27+
readonly disconnect = vi.fn();
28+
readonly observe = vi.fn();
29+
30+
constructor(readonly callback: MutationCallback) {
31+
TestMutationObserver.instances.push(this);
32+
}
33+
}
34+
35+
class TestResizeObserver {
36+
static instances: TestResizeObserver[] = [];
37+
readonly disconnect = vi.fn();
38+
readonly observe = vi.fn();
39+
readonly unobserve = vi.fn();
40+
41+
constructor(readonly callback: ResizeObserverCallback) {
42+
TestResizeObserver.instances.push(this);
43+
}
44+
}
45+
46+
function interactiveOwner(): {
47+
owner: HTMLElement;
48+
shell: HTMLElement;
49+
viewport: HTMLElement;
50+
} {
51+
const owner = document.createElement("div");
52+
owner.className = "chat-thread";
53+
owner.innerHTML = `<div class="chat-text">${toSanitizedMarkdownHtml(markdown, {
54+
progressBars: true,
55+
sessionLinks: true,
56+
tableInteractions: "enabled",
57+
})}</div>`;
58+
document.body.append(owner);
59+
const shell = owner.querySelector<HTMLElement>(".markdown-table")!;
60+
const viewport = owner.querySelector<HTMLElement>(".markdown-table__viewport")!;
61+
Object.defineProperties(viewport, {
62+
clientWidth: { configurable: true, value: 100 },
63+
scrollLeft: { configurable: true, value: 0, writable: true },
64+
scrollWidth: { configurable: true, value: 300 },
65+
});
66+
owner.addEventListener("click", handleMarkdownTableInteraction);
67+
enhanceMarkdownTables(owner);
68+
return { owner, shell, viewport };
69+
}
70+
71+
describe("Markdown table interactions", () => {
72+
beforeEach(() => {
73+
TestMutationObserver.instances = [];
74+
TestResizeObserver.instances = [];
75+
vi.stubGlobal("MutationObserver", TestMutationObserver);
76+
vi.stubGlobal("ResizeObserver", TestResizeObserver);
77+
copyToClipboard.mockClear();
78+
copyToClipboard.mockResolvedValue(true);
79+
Object.defineProperty(HTMLDialogElement.prototype, "showModal", {
80+
configurable: true,
81+
value: vi.fn(function (this: HTMLDialogElement) {
82+
this.setAttribute("open", "");
83+
}),
84+
});
85+
Object.defineProperty(HTMLDialogElement.prototype, "close", {
86+
configurable: true,
87+
value: vi.fn(function (this: HTMLDialogElement) {
88+
this.removeAttribute("open");
89+
this.dispatchEvent(new Event("close"));
90+
}),
91+
});
92+
});
93+
94+
afterEach(() => {
95+
vi.useRealTimers();
96+
vi.unstubAllGlobals();
97+
document.body.replaceChildren();
98+
});
99+
100+
it("composes table chrome with session links and progress markup", () => {
101+
const disabled = toSanitizedMarkdownHtml(markdown, {
102+
progressBars: true,
103+
sessionLinks: true,
104+
});
105+
const enabled = toSanitizedMarkdownHtml(markdown, {
106+
progressBars: true,
107+
sessionLinks: true,
108+
tableInteractions: "enabled",
109+
});
110+
111+
expect(disabled).not.toContain("data-table-interactions");
112+
expect(enabled).toContain("data-table-interactions");
113+
expect(enabled).toContain('data-session-key="agent:main:dashboard:table"');
114+
expect(enabled).toContain('<progress value="3" max="7"></progress>');
115+
});
116+
117+
it("tracks hidden columns in both scroll directions", () => {
118+
const { shell, viewport } = interactiveOwner();
119+
120+
expect(shell.classList.contains("markdown-table--can-scroll-left")).toBe(false);
121+
expect(shell.classList.contains("markdown-table--can-scroll-right")).toBe(true);
122+
123+
viewport.scrollLeft = 100;
124+
viewport.dispatchEvent(new Event("scroll"));
125+
expect(shell.classList.contains("markdown-table--can-scroll-left")).toBe(true);
126+
expect(shell.classList.contains("markdown-table--can-scroll-right")).toBe(true);
127+
128+
viewport.scrollLeft = 200;
129+
viewport.dispatchEvent(new Event("scroll"));
130+
expect(shell.classList.contains("markdown-table--can-scroll-left")).toBe(true);
131+
expect(shell.classList.contains("markdown-table--can-scroll-right")).toBe(false);
132+
});
133+
134+
it("copies TSV and restores focus after the table dialog closes", async () => {
135+
vi.useFakeTimers();
136+
const { owner } = interactiveOwner();
137+
const copy = owner.querySelector<HTMLButtonElement>(".markdown-table__copy")!;
138+
copy.click();
139+
await vi.waitFor(() => {
140+
expect(copyToClipboard).toHaveBeenCalledWith("Name\tValue\nAlpha\tOne");
141+
});
142+
expect(copy.getAttribute("aria-label")).toBe("Copied!");
143+
144+
const expand = owner.querySelector<HTMLButtonElement>(".markdown-table__expand")!;
145+
expand.focus();
146+
expand.click();
147+
const dialog = document.querySelector<HTMLDialogElement>(".markdown-table-dialog")!;
148+
expect(dialog.hasAttribute("open")).toBe(true);
149+
expect(dialog.querySelector("table")?.textContent).toContain("Alpha");
150+
151+
dialog.querySelector<HTMLButtonElement>(".markdown-table-dialog__close")!.click();
152+
expect(document.querySelector(".markdown-table-dialog")).toBeNull();
153+
expect(document.activeElement).toBe(expand);
154+
});
155+
156+
it("disconnects observers when the transcript owner is released", () => {
157+
const { owner } = interactiveOwner();
158+
const mutation = TestMutationObserver.instances.at(-1)!;
159+
const resize = TestResizeObserver.instances.at(-1)!;
160+
161+
releaseMarkdownTables(owner);
162+
163+
expect(mutation.disconnect).toHaveBeenCalledOnce();
164+
expect(resize.disconnect).toHaveBeenCalledOnce();
165+
});
166+
});

0 commit comments

Comments
 (0)