|
| 1 | +--- |
| 2 | +layout: null |
| 3 | +--- |
| 4 | +(function (jtd, undefined) { |
| 5 | + |
| 6 | +// From https://github.com/just-the-docs/just-the-docs/blob/main/assets/js/just-the-docs.js |
| 7 | +// Event handling |
| 8 | + |
| 9 | +jtd.addEvent = function(el, type, handler) { |
| 10 | + if (el.attachEvent) el.attachEvent('on'+type, handler); else el.addEventListener(type, handler); |
| 11 | +} |
| 12 | +jtd.removeEvent = function(el, type, handler) { |
| 13 | + if (el.detachEvent) el.detachEvent('on'+type, handler); else el.removeEventListener(type, handler); |
| 14 | +} |
| 15 | +jtd.onReady = function(ready) { |
| 16 | + // in case the document is already rendered |
| 17 | + if (document.readyState!='loading') ready(); |
| 18 | + // modern browsers |
| 19 | + else if (document.addEventListener) document.addEventListener('DOMContentLoaded', ready); |
| 20 | + // IE <= 8 |
| 21 | + else document.attachEvent('onreadystatechange', function(){ |
| 22 | + if (document.readyState=='complete') ready(); |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +jtd.onReady(function(){ |
| 27 | + |
| 28 | + if (!window.isSecureContext) { |
| 29 | + console.log('Window does not have a secure context, therefore code clipboard copy functionality will not be available. For more details see https://web.dev/async-clipboard/#security-and-permissions'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + var codeBlocks = document.querySelectorAll('div.highlight'); |
| 34 | + |
| 35 | + var svgCopied = '<i class="fa fa-check" aria-hidden="true"></i> Copied'; |
| 36 | + var svgCopy = '<i class="fa fa-clipboard" aria-hidden="true"></i> Copy'; |
| 37 | + |
| 38 | + codeBlocks.forEach(codeBlock => { |
| 39 | + var copyButton = document.createElement('button'); |
| 40 | + var timeout = null; |
| 41 | + copyButton.type = 'button'; |
| 42 | + copyButton.ariaLabel = 'Copy code to clipboard'; |
| 43 | + copyButton.innerHTML = svgCopy; |
| 44 | + codeBlock.append(copyButton); |
| 45 | + |
| 46 | + copyButton.addEventListener('click', function () { |
| 47 | + if(timeout === null) { |
| 48 | + var code = (codeBlock.querySelector('pre:not(.lineno, .highlight)') || codeBlock.querySelector('code')).innerText; |
| 49 | + window.navigator.clipboard.writeText(code); |
| 50 | + |
| 51 | + copyButton.innerHTML = svgCopied; |
| 52 | + |
| 53 | + var timeoutSetting = 4000; |
| 54 | + |
| 55 | + timeout = setTimeout(function () { |
| 56 | + copyButton.innerHTML = svgCopy; |
| 57 | + timeout = null; |
| 58 | + }, timeoutSetting); |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | +}); |
| 64 | + |
| 65 | +})(window.jtd = window.jtd || {}); |
0 commit comments