mirror of
https://github.com/jbowdre/runtimeterror.git
synced 2024-11-22 23:12:19 +00:00
get the code copy button working again
This commit is contained in:
parent
5ae7f2f132
commit
edb1f01c5d
3 changed files with 16 additions and 8 deletions
|
@ -16,10 +16,18 @@ function createCopyButton(highlightDiv) {
|
||||||
document.querySelectorAll(".highlight").forEach((highlightDiv) => createCopyButton(highlightDiv));
|
document.querySelectorAll(".highlight").forEach((highlightDiv) => createCopyButton(highlightDiv));
|
||||||
|
|
||||||
async function copyCodeToClipboard(button, highlightDiv) {
|
async function copyCodeToClipboard(button, highlightDiv) {
|
||||||
// Need to get just the last-child of each .line element to avoid copying the line numbers
|
// capture all code lines in the selected block which aren't classed `cmd_return`
|
||||||
const nodeListToCopy = highlightDiv.querySelectorAll(":last-child > .chroma > code > .line > :last-child");
|
let codeToCopy = highlightDiv.querySelectorAll(":last-child > .torchlight > code > .line:not(.cmd_return)");
|
||||||
// Reduce the nodeList to a string of text with each line separated by a newline
|
// now remove the first-child of each line which has class `line-number`
|
||||||
const codeToCopy = Array.from(nodeListToCopy).reduce((accumulator, line) => accumulator + line.innerText, "");
|
codeToCopy = Array.from(codeToCopy).reduce((accumulator, line) => {
|
||||||
|
if (line.firstChild.className != "line-number") {
|
||||||
|
return accumulator + line.innerText + "\n"; }
|
||||||
|
else {
|
||||||
|
return accumulator + Array.from(line.children).filter(
|
||||||
|
(child) => child.className != "line-number").reduce(
|
||||||
|
(accumulator, child) => accumulator + child.innerText, "") + "\n";
|
||||||
|
}
|
||||||
|
}, "");
|
||||||
try {
|
try {
|
||||||
var result = await navigator.permissions.query({ name: "clipboard-write" });
|
var result = await navigator.permissions.query({ name: "clipboard-write" });
|
||||||
if (result.state == "granted" || result.state == "prompt") {
|
if (result.state == "granted" || result.state == "prompt") {
|
||||||
|
|
|
@ -4,6 +4,6 @@
|
||||||
<br><a target="_blank" href="https://github.com/jbowdre/runtimeterror">View source</a>.</p>
|
<br><a target="_blank" href="https://github.com/jbowdre/runtimeterror">View source</a>.</p>
|
||||||
|
|
||||||
{{ if (findRE "<pre" .Content 1) }}
|
{{ if (findRE "<pre" .Content 1) }}
|
||||||
<!-- {{ $jsCopy := resources.Get "js/code-copy-button.js" | minify }}
|
{{ $jsCopy := resources.Get "js/code-copy-button.js" | minify }}
|
||||||
<script src="{{ $jsCopy.RelPermalink }}"></script> -->
|
<script src="{{ $jsCopy.RelPermalink }}"></script>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
|
@ -25,6 +25,6 @@
|
||||||
{{ if (findRE "<pre" .Content 1) }}
|
{{ if (findRE "<pre" .Content 1) }}
|
||||||
{{ $syntax := resources.Get "css/torchlight.css" | minify }}
|
{{ $syntax := resources.Get "css/torchlight.css" | minify }}
|
||||||
<link href="{{ $syntax.RelPermalink }}" rel="stylesheet">
|
<link href="{{ $syntax.RelPermalink }}" rel="stylesheet">
|
||||||
<!-- {{ $copyCss := resources.Get "css/code-copy-button.css" | minify }}
|
{{ $copyCss := resources.Get "css/code-copy-button.css" | minify }}
|
||||||
<link href="{{ $copyCss.RelPermalink }}" rel="stylesheet"> -->
|
<link href="{{ $copyCss.RelPermalink }}" rel="stylesheet">
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
Loading…
Reference in a new issue