|
| 1 | +function addCopyButtonToCode(){ |
| 2 | +// get all code elements |
| 3 | +var allCodeBlocksElements = $( "div.highlight pre" ); |
| 4 | + |
| 5 | +// For each element, do the following steps |
| 6 | +allCodeBlocksElements.each(function(ii) { |
| 7 | +// define a unique id for this element and add it |
| 8 | +var currentId = "codeblock" + (ii + 1); |
| 9 | +$(this).attr('id', currentId); |
| 10 | + |
| 11 | +// create a button that's configured for clipboard.js |
| 12 | +// point it to the text that's in this code block |
| 13 | +// add the button just after the text in the code block w/ jquery |
| 14 | +var clipButton = '<button class="btn copybtn" data-tooltip="Copy" data-clipboard-target="#' + currentId + '"><img src="/images/copy-button.svg" width="13" alt="Copy to clipboard"></button>'; |
| 15 | + $(this).after(clipButton); |
| 16 | +}); |
| 17 | + |
| 18 | +// tell clipboard.js to look for clicks that match this query |
| 19 | +new Clipboard('.btn'); |
| 20 | +} |
| 21 | + |
| 22 | +$(document).ready(function () { |
| 23 | +// Once the DOM is loaded for the page, attach clipboard buttons |
| 24 | +addCopyButtonToCode(); |
| 25 | +}); |
| 26 | + |
| 27 | +// i don't know enough about js to make this implement for this code |
| 28 | +// the code below is for a sphinx site. ally might know? |
| 29 | + |
| 30 | +const codeCellId = index => `codecell${index}` |
| 31 | + |
| 32 | +// Clears selected text since ClipboardJS will select the text when copying |
| 33 | +const clearSelection = () => { |
| 34 | + if (window.getSelection) { |
| 35 | + window.getSelection().removeAllRanges() |
| 36 | + } else if (document.selection) { |
| 37 | + document.selection.empty() |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// Changes tooltip text for two seconds, then changes it back |
| 42 | +const temporarilyChangeTooltip = (el, newText) => { |
| 43 | + const oldText = el.getAttribute('data-tooltip') |
| 44 | + el.setAttribute('data-tooltip', newText) |
| 45 | + setTimeout(() => el.setAttribute('data-tooltip', oldText), 2000) |
| 46 | +} |
| 47 | + |
| 48 | +// to work on tooltips and colors |
| 49 | +// https://earthlab-hub-ops.readthedocs.io/en/latest/_static/copybutton.js |
0 commit comments