-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve copy of console command examples
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function cleanupClipboardText(targetSelector) { | ||
const targetElement = document.querySelector(targetSelector); | ||
|
||
// exclude "Generic Prompt" and "Generic Output" spans from copy | ||
const excludedClasses = ["gp", "go"]; | ||
|
||
const clipboardText = Array.from(targetElement.childNodes) | ||
.filter(node => !excludedClasses.some((className) => node?.classList?.contains(className))) | ||
.map(node => node.textContent) | ||
.filter(s => s != ""); | ||
return clipboardText.join("").trim(); | ||
} | ||
|
||
// Sets copy text to attributes lazily using an Intersection Observer. | ||
function setCopyText() { | ||
// The `data-clipboard-text` attribute allows for customized content in the copy | ||
// See: https://www.npmjs.com/package/clipboard#copy-text-from-attribute | ||
const attr = 'clipboardText'; | ||
// all "copy" buttons whose target selector is a <code> element | ||
const elements = document.querySelectorAll( | ||
'button.md-clipboard[data-clipboard-target$="code"]' | ||
); | ||
const observer = new IntersectionObserver(entries => { | ||
entries.forEach(entry => { | ||
// target in the viewport that have not been patched | ||
if (entry.intersectionRatio > 0 && entry.target.dataset[attr] === undefined) { | ||
entry.target.dataset[attr] = cleanupClipboardText(entry.target.dataset.clipboardTarget); | ||
} | ||
}); | ||
}); | ||
|
||
elements.forEach(elt => { | ||
observer.observe(elt); | ||
}); | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
setCopyText(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters