|
| 1 | +// Copy to Clipboard. |
| 2 | +let codeBlocks = document.querySelectorAll('#content pre') |
| 3 | + |
| 4 | +codeBlocks.forEach((element, key) => { |
| 5 | + let wrapper = document.createElement('div') |
| 6 | + wrapper.classList.add('relative', 'code-block') |
| 7 | + |
| 8 | + element.parentNode.insertBefore(wrapper, element) |
| 9 | + wrapper.appendChild(element) |
| 10 | + |
| 11 | + let codeElement = element.querySelector('code') |
| 12 | + codeElement.id = `clipText-${key}` |
| 13 | + |
| 14 | + // Copy to clipboard button. |
| 15 | + const copyToClipboardContainer = document.createElement('div') |
| 16 | + |
| 17 | + copyToClipboardContainer.innerHTML = ` |
| 18 | + <div x-data="{ |
| 19 | + copyNotification: false, |
| 20 | + copyToClipboard() { |
| 21 | + this.copyNotification = true |
| 22 | + const clipboardItem = new ClipboardItem({ |
| 23 | + 'text/plain': new Blob([\`${codeElement.innerText}\`], { type: 'text/plain' }) |
| 24 | + }) |
| 25 | + navigator.clipboard.write([clipboardItem]).then(() => { |
| 26 | + console.log('Copied to clipboard') |
| 27 | + }) |
| 28 | + let that = this |
| 29 | + setTimeout(function() { |
| 30 | + that.copyNotification = false |
| 31 | + }, 3000) |
| 32 | + } |
| 33 | + }" class="relative z-20 flex items-center"> |
| 34 | + <div x-show="copyNotification" x-transition:enter="transition ease-out duration-300" x-transition:enter-start="opacity-0 translate-x-2" x-transition:enter-end="opacity-100 translate-x-0" x-transition:leave="transition ease-in duration-300" x-transition:leave-start="opacity-100 translate-x-0" x-transition:leave-end="opacity-0 translate-x-2" class="absolute left-0" x-cloak> |
| 35 | + <div class="px-3 h-7 -ml-1.5 items-center flex text-xs bg-primary-500 border-r border-primary-500 -translate-x-full text-white rounded"> |
| 36 | + <span>Copié!</span> |
| 37 | + <div class="absolute right-0 inline-block h-full -mt-px overflow-hidden translate-x-3 -translate-y-2 top-1/2"> |
| 38 | + <div class="w-3 h-3 origin-top-left transform rotate-45 bg-primary-500 border border-transparent"></div> |
| 39 | + </div> |
| 40 | + </div> |
| 41 | + </div> |
| 42 | + <button @click="copyToClipboard();" class="flex items-center justify-center h-8 text-xs bg-gray-700 rounded-md cursor-pointer w-9 hover:bg-gray-900/50 active:bg-gray-600 focus:bg-gray-700 focus:outline-none text-slate-300 hover:text-white group"> |
| 43 | + <svg x-show="copyNotification" class="w-4 h-4 text-primary-500 stroke-current" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" x-cloak> |
| 44 | + <path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12.75l6 6 9-13.5" /> |
| 45 | + </svg> |
| 46 | + <svg x-show="!copyNotification" class="w-4 h-4 stroke-current" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"> |
| 47 | + <g fill="none" stroke="none"> |
| 48 | + <path d="M7.75 7.757V6.75a3 3 0 0 1 3-3h6.5a3 3 0 0 1 3 3v6.5a3 3 0 0 1-3 3h-.992" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> |
| 49 | + <path d="M3.75 10.75a3 3 0 0 1 3-3h6.5a3 3 0 0 1 3 3v6.5a3 3 0 0 1-3 3h-6.5a3 3 0 0 1-3-3v-6.5z" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/> |
| 50 | + </g> |
| 51 | + </svg> |
| 52 | + </button> |
| 53 | + </div> |
| 54 | + ` |
| 55 | + |
| 56 | + copyToClipboardContainer.setAttribute('aria-label', 'Copy to Clipboard') |
| 57 | + copyToClipboardContainer.setAttribute('title', 'Copy to Clipboard') |
| 58 | + copyToClipboardContainer.classList.add('copyBtn'); |
| 59 | + |
| 60 | + wrapper.append(copyToClipboardContainer) |
| 61 | +}) |
0 commit comments