fix: eliminate extra lines when copying to clipboard

This commit is contained in:
Harold Cheng 2023-03-22 22:44:08 +08:00
parent 98646473d6
commit 638cd92532
No known key found for this signature in database
GPG Key ID: B1F5E036060DA9B7
2 changed files with 11 additions and 30 deletions

View File

@ -10,36 +10,17 @@
<script type="text/javascript"> <script type="text/javascript">
function copyCode(elem){ function copyCode(elem){
if (document.getElementById(elem)) { if (document.getElementById(elem)) {
// create hidden text element, if it doesn't already exist if (navigator.clipboard) {
var targetId = "_hiddenCopyText_"; navigator.clipboard.writeText(document.getElementById(elem).textContent).then(
// must use a temporary form element for the selection and copy function () {
target = document.getElementById(targetId); swal("Copied to clipboard: ",elem);
if (!target) { },
var target = document.createElement("textarea"); function () {
target.style.position = "absolute"; swal("Oh, no…","Failed to copy to clipboard: ",elem);
target.style.left = "-9999px"; },
target.style.top = "0"; );
target.id = targetId;
document.body.appendChild(target);
}
target.value = document.getElementById(elem).innerText;
// select the content
target.select();
// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch(e) {
swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
succeed = false;
}
if (succeed) {
swal("Copied to clipboard: ",elem);
return succeed;
} else { } else {
swal("Oops!", elem + " not found when trying to copy code"); swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
return false;
} }
} }
} }

View File

@ -26,7 +26,7 @@
</img> </img>
</div> </div>
<div class="includecode" id="{{ $file | anchorize }}"> <div class="includecode" id="{{ $file | anchorize }}">
{{ highlight . $codelang "" }} {{- highlight . $codelang "" -}}
</div> </div>
</div> </div>
{{ end }} {{ end }}