Skip to content

Commit 6d16ef9

Browse files
committed
Add copy-to-clipboard
1 parent c372925 commit 6d16ef9

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

_config/site.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ semantic_ui_css_url: https://cdn.jsdelivr.net/npm/[email protected]/dist/semant
3838
slick_js_url: https://cdn.jsdelivr.net/npm/[email protected]/slick/slick
3939
slick_js_url_integrity: "sha384-wBo76R9TGH6DxuCcambHkpxJKY96xJq59OwZONC0A2v0rlNK9Kb75uRdSgxK5j9+"
4040
slick_css_url: https://cdn.jsdelivr.net/npm/[email protected]/slick/slick
41+
clipboard_js_url: https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard
42+
clipboard_js_url_integrity: "sha256-4XodgW4TwIJuDtf+v6vDJ39FVxI0veC/kSCCmnFp7ck="
4143

4244
jborg_fonts_url: https://static.jboss.org/theme/fonts
4345
jborg_images_url: https://static.jboss.org/theme/images
@@ -754,6 +756,7 @@ profiles:
754756
jquery_js_url_integrity: "sha384-VC7EHu0lDzZyFfmjTPJq+DFyIn8TUGAJbEtpXquazFVr00Q/OOx//RjiZ9yU9+9m"
755757
semantic_ui_js_url_integrity: "sha384-/OCHdyuUZjDPStDj7ti/VaVeGQ4U9HxuJhh0FNfMTf0eO1VeBLAam8EiKCIK+jso"
756758
slick_js_url_integrity: "sha384-vIgLoBvIiHAJ0ttXa/pf/vpP9JgaN/9dCH6zbhDPEF3H8fvQXE0wHjS/LauoTNF1"
759+
clipboard_js_url_integrity: "sha256-ul94Jn0MBOijGuvXhgx/1/wjaJHN3Ud6TH/cGaDrF24="
757760
base_url: http://0.0.0.0:4242
758761
news_feed_root_url: https://staging.in.relation.to/feeds
759762
test:
@@ -764,6 +767,7 @@ profiles:
764767
jquery_js_url_integrity: "sha384-VC7EHu0lDzZyFfmjTPJq+DFyIn8TUGAJbEtpXquazFVr00Q/OOx//RjiZ9yU9+9m"
765768
semantic_ui_js_url_integrity: "sha384-/OCHdyuUZjDPStDj7ti/VaVeGQ4U9HxuJhh0FNfMTf0eO1VeBLAam8EiKCIK+jso"
766769
slick_js_url_integrity: "sha384-vIgLoBvIiHAJ0ttXa/pf/vpP9JgaN/9dCH6zbhDPEF3H8fvQXE0wHjS/LauoTNF1"
770+
clipboard_js_url_integrity: "sha256-ul94Jn0MBOijGuvXhgx/1/wjaJHN3Ud6TH/cGaDrF24="
767771
base_url: http://0.0.0.0:4242
768772
news_feed_root_url: https://in.relation.to/feeds
769773
staging:

_layouts/base.html.haml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,47 @@
5858
$('body, html').animate({scrollTop:0}, 800);
5959
});
6060
});
61+
62+
:javascript
63+
/*
64+
* This is an adapted version of https://github.com/quarkusio/quarkusio.github.io/blob/317db07ca629dc331455692d2976ecece95319cd/assets/javascript/copy.js
65+
*/
66+
const codes = document.querySelectorAll('pre.highlight > code');
67+
let index = 0;
68+
codes.forEach((code) => {
69+
70+
code.setAttribute("id", "code" + index);
71+
72+
const block = document.createElement('div');
73+
block.className = "tooltip";
74+
75+
const btn = document.createElement('i');
76+
btn.className = "btn-copy ui copy outline icon";
77+
btn.setAttribute("data-clipboard-action", "copy");
78+
btn.setAttribute("data-clipboard-target", "#code" + index);
79+
btn.setAttribute("title", "Copy to clipboard");
80+
btn.setAttribute("float-right", "true");
81+
code.before(btn);
82+
83+
const tooltip = document.createElement('div');
84+
tooltip.className = "tooltip-text";
85+
tooltip.textContent = "Copied!";
86+
code.before(tooltip);
87+
88+
index++;
89+
});
90+
91+
const clipboard = new ClipboardJS('.btn-copy');
92+
clipboard.on('success', function (e) {
93+
e.clearSelection();
94+
e.trigger.className = e.trigger.className.replace("copy outline icon", "check icon");
95+
e.trigger.setAttribute("title", "Copied!");
96+
e.trigger.nextSibling.classList.toggle("show");
97+
e.trigger.blur();
98+
99+
setTimeout(function () {
100+
e.trigger.className = e.trigger.className.replace("check icon", "copy outline icon");
101+
e.trigger.setAttribute("title", "Copy to clipboard");
102+
e.trigger.nextSibling.classList.toggle("show");
103+
}, 1500);
104+
});

_partials/head.html.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
%link(href="https://fonts.googleapis.com/css2?family=Inconsolata:wght@400;700&display=swap" rel="stylesheet")
4141
%script(src="#{site.jquery_js_url}#{site.minified}.js" crossorigin="anonymous" type="text/javascript" integrity="#{site.jquery_js_url_integrity}")
4242
%script(src="#{site.semantic_ui_js_url}#{site.minified}.js" crossorigin="anonymous" type="text/javascript" integrity="#{site.semantic_ui_js_url_integrity}")
43+
%script(src="#{site.clipboard_js_url}#{site.minified}.js" crossorigin="anonymous" type="text/javascript" integrity="#{site.clipboard_js_url_integrity}")
4344
- if real_page.javascripts
4445
- real_page.javascripts.each do |javascript|
4546
%script{:src=>javascript, :type=>'text/javascript'}

stylesheets/styles.scss

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,3 +1016,71 @@ pre .comment .conum {
10161016
}
10171017
}
10181018

1019+
// copy to clipboard:
1020+
1021+
pre.highlight {
1022+
position: relative;
1023+
1024+
.btn-copy {
1025+
cursor: pointer;
1026+
transition: opacity 0.3s ease-in-out;
1027+
opacity: 0;
1028+
padding: 2px 6px;
1029+
position: absolute;
1030+
right: 0.825rem;
1031+
top: 0.025rem;
1032+
background-color: transparent;
1033+
border: none;
1034+
1035+
&:focus {
1036+
opacity: 0.5;
1037+
}
1038+
}
1039+
}
1040+
1041+
.listingblock {
1042+
&:hover {
1043+
.btn-copy {
1044+
opacity: 0.5;
1045+
}
1046+
1047+
code[data-lang]::before {
1048+
opacity: 0.5;
1049+
}
1050+
1051+
.tooltip-text.show {
1052+
opacity: 1;
1053+
}
1054+
}
1055+
1056+
code[data-lang] {
1057+
&::before {
1058+
display: block;
1059+
content: attr(data-lang) '|';
1060+
top: .425rem;
1061+
right: 2.2rem;
1062+
transition: opacity 0.3s ease-in-out;
1063+
opacity: 0;
1064+
position: absolute;
1065+
font-size: .75em;
1066+
line-height: 1;
1067+
text-transform: uppercase;
1068+
color: inherit;
1069+
}
1070+
}
1071+
}
1072+
1073+
.tooltip-text {
1074+
display: block;
1075+
position: absolute;
1076+
top: 1.5rem;
1077+
right: 4px;
1078+
background: #444;
1079+
color: #fff;
1080+
padding: 6px 10px;
1081+
border-radius: 4px;
1082+
white-space: nowrap;
1083+
z-index: 1;
1084+
opacity: 0;
1085+
font-size: 11px;
1086+
}

0 commit comments

Comments
 (0)