|
4 | 4 | <!-- _layouts/guide.html --> |
5 | 5 |
|
6 | 6 |
|
7 | | -<!-- TODO don't rely on jquery --> |
8 | | -<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> |
9 | 7 | <!-- TODO don't rely resource available from site --> |
10 | 8 | <script src="https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js"></script> |
11 | 9 |
|
@@ -44,29 +42,49 @@ <h1 class="card-title fs-3"> |
44 | 42 | </div> |
45 | 43 | </div> |
46 | 44 |
|
47 | | -<script> |
48 | | - |
49 | | -var allCodeBlocksElements = $( "code" ); |
50 | 45 |
|
51 | | -allCodeBlocksElements.each(function(i) { |
| 46 | +<script> |
52 | 47 |
|
53 | | -if ($(this).parents('.listingblock').length) { |
54 | 48 |
|
55 | | - var code = $(this).text().split(/\n/); |
56 | | - for (var idx = 0; idx < code.length; idx++) { code[idx] = code[idx].replace("$ ", "") } |
| 49 | + document.querySelectorAll("code").forEach((codeElem) => { |
| 50 | + if (codeElem.textContent && codeElem.closest('.listingblock')) { |
| 51 | + let codeLines = codeElem.textContent.split(/\n/); |
| 52 | + for (var idx = 0; idx < codeLines.length; idx++) { |
| 53 | + // replace any console prompt |
| 54 | + codeLines[idx] = codeLines[idx].replace("$ ", "") |
| 55 | + } |
| 56 | + let selectableCode = codeLines.join("\n"); |
| 57 | + let clipboardBtn = document.createElement("button"); |
| 58 | + clipboardBtn.setAttribute('data-clipboard-text', selectableCode); |
| 59 | + clipboardBtn.className = "clipboardBtn"; |
| 60 | + codeElem.parentNode.insertBefore(clipboardBtn, codeElem.nextSibling); |
| 61 | + } |
| 62 | + }); |
| 63 | + // |
| 64 | + // var allCodeBlocksElements = $("code"); |
| 65 | + // |
| 66 | + // allCodeBlocksElements.each(function (i) { |
| 67 | + // |
| 68 | + // if ($(this).parents('.listingblock').length) { |
| 69 | + // |
| 70 | + // var code = $(this).text().split(/\n/); |
| 71 | + // for (var idx = 0; idx < code.length; idx++) { |
| 72 | + // code[idx] = code[idx].replace("$ ", "") |
| 73 | + // } |
| 74 | + // |
| 75 | + // //trigger |
| 76 | + // var clipButton = '<button class="btn" data-clipboard-text="' + code.join("\n") + '"><img src="/assets/theme/images/clippy.svg" width="13" alt="Copy to clipboard"></button>'; |
| 77 | + // $(this).parent().before(clipButton); |
| 78 | + // } |
| 79 | + // } |
| 80 | + // ); |
57 | 81 |
|
58 | | - //trigger |
59 | | - var clipButton = '<button class="btn" data-clipboard-text="' + code.join("\n") + '"><img src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard"></button>'; |
60 | | - $(this).parent().before(clipButton); |
61 | | - } |
62 | | -} |
63 | | -); |
64 | | - |
65 | | - new Clipboard('.btn'); |
| 82 | + new Clipboard('.clipboardBtn'); |
66 | 83 |
|
67 | 84 | </script> |
68 | 85 |
|
69 | 86 |
|
| 87 | + |
70 | 88 | {% include asciidoctor-scripts/icons.html %} |
71 | 89 | {% include asciidoctor-scripts/sectanchors.html %} |
72 | 90 |
|
|
0 commit comments