Skip to content

Commit c502cf5

Browse files
committed
Add a copy button to each code panel
The whole point of the site is lifting a snippet into your own notebook, which until now meant selecting the pygments markup by hand. One delegated click handler covers all 101 panels and falls back to a "Press ⌘C" hint where the clipboard API is unavailable. Also drops the $.bigfoot() call: the library was never loaded, so it threw on every page load.
1 parent a82d835 commit c502cf5

2 files changed

Lines changed: 49 additions & 2 deletions

File tree

templates/t_index.html

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,10 @@ <h3 class="card-title mb-1"><a href="#{{ slug }}"><i class="fa fa-link" aria-hid
135135
</a>
136136
</div>
137137
<div class="col-lg-6 col-xs-12 p-0 right-col d-inline-flex flex-column">
138-
<h6 class="text-muted mb-0 pb-0 pt-3 px-3">Code:</h6>
138+
<div class="code-head d-flex justify-content-between align-items-center pt-3 px-3">
139+
<h6 class="text-muted mb-0 pb-0">Code:</h6>
140+
<button type="button" class="copy-btn" data-copy aria-label="Copy code for {{ name }} with {{ plot['package'] }}">Copy</button>
141+
</div>
139142
{% if plot['package'] == "ggplot" %}
140143
<code class="p-0 mb-auto">{% highlight 'R' %}{{ plot['content'] }}{% endhighlight %}</code>
141144
{% else %}
@@ -181,7 +184,30 @@ <h6 class="text-muted">Note:</h6>
181184
event.preventDefault();
182185
$(this).ekkoLightbox({alwaysShowClose: true});
183186
});
184-
$.bigfoot();
187+
</script>
188+
189+
<script type="text/javascript">
190+
document.addEventListener('click', function (event) {
191+
var button = event.target.closest('[data-copy]');
192+
if (!button) { return; }
193+
var panel = button.closest('.right-col');
194+
var code = panel && panel.querySelector('code');
195+
if (!code) { return; }
196+
var text = code.textContent.replace(/\s+$/, '');
197+
var done = function (label) {
198+
button.textContent = label;
199+
setTimeout(function () { button.textContent = 'Copy'; }, 1500);
200+
};
201+
if (navigator.clipboard && navigator.clipboard.writeText) {
202+
navigator.clipboard.writeText(text).then(function () {
203+
done('Copied');
204+
}, function () {
205+
done('Press ⌘C');
206+
});
207+
} else {
208+
done('Press ⌘C');
209+
}
210+
});
185211
</script>
186212
</body>
187213
</html>

web/css/custom.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ table {
2727
font-family: monospace;
2828
}
2929

30+
/* Copy button sitting in the dark code panel header. */
31+
.copy-btn {
32+
background: transparent;
33+
border: 1px solid #4a4a4a;
34+
border-radius: 3px;
35+
color: #9a9a9a;
36+
cursor: pointer;
37+
font-size: .75rem;
38+
line-height: 1.2;
39+
min-width: 4.5rem;
40+
padding: .15rem .5rem;
41+
transition: color .15s ease, border-color .15s ease;
42+
}
43+
44+
.copy-btn:hover,
45+
.copy-btn:focus {
46+
border-color: #7a7a7a;
47+
color: #e6e6e6;
48+
outline: none;
49+
}
50+
3051
@media (min-width: 1600px) {
3152
.container {
3253
width: 1440px;

0 commit comments

Comments
 (0)