trumbowyg.mathml.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* ===========================================================
  2. * trumbowyg.mathMl.js v1.0
  3. * MathML plugin for Trumbowyg
  4. * http://alex-d.github.com/Trumbowyg
  5. * ===========================================================
  6. * Author : loclamor
  7. */
  8. /* globals MathJax */
  9. (function($) {
  10. 'use strict';
  11. $.extend(true, $.trumbowyg, {
  12. langs: {
  13. // jshint camelcase:false
  14. en: {
  15. mathml: 'Insert Formulas',
  16. formulas: 'Formulas',
  17. inline: 'Inline'
  18. },
  19. da: {
  20. mathml: 'Indsæt formler',
  21. formulas: 'Formler',
  22. inline: 'Inline'
  23. },
  24. fr: {
  25. mathml: 'Inserer une formule',
  26. formulas: 'Formule',
  27. inline: 'En ligne'
  28. },
  29. hu: {
  30. mathml: 'Formulák beszúrás',
  31. formulas: 'Formulák',
  32. inline: 'Inline'
  33. },
  34. ko: {
  35. mathml: '수식 넣기',
  36. formulas: '수식',
  37. inline: '글 안에 넣기'
  38. },
  39. pt_br: {
  40. mathml: 'Inserir fórmulas',
  41. formulas: 'Fórmulas',
  42. inline: 'Em linha'
  43. },
  44. tr: {
  45. mathml: 'Formül Ekle',
  46. formulas: 'Formüller',
  47. inline: 'Satır içi'
  48. },
  49. zh_tw: {
  50. mathml: '插入方程式',
  51. formulas: '方程式',
  52. inline: '內嵌'
  53. },
  54. },
  55. // jshint camelcase:true
  56. plugins: {
  57. mathml: {
  58. init: function(trumbowyg) {
  59. var btnDef = {
  60. fn: function() {
  61. trumbowyg.saveRange();
  62. var mathMLoptions = {
  63. formulas: {
  64. label: trumbowyg.lang.formulas,
  65. required: true,
  66. value: ''
  67. },
  68. inline: {
  69. label: trumbowyg.lang.inline,
  70. attributes: {
  71. checked: true
  72. },
  73. type: 'checkbox',
  74. required: false,
  75. }
  76. };
  77. var mathmlCallback = function(v) {
  78. var delimiter = v.inline ? '$' : '$$';
  79. if (trumbowyg.currentMathNode) {
  80. $(trumbowyg.currentMathNode)
  81. .html(delimiter + ' ' + v.formulas + ' ' + delimiter)
  82. .attr('formulas', v.formulas)
  83. .attr('inline', (v.inline ? 'true' : 'false'));
  84. } else {
  85. var html = '<span class="mathMlContainer" contenteditable="false" formulas="' + v.formulas + '" inline="' + (v.inline ? 'true' : 'false') + '" >' + delimiter + ' ' + v.formulas + ' ' + delimiter + '</span>';
  86. var node = $(html)[0];
  87. node.onclick = function() {
  88. trumbowyg.currentMathNode = this;
  89. mathMLoptions.formulas.value = $(this).attr('formulas');
  90. if ($(this).attr('inline') === 'true') {
  91. mathMLoptions.inline.attributes.checked = true;
  92. } else {
  93. delete mathMLoptions.inline.attributes.checked;
  94. }
  95. trumbowyg.openModalInsert(trumbowyg.lang.mathml, mathMLoptions, mathmlCallback);
  96. };
  97. trumbowyg.range.deleteContents();
  98. trumbowyg.range.insertNode(node);
  99. }
  100. trumbowyg.currentMathNode = false;
  101. MathJax.Hub.Queue(['Typeset', MathJax.Hub]);
  102. return true;
  103. };
  104. mathMLoptions.formulas.value = trumbowyg.getRangeText();
  105. mathMLoptions.inline.attributes.checked = true;
  106. trumbowyg.openModalInsert(trumbowyg.lang.mathml, mathMLoptions, mathmlCallback);
  107. }
  108. };
  109. trumbowyg.addBtnDef('mathml', btnDef);
  110. }
  111. }
  112. }
  113. });
  114. })(jQuery);