Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. (function(){
  2. if (typeof self === 'undefined' || !self.Prism || !self.document) {
  3. return;
  4. }
  5.  
  6. if (!Prism.plugins.toolbar) {
  7. console.warn('Copy to Clipboard plugin loaded before Toolbar plugin.');
  8.  
  9. return;
  10. }
  11.  
  12. var Clipboard = window.Clipboard || undefined;
  13.  
  14. if (!Clipboard && typeof require === 'function') {
  15. Clipboard = require('clipboard');
  16. }
  17.  
  18. var callbacks = [];
  19.  
  20. if (!Clipboard) {
  21. var script = document.createElement('script');
  22. var head = document.querySelector('head');
  23.  
  24. script.onload = function() {
  25. Clipboard = window.Clipboard;
  26.  
  27. if (Clipboard) {
  28. while (callbacks.length) {
  29. callbacks.pop()();
  30. }
  31. }
  32. };
  33.  
  34. script.src = 'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.8/clipboard.min.js';
  35. head.appendChild(script);
  36. }
  37.  
  38. Prism.plugins.toolbar.registerButton('copy-to-clipboard', function (env) {
  39. var linkCopy = document.createElement('a');
  40. linkCopy.textContent = 'Copy';
  41.  
  42. if (!Clipboard) {
  43. callbacks.push(registerClipboard);
  44. } else {
  45. registerClipboard();
  46. }
  47.  
  48. return linkCopy;
  49.  
  50. function registerClipboard() {
  51. var clip = new Clipboard(linkCopy, {
  52. 'text': function () {
  53. return env.code;
  54. }
  55. });
  56.  
  57. clip.on('success', function() {
  58. linkCopy.textContent = 'Copied!';
  59.  
  60. resetText();
  61. });
  62. clip.on('error', function () {
  63. linkCopy.textContent = 'Press Ctrl+C to copy';
  64.  
  65. resetText();
  66. });
  67. }
  68.  
  69. function resetText() {
  70. setTimeout(function () {
  71. linkCopy.textContent = 'Copy';
  72. }, 5000);
  73. }
  74. });
  75. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement