Skip to content

Commit cf50ae5

Browse files
committed
fix(web): preserve Mermaid source selections
1 parent 38e6fa6 commit cf50ae5

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

‎apps/web/src/markdown-clipboard.test.ts‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
22

3-
import { serializeRenderedMarkdownFragment } from "./markdown-clipboard";
3+
import {
4+
chatMarkdownClipboardPayload,
5+
serializeRenderedMarkdownFragment,
6+
} from "./markdown-clipboard";
47
import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
58
import {
69
collectAssistantCitations,
@@ -13,13 +16,15 @@ const ELEMENT_NODE = 1;
1316
class FakeText {
1417
readonly nodeType = TEXT_NODE;
1518
readonly childNodes: ReadonlyArray<never> = [];
19+
parentElement: FakeElement | null = null;
1620

1721
constructor(readonly textContent: string) {}
1822
}
1923

2024
class FakeElement {
2125
readonly nodeType = ELEMENT_NODE;
2226
readonly childNodes: Array<FakeElement | FakeText> = [];
27+
parentElement: FakeElement | null = null;
2328
readonly classList = {
2429
contains: (name: string) => this.classNames.includes(name),
2530
};
@@ -43,10 +48,16 @@ class FakeElement {
4348
}
4449

4550
append(...children: Array<FakeElement | FakeText>): this {
51+
for (const child of children) child.parentElement = this;
4652
this.childNodes.push(...children);
4753
return this;
4854
}
4955

56+
appendChild<T extends FakeElement | FakeText>(child: T): T {
57+
this.append(child);
58+
return child;
59+
}
60+
5061
getAttribute(name: string): string | null {
5162
return this.attributes[name] ?? null;
5263
}
@@ -55,8 +66,20 @@ class FakeElement {
5566
return Object.hasOwn(this.attributes, name);
5667
}
5768

58-
closest(): FakeElement | null {
59-
return null;
69+
closest(selector: string): FakeElement | null {
70+
if (selector === "[data-markdown-copy]" && this.hasAttribute("data-markdown-copy")) {
71+
return this;
72+
}
73+
if (this.tagName === selector.toUpperCase()) return this;
74+
return this.parentElement?.closest(selector) ?? null;
75+
}
76+
77+
querySelectorAll(): ReadonlyArray<FakeElement> {
78+
return [];
79+
}
80+
81+
get innerHTML(): string {
82+
return this.textContent;
6083
}
6184

6285
/** Supports only the selectors markdown-clipboard actually asks for. */
@@ -262,4 +285,26 @@ describe("serializeRenderedMarkdownFragment", () => {
262285
"Hello World (Document template)",
263286
);
264287
});
288+
289+
it("uses an ancestor's explicit Markdown when a selection only clones its children", () => {
290+
const label = new FakeElement("TEXT").append(new FakeText("A"));
291+
new FakeElement("DIV", [], {
292+
"data-markdown-copy": "```mermaid\nflowchart LR\nA-->B\n```\n\n",
293+
}).append(new FakeElement("SVG").append(label));
294+
vi.stubGlobal("document", {
295+
createElement: () => new FakeElement("DIV"),
296+
});
297+
298+
const payload = chatMarkdownClipboardPayload({
299+
rangeCount: 1,
300+
getRangeAt: () => ({
301+
collapsed: false,
302+
cloneContents: () => new FakeElement("SVG").append(new FakeText("A")),
303+
commonAncestorContainer: label,
304+
toString: () => "A",
305+
}),
306+
} as unknown as Selection);
307+
308+
expect(payload?.text).toBe("```mermaid\nflowchart LR\nA-->B\n```\n\n");
309+
});
265310
});

‎apps/web/src/markdown-clipboard.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,14 @@ export function chatMarkdownClipboardPayload(
395395
const ancestor = range.commonAncestorContainer;
396396
const ancestorElement =
397397
ancestor.nodeType === Node.ELEMENT_NODE ? (ancestor as Element) : ancestor.parentElement;
398+
const markdownCopy = ancestorElement
399+
?.closest("[data-markdown-copy]")
400+
?.getAttribute("data-markdown-copy");
401+
if (markdownCopy != null) {
402+
texts.push(markdownCopy);
403+
htmls.push(sanitizedHtmlFrom(container));
404+
continue;
405+
}
398406
if (ancestorElement?.closest("pre")) {
399407
const text = range.toString();
400408
if (text) {

0 commit comments

Comments
 (0)