You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
4.0 KiB
JavaScript

tinymce.PluginManager.add("mathjax",function(t,e){let n=t.settings.mathjax.className||"math-tex",a=n+"-original",i=t.settings.mathjax.symbols||{start:"\\(",end:"\\)"},l=t.settings.mathjax.lib||null,o=[(t.settings.mathjax.configUrl||e+"/config.js")+"?class="+a];l&&o.push(l),t.on("init",function(){let e=t.getDoc().getElementsByTagName("script");for(let n=0;n<o.length;n++){let a=t.dom.uniqueId(),i=t.dom.create("script",{id:a,type:"text/javascript",src:o[n]}),l=!1;for(let t=0;t<e.length;t++)if(e[t].src==i.src){l=!0;break}l||t.getDoc().getElementsByTagName("head")[0].appendChild(i)}}),t.on("GetContent",function(e){let a=t.dom.create("div");a.innerHTML=e.content;let i=a.querySelectorAll("."+n);for(let t=0;t<i.length;t++){let e=i[t].querySelectorAll("span");for(let t=0;t<e.length;t++)e[t].remove();let n=i[t].getAttribute("data-latex");i[t].removeAttribute("contenteditable"),i[t].removeAttribute("style"),i[t].removeAttribute("data-latex"),i[t].innerHTML=n}e.content=a.innerHTML});let r=function(e){if(2!=e.childNodes.length){e.setAttribute("contenteditable",!1),e.style.cursor="pointer";let n=e.getAttribute("data-latex")||e.innerHTML;e.setAttribute("data-latex",n),e.innerHTML="";let i=t.dom.create("span");i.innerHTML=n,i.classList.add(a),e.appendChild(i);let l=t.dom.create("span");l.classList.add("dummy"),l.innerHTML="dummy",l.setAttribute("hidden","hidden"),e.appendChild(l)}};t.on("BeforeSetContent",function(e){let a=t.dom.create("div");a.innerHTML=e.content;let i=a.querySelectorAll("."+n);for(let t=0;t<i.length;t++)r(i[t]);e.content=a.innerHTML}),t.on("SetContent",function(e){t.getDoc().defaultView.MathJax&&(t.getDoc().defaultView.MathJax.startup.getComponents(),t.getDoc().defaultView.MathJax.typeset())}),t.on("Change",function(e){if(elements=t.dom.getRoot().querySelectorAll("."+n),elements.length){for(let t=0;t<elements.length;t++)r(elements[t]);t.getDoc().defaultView.MathJax&&(t.getDoc().defaultView.MathJax.startup.getComponents(),t.getDoc().defaultView.MathJax.typeset())}}),t.ui.registry.addToggleButton("mathjax",{text:"Σ",tooltip:"Mathjax",onAction:function(){let e=t.selection.getNode(),a=void 0;e.classList.contains(n)&&(a=e),s(a)},onSetup:function(e){return t.selection.selectorChangedWithUnbind("."+n,e.setActive).unbind}}),t.on("click",function(t){let e=t.target.closest("."+n);e&&s(e)});let s=function(e){let l=t.id+"_"+t.dom.uniqueId(),s="";e&&(latex_attribute=e.getAttribute("data-latex"),latex_attribute.length>=(i.start+i.end).length&&(s=latex_attribute.substr(i.start.length,latex_attribute.length-(i.start+i.end).length))),t.windowManager.open({title:"Mathjax",width:600,height:300,body:{type:"panel",items:[{type:"textarea",name:"title",label:"LaTex"},{type:"htmlpanel",html:'<div style="text-align:right"><a href="https://wikibooks.org/wiki/LaTeX/Mathematics" target="_blank" style="font-size:small">LaTex</a></div>'},{type:"htmlpanel",html:'<iframe id="'+l+'" style="width: 100%; min-height: 50px;"></iframe>'}]},buttons:[{type:"submit",text:"OK"}],onSubmit:function(a){let i=a.getData().title.trim();if(e)e.innerHTML="",e.setAttribute("data-latex",h(i)),r(e);else{let e=t.getDoc().createElement("span");e.innerHTML=h(i),e.classList.add(n),r(e),t.insertContent(e.outerHTML)}t.getDoc().defaultView.MathJax.startup.getComponents(),t.getDoc().defaultView.MathJax.typeset(),a.close()},onChange:function(t){var e=t.getData().title.trim();e!=s&&(p(e,document.getElementById(l)),s=e)},initialData:{title:s}});let d=document.getElementById(l),c=d.contentWindow||d.contentDocument.document||d.contentDocument,u=c.document,g=u.getElementsByTagName("head")[0],m=u.getElementsByTagName("body")[0],h=function(t,e){return e||(e=i),e.start+" "+t+" "+e.end},p=function(t){let e=c.MathJax,n=m.querySelector("div");n||((n=u.createElement("div")).classList.add(a),m.appendChild(n)),n.innerHTML=h(t,{start:"$$",end:"$$"}),e&&e.startup&&(e.startup.getComponents(),e.typeset())};p(s);for(let t=0;t<o.length;t++){let e=c.document.createElement("script");e.src=o[t],e.type="text/javascript",e.async=!1,e.charset="utf-8",g.appendChild(e)}}});