Skip to content

Commit 19108f7

Browse files
samhvw8daniel-lxs
andauthored
Fix Copy button logic (#4458)
* refactor: streamline text copying by removing history highlight spans * refactor: improve regex for stripping history highlight spans to handle all content types --------- Co-authored-by: Daniel Riccio <[email protected]>
1 parent 360b0e7 commit 19108f7

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

webview-ui/src/components/history/CopyButton.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,30 @@ type CopyButtonProps = {
1010
className?: string
1111
}
1212

13+
/**
14+
* Strips only history highlight spans from text while preserving other HTML
15+
* Targets: <span class="history-item-highlight">content</span>
16+
* @param text - Text that may contain highlight spans
17+
* @returns Text with highlight spans removed but content preserved
18+
*/
19+
const stripHistoryHighlightSpans = (text: string): string => {
20+
// Match opening tag, capture content until closing tag
21+
// The [\s\S]*? pattern matches any character (including newlines) non-greedily,
22+
// which properly handles content with < characters
23+
return text.replace(/<span\s+class="history-item-highlight">([\s\S]*?)<\/span>/g, "$1")
24+
}
25+
1326
export const CopyButton = ({ itemTask, className }: CopyButtonProps) => {
1427
const { isCopied, copy } = useClipboard()
1528
const { t } = useAppTranslation()
1629

1730
const onCopy = useCallback(
1831
(e: React.MouseEvent) => {
1932
e.stopPropagation()
20-
const tempDiv = document.createElement("div")
21-
tempDiv.innerHTML = itemTask
22-
const text = tempDiv.textContent || tempDiv.innerText || ""
23-
2433
if (!isCopied) {
25-
copy(text)
34+
// Strip only history highlight spans before copying to clipboard
35+
const cleanText = stripHistoryHighlightSpans(itemTask)
36+
copy(cleanText)
2637
}
2738
},
2839
[isCopied, copy, itemTask],

0 commit comments

Comments
 (0)