formula.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. <link rel="stylesheet" href="../../third-party/mathquill/mathquill.css"/>
  7. <style>
  8. html, body, .main{
  9. margin: 0;
  10. padding: 0;
  11. overflow: hidden;
  12. }
  13. .main{
  14. width:1024px;
  15. height:1024px;
  16. }
  17. .mathquill-editable,
  18. .mathquill-rendered-math{
  19. border: 0px;
  20. padding: 0px;
  21. margin:4px;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div class="main">
  27. <div class="mathquill-editable"></div>
  28. </div>
  29. <div>
  30. <input id="blurHelper" />
  31. </div>
  32. <script src="../../third-party/jquery.min.js"></script>
  33. <script src="../../third-party/mathquill/mathquill.js"></script>
  34. <script>
  35. $(function(){
  36. var UM = parent.UM,
  37. $iframe = $(getSelfIframe()),
  38. editorId = $iframe.parents('.edui-body-container').attr('id'),
  39. editor = UM.getEditor(editorId),
  40. timer;
  41. /* 获得当前公式所在的iframe节点 */
  42. function getSelfIframe(){
  43. var iframes = parent.document.getElementsByTagName('iframe');
  44. for (var key in iframes) {
  45. if (iframes[key].contentWindow == window) {
  46. return iframes[key];
  47. }
  48. }
  49. return null;
  50. }
  51. /* 获得当前url上的hash存储的参数值 */
  52. function getLatex() {
  53. return $iframe.attr('data-latex') || '';
  54. }
  55. /* 保存场景 */
  56. function saveScene(){
  57. timer && clearTimeout(timer);
  58. timer = setTimeout(function(){
  59. editor.fireEvent('savescene');
  60. editor.fireEvent('contentchange');
  61. editor.fireEvent('selectionchange');
  62. timer = null;
  63. }, 300);
  64. }
  65. /* 设置编辑器可编辑 */
  66. function enableEditor(){
  67. if(editor.body.contentEditable == 'false') {
  68. editor.setEnabled();
  69. }
  70. }
  71. /* 设置编辑器不可编辑 */
  72. function disableEditor(){
  73. if(editor.body.contentEditable == 'true') {
  74. editor.setDisabled(['undo', 'redo', 'preview', 'formula'], true);
  75. }
  76. }
  77. /* 公式 */
  78. var Formula = function(){
  79. var _this = this,
  80. latex = getLatex();
  81. this.isFocus = false;
  82. this.isDisabled = false;
  83. /* 加载公式内容 */
  84. this.$mathquill = $('.mathquill-editable').mathquill('latex', latex);
  85. /* 设置活动状态的公式iframe */
  86. this.$mathquill.on('mousedown', function(){
  87. /* 编辑器不可用时,公式也不可用 */
  88. if(_this.disabled) return false;
  89. /* 第一次点击当前公式,设置公式活动 */
  90. if(!$iframe.hasClass('edui-formula-active')) {
  91. disableEditor();
  92. editor.blur();
  93. editor.$body.find('iframe').not($iframe).each(function(k, v){
  94. v.contentWindow.formula.blur();
  95. });
  96. if(_this.$mathquill.find('.cursor').css('display') == 'none') {
  97. _this.refresh();
  98. _this.$mathquill.addClass('hasCursor');
  99. }
  100. }
  101. _this.focus();
  102. });
  103. editor.addListener('click', function(){
  104. _this.blur();
  105. enableEditor();
  106. });
  107. /* 里面focus,编辑器也判断为focus */
  108. editor.addListener('isFocus', function(){
  109. return _this.isFocus;
  110. });
  111. /* um不可用,公式也不可编辑 */
  112. editor.addListener('setDisabled', function(type, except){
  113. if (!(except && except.join(' ').indexOf('formula') != -1) && _this.isDisabled != true ) {
  114. _this.setDisabled();
  115. }
  116. });
  117. editor.addListener('setEnabled', function(){
  118. if (_this.isDisabled != false) {
  119. _this.setEnabled();
  120. }
  121. });
  122. /* 设置更新外层iframe的大小和属性 */
  123. $(document.body).on('keydown', function(){
  124. _this.updateIframe();
  125. }).on('keyup', function(){
  126. _this.updateIframe();
  127. });
  128. /* 清除初始化的高亮状态 */
  129. this.$mathquill.removeClass('hasCursor');
  130. /* 初始化后延迟刷新外层iframe大小 */
  131. setTimeout(function(){
  132. _this.updateIframe();
  133. }, 300);
  134. };
  135. Formula.prototype = {
  136. focus:function(){
  137. $iframe.addClass('edui-formula-active');
  138. this.isFocus = true;
  139. },
  140. blur:function(){
  141. $iframe.removeClass('edui-formula-active');
  142. this.removeCursor();
  143. this.isFocus = false;
  144. },
  145. removeCursor: function(){
  146. this.$mathquill.find('span.cursor').hide();
  147. this.$mathquill.parent().find('.hasCursor').removeClass('hasCursor');
  148. },
  149. updateIframe: function(){
  150. $iframe.width(this.$mathquill.width()+8).height(this.$mathquill.height()+8);
  151. var latex = $iframe.attr('data-latex'),
  152. newLatex = this.getLatex();
  153. if(latex != newLatex) {
  154. $iframe.attr('data-latex', this.getLatex());
  155. saveScene();
  156. }
  157. },
  158. insertLatex: function(latex){
  159. this.$mathquill.mathquill('write', latex);
  160. this.updateIframe();
  161. this.removeCursor();
  162. },
  163. setLatex: function(latex){
  164. this.$mathquill.mathquill('latex', latex);
  165. this.updateIframe();
  166. },
  167. getLatex: function(){
  168. return this.$mathquill.mathquill('latex');
  169. },
  170. redraw: function(){
  171. this.$mathquill.mathquill('redraw');
  172. },
  173. setDisabled: function(){
  174. this.blur();
  175. var latex = this.getLatex();
  176. this.$mathquill.mathquill('revert').text(latex).mathquill();
  177. this.updateIframe();
  178. this.isDisabled = true;
  179. },
  180. setEnabled: function(){
  181. this.$mathquill.removeClass('mathquill-rendered-math');
  182. this.refresh();
  183. this.isDisabled = false;
  184. },
  185. refresh: function(){
  186. var latex = this.getLatex();
  187. this.$mathquill.mathquill('revert').text(latex).mathquill('editable');
  188. this.updateIframe();
  189. }
  190. };
  191. /* 绑定到window上,给上级window调用 */
  192. window.formula = new Formula();
  193. });
  194. </script>
  195. </body>
  196. </html>