Skip to content

Commit a72cd0c

Browse files
authored
Merge pull request #40226 from cjc7373/fix-copy-to-clipboard
fix: eliminate extra lines when copying to clipboard
2 parents 90c0749 + 638cd92 commit a72cd0c

File tree

2 files changed

+11
-30
lines changed

2 files changed

+11
-30
lines changed

layouts/partials/hooks/body-end.html

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,17 @@
1010
<script type="text/javascript">
1111
function copyCode(elem){
1212
if (document.getElementById(elem)) {
13-
// create hidden text element, if it doesn't already exist
14-
var targetId = "_hiddenCopyText_";
15-
// must use a temporary form element for the selection and copy
16-
target = document.getElementById(targetId);
17-
if (!target) {
18-
var target = document.createElement("textarea");
19-
target.style.position = "absolute";
20-
target.style.left = "-9999px";
21-
target.style.top = "0";
22-
target.id = targetId;
23-
document.body.appendChild(target);
24-
}
25-
target.value = document.getElementById(elem).innerText;
26-
// select the content
27-
target.select();
28-
29-
// copy the selection
30-
var succeed;
31-
try {
32-
succeed = document.execCommand("copy");
33-
} catch(e) {
34-
swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
35-
succeed = false;
36-
}
37-
if (succeed) {
38-
swal("Copied to clipboard: ",elem);
39-
return succeed;
13+
if (navigator.clipboard) {
14+
navigator.clipboard.writeText(document.getElementById(elem).textContent).then(
15+
function () {
16+
swal("Copied to clipboard: ",elem);
17+
},
18+
function () {
19+
swal("Oh, no…","Failed to copy to clipboard: ",elem);
20+
},
21+
);
4022
} else {
41-
swal("Oops!", elem + " not found when trying to copy code");
42-
return false;
23+
swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
4324
}
4425
}
4526
}

layouts/shortcodes/codenew.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</img>
2727
</div>
2828
<div class="includecode" id="{{ $file | anchorize }}">
29-
{{ highlight . $codelang "" }}
29+
{{- highlight . $codelang "" -}}
3030
</div>
3131
</div>
3232
{{ end }}

0 commit comments

Comments
 (0)