Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(web): copy code block selections without markdown #9175

Closedflamboh(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/9175/headopened View on GitHub

Showing 1 changed file with 14 additions and 4 deletions.

x
0 of 1 viewed, 1 left
14 additions and 4 deletionsapps/web/src/markdown-clipboard.tsModified
View file
@@ -309,6 +309,19 @@
return `<meta charset="utf-8">${container.innerHTML}`;
​
+function isSelectionInsideSingleCodeBlock(range: Range, container: Element): boolean {
+ const ancestor = range.commonAncestorContainer;
+ const ancestorElement =
+ ancestor.nodeType === Node.ELEMENT_NODE ? (ancestor as Element) : ancestor.parentElement;
+ if (ancestorElement?.closest("pre")) return true;
+​
+ const remainder = container.cloneNode(true) as Element;
+ const codeBlocks = remainder.querySelectorAll("pre");
+ if (codeBlocks.length !== 1) return false;
+ codeBlocks[0]?.remove();
+ return serializeRenderedMarkdownFragment(remainder) === "";
+}
+​
export function chatMarkdownClipboardPayload(
selection: Selection,
): MarkdownClipboardPayload | null {
@@ -319,10 +332,7 @@
if (range.collapsed) continue;
const container = document.createElement("div");
container.appendChild(range.cloneContents());
- const ancestor = range.commonAncestorContainer;
- const ancestorElement =
- ancestor.nodeType === Node.ELEMENT_NODE ? (ancestor as Element) : ancestor.parentElement;
- if (ancestorElement?.closest("pre")) {
+ if (isSelectionInsideSingleCodeBlock(range, container)) {