-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
1 lines (1 loc) · 5.77 KB
/
plugin.js
1
"use strict";CKEDITOR.plugins.add("mathex",{lang:"en,en-au,en-gb",requires:"widget,dialog",icons:"mathex",hidpi:!0,init:function(t){var e=t.config.mathexClass||"math";t.config.mathJaxLib||CKEDITOR.error("mathjax-no-config"),t.widgets.add("mathex",{inline:!0,dialog:"mathex",button:t.lang.mathex.button,mask:!0,allowedContent:"span(!"+e+")",styleToAllowedContentRules:function(t){var a=t.getClassesArray();return a?(a.push("!"+e),"span("+a.join(",")+")"):null},pathName:t.lang.mathex.pathName,template:'<span class="'+e+'" style="display:inline-block" data-cke-survive=1></span>',parts:{span:"span"},defaults:{math:"\\(x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}\\)"},init:function(){var e=this.parts.span.getChild(0);e&&e.type==CKEDITOR.NODE_ELEMENT&&e.is("iframe")||((e=new CKEDITOR.dom.element("iframe")).setAttributes({style:"border:0;width:0;height:0",scrolling:"no",frameborder:0,allowTransparency:!0,src:CKEDITOR.plugins.mathex.fixSrc}),this.parts.span.append(e)),this.once("ready",function(){CKEDITOR.env.ie&&e.setAttribute("src",CKEDITOR.plugins.mathex.fixSrc),this.frameWrapper=new CKEDITOR.plugins.mathex.frameWrapper(e,t),this.frameWrapper.setValue(this.data.math)})},data:function(){this.frameWrapper&&this.frameWrapper.setValue(this.data.math)},upcast:function(t,a){if("span"==t.name&&t.hasClass(e)&&!(t.children.length>1||t.children[0].type!=CKEDITOR.NODE_TEXT)){a.math=CKEDITOR.tools.htmlDecode(t.children[0].value);var n=t.attributes;return n.style?n.style+=";display:inline-block":n.style="display:inline-block",n["data-cke-survive"]=1,t.children[0].remove(),t}},downcast:function(t){t.children[0].replaceWith(new CKEDITOR.htmlParser.text(CKEDITOR.tools.htmlEncode(this.data.math)));var e=t.attributes;return e.style=e.style.replace(/display:\s?inline-block;?\s?/,""),""===e.style&&delete e.style,t}}),CKEDITOR.dialog.add("mathex",this.path+"dialogs/mathex.js"),t.on("contentPreview",function(e){e.data.dataValue=e.data.dataValue.replace(/<\/head>/,'<script src="'+CKEDITOR.getUrl(t.config.mathJaxLib)+'"><\/script></head>')}),t.on("paste",function(t){var a=new RegExp("<span[^>]*?"+e+".*?</span>","ig");t.data.dataValue=t.data.dataValue.replace(a,function(t){return t.replace(/(<iframe.*?\/iframe>)/i,"")})})}}),CKEDITOR.plugins.mathex={},CKEDITOR.plugins.mathex.fixSrc=CKEDITOR.env.gecko?"javascript:true":CKEDITOR.env.ie?"javascript:void((function(){"+encodeURIComponent("document.open();("+CKEDITOR.tools.fixDomain+")();document.close();")+"})())":"javascript:void(0)",CKEDITOR.plugins.mathex.loadingIcon=CKEDITOR.plugins.get("mathex").path+"images/loader.gif",CKEDITOR.plugins.mathex.copyStyles=function(t,e){for(var a=["color","font-family","font-style","font-weight","font-variant","font-size"],n=0;n<a.length;n++){var i=a[n],s=t.getComputedStyle(i);s&&e.setStyle(i,s)}},CKEDITOR.plugins.mathex.trim=function(t){var e=t.indexOf("\\(")+2,a=t.lastIndexOf("\\)");return t.substring(e,a)},CKEDITOR.env.ie&&8==CKEDITOR.env.version?CKEDITOR.plugins.mathex.frameWrapper=function(t,e){return t.getFrameDocument().write('<!DOCTYPE html><html><head><meta charset="utf-8"></head><body style="padding:0;margin:0;background:transparent;overflow:hidden"><span style="white-space:nowrap;" id="tex"></span></body></html>'),{setValue:function(a){var n=t.getFrameDocument(),i=n.getById("tex");i.setHtml(CKEDITOR.plugins.mathex.trim(CKEDITOR.tools.htmlEncode(a))),CKEDITOR.plugins.mathex.copyStyles(t,i),e.fire("lockSnapshot"),t.setStyles({width:Math.min(250,i.$.offsetWidth)+"px",height:n.$.body.offsetHeight+"px",display:"inline","vertical-align":"middle"}),e.fire("unlockSnapshot")}}}:CKEDITOR.plugins.mathex.frameWrapper=function(t,e){var a,n,i,s,o=t.getFrameDocument(),l=!1,r=!1,h=CKEDITOR.tools.addFunction(function(){n=o.getById("preview"),a=o.getById("buffer"),l=!0,s&&m(),CKEDITOR.fire("mathexLoaded",t)}),d=CKEDITOR.tools.addFunction(function(){CKEDITOR.plugins.mathex.copyStyles(t,n),n.setHtml(a.getHtml()),e.fire("lockSnapshot"),t.setStyles({height:0,width:0});var l=Math.max(o.$.body.offsetHeight,o.$.documentElement.offsetHeight),h=1.2*Math.max(n.$.offsetWidth,o.$.body.scrollWidth);t.setStyles({height:l+"px",width:h+"px"}),e.fire("unlockSnapshot"),CKEDITOR.fire("mathexUpdateDone",t),i!=s?m():r=!1});function p(){(o=t.getFrameDocument()).getById("preview")||(CKEDITOR.env.ie&&t.removeAttribute("src"),o.write('<!DOCTYPE html><html><head><meta charset="utf-8"><script type="text/x-mathex-config">mathex.Hub.Config( {showMathMenu: false,messageStyle: "none",jax: ["input/TeX", "input/MathML", "output/CommonHTML"],extensions: ["tex2jax.js", "mml2jax.js", "MathMenu.js", "MathZoom.js"],TeX: { extensions: ["AMSmath.js", "AMSsymbols.js", "noErrors.js", "noUndefined.js"] },} );function getCKE() {if ( typeof window.parent.CKEDITOR == \'object\' ) {return window.parent.CKEDITOR;} else {return window.parent.parent.CKEDITOR;}}function update() {window.sessionStorage.setItem("MathjaxRenderStart",true);mathex.Hub.Queue([ \'Typeset\', mathex.Hub, this.buffer ],function() {getCKE().tools.callFunction( '+d+" );});}mathex.Hub.Queue( function() {getCKE().tools.callFunction("+h+');} );<\/script><script src="'+e.config.mathJaxLib+'"><\/script></head><body style="padding:0;margin:0;background:transparent;overflow:hidden"><span id="preview"></span><span id="buffer" style="display:none"></span></body></html>'))}function m(){r=!0,i=s,e.fire("lockSnapshot"),CKEDITOR.tools.htmlDecode(CKEDITOR.plugins.mathex.trim(i)).includes("math")?a.setHtml(CKEDITOR.tools.htmlDecode(CKEDITOR.plugins.mathex.trim(i))):a.setHtml(i),n.setHtml("<img src="+CKEDITOR.plugins.mathex.loadingIcon+" alt="+e.lang.mathex.loading+">"),t.setStyles({height:"16px",width:"16px",display:"inline","vertical-align":"middle"}),e.fire("unlockSnapshot"),o.getWindow().$.update(CKEDITOR.tools.htmlDecode(CKEDITOR.plugins.mathex.trim(i)))}return t.on("load",p),p(),{setValue:function(t){s=CKEDITOR.tools.htmlEncode(t),l&&!r&&m()}}};