-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
404 lines (340 loc) · 13.3 KB
/
index.html
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<!--META HTTP-EQUIV='Content-Security-Policy' CONTENT="default-src 'self' * 'unsafe-inline' 'unsafe-eval'; script-src 'self' 'unsafe-inline' *.wiris.net 'unsafe-eval' *; style-src 'self' 'unsafe-inline' *.wiris.net * ; img-src 'self' *; media-src 'self' * ; frame-src *;"--!>
<META HTTP-EQUIV='Content-Security-Policy' CONTENT="default-src 'self' * 'unsafe-inline' * *.damaru.org ; script-src 'self' 'unsafe-inline' * 'unsafe-eval' *.damaru.org *.googleapis.com; style-src 'self' 'unsafe-inline' * *.wiris.net *.googleapis.com; img-src * 'self' data: blob:;font-src * 'self' data:">
<meta content="utf-8" http-equiv="encoding">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!--meta http-equiv="Content-Security-Policy" content="default-src *; img-src https://*; child-src 'none';"-->
<link rel="stylesheet" href="mathquill/mathquill.css"/>
<link rel="stylesheet" href="material-button.css"/>
<script>var exports = {};</script>
<script src="mathquill/mathquill.min.js"> </script>
<!--script src="evaluatex/dist/evaluatex.min.js"></script-->
<script src="https://www.wiris.net/demo/hand/hand.js"></script>
<script src="nerdamer/all.min.js"></script>
<script src="mathml2latex/dist/mathml2latex.js"></script>
<script defer src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML'></script>
</head>
<style>
/*
Name: Powerline.css
Author: @vivekascoder (Github/Facebook/Instagram/Gmail)
Email: [email protected]
Website: https://github.com/vivekascoder/powerline.css
*/
/* Font: FiraCode
@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@500&display=swap');
*/
/* Dealing with the Powerline */
*{
padding: 0;
margin: 0;
/* font-family: 'Fira Code';*/
}
.powerline{
display: flex;
justify-content: left;
vertical-align: middle;
}
.powerline__prompt{
background-color: yellow;
font-size: 3em;
font-weight: 500;
position: relative;
padding: 3px 1.25rem;
margin-right: -14px;
cursor: pointer;
/*clip-path: polygon(0% 0%, calc(100% - 14px) 0%, 100% 50%, calc(100% - 14px) 100%, 100% 100%, 0% 100%, 14px 50%);*/
clip-path: polygon(0% 0%, calc(100% - 14px) 0%, 100% 50%, calc(100% - 14px) 100%, 100% 100%, 0% 100%, 0 50%);
}
.powerline__component{
background-color: yellow;
font-size: 3em;
font-weight: 500;
position: relative;
padding: 3px 1.25rem;
margin-right: -14px;
cursor: pointer;
clip-path: polygon(0% 0%, calc(100% - 14px) 0%, 100% 50%, calc(100% - 14px) 100%, 100% 100%, 0% 100%, 14px 50%);
}
/* Background */
.dracula-background{background-color: #282a36;}
.dracula-currentline{background-color: #44475a;}
.dracula-foreground{background-color: #f8f8f2;}
.dracula-comment{background-color: #6272a4;}
.dracula-cyan{background-color: #8be9fd;}
.dracula-green{background-color: #50fa7b;}
.dracula-orange{background-color: #ffb86c;}
.dracula-pink{background-color: #ff79c6;}
.dracula-purple{background-color: #bd93f9;}
.dracula-red{background-color: #ff5555;}
.dracula-yellow{background-color: #f1fa8c;}
.dracula-blue{background-color: #2196f3;}
/* Foreground */
.dracula-text-light{color: #f8f8f2;}
.dracula-text-dark{color: #282a36;}
#math-field {
float:left;
width: 29vw;
height: 25vh;
margin: 2px;
}
#wiris{
padding: 1px;
float:left;
width: 69vw;
height: 25vh;
margin: 2px;
}
.question, .answer {
font-size: 3em;
width: 100%;
background-color: pink;
font-weight: 500;
position: relative;
padding: 3px 1.25rem;
margin-right: -14px;
cursor: pointer;
clip-path: polygon(0% 0%, calc(100% - 14px) 0%, 100% 50%, calc(100% - 14px) 100%, 100% 100%, 0% 100%, 14px 50%);
}
.question{
border-top: black 1px solid;
}
.answer{
background-color: white;
border-bottom: black 1px dashed;
}
#toolbar {
position: fixed;
bottom: 0;
height: auto;
width: 100vw;
padding: 5;
overflow: hidden;
background: white;
border-top: black 2px dashed;
}
#toolbar button{
margin: 10px;
}
#prompt{
padding: 5px;
}
#log {
overflow: scroll;
position: fixed;
height: 60vh;
width: 100%;
}
</style>
<div id=log> </div>
<div id=toolbar>
<div id=prompt> Type Your Question</div>
<div>
<div id=math-field class=mathfield> </div>
<div id="wiris" class="editor-container"></div>
</div>
<div id='buttons' style=" clear: both;">
<button class="mui-btn mui-btn--danger mui-btn--raised" style="float:left;margin-left:15%" onclick="do_clear()" > clear </button>
<button id=show class="mui-btn mui-btn--danger mui-btn--raised" style="float:right;margin-right:5%;display: none;" onclick="do_show()" > Show </button>
<button class="mui-btn mui-btn--primary mui-btn--raised" style="float:right;margin-right:15%;" onclick="check_ans(window._mf)" > OK </button>
</div>
</div>
<script>
function do_show(){
_mf.latex(nerdamer(window._answer).latex()); check_ans(_mf)
}
function do_clear(){
window._mf.latex('');
window._question = '';
window._answer = '';
hand.clear();
window._current = '';
prompt('Type Your Question:')
document.getElementById('log').innerHTML = window._log + window._current;
do_mathjax();
}
function do_mathjax(){
if (window.MathJax && window.MathJax.Hub && typeof window.MathJax.Hub.Typeset !== 'undefined') {
window.MathJax.Hub.Typeset()
}
if (window.MathJax && typeof window.MathJax.typeset !== 'undefined') {
window.MathJax.typeset()
}
}
function prompt(str) {
document.getElementById('prompt').innerHTML = str;
}
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
window._log = '';
function replace_symbols(txt){
if (txt.match(/\[x\]/)){
txt = txt.replace(/\[x\]/, "*");
console.log("fixmul:", txt);
}
if(txt.match(/sqrt\[\d+\]\(\d+\)/)){
txt = txt.replace(/sqrt\[(\d+)\]\((\d+)\)/, "($2)^(1/($1))");
}
if (txt.match(/\[\/\]/)){
txt = txt.replace(/\[\/\]/, "/");
console.log("fixdiv:", txt);
}
if (txt.match(/\d+\s+\(/) ){
txt = txt.replace(/(\d)\s+\(/, "$1+(");
console.log("fix:", txt);
}
return txt;
}
function check_ans(mf){
var ltx = mf.latex();
var txt = replace_symbols(mf.text());
console.log("ltx", ltx)
console.log("txt", txt)
if(txt === "")
return;
var id = makeid(10);
var log = document.getElementById('log');
if(window._question == ''){
window._question = ltx;
window._errors = 0;
document.getElementById('show').style.display = 'none';
window._answer = nerdamer(txt, ['numer']).evaluate().text();
window._current = `<div class=powerline> <span class="powerline__prompt dracula-blue"> Q${window._qcount}: </span> <span class=" question" id="q${window._qcount}" >\\( ${ltx} \\)<br id=${id}> </span></div>`;
prompt('Type Your Answer:')
} else if(window._question === ltx){
window._current += `<div class=powerline> <span class="powerline__prompt dracula-red">😟</span> <span class="answer" style=color:red> you can't just type the question back <br id=${id}> </span></div>`;
window._errors++;
} else {
console.log("ans", window._answer)
console.log("q", window._question)
console.log("YOU", nerdamer(txt,['numer']).evaluate().text())
if(txt === window._answer || window._answer === nerdamer(txt,['numer']).evaluate().text()){
window._current += `<div class=powerline> <span class="powerline__prompt dracula-green">😀</span> <span class="answer" style=color:#004400> \\( ${ltx} ${txt!=_answer?" = " + _answer:""}\\) <br id=${id}> </span></div><hr>`;
document.getElementById('show').style.display = 'none';
//window._current += `<span style=color:green class=answer>Correct Answer: \\( ${ltx} \\) </span> <br id=${id}><hr>`;
window._answer = window._question = '' ;
prompt('Type Your Question:')
_qcount +=1 ;
window._log += window._current;
window._current = '';
} else {
window._current += `<div class=powerline> <span class="powerline__prompt dracula-red">😟</span><span class="answer" style=color:red > \\( ${ltx}\\) (wrong answer) <br id=${id}> </span></div>`;
window._errors++;
//window._current += `<span style=color:red class=answer> Wrong Answer: \\( ${ltx} \\)<br id=${id}> </span>`;
prompt('Try Again: ')
if(window._errors > 5){
document.getElementById('show').style.display = 'block';
}
}
}
log.innerHTML = window._log + window._current;
log.style.height = (window.visualViewport.height - document.getElementById('toolbar').getBoundingClientRect().height )+'px';
setTimeout(()=>{ document.getElementById(id).scrollIntoView({block:"end"}); }, 100);
do_mathjax();
mf.latex('');
hand.clear();
}
var mathFieldSpan = document.getElementById('math-field');
//var latexSpan = document.getElementById('answer');
var MQ = MathQuill.getInterface(2); // for backcompat
var mathField = MQ.MathField(mathFieldSpan, {
//spaceBehavesLikeTab: true, // configurable
autoCommands: "frac",
handlers: {
edit: function() { // useful event handlers
//latexSpan.value = mathField.latex(); // simple API
},
enter: function() {
check_ans(mathField);
}
}
});
mathField.focus();
window._mf = mathField;
window._qcount = 1;
window._question = '';
window._answer = '';
function getDOM(xmlstring) {
parser=new DOMParser();
return parser.parseFromString(xmlstring, "text/xml");
}
function remove_tags(node) {
var result = "";
var nodes = node.childNodes;
var tagName = node.tagName;
if (!nodes.length) {
if (node.nodeValue == "π") result = "pi";
else if (node.nodeValue == " ") result = "";
else result = node.nodeValue;
} else if (tagName == "mfrac") {
result = "("+remove_tags(nodes[0])+")/("+remove_tags(nodes[1])+")";
} else if (tagName == "msup") {
result = "Math.pow(("+remove_tags(nodes[0])+"),("+remove_tags(nodes[1])+"))";
} else for (var i = 0; i < nodes.length; ++i) {
result += remove_tags(nodes[i]);
}
if (tagName == "mfenced") result = "("+result+")";
if (tagName == "msqrt") result = "Math.sqrt("+result+")";
return result;
}
function stringifyMathML(mml) {
xmlDoc = getDOM(mml);
return remove_tags(xmlDoc.documentElement);
}
function getLaTeX(mathml, callback) {
var req = new XMLHttpRequest();
req.open("POST","https://www.wiris.net/demo/editor/mathml2latex", false);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
var params = "mml="+encodeURIComponent(mathml);
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status != 200) {
callback("Error generating LaTeX.");
}
else {
callback(req.responseText);
}
}
}
req.send(params);
}
var hand;
window.onload = function () {
com.wiris.js.JsHand.DEFAULT_RECOGNITION_DELAY=50;
hand = com.wiris.js.JsHand.newInstance( {'lineWidth':5, 'lineColor':'#030'} );
hand.setParams({'constraints': { 'symbols':["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "=", "+", "-", "/", "\u00D7", "\u00F7", ",", ".", "\u221A", "(", ")"] , 'structure':["General","Fractions","Radicals"] } });
hand.insertInto(document.getElementById('wiris'));
hand.addHandListener({
contentChanged: function (instance) {
//console.log(hand.getMathML())
var mml = hand.getMathML();
//window._mf.mathml(mml);
//getLaTeX(mml,(l)=>{console.log("lat", l); window._mf.latex(l); })
window._mf.latex(MathML2LaTeX.convert(hand.getMathML()))
//console.log(stringifyMathML(hand.getMathML()))
//window._mf.latex(stringifyMathML(hand.getMathML()));
//setTimeout(function(){ do_check(); }, 500)
},
recognitionError: function (instance, msg) {
//document.getElementById('answer').value = "Error: " + msg;
},
strokesChanged: function(instance) {
//window._mf.latex(stringifyMathML(hand.getMathML()));
window._mf.latex(MathML2LaTeX.convert(hand.getMathML()))
//setTimeout(function(){ do_check(); }, 500)
}
});
document.getElementById("wiris").focus();
};
</script>