Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add copy functionality to code boxes #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions slides/img/clippy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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)
}