trumbowyg.mathml.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* ===========================================================
  2. * trumbowyg.mathMl.js v1.0
  3. * MathML plugin for Trumbowyg
  4. * https://github.com/loclamor/Trumbowyg/tree/mathml-plugin/plugins/mathml
  5. * ===========================================================
  6. * Author : loclamor
  7. */
  8. /* globals MathJax */
  9. (function($) {
  10. 'use strict';
  11. $.extend(true, $.trumbowyg, {
  12. langs: {
  13. en: {
  14. mathml: 'Insert Formulas',
  15. formulas: 'Formulas',
  16. inline: 'Inline'
  17. },
  18. da: {
  19. mathml: 'Indsæt formler',
  20. formulas: 'Formler',
  21. inline: 'Inline'
  22. },
  23. fr: {
  24. mathml: 'Inserer une formule',
  25. formulas: 'Formule',
  26. inline: 'En ligne'
  27. },
  28. tr: {
  29. mathml: 'Formül Ekle',
  30. formulas: 'Formüller',
  31. inline: 'Satır içi'
  32. },
  33. zh_tw: {
  34. mathml: '插入方程式',
  35. formulas: '方程式',
  36. inline: '內嵌'
  37. },
  38. },
  39. plugins: {
  40. mathml: {
  41. init: function(trumbowyg) {
  42. var btnDef = {
  43. fn: function() {
  44. trumbowyg.saveRange();
  45. var mathMLoptions = {
  46. formulas: {
  47. label: trumbowyg.lang.formulas,
  48. required: true,
  49. value: ''
  50. },
  51. inline: {
  52. label: trumbowyg.lang.inline,
  53. attributes: {
  54. checked: true
  55. },
  56. type: 'checkbox',
  57. required: false,
  58. }
  59. };
  60. var mathmlCallback = function(v) {
  61. var delimitor = v.inline ? '$' : '$$';
  62. if (trumbowyg.currentMathNode) {
  63. $(trumbowyg.currentMathNode).html(delimitor + ' ' + v.formulas + ' ' + delimitor).attr('formulas', v.formulas).attr('inline', (v.inline ? 'true' : 'false'));
  64. } else {
  65. var html = '<span class="mathMlContainer" contenteditable="false" formulas="' + v.formulas + '" inline="' + (v.inline ? 'true' : 'false') + '" >' + delimitor + ' ' + v.formulas + ' ' + delimitor + '</span>';
  66. var node = $(html)[0];
  67. node.onclick = function(e) {
  68. trumbowyg.currentMathNode = this;
  69. mathMLoptions.formulas.value = $(this).attr('formulas');
  70. if ($(this).attr('inline') === "true") {
  71. mathMLoptions.inline.attributes.checked = true;
  72. } else {
  73. delete mathMLoptions.inline.attributes.checked;
  74. }
  75. trumbowyg.openModalInsert(trumbowyg.lang.mathml, mathMLoptions, mathmlCallback);
  76. };
  77. trumbowyg.range.deleteContents();
  78. trumbowyg.range.insertNode(node);
  79. }
  80. trumbowyg.currentMathNode = false;
  81. MathJax.Hub.Queue(['Typeset', MathJax.Hub]);
  82. return true;
  83. };
  84. mathMLoptions.formulas.value = trumbowyg.getRangeText();
  85. mathMLoptions.inline.attributes.checked = true;
  86. trumbowyg.openModalInsert(trumbowyg.lang.mathml, mathMLoptions, mathmlCallback);
  87. }
  88. };
  89. trumbowyg.addBtnDef('mathml', btnDef);
  90. }
  91. }
  92. }
  93. });
  94. })(jQuery);