github.com/cilium/cilium@v1.16.2/Documentation/_static/copybutton.js (about) 1 function runWhenDOMLoaded(cb) { 2 if (document.readyState !== "loading") { 3 cb(); 4 } else if (document.addEventListener) { 5 document.addEventListener("DOMContentLoaded", cb); 6 } else { 7 document.attachEvent("onreadystatechange", function() { 8 if (document.readyState == "complete") cb(); 9 }); 10 } 11 } 12 13 function codeCellId(index) { 14 return "codecell" + index; 15 } 16 17 function clearSelection() { 18 if (window.getSelection) { 19 window.getSelection().removeAllRanges(); 20 } else if (document.selection) { 21 document.selection.empty(); 22 } 23 } 24 25 function addCopyButtonToCodeCells() { 26 if (window.ClipboardJS === undefined) { 27 setTimeout(addCopyButtonToCodeCells, 1000); 28 return; 29 } 30 const promptRegExp = /^\s*(\$|#)\s/; 31 var codeCells = document.querySelectorAll(".rst-content pre"); 32 codeCells.forEach(function(codeCell, index) { 33 var wrapper = document.createElement("div"); 34 wrapper.className = "code-wrapper"; 35 codeCell.parentNode.insertBefore(wrapper, codeCell); 36 wrapper.appendChild(codeCell); 37 var id = codeCellId(index); 38 codeCell.setAttribute("id", id); 39 function clipboardButton(id) { 40 var lines = codeCell.textContent.trim().split("\n"); 41 var linesCount = lines.length; 42 var buttonHtml = []; 43 buttonHtml.push('<div class="copybutton-wrapper">'); 44 buttonHtml.push( 45 '<img class="copy-icon" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAMAAABF0y+mAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAACiUExURUdwTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJkEv5MAAAA1dFJOUwAwnr/H5BShnG3DCRPPm5OdyC4jGzHhB7o4AmkDyTJE1vn6ymWjbAHtTIQE6dwNoGH71I2zlJ78SQAAAI9JREFUKM/t0kcOwkAMhWGnTZhUOoQWeu8w979aJEDiOWhGOQD/wpL1LbwxER32ivUU9E0e6zXsngDOfWLJGBZVwl36i6H4FL3mCDGfeqygDegkFjYZ9wEbQ37ZVoCu5Oj/sSLaBnSCTjPUYSv2upkOqSeigRbJcPPdYs1xNYMlPbHvvWyXgOcHe/vbdUPmCj5KFX8s6tJBAAAAAElFTkSuQmCC" alt="" />' 46 ); 47 buttonHtml.push( 48 '<a class="copybutton" data-clipboard-mode="first-line" data-clipboard-target="#' + 49 id + 50 '">' 51 ); 52 buttonHtml.push(linesCount > 1 ? "Copy First Line" : "Copy Line"); 53 buttonHtml.push("</a>"); 54 if (linesCount > 1) { 55 /* 56 * Add a button to print commands for literal and code blocks that may 57 * have prompt symbols to distinguish the commands from their output. 58 * 59 * Add it to: 60 * - "code-block" with language "shell-session", with a parent of class 61 * ".highlight-shell-session" 62 * - Literal blocks ("::"), with a parent of class ".highlight-default" 63 * - Parsed literal blocks, with a parent of class ".literal-block" 64 * 65 * Do not add it to a "code-block" with a language other than 66 * "shell-session". 67 */ 68 if (codeCell.closest(".highlight-shell-session") || 69 codeCell.closest(".highlight-default") || 70 codeCell.closest("literal-block")) { 71 for (const l of lines) { 72 /* Additionally, only add button if we find at least one command */ 73 if (promptRegExp.test(l)) { 74 buttonHtml.push( 75 '<a class="copybutton" data-clipboard-mode="commands" data-clipboard-target="#' + 76 id + 77 '">' 78 ); 79 buttonHtml.push("Copy Commands"); 80 buttonHtml.push("</a>"); 81 break; 82 } 83 } 84 } 85 86 buttonHtml.push( 87 '<a class="copybutton" data-clipboard-mode="all" data-clipboard-target="#' + 88 id + 89 '">' 90 ); 91 buttonHtml.push("Copy All"); 92 buttonHtml.push("</a>"); 93 } 94 buttonHtml.push("</div>"); 95 return buttonHtml.join("\n"); 96 } 97 codeCell.insertAdjacentHTML("afterend", clipboardButton(id)); 98 }); 99 100 new ClipboardJS(".copybutton", { 101 text: function(trigger) { 102 var parent = trigger.parentNode.parentNode; 103 var code = parent.querySelector("pre"); 104 var mode = trigger.getAttribute("data-clipboard-mode"); 105 if (mode === "first-line") { 106 return code.textContent 107 .split("\n")[0] 108 .trim() 109 .replace(/^\$/, "") 110 .trim(); 111 } else if (mode === "commands") { 112 /* 113 * Copy lines with "commands": each line starting with a prompt symbol 114 * ($ or #), plus the continuation lines, for commands ending with a 115 * backslash. 116 */ 117 var cmds = ""; 118 var continuation = false; 119 var lines = code.textContent.split("\n"); 120 for (const l of lines) { 121 if (promptRegExp.test(l) || continuation) { 122 /* Keep line but remove prompt */ 123 cmds += l.replace(promptRegExp, "") + "\n"; 124 /* Expect a continuation line if command ends with a backslash */ 125 continuation = /\\\s*$/.test(l); 126 } 127 } 128 return cmds; 129 } else { 130 return code.textContent; 131 } 132 } 133 }); 134 } 135 136 runWhenDOMLoaded(addCopyButtonToCodeCells);