| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -136,6 +136,36 @@ | |||
| 136 | 136 | updateHashes(); | |
| 137 | 137 | } | |
| 138 | 138 | ||
| 139 | + function setupCopyButton() { | ||
| 140 | + const buttons = document.querySelectorAll('.copy-button'); | ||
| 141 | + buttons.forEach((button) => { | ||
| 142 | + button.addEventListener('click', (el) => { | ||
| 143 | + const parentNode = el.target.parentNode; | ||
| 144 | + | ||
| 145 | + const flavorSelector = parentNode.querySelector('.js-flavor-selector'); | ||
| 146 | + | ||
| 147 | + let code = ''; | ||
| 148 | + | ||
| 149 | + if (flavorSelector) { | ||
| 150 | + if (flavorSelector.checked) { | ||
| 151 | + code = parentNode.querySelector('.mjs').textContent; | ||
| 152 | + } else { | ||
| 153 | + code = parentNode.querySelector('.cjs').textContent; | ||
| 154 | + } | ||
| 155 | + } else { | ||
| 156 | + code = parentNode.querySelector('code').textContent; | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + button.textContent = 'Copied'; | ||
| 160 | + navigator.clipboard.writeText(code); | ||
| 161 | + | ||
| 162 | + setTimeout(() => { | ||
| 163 | + button.textContent = 'Copy'; | ||
| 164 | + }, 2500); | ||
| 165 | + }); | ||
| 166 | + }); | ||
| 167 | + } | ||
| 168 | + | ||
| 139 | 169 | function bootstrap() { | |
| 140 | 170 | // Check if we have JavaScript support. | |
| 141 | 171 | document.documentElement.classList.add('has-js'); | |
@@ -151,6 +181,8 @@ | |||
| 151 | 181 | ||
| 152 | 182 | // Make link to other versions of the doc open to the same hash target (if it exists). | |
| 153 | 183 | setupAltDocsLink(); | |
| 184 | + | ||
| 185 | + setupCopyButton(); | ||
| 154 | 186 | } | |
| 155 | 187 | ||
| 156 | 188 | if (document.readyState === 'loading') { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -973,6 +973,33 @@ kbd { | |||
| 973 | 973 | .dark-mode .js-flavor-selector { | |
| 974 | 974 | filter: invert(1); | |
| 975 | 975 | } | |
| 976 | + | ||
| 977 | + .copy-button { | ||
| 978 | + float: right; | ||
| 979 | + | ||
| 980 | + outline: none; | ||
| 981 | + font-size: 10px; | ||
| 982 | + color: #fff; | ||
| 983 | + background-color: var(--green1); | ||
| 984 | + line-height: 1; | ||
| 985 | + border-radius: 500px; | ||
| 986 | + border: 1px solid transparent; | ||
| 987 | + letter-spacing: 2px; | ||
| 988 | + min-width: 7.5rem; | ||
| 989 | + text-transform: uppercase; | ||
| 990 | + font-weight: 700; | ||
| 991 | + padding: 0 .5rem; | ||
| 992 | + margin-right: .2rem; | ||
| 993 | + height: 1.5rem; | ||
| 994 | + transition-property: background-color,border-color,color,box-shadow,filter; | ||
| 995 | + transition-duration: .3s; | ||
| 996 | + cursor: pointer; | ||
| 997 | + } | ||
| 998 | + | ||
| 999 | + .copy-button:hover { | ||
| 1000 | + background-color: var(--green2); | ||
| 1001 | + } | ||
| 1002 | + | ||
| 976 | 1003 | @supports (aspect-ratio: 1 / 1) { | |
| 977 | 1004 | .js-flavor-selector { | |
| 978 | 1005 | height: 1.5em; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -226,10 +226,13 @@ export function preprocessElements({ filename }) { | |||
| 226 | 226 | const className = isJSFlavorSnippet(node) ? | |
| 227 | 227 | `language-js ${node.lang}` : | |
| 228 | 228 | `language-${node.lang}`; | |
| 229 | + | ||
| 229 | 230 | const highlighted = | |
| 230 | 231 | `<code class='${className}'>${(getLanguage(node.lang || '') ? highlight(node.value, { language: node.lang }) : node).value}</code>`; | |
| 231 | 232 | node.type = 'html'; | |
| 232 | 233 | ||
| 234 | + const copyButton = '<button class="copy-button">copy</button>'; | ||
| 235 | + | ||
| 233 | 236 | if (isJSFlavorSnippet(node)) { | |
| 234 | 237 | const previousNode = parent.children[index - 1] || {}; | |
| 235 | 238 | const nextNode = parent.children[index + 1] || {}; | |
@@ -253,16 +256,17 @@ export function preprocessElements({ filename }) { | |||
| 253 | 256 | ' aria-label="Show modern ES modules syntax">' + | |
| 254 | 257 | previousNode.value + | |
| 255 | 258 | highlighted + | |
| 259 | + copyButton + | ||
| 256 | 260 | '</pre>'; | |
| 257 | 261 | node.lang = null; | |
| 258 | 262 | previousNode.value = ''; | |
| 259 | 263 | previousNode.lang = null; | |
| 260 | 264 | } else { | |
| 261 | 265 | // Isolated JS snippet, no need to add the checkbox. | |
| 262 | - node.value = `<pre>${highlighted}</pre>`; | ||
| 266 | + node.value = `<pre>${highlighted} ${copyButton}</pre>`; | ||
| 263 | 267 | } | |
| 264 | 268 | } else { | |
| 265 | - node.value = `<pre>${highlighted}</pre>`; | ||
| 269 | + node.value = `<pre>${highlighted} ${copyButton}</pre>`; | ||
| 266 | 270 | } | |
| 267 | 271 | } else if (node.type === 'html' && common.isYAMLBlock(node.value)) { | |
| 268 | 272 | node.value = parseYAML(node.value); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments