trumbowyg.highlight.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* globals Prism */
  2. (function ($, Prism) {
  3. 'use strict';
  4. // My plugin default options
  5. var defaultOptions = {};
  6. function highlightIt(text, language) {
  7. return [
  8. '<pre class="language-' + language + '">',
  9. '<code class="language-' + language + '">' + Prism.highlight(text, Prism.languages[language]) + '</code>',
  10. '</pre>',
  11. ].join('');
  12. }
  13. // If my plugin is a button
  14. function buildButtonDef(trumbowyg) {
  15. return {
  16. fn: function () {
  17. var $modal = trumbowyg.openModal('Code', [
  18. '<div class="' + trumbowyg.o.prefix + 'highlight-form-group">',
  19. ' <select class="' + trumbowyg.o.prefix + 'highlight-form-control language">',
  20. (function () {
  21. var options = '';
  22. for (var lang in Prism.languages) {
  23. if (Prism.languages.hasOwnProperty(lang)) {
  24. options += '<option value="' + lang + '">' + lang + '</option>';
  25. }
  26. }
  27. return options;
  28. })(),
  29. ' </select>',
  30. '</div>',
  31. '<div class="' + trumbowyg.o.prefix + 'highlight-form-group">',
  32. ' <textarea class="' + trumbowyg.o.prefix + 'highlight-form-control code"></textarea>',
  33. '</div>',
  34. ].join('\n')),
  35. $language = $modal.find('.language'),
  36. $code = $modal.find('.code');
  37. // Listen clicks on modal box buttons
  38. $modal.on('tbwconfirm', function () {
  39. trumbowyg.restoreRange();
  40. trumbowyg.execCmd('insertHTML', highlightIt($code.val(), $language.val()));
  41. trumbowyg.execCmd('insertHTML', '<p><br></p>');
  42. trumbowyg.closeModal();
  43. });
  44. $modal.on('tbwcancel', function () {
  45. trumbowyg.closeModal();
  46. });
  47. }
  48. };
  49. }
  50. $.extend(true, $.trumbowyg, {
  51. // Add some translations
  52. langs: {
  53. en: {
  54. highlight: 'Code syntax highlight'
  55. }
  56. },
  57. // Add our plugin to Trumbowyg registred plugins
  58. plugins: {
  59. highlight: {
  60. init: function (trumbowyg) {
  61. // Fill current Trumbowyg instance with my plugin default options
  62. trumbowyg.o.plugins.highlight = $.extend(true, {},
  63. defaultOptions,
  64. trumbowyg.o.plugins.highlight || {}
  65. );
  66. // If my plugin is a button
  67. trumbowyg.addBtnDef('highlight', buildButtonDef(trumbowyg));
  68. }
  69. }
  70. }
  71. });
  72. })(jQuery, Prism);