formula.html 7.1 KB

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