-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(platform): preview project name on top of DSN #13041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b763840
afe7fa7
7ed48e6
902e76b
e9efe46
550d07e
8117824
bf09870
1a27ff5
717b891
0ddebfc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,4 +150,5 @@ | |
border: none; | ||
color: var(--white); | ||
transition: opacity 150ms; | ||
z-index: 10000; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,15 +15,58 @@ export interface CodeBlockProps { | |
title?: string; | ||
} | ||
|
||
/** | ||
* | ||
* Copy `element`'s text children as long as long as they are not `.no-copy` | ||
*/ | ||
function getCopiableText(element: HTMLDivElement) { | ||
let text = ''; | ||
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, { | ||
acceptNode: function (node) { | ||
// Skip if parent has .no-copy class | ||
if (node.parentElement?.classList.contains('no-copy')) { | ||
return NodeFilter.FILTER_REJECT; | ||
} | ||
return NodeFilter.FILTER_ACCEPT; | ||
}, | ||
}); | ||
|
||
let node: Node | null; | ||
// eslint-disable-next-line no-cond-assign | ||
while ((node = walker.nextNode())) { | ||
text += node.textContent; | ||
} | ||
|
||
return text.trim(); | ||
} | ||
|
||
export function CodeBlock({filename, language, children}: CodeBlockProps) { | ||
const [showCopied, setShowCopied] = useState(false); | ||
const codeRef = useRef<HTMLDivElement>(null); | ||
|
||
// Show the copy button after js has loaded | ||
// otherwise the copy button will not work | ||
const [showCopyButton, setShowCopyButton] = useState(false); | ||
|
||
useEffect(() => { | ||
setShowCopyButton(true); | ||
// prevent .no-copy elements from being copied during selection Right click copy or / Cmd+C | ||
const noCopyElements = codeRef.current?.querySelectorAll<HTMLSpanElement>('.no-copy'); | ||
const handleSelectionChange = () => { | ||
// hide no copy elements within the selection | ||
const selection = window.getSelection(); | ||
noCopyElements?.forEach(element => { | ||
if (selection?.containsNode(element, true)) { | ||
element.style.display = 'none'; | ||
} else { | ||
element.style.display = 'inline'; | ||
} | ||
}); | ||
}; | ||
document.addEventListener('selectionchange', handleSelectionChange); | ||
return () => { | ||
document.removeEventListener('selectionchange', handleSelectionChange); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Selection Listener Fails with Dynamic ContentThe |
||
}, []); | ||
|
||
useCleanSnippetInClipboard(codeRef, {language}); | ||
|
@@ -33,7 +76,9 @@ export function CodeBlock({filename, language, children}: CodeBlockProps) { | |
return; | ||
} | ||
|
||
const code = cleanCodeSnippet(codeRef.current.innerText, {language}); | ||
const code = cleanCodeSnippet(getCopiableText(codeRef.current), { | ||
language, | ||
}); | ||
|
||
try { | ||
await navigator.clipboard.writeText(code); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this is really necessary, but should not interfere with anything else I think