plugin.js 5.53 KB
Newer Older
1
CKEDITOR.plugins.add("mathjax",{lang:"af,ar,az,bg,ca,cs,cy,da,de,de-ch,el,en,en-au,en-gb,eo,es,es-mx,et,eu,fa,fi,fr,gl,he,hr,hu,id,it,ja,km,ko,ku,lt,lv,nb,nl,no,oc,pl,pt,pt-br,ro,ru,sk,sl,sq,sr,sr-latn,sv,tr,tt,ug,uk,vi,zh,zh-cn",requires:"widget,dialog",icons:"mathjax",hidpi:!0,isSupportedEnvironment:function(){return!CKEDITOR.env.ie||8<CKEDITOR.env.version},init:function(t){var e=t.config.mathJaxClass||"math-tex";t.config.mathJaxLib||CKEDITOR.error("mathjax-no-config"),t.widgets.add("mathjax",{inline:!0,dialog:"mathjax",button:t.lang.mathjax.button,mask:!0,allowedContent:"span(!"+e+")",styleToAllowedContentRules:function(t){return(t=t.getClassesArray())?(t.push("!"+e),"span("+t.join(",")+")"):null},pathName:t.lang.mathjax.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.mathjax.fixSrc}),this.parts.span.append(e)),this.once("ready",(function(){CKEDITOR.env.ie&&e.setAttribute("src",CKEDITOR.plugins.mathjax.fixSrc),this.frameWrapper=new CKEDITOR.plugins.mathjax.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)&&!(1<t.children.length||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?n.style+";display:inline-block":"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("mathjax",this.path+"dialogs/mathjax.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){t.data.dataValue=t.data.dataValue.replace(new RegExp("<span[^>]*?"+e+".*?</span>","ig"),(function(t){return t.replace(/(<iframe.*?\/iframe>)/i,"")}))}))}}),CKEDITOR.plugins.mathjax={},CKEDITOR.plugins.mathjax.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.mathjax.loadingIcon=CKEDITOR.plugins.get("mathjax").path+"images/loader.gif",CKEDITOR.plugins.mathjax.copyStyles=function(t,e){for(var a="color font-family font-style font-weight font-variant font-size".split(" "),n=0;n<a.length;n++){var i=a[n],s=t.getComputedStyle(i);s&&e.setStyle(i,s)}},CKEDITOR.plugins.mathjax.trim=function(t){var e=t.indexOf("\\(")+2,a=t.lastIndexOf("\\)");return t.substring(e,a)},CKEDITOR.plugins.mathjax.frameWrapper=CKEDITOR.env.ie&&8==CKEDITOR.env.version?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.mathjax.trim(CKEDITOR.tools.htmlEncode(a))),CKEDITOR.plugins.mathjax.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")}}}:function(t,e){function a(){(r=t.getFrameDocument()).getById("preview")||(CKEDITOR.env.ie&&t.removeAttribute("src"),r.write('<!DOCTYPE html><html><head><meta charset="utf-8"><script type="text/x-mathjax-config">MathJax.Hub.Config( {showMathMenu: false,messageStyle: "none"} );function getCKE() {if ( typeof window.parent.CKEDITOR == \'object\' ) {return window.parent.CKEDITOR;} else {return window.parent.parent.CKEDITOR;}}function update() {MathJax.Hub.Queue([ \'Typeset\', MathJax.Hub, this.buffer ],function() {getCKE().tools.callFunction( '+c+" );});}MathJax.Hub.Queue( function() {getCKE().tools.callFunction("+p+');} );<\/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 n(){d=!0,o=l,e.fire("lockSnapshot"),i.setHtml(o),s.setHtml("<img src="+CKEDITOR.plugins.mathjax.loadingIcon+" alt="+e.lang.mathjax.loading+">"),t.setStyles({height:"16px",width:"16px",display:"inline","vertical-align":"middle"}),e.fire("unlockSnapshot"),r.getWindow().$.update(o)}var i,s,o,l,r=t.getFrameDocument(),h=!1,d=!1,p=CKEDITOR.tools.addFunction((function(){s=r.getById("preview"),i=r.getById("buffer"),h=!0,l&&n(),CKEDITOR.fire("mathJaxLoaded",t)})),c=CKEDITOR.tools.addFunction((function(){CKEDITOR.plugins.mathjax.copyStyles(t,s),s.setHtml(i.getHtml()),e.fire("lockSnapshot"),t.setStyles({height:0,width:0});var a=Math.max(r.$.body.offsetHeight,r.$.documentElement.offsetHeight),h=Math.max(s.$.offsetWidth,r.$.body.scrollWidth);t.setStyles({height:a+"px",width:h+"px"}),e.fire("unlockSnapshot"),CKEDITOR.fire("mathJaxUpdateDone",t),o!=l?n():d=!1}));return t.on("load",a),a(),{setValue:function(t){l=CKEDITOR.tools.htmlEncode(t),h&&!d&&n()}}};