-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake.js
209 lines (182 loc) · 6.84 KB
/
make.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
var root = document.querySelector("#root");
var info = [{type:"none"}];
var uinfo = [];
var lastFocused={id:"e0"};
var pnum=0;
var n;
var l;
var holder;
var obj;
var num;
var e;
/*
elementToObserve = window.document.getElementById('out');
// create a new instance of `MutationObserver` named `observer`,
// passing it a callback function
observer = new MutationObserver(function(mutationsList, observer) {
makeCourse();
});
// call `observe` on that MutationObserver instance,
// passing it the element to observe, and the options object
observer.observe(elementToObserve, {characterData: false, childList: true, attributes: false});
*/
function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
function addEquation(toggle=true){
n = document.createElement("SPAN");
/*if(!!document.getElementById(lastFocused.id)){
$(n).insertAfter("#"+lastFocused.id);}
else{$(n).insertAfter("#"+(lastFocused.id-1));}*/
root.appendChild(n);
n.outerHTML = `<div class="rgba-indigo-strong form-control-sm answerb or" style="display: inline-block"><span id='e${info.length}' class='text-light' style="border:none !important;" onclick='lastFocused=this.parentElement;'> </span><button class="badge btn close" onclick="rmFromInfo('e${info.length}');rmFromDOM('e${info.length}')">×</button></div>`;
var answerMathField = MQ.MathField(document.querySelector("#e"+info.length), {
handlers: {}});document.getElementById("e"+info.length).click();
console.log("hi");
l = info.length;
info.push({type:"mq",
id: "e"+info.length,
elem: document.querySelector("#e"+info.length),
mathfield: answerMathField
});
uinfo.push({type:"mq",
id: "e"+l,
elem: document.querySelector("#e"+l),
mathfield: answerMathField
});
if (toggle){
answerMathField.focus();}
return answerMathField
};
function addDisplayEquation(toggle=true){
n = document.createElement("DIV");
root.appendChild(n);
/*if(!!document.getElementById(lastFocused.id)){
$(holder).insertAfter("#"+lastFocused.id);}
else{$(holder).insertAfter("#"+(lastFocused.id-1));}*/
n.outerHTML = `<div class="form-control-sm answerb light-blue darken-2 or mt-0 text-center" onclick="info[${info.length}].mathfield.focus()" ><span id="e${info.length}" style="border:none !important;" class="answerb form-control-sm text-light or" onclick="lastFocused = this.parentElement.parentElement;"></span><button class="badge btn close float-right" onclick="rmFromInfo('e${info.length}');rmFromDOM('e${info.length}')">×</button></div>`;
var answerMathField = MQ.MathField(document.querySelector("#e"+info.length), {
handlers: {}});
l = info.length;
info.push({type:"dmq",
id: "e"+info.length,
elem: document.querySelector("#e"+info.length),
mathfield: answerMathField
});
uinfo.push({type:"dmq",
id: "e"+l,
elem: document.querySelector("#e"+l),
mathfield: answerMathField
});
n.onclick=`info[${info.length}].mathfield.focus()`;
if (toggle){
answerMathField.focus()}
return answerMathField
};
function addText(){
n = document.createElement("span");
/*if(!!document.getElementById(lastFocused.id)){
$(n).insertAfter("#"+lastFocused.id);}
else{$(n).insertAfter("#"+(lastFocused.id-1));}*/
root.appendChild(n);
n.outerHTML = `<span class="form-control-sm answerb my-2 input-sm rgba-green-strong or"><span contenteditable="true" id="e`+info.length+`" class=" or text-light" style="font-size:14pt" onfocus="lastFocused=this.parentElement;"></span><button class="badge btn close" onclick="rmFromInfo('e${info.length}');rmFromDOM('e${info.length}')">×</button></span>`;
document.getElementById("e"+info.length).focus();
l = info.length;
info.push({type:"text",
id: "e"+l,
elem: document.querySelector("#e"+l)
});
};
function addBreak(){
n = document.createElement("DIV");
/*if(!!document.getElementById(lastFocused.id)){
$(n).insertAfter("#"+lastFocused.id);}
else{$(n).insertAfter("#"+(lastFocused.id-1));}*/
root.appendChild(n);
n.outerHTML=`<div class="row"><hr class="rgba-lime-strong col-11" id="e`+info.length+`" onclick="lastFocused=this.parentElement;"><button class="badge btn close float-right col" onclick="rmFromInfo('e${info.length}');rmFromDOM('e${info.length}')">×</button><span>`;
document.getElementById("e"+info.length).click();
l = info.length;
info.push({type:"break",
id: "e"+l,
elem: document.querySelector("#e"+l)
});
};
function addItem(){
n = document.createElement("li");
if(!!document.getElementById(lastFocused.id)){
$(n).insertAfter("#"+lastFocused.id);}
else{$(n).insertAfter("#"+(lastFocused.id-1));}
n.outerHTML = '<li ><span contenteditable="true" id="e'+info.length+'" class="form-control-sm answerb my-2 input-sm bg-white" style="font-size:14pt" onfocus="lastFocused=this;"></span></li>';
document.getElementById("e"+info.length).focus();
l = info.length;
info.push({type:"item",
id: "e"+l,
elem: document.querySelector("#e"+l)
});
uinfo.push({type:"item",
id: "e"+l,
elem: document.querySelector("#e"+l)
});
};
function makeCourse(data){
var final = "<p>";
var lat = "";
for (num in data){
obj = data[num];
if (obj.type == "text"){
e = document.createElement("p");
e.innerHTML = document.querySelector("#"+obj.id).innerHTML;
final += e.innerHTML;
lat += e.innerHTML
}
if (obj.type == "item"){
e = document.createElement("li");
e.innerHTML = document.querySelector("#"+obj.id).innerHTML;
final += e.outerHTML;
if (e.innerHTML.slice(-4)=="<br>"){n = e.innerHTML.slice(0,-4)}else{n=e.innerHTML}
lat += "\\item " + n;
}
if (obj.type == "mq"){
e = document.createElement("p");
e.innerHTML = " $"+obj.mathfield.latex()+"$ ";
final += e.innerHTML;
lat += e.innerHTML
}
if (obj.type == "dmq"){
e = document.createElement("p");
e.innerHTML = " $$"+obj.mathfield.latex()+"$$ ";
final += e.innerHTML;
lat += "\n"+e.innerHTML+"\n"
}
if (obj.type =="break"){
e = document.createElement("BR");
//final+=e.outerHTML;
final += "</p><p>";
lat += "~\\\\~\\\\ \n"
}
}
final += "</p>"
document.querySelector("#out").innerHTML = final;
MathJax.typeset();
document.querySelector("#out2").innerHTML = decodeHtml(lat);
Prism.highlightAll();
tex=decodeHtml(lat);
}
function rmFromInfo(id){
for (n in info){
var b= info[n];
if (b.id==id){
info.splice(n,1);
return true
}
}
return false
}
function rmFromDOM(id){
var el = document.getElementById(id);
if (el.parentElement==root){el.remove()}
else if (el.parentElement.parentElement==root){el.parentElement.remove()}
else if (el.parentElement.parentElement.parentElement==root){el.parentElement.parentElement.remove()}
}