| 1 |
(function () { |
| 2 |
|
| 3 |
if (typeof Prism === 'undefined' || typeof document === 'undefined') { |
| 4 |
return; |
| 5 |
} |
| 6 |
|
| 7 |
if (!Prism.plugins.toolbar) { |
| 8 |
console.warn('Copy to Clipboard plugin loaded before Toolbar plugin.'); |
| 9 |
|
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
function registerClipboard(element, copyInfo) { |
| 25 |
element.addEventListener('click', function () { |
| 26 |
copyTextToClipboard(copyInfo); |
| 27 |
}); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
function fallbackCopyTextToClipboard(copyInfo) { |
| 34 |
var textArea = document.createElement('textarea'); |
| 35 |
textArea.value = copyInfo.getText(); |
| 36 |
|
| 37 |
|
| 38 |
textArea.style.top = '0'; |
| 39 |
textArea.style.left = '0'; |
| 40 |
textArea.style.position = 'fixed'; |
| 41 |
|
| 42 |
document.body.appendChild(textArea); |
| 43 |
textArea.focus(); |
| 44 |
textArea.select(); |
| 45 |
|
| 46 |
try { |
| 47 |
var successful = document.execCommand('copy'); |
| 48 |
setTimeout(function () { |
| 49 |
if (successful) { |
| 50 |
copyInfo.success(); |
| 51 |
} else { |
| 52 |
copyInfo.error(); |
| 53 |
} |
| 54 |
}, 1); |
| 55 |
} catch (err) { |
| 56 |
setTimeout(function () { |
| 57 |
copyInfo.error(err); |
| 58 |
}, 1); |
| 59 |
} |
| 60 |
|
| 61 |
document.body.removeChild(textArea); |
| 62 |
} |
| 63 |
|
| 64 |
function copyTextToClipboard(copyInfo) { |
| 65 |
if (navigator.clipboard) { |
| 66 |
navigator.clipboard.writeText(copyInfo.getText()).then(copyInfo.success, function () { |
| 67 |
|
| 68 |
fallbackCopyTextToClipboard(copyInfo); |
| 69 |
}); |
| 70 |
} else { |
| 71 |
fallbackCopyTextToClipboard(copyInfo); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
function selectElementText(element) { |
| 81 |
|
| 82 |
window.getSelection().selectAllChildren(element); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
function getSettings(startElement) { |
| 93 |
|
| 94 |
var settings = { |
| 95 |
'copy': 'Copy', |
| 96 |
'copy-error': 'Press Ctrl+C to copy', |
| 97 |
'copy-success': 'Copied!', |
| 98 |
'copy-timeout': 5000 |
| 99 |
}; |
| 100 |
|
| 101 |
var prefix = 'data-prismjs-'; |
| 102 |
for (var key in settings) { |
| 103 |
var attr = prefix + key; |
| 104 |
var element = startElement; |
| 105 |
while (element && !element.hasAttribute(attr)) { |
| 106 |
element = element.parentElement; |
| 107 |
} |
| 108 |
if (element) { |
| 109 |
settings[key] = element.getAttribute(attr); |
| 110 |
} |
| 111 |
} |
| 112 |
return settings; |
| 113 |
} |
| 114 |
|
| 115 |
Prism.plugins.toolbar.registerButton('copy-to-clipboard', function (env) { |
| 116 |
var element = env.element; |
| 117 |
|
| 118 |
var settings = getSettings(element); |
| 119 |
|
| 120 |
var linkCopy = document.createElement('button'); |
| 121 |
linkCopy.className = 'copy-to-clipboard-button'; |
| 122 |
linkCopy.setAttribute('type', 'button'); |
| 123 |
var linkSpan = document.createElement('span'); |
| 124 |
linkCopy.appendChild(linkSpan); |
| 125 |
|
| 126 |
setState('copy'); |
| 127 |
|
| 128 |
registerClipboard(linkCopy, { |
| 129 |
getText: function () { |
| 130 |
return element.textContent; |
| 131 |
}, |
| 132 |
success: function () { |
| 133 |
setState('copy-success'); |
| 134 |
|
| 135 |
resetText(); |
| 136 |
}, |
| 137 |
error: function () { |
| 138 |
setState('copy-error'); |
| 139 |
|
| 140 |
setTimeout(function () { |
| 141 |
selectElementText(element); |
| 142 |
}, 1); |
| 143 |
|
| 144 |
resetText(); |
| 145 |
} |
| 146 |
}); |
| 147 |
|
| 148 |
return linkCopy; |
| 149 |
|
| 150 |
function resetText() { |
| 151 |
setTimeout(function () { setState('copy'); }, settings['copy-timeout']); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
function setState(state) { |
| 156 |
linkSpan.textContent = settings[state]; |
| 157 |
linkCopy.setAttribute('data-copy-state', state); |
| 158 |
} |
| 159 |
}); |
| 160 |
}()); |