|
| 1 | +import {TeX} from "mathjax3/input/tex.js"; |
| 2 | +import {CHTML} from "mathjax3/output/chtml.js"; |
| 3 | +import {AbstractMathItem} from "mathjax3/core/MathItem.js"; |
| 4 | +import {AbstractMathDocument} from "mathjax3/core/MathDocument.js"; |
| 5 | +import {DOM} from "mathjax3/util/DOM.js"; |
| 6 | +import {handleRetriesFor} from "mathjax3/util/Retries.js"; |
| 7 | + |
| 8 | +let tex = new TeX(); |
| 9 | +let chtml = new CHTML(); |
| 10 | + |
| 11 | +let doc = new AbstractMathDocument(DOM.document,{OutputJax: chtml}); |
| 12 | + |
| 13 | +chtml.nodes.document = doc.document; |
| 14 | +DOM.document.head.appendChild(chtml.styleSheet(doc)); |
| 15 | + |
| 16 | +const Lab = DOM.window.Lab = { |
| 17 | + tex: DOM.document.getElementById('tex'), |
| 18 | + output: DOM.document.getElementById('output'), |
| 19 | + display: true, |
| 20 | + |
| 21 | + Typeset() { |
| 22 | + let TeX = this.tex.value; |
| 23 | + let math = new AbstractMathItem(TeX,tex); |
| 24 | + math.setMetrics(16,8,1000000,100000,1); |
| 25 | + math.display = this.display; |
| 26 | + this.jax = math; |
| 27 | + |
| 28 | + handleRetriesFor(function () { |
| 29 | + math.compile(); |
| 30 | + math.typeset(doc); |
| 31 | + }).then(() => Lab.Update(math.typesetRoot.outerHTML)) |
| 32 | + .catch(err => {console.log("Error: "+err.message); console.log(err.stack)}); |
| 33 | + }, |
| 34 | + |
| 35 | + Keep() { |
| 36 | + DOM.window.location.search = "?" + (this.display ? 1 : 0) + encodeURIComponent(this.tex.value); |
| 37 | + }, |
| 38 | + |
| 39 | + Update(html) { |
| 40 | + this.output.innerHTML = html; |
| 41 | + }, |
| 42 | + |
| 43 | + setDisplay(checked) { |
| 44 | + this.display = checked; |
| 45 | + this.Typeset(); |
| 46 | + }, |
| 47 | + |
| 48 | + checkKey: function (textarea, event) { |
| 49 | + if (!event) event = window.event; |
| 50 | + var code = event.which || event.keyCode; |
| 51 | + if ((event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) && |
| 52 | + (code === 13 || code === 10)) { |
| 53 | + if (event.preventDefault) event.preventDefault(); |
| 54 | + event.returnValue = false; |
| 55 | + this.Typeset(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | + |
| 61 | +if (DOM.window.location.search !== "") { |
| 62 | + Lab.tex.value = decodeURIComponent(DOM.window.location.search.substr(2)); |
| 63 | + Lab.display = DOM.window.location.search.substr(1,1) === '1'; |
| 64 | + DOM.document.getElementById('display').checked = Lab.display; |
| 65 | + Lab.Typeset(); |
| 66 | +} |
| 67 | + |
0 commit comments