From 3b872067850fcfdc75810f8835089fdc712d00a5 Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Thu, 15 Nov 2018 11:41:40 +0000 Subject: [PATCH] Add copy functionality to code boxes --- slides/img/clippy.svg | 3 ++ slides/template.html | 46 ++++++++++++++++++++++++++- slides/workshop.css | 72 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 slides/img/clippy.svg diff --git a/slides/img/clippy.svg b/slides/img/clippy.svg new file mode 100644 index 0000000..e1b1703 --- /dev/null +++ b/slides/img/clippy.svg @@ -0,0 +1,3 @@ + + + diff --git a/slides/template.html b/slides/template.html index 13ab47c..5ef7d86 100644 --- a/slides/template.html +++ b/slides/template.html @@ -46,7 +46,51 @@ ] }); Reveal.configure({ slideNumber: 'c/t' }); - new ClipboardJS('.hljs'); + + Reveal.addEventListener( 'ready', function(event){ + var snippets = document.querySelectorAll('pre > code'); + [].forEach.call(snippets, function(snippet) { + snippet.insertAdjacentHTML("beforebegin", ""); + }); + + var clipboardSnippets = new ClipboardJS('[data-clipboard-snippet]', { + target: function(trigger) { + return trigger.nextElementSibling; + } + }); + + clipboardSnippets.on('success', function(e) { + e.clearSelection(); + showTooltip(e.trigger, "Copied to clipboard"); + }); + clipboardSnippets.on('error', function(e) { + showTooltip(e.trigger, fallbackMessage(e.action)); + }); + var btns = document.querySelectorAll('.btn-copy'); + for (var i = 0; i < btns.length; i++) { + btns[i].addEventListener('mouseleave', function(e) { + e.currentTarget.setAttribute('class', 'btn-copy'); + e.currentTarget.removeAttribute('aria-label'); + }); + } + }); + + function showTooltip(elem, msg) { + elem.setAttribute('class', 'btn-copy tooltipped tooltipped-s'); + elem.setAttribute('aria-label', msg); + } + function fallbackMessage(action) { + var actionMsg = ''; + var actionKey = (action === 'cut' ? 'X' : 'C'); + if (/iPhone|iPad/i.test(navigator.userAgent)) { + actionMsg = 'No support :('; + } else if (/Mac/i.test(navigator.userAgent)) { + actionMsg = 'Press ⌘-' + actionKey + ' to ' + action; + } else { + actionMsg = 'Press Ctrl-' + actionKey + ' to ' + action; + } + return actionMsg; + } diff --git a/slides/workshop.css b/slides/workshop.css index ec39efa..e858bf7 100644 --- a/slides/workshop.css +++ b/slides/workshop.css @@ -62,4 +62,74 @@ body { bottom: unset; font-size: 16px; background-color: #0075BC; -} \ No newline at end of file +} + +/* Control the look and feel of the copy box applied to code sections */ +.btn-copy[disabled] .clippy { + opacity: .3; +} +pre .btn-copy { + -webkit-transition: opacity 0.3s ease-in-out; + -o-transition: opacity 0.3s ease-in-out; + transition: opacity 0.3s ease-in-out; + opacity: 0; + padding: 2px 6px; + float: right; +} +pre .btn-copy img { + margin: 0; + border: 0; +} +pre:hover .btn-copy { + opacity: 1; +} +.tooltipped { + position: relative +} +.tooltipped:after { + position: absolute; + z-index: 1000000; + display: none; + padding: 5px 8px; + font: normal normal 11px/1.5 Helvetica, arial, nimbussansl, liberationsans, freesans, clean, sans-serif, "Segoe UI Emoji", "Segoe UI Symbol"; + color: #fff; + text-align: center; + text-decoration: none; + text-shadow: none; + text-transform: none; + letter-spacing: normal; + word-wrap: break-word; + white-space: pre; + pointer-events: none; + content: attr(aria-label); + background: rgba(0, 0, 0, 0.8); + border-radius: 3px; + -webkit-font-smoothing: subpixel-antialiased +} +.tooltipped:before { + position: absolute; + z-index: 1000001; + display: none; + width: 0; + height: 0; + color: rgba(0, 0, 0, 0.8); + pointer-events: none; + content: ""; + border: 5px solid transparent +} +.tooltipped:hover:before, .tooltipped:hover:after, .tooltipped:active:before, .tooltipped:active:after, .tooltipped:focus:before, .tooltipped:focus:after { + display: inline-block; + text-decoration: none +} +.tooltipped-s:after, .tooltipped-se:after, .tooltipped-sw:after { + top: 100%; + right: 50%; + margin-top: 5px +} +.tooltipped-s:before, .tooltipped-se:before, .tooltipped-sw:before { + top: auto; + right: 50%; + bottom: -5px; + margin-right: -5px; + border-bottom-color: rgba(0, 0, 0, 0.8) +}