|
| 1 | +;(function () { |
| 2 | + 'use strict' |
| 3 | + |
| 4 | + var CMD_RX = /^\$ (\S[^\\\n]*(\\\n(?!\$ )[^\\\n]*)*)(?=\n|$)/gm |
| 5 | + var LINE_CONTINUATION_RX = /( ) *\\\n *|\\\n( ?) */g |
| 6 | + var TRAILING_SPACE_RX = / +$/gm |
| 7 | + var config = (document.getElementById('site-script') || { dataset: {} }).dataset |
| 8 | + |
| 9 | + ;[].slice.call(document.querySelectorAll('.doc pre.highlight, .doc .literalblock pre')).forEach(function (pre) { |
| 10 | + var code, language, lang, copy, toast, toolbox |
| 11 | + if (pre.classList.contains('highlight')) { |
| 12 | + code = pre.querySelector('code') |
| 13 | + if ((language = code.dataset.lang) && language !== 'console') { |
| 14 | + ;(lang = document.createElement('span')).className = 'source-lang' |
| 15 | + lang.appendChild(document.createTextNode(language)) |
| 16 | + } |
| 17 | + } else if (pre.innerText.startsWith('$ ')) { |
| 18 | + var block = pre.parentNode.parentNode |
| 19 | + block.classList.remove('literalblock') |
| 20 | + block.classList.add('listingblock') |
| 21 | + pre.classList.add('highlightjs', 'highlight') |
| 22 | + ;(code = document.createElement('code')).className = 'language-console hljs' |
| 23 | + code.dataset.lang = 'console' |
| 24 | + code.appendChild(pre.firstChild) |
| 25 | + pre.appendChild(code) |
| 26 | + } else { |
| 27 | + return |
| 28 | + } |
| 29 | + ;(toolbox = document.createElement('div')).className = 'source-toolbox' |
| 30 | + if (lang) toolbox.appendChild(lang) |
| 31 | + if (window.navigator.clipboard) { |
| 32 | + ;(copy = document.createElement('button')).className = 'copy-button' |
| 33 | + copy.setAttribute('title', 'Copy to clipboard') |
| 34 | + if (config.svgAs === 'svg') { |
| 35 | + var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg') |
| 36 | + svg.setAttribute('class', 'copy-icon') |
| 37 | + var use = document.createElementNS('http://www.w3.org/2000/svg', 'use') |
| 38 | + use.setAttribute('href', window.uiRootPath + '/img/octicons-16.svg#icon-clippy') |
| 39 | + svg.appendChild(use) |
| 40 | + copy.appendChild(svg) |
| 41 | + } else { |
| 42 | + var img = document.createElement('img') |
| 43 | + img.src = window.uiRootPath + '/img/octicons-16.svg#view-clippy' |
| 44 | + img.alt = 'copy icon' |
| 45 | + img.className = 'copy-icon' |
| 46 | + copy.appendChild(img) |
| 47 | + } |
| 48 | + ;(toast = document.createElement('span')).className = 'copy-toast' |
| 49 | + toast.appendChild(document.createTextNode('Copied!')) |
| 50 | + copy.appendChild(toast) |
| 51 | + toolbox.appendChild(copy) |
| 52 | + } |
| 53 | + pre.appendChild(toolbox) |
| 54 | + if (copy) copy.addEventListener('click', writeToClipboard.bind(copy, code)) |
| 55 | + }) |
| 56 | + |
| 57 | + function extractCommands (text) { |
| 58 | + var cmds = [] |
| 59 | + var m |
| 60 | + while ((m = CMD_RX.exec(text))) cmds.push(m[1].replace(LINE_CONTINUATION_RX, '$1$2')) |
| 61 | + return cmds.join(' && ') |
| 62 | + } |
| 63 | + |
| 64 | + function writeToClipboard (code) { |
| 65 | + var text = code.innerText.replace(TRAILING_SPACE_RX, '') |
| 66 | + if (code.dataset.lang === 'console' && text.startsWith('$ ')) text = extractCommands(text) |
| 67 | + window.navigator.clipboard.writeText(text).then( |
| 68 | + function () { |
| 69 | + this.classList.add('clicked') |
| 70 | + this.offsetHeight // eslint-disable-line no-unused-expressions |
| 71 | + this.classList.remove('clicked') |
| 72 | + }.bind(this), |
| 73 | + function () {} |
| 74 | + ) |
| 75 | + } |
| 76 | +})() |
0 commit comments