Skip to content

Commit cb68855

Browse files
authored
Merge pull request #24229 from sftim/20200929_load_sweetalert_async
Revise copy-and-paste helper for codenew shortcode
2 parents 45e139f + 735410f commit cb68855

File tree

5 files changed

+44
-72
lines changed

5 files changed

+44
-72
lines changed

layouts/case-studies/single-baseof.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
{{ partial "deprecation-warning.html" . }}
99
{{ block "main" . }}{{ end }}
1010
{{ partialCached "footer.html" . }}
11-
<!-- Disabling this as elements queries do not appear to exist on case studies -->
12-
<!-- {{ partialCached "footer-scripts.html" . }} -->
1311
{{ partialCached "scripts.html" . }}
1412

1513
</body>

layouts/partials/footer-scripts.html

Lines changed: 0 additions & 35 deletions
This file was deleted.

layouts/partials/head.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
<meta property="og:image" content="{{ "/images/kubernetes-horizontal-color.png" | relURL }}">
8686
{{ end }}
8787
<meta property="og:type" content="{{ $ogType }}">
88-
<script src="{{ "js/sweetalert-2.1.2.min.js" | relURL }}"></script>
8988

9089
{{ if .HasShortcode "mermaid" }}
9190
<!-- Copied from https://unpkg.com/[email protected]/dist/mermaid.min.js -->

layouts/partials/hooks/body-end.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{{ with .Site.Params.algolia_docsearch }}
2+
<!-- scripts for algolia docsearch -->
3+
{{ end }}
4+
{{/* copy-and-paste helper for codenew shortcode */}}
5+
{{- if .HasShortcode "codenew" -}}
6+
<script async src="{{ "js/sweetalert-2.1.2.min.js" | relURL }}"></script>
7+
<script type="text/javascript">
8+
function copyCode(elem){
9+
if (document.getElementById(elem)) {
10+
// create hidden text element, if it doesn't already exist
11+
var targetId = "_hiddenCopyText_";
12+
// must use a temporary form element for the selection and copy
13+
target = document.getElementById(targetId);
14+
if (!target) {
15+
var target = document.createElement("textarea");
16+
target.style.position = "absolute";
17+
target.style.left = "-9999px";
18+
target.style.top = "0";
19+
target.id = targetId;
20+
document.body.appendChild(target);
21+
}
22+
target.value = document.getElementById(elem).innerText;
23+
// select the content
24+
target.select();
25+
26+
// copy the selection
27+
var succeed;
28+
try {
29+
succeed = document.execCommand("copy");
30+
} catch(e) {
31+
swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
32+
succeed = false;
33+
}
34+
if (succeed) {
35+
swal("Copied to clipboard: ",elem);
36+
return succeed;
37+
} else {
38+
swal("Oops!", elem + " not found when trying to copy code");
39+
return false;
40+
}
41+
}
42+
}
43+
</script>
44+
{{- end -}}

static/js/script.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,6 @@
99
var body;
1010

1111
//helper functions
12-
function copyCode(elem){
13-
if (document.getElementById(elem)) {
14-
// create hidden text element, if it doesn't already exist
15-
var targetId = "_hiddenCopyText_";
16-
// must use a temporary form element for the selection and copy
17-
target = document.getElementById(targetId);
18-
if (!target) {
19-
var target = document.createElement("textarea");
20-
target.style.position = "absolute";
21-
target.style.left = "-9999px";
22-
target.style.top = "0";
23-
target.id = targetId;
24-
document.body.appendChild(target);
25-
}
26-
target.value = document.getElementById(elem).innerText;
27-
// select the content
28-
target.select();
29-
30-
// copy the selection
31-
var succeed;
32-
try {
33-
succeed = document.execCommand("copy");
34-
} catch(e) {
35-
swal("Oh, no...","Sorry, your browser doesn't support document.execCommand('copy'), so we can't copy this code to your clipboard.");
36-
succeed = false;
37-
}
38-
if (succeed) swal("Copied to clipboard: ",elem);
39-
return succeed;
40-
} else {
41-
swal("Oops!",elem + " not found when trying to copy code");
42-
return false;
43-
}
44-
}
45-
4612
function booleanAttributeValue(element, attribute, defaultValue){
4713
// returns true if an attribute is present with no value
4814
// e.g. booleanAttributeValue(element, 'data-modal', false);

0 commit comments

Comments
 (0)