forked from saxarona/mathjax-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
299 lines (269 loc) · 10.4 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
var app = new Vue({
el: "#app",
data: {
theme: "default",
toolbox: "generic",
inputty: "",
autorender: true,
dlformat: "png",
showCheatSheet: false,
jaxRenderer: null,
mathInput: "TeX",
cTextColor: "#ffffff",
cBkgColor: "#333333",
cBorder: "",
// Loaded separately
// TODO avoid redundancy
themes: {},
groupedthemes: {},
groupedtoolboxes: {},
},
methods: {
inputCallback: function(e) {
if (this.autorender || e.which == 13)
this.render()
},
render: function() {
var el = document.getElementById("putty")
el.innerHTML = this.getPrerender()
MathJax.Hub.Typeset(el);
},
updateTheme: function() {
if(this.theme == "custom") {return}
this.cTextColor = this.themes[this.theme].text;
this.cBkgColor = this.themes[this.theme].bg;
if("border" in this.themes[this.theme]) {
this.cBorder = this.themes[this.theme].border;
} else {this.cBorder = "";}
},
updateInputMethod: function() {
this.render()
},
insertMath: function(tool) {
var text, offset;
switch (this.mathInput) {
case "AsciiMath":
text = (tool.hasOwnProperty("ascii")) ? tool.ascii:tool.latex;
break;
default:
text = tool.latex;
break;
}
switch (this.mathInput) {
case "AsciiMath":
offset = (tool.hasOwnProperty("asciiOffset")) ? tool.asciiOffset:((tool.hasOwnProperty("latexOffset")) ? tool.latexOffset:0);
break;
default:
offset = (tool.hasOwnProperty("latexOffset")) ? tool.latexOffset:0;
break;
}
// will give the current postion of the cursor
var el = document.getElementById("inputty")
// TODO, make things with nonzero offset insert things on both sides of the selected text.
var selStart = el.selectionStart + 0;
var selEnd = el.selectionEnd + 0;
// will get the value of the text area
var x = el.value
// setting the updated value in the text area
el.value = x.slice(0,selEnd)+text+x.slice(selEnd);
// Force re-render.
this.inputty = el.value
this.inputCallback({which: 0})
el.focus()
var tarPos = selEnd + offset + text.length
el.setSelectionRange(tarPos, tarPos)
},
getLink: function() {
// Compute data string
var data = ""
try {
data =
"base64=" + encodeURI(btoa(this.inputty))
} catch (err) {
if (err instanceof DOMException) {
try {
var enc = new TextEncoder("utf-8");
data = "b64_utf8=" + encodeURI(btoa(enc.encode(this.inputty)))
} catch(error) {alert("Failed to copy: " + error)}
} else alert("Failed to copy: " + err)
}
// Compute theme string
var theme = ""
if(this.theme=="custom")
theme = "theme=" + (this.cTextColor.replace("#","")+"-"+this.cBkgColor.replace("#",""))
else if(this.theme != "default")
theme = "this.theme"
// Compute toolbox string
var toolbox = (this.toolbox == "generic") ? "":"toolbox=" + this.toolbox
var inputMode = (this.mathInput == "TeX") ? "":"inputmode=" + this.mathInput
return location.href.split("#!")[0] + "#!" + [theme, toolbox, inputMode, data].filter(a => a != "").join("&")
},
//
// Download utilities
//
getPrerender: function() {
if(this.mathInput == "AsciiMath")
return "`" + this.inputty + "`";
else
return "\\[" + this.inputty + "\\]";
},
getJaxRenderer: function() {
if(Cookies.get("mjx.menu") != undefined && Cookies.get("mjx.menu").includes("renderer"))
return Cookies.get("mjx.menu").split("renderer:")[1].split("&;")[0]
else
return MathJax.Hub.config.jax[0].split("output/")[1]
},
getSVG: function(element) {
var root = element.childNodes[1].firstChild
var data = (root.nodeName == "svg") ? root.outerHTML : root.innerHTML;
const head = '<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" version="1.2"'
// TODO, preferably we would use DOM for this.
data = head + data.substring(4).replace(/currentColor/g, this.cTextColor).replace(/style="/, "style=\"background-color:"+this.cBkgColor+";")
return data
},
renderSVG: async function(callback) {
// TODO sleeping is a dirty fix to give MathJax time to set the renderer. This could probably be done more rigorously.
// This is also done for PNG.
var oldJaxRenderer = this.getJaxRenderer()
MathJax.Hub.setRenderer("SVG")
await sleep(200)
var element = document.createElement("DIV")
element.innerHTML = this.getPrerender();
MathJax.Hub.Typeset(element, function() {
MathJax.Hub.setRenderer(oldJaxRenderer)
callback(app.getSVG(element))
});
},
renderPNG: async function(callback) {
var oldJaxRenderer = this.getJaxRenderer()
MathJax.Hub.setRenderer("SVG")
await sleep(200)
var element = document.createElement("DIV")
element.innerHTML = this.getPrerender();
MathJax.Hub.Typeset(element, function() {
MathJax.Hub.setRenderer(oldJaxRenderer)
// Get the aspect ratio
var root = element.childNodes[1].firstChild
root = (root.nodeName == "svg") ? root:root.firstChild;
var width = parseFloat(root.getAttribute("width").replace(/ex/,"")),
height = parseFloat(root.getAttribute("height").replace(/ex/,"")),
scale = 128;
// See https://stackoverflow.com/questions/5433806/convert-embedded-svg-to-png-in-place
var svgData = app.getSVG(element),
can = document.createElement('canvas'),
ctx = can.getContext("2d"),
loader = new Image;
loader.width = can.width = scale*width//TARGET. Needs to be computed.
loader.height = can.height = scale*height//TARGET. ----------=---------
loader.onload = function() {
ctx.drawImage(loader, 0, 0, loader.width, loader.height);
callback(can.toDataURL())
}
loader.src = 'data:image/svg+xml,' + encodeURIComponent( svgData );
})
},
},
})
/*
async function clipboardPNG(evt) {
console.log("EVENT")
//app.renderPNG(function(data) {window.open(data)})
app.renderPNG(dataToClipboard)
}
async function dataToClipboard(data) {
if (!navigator.clipboard) {
alert("Your browser is too old.")
return
}
try {
await navigator.clipboard.writeText(data)
} catch (err) {
console.error('Failed to copy!', err)
}
}
*/
// TODO rename
getQuery = function(key) {
if(location.href.includes("#!") && location.href.includes(key + "=")) {
return location.href.split("#!")[1].split(key + "=")[1].split("&")[0]
} else {
return false
}
}
linkToClipboard = async function (event) {
if (!navigator.clipboard) {
alert("Your browser is too old.")
return
}
try {
await navigator.clipboard.writeText([app.getLink()])
} catch (err) {
console.error('Failed to copy!', err)
}
}
// Should be called with "await"
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
function fetchJSONFile(path, callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
httpRequest.open('GET', path);
httpRequest.send();
}
// Has to be done before the body onload below due to theme and toolbox loading
fetchJSONFile("themes.json", function(groupedthemes) {
for (var groupkey in groupedthemes) {
for (var themekey in groupedthemes[groupkey]) {
app.themes[themekey] = groupedthemes[groupkey][themekey]
}
}
app.groupedthemes = groupedthemes
})
fetchJSONFile("toolboxes.json", function(groupedtoolboxes) {
app.groupedtoolboxes = groupedtoolboxes
})
document.body.onload = function() {
try {
var inputty_b64_encoded = getQuery("base64")
var inputty_utf8_encoded = getQuery("b64_utf8")
var theme = getQuery("theme")
var toolbox = getQuery("toolbox")
var inputMode = getQuery("inputmode")
if(inputty_b64_encoded) {
var inputty_decoded = atob(decodeURI(inputty_b64_encoded))
app.inputty = inputty_decoded
app.render()
} else if(inputty_utf8_encoded) {
var dec = new TextDecoder("utf-8")
var buffer = new Uint8Array(atob(decodeURI(inputty_utf8_encoded)).split(",").map(parseFloat))
var inputty_decoded = dec.decode(buffer)
app.inputty = inputty_decoded
app.render()
}
if(theme && app.themes.hasOwnProperty(theme)) {
app.theme = theme
app.updateTheme()
} else if(theme && theme[6]=="-") {
app.theme = "custom"
theme = theme.replace(/\#/g, "")
app.cTextColor = "#"+theme.substr(0,6)
app.cBkgColor = "#"+theme.substr(7,6)
}
// TODO validate input
if(toolbox)
app.toolbox = toolbox
if(inputMode) {
app.mathInput = inputMode
app.render()
}
} finally {
}
}