Skip to content

Commit

Permalink
Add copy functionality to code boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalberger committed Nov 15, 2018
1 parent 38d16a9 commit b2aefb5
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
46 changes: 45 additions & 1 deletion slides/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<button class='btn-copy' data-clipboard-snippet><img class='clippy' width=13 src='/img/clippy.svg' alt='Copy to clipboard'></button>");
});

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;
}
</script>
</body>
</html>
72 changes: 71 additions & 1 deletion slides/workshop.css
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,74 @@ body {
bottom: unset;
font-size: 16px;
background-color: #0075BC;
}
}

/* 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)
}

0 comments on commit b2aefb5

Please sign in to comment.