-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Expand file tree
/
Copy pathcopy-to-clipboard.js
More file actions
52 lines (48 loc) · 1.62 KB
/
Copy pathcopy-to-clipboard.js
File metadata and controls
52 lines (48 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
(function() {
var snippets = document.querySelectorAll('pre');
[].forEach.call(snippets, function(snippet) {
if (snippet.closest('.copyable') !== null) {
snippet.firstChild.insertAdjacentHTML('beforebegin', '<button class="btn" data-clipboard-snippet><i class="far fa-copy"></i></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!');
});
clipboardSnippets.on('error', function(e) {
showTooltip(e.trigger, fallbackMessage(e.action));
});
var btns = document.querySelectorAll('.btn');
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener('mouseleave', clearTooltip);
btns[i].addEventListener('blur', clearTooltip);
btns[i].addEventListener('mouseenter', function(e) {
showTooltip(e.currentTarget, "copy to clipboard")
}, false);
}
function clearTooltip(e) {
e.currentTarget.setAttribute('class', 'btn');
e.currentTarget.removeAttribute('aria-label');
}
function showTooltip(elem, msg) {
elem.setAttribute('class', 'btn 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;
}
})();