| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- (function(){
- if (typeof self === 'undefined' || !self.Prism || !self.document) {
- return;
- }
- if (!Prism.plugins.toolbar) {
- console.warn('Copy to Clipboard plugin loaded before Toolbar plugin.');
- return;
- }
- var ClipboardJS = window.ClipboardJS || undefined;
- if (!ClipboardJS && typeof require === 'function') {
- ClipboardJS = require('clipboard');
- }
- var callbacks = [];
- if (!ClipboardJS) {
- var script = document.createElement('script');
- var head = document.querySelector('head');
- script.onload = function() {
- ClipboardJS = window.ClipboardJS;
- if (ClipboardJS) {
- while (callbacks.length) {
- callbacks.pop()();
- }
- }
- };
- script.src = 'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js';
- head.appendChild(script);
- }
- Prism.plugins.toolbar.registerButton('copy-to-clipboard', function (env) {
- var linkCopy = document.createElement('button');
- linkCopy.textContent = 'Copy';
- var element = env.element;
- if (!ClipboardJS) {
- callbacks.push(registerClipboard);
- } else {
- registerClipboard();
- }
- return linkCopy;
- function registerClipboard() {
- var clip = new ClipboardJS(linkCopy, {
- 'text': function () {
- return element.textContent;
- }
- });
- clip.on('success', function() {
- linkCopy.textContent = 'Copied!';
- resetText();
- });
- clip.on('error', function () {
- linkCopy.textContent = 'Press Ctrl+C to copy';
- resetText();
- });
- }
- function resetText() {
- setTimeout(function () {
- linkCopy.textContent = 'Copy';
- }, 5000);
- }
- });
- })();
|