formula.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /* 获得当前公式所在的iframe节点 */
  39. function getSelfIframe(){
  40. var iframes = parent.document.getElementsByTagName('iframe');
  41. for (var key in iframes) {
  42. if (iframes[key].contentWindow == window) {
  43. return iframes[key];
  44. }
  45. }
  46. return null;
  47. }
  48. /* 获得当前url上的hash存储的参数值 */
  49. function getLatex() {
  50. return $iframe.attr('data-latex') || '';
  51. }
  52. /* 保存场景 */
  53. function saveScene(){
  54. }
  55. /* 设置编辑器可编辑 */
  56. function enableEditor(){
  57. }
  58. /* 设置编辑器不可编辑 */
  59. function disableEditor(){
  60. }
  61. /* 公式 */
  62. var Formula = function(){
  63. var _this = this,
  64. latex = getLatex();
  65. this.isFocus = false;
  66. this.isDisabled = false;
  67. /* 加载公式内容 */
  68. this.$mathquill = $('.mathquill-editable').mathquill('latex', latex);
  69. /* 设置活动状态的公式iframe */
  70. this.$mathquill.on('mousedown', function(){
  71. /* 编辑器不可用时,公式也不可用 */
  72. if(_this.disabled) return false;
  73. /* 第一次点击当前公式,设置公式活动 */
  74. if(!$iframe.hasClass('edui-formula-active')) {
  75. }
  76. _this.focus();
  77. });
  78. /* 设置更新外层iframe的大小和属性 */
  79. $(document.body).on('keydown', function(){
  80. _this.updateIframe();
  81. }).on('keyup', function(){
  82. _this.updateIframe();
  83. });
  84. /* 清除初始化的高亮状态 */
  85. this.$mathquill.removeClass('hasCursor');
  86. /* 初始化后延迟刷新外层iframe大小 */
  87. setTimeout(function(){
  88. _this.updateIframe();
  89. }, 300);
  90. };
  91. Formula.prototype = {
  92. focus:function(){
  93. $iframe.addClass('edui-formula-active');
  94. this.isFocus = true;
  95. },
  96. blur:function(){
  97. $iframe.removeClass('edui-formula-active');
  98. this.removeCursor();
  99. this.isFocus = false;
  100. },
  101. removeCursor: function(){
  102. this.$mathquill.find('span.cursor').hide();
  103. this.$mathquill.parent().find('.hasCursor').removeClass('hasCursor');
  104. },
  105. updateIframe: function(){
  106. $iframe.width(this.$mathquill.width()+8).height(this.$mathquill.height()+8);
  107. var latex = $iframe.attr('data-latex'),
  108. newLatex = this.getLatex();
  109. if(latex != newLatex) {
  110. $iframe.attr('data-latex', this.getLatex());
  111. saveScene();
  112. }
  113. },
  114. insertLatex: function(latex){
  115. this.$mathquill.mathquill('write', latex);
  116. this.updateIframe();
  117. this.removeCursor();
  118. },
  119. setLatex: function(latex){
  120. this.$mathquill.mathquill('latex', latex);
  121. this.updateIframe();
  122. },
  123. getLatex: function(){
  124. return this.$mathquill.mathquill('latex');
  125. },
  126. redraw: function(){
  127. this.$mathquill.mathquill('redraw');
  128. },
  129. setDisabled: function(){
  130. this.blur();
  131. var latex = this.getLatex();
  132. this.$mathquill.mathquill('revert').text(latex).mathquill();
  133. this.updateIframe();
  134. this.isDisabled = true;
  135. },
  136. setEnabled: function(){
  137. this.$mathquill.removeClass('mathquill-rendered-math');
  138. this.refresh();
  139. this.isDisabled = false;
  140. },
  141. refresh: function(){
  142. var latex = this.getLatex();
  143. this.$mathquill.mathquill('revert').text(latex).mathquill('editable');
  144. this.updateIframe();
  145. }
  146. };
  147. /* 绑定到window上,给上级window调用 */
  148. window.formula = new Formula();
  149. });
  150. </script>
  151. </body>
  152. </html>