|
| 1 | +window.addEventListener('load', function () { |
| 2 | + function button(label, ariaLabel, icon, className) { |
| 3 | + const btn = document.createElement('button'); |
| 4 | + btn.classList.add('btnIcon', className); |
| 5 | + btn.setAttribute('type', 'button'); |
| 6 | + btn.setAttribute('aria-label', ariaLabel); |
| 7 | + btn.innerHTML = |
| 8 | + '<div class="btnIcon__body">' + |
| 9 | + icon + |
| 10 | + '<strong class="btnIcon__label">' + |
| 11 | + label + |
| 12 | + '</strong>' + |
| 13 | + '</div>'; |
| 14 | + return btn; |
| 15 | + } |
| 16 | + |
| 17 | + function addButtons(codeBlockSelector, btn) { |
| 18 | + document.querySelectorAll(codeBlockSelector).forEach(function (code) { |
| 19 | + code.parentNode.appendChild(btn.cloneNode(true)); |
| 20 | + }); |
| 21 | + } |
| 22 | + |
| 23 | + const copyIcon = |
| 24 | + '<svg width="12" height="12" viewBox="340 364 14 15" xmlns="http://www.w3.org/2000/svg"><path fill="currentColor" d="M342 375.974h4v.998h-4v-.998zm5-5.987h-5v.998h5v-.998zm2 2.994v-1.995l-3 2.993 3 2.994v-1.996h5v-1.995h-5zm-4.5-.997H342v.998h2.5v-.997zm-2.5 2.993h2.5v-.998H342v.998zm9 .998h1v1.996c-.016.28-.11.514-.297.702-.187.187-.422.28-.703.296h-10c-.547 0-1-.452-1-.998v-10.976c0-.546.453-.998 1-.998h3c0-1.107.89-1.996 2-1.996 1.11 0 2 .89 2 1.996h3c.547 0 1 .452 1 .998v4.99h-1v-2.995h-10v8.98h10v-1.996zm-9-7.983h8c0-.544-.453-.996-1-.996h-1c-.547 0-1-.453-1-.998 0-.546-.453-.998-1-.998-.547 0-1 .452-1 .998 0 .545-.453.998-1 .998h-1c-.547 0-1 .452-1 .997z" fill-rule="evenodd"/></svg>'; |
| 25 | + |
| 26 | + addButtons( |
| 27 | + '.hljs', |
| 28 | + button('Copy', 'Copy code to clipboard', copyIcon, 'btnClipboard') |
| 29 | + ); |
| 30 | + |
| 31 | + const clipboard = new ClipboardJS('.btnClipboard', { |
| 32 | + target: function (trigger) { |
| 33 | + return trigger.parentNode.querySelector('code'); |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + clipboard.on('success', function (event) { |
| 38 | + event.clearSelection(); |
| 39 | + const textEl = event.trigger.querySelector('.btnIcon__label'); |
| 40 | + textEl.textContent = 'Copied'; |
| 41 | + setTimeout(function () { |
| 42 | + textEl.textContent = 'Copy'; |
| 43 | + }, 2000); |
| 44 | + }); |
| 45 | +}); |
0 commit comments