-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
230 lines (202 loc) · 6.62 KB
/
main.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
import './style.css'
import initialState from "./ext/initialState.js"
import { EditorState } from "@codemirror/state"
import { StreamLanguage } from '@codemirror/language'
import { EditorView, basicSetup } from "codemirror"
import { mathjs } from './mathjs.js'
import { insertExampleFunc } from "./examples.js";
const md = markdownit({ html: true })
.use(texmath, {
engine: katex,
delimiters: ['dollars', 'beg_end'],
katexOptions: { macros: { "\\RR": "\\mathbb{R}" } }
})
const wait = 300;
const tabsField = document.getElementById("tabs")
const insertButton = document.getElementById('exampleInsert')
const exampleSelect = document.getElementById('exampleSelector')
const outputs = document.getElementById("OUTPUT")
const numberOfSessions = 20
const listOfSessions = Array.from({ length: numberOfSessions }, (_, i) => i + 1)
let workerState = initialState
let parserState = {}
let lastTab = localStorage.getItem("lastTab") === null ? 1 : localStorage.getItem("lastTab")
let sessions = []
let sessionNames = []
for (let ID of listOfSessions) {
// Create tabs
const radioInput = document.createElement('input');
radioInput.type = 'radio';
radioInput.value = ID;
radioInput.name = 'sessionTab';
radioInput.id = 'tab' + ID;
radioInput.checked = lastTab == ID ? true : false;
tabsField.appendChild(radioInput);
const label = document.createElement('label');
label.htmlFor = 'tab' + ID;
label.id = 'tabL' + ID;
label.textContent = ID;
tabsField.appendChild(label);
// Create sessions
const thisSession = getSessionName(ID);
const sessionText = localStorage.getItem(thisSession)
sessions[ID] = null
// Remove empty sessions
if (sessionText && !sessionText.trim()) {
localStorage.removeItem(thisSession)
}
sessionNames[ID] = setSessionName(ID);
}
const tabIDs = document.forms.topBar.elements.sessionTab
tabIDs.value = lastTab
let timer;
sessions[lastTab] = createState(lastTab)
let editor = new EditorView({
state: sessions[lastTab],
parent: document.querySelector('#INPUT')
})
tabsField.addEventListener('change', () => {
const ID = tabIDs.value;
if (sessions[ID] === null) {
sessions[ID] = createState(ID);
}
sessions[lastTab] = editor.state
saveSession(lastTab)
editor.setState(sessions[ID]);
editor.focus()
localStorage.setItem("lastTab", ID)
lastTab = ID
})
insertButton.addEventListener('click', () => {
const exampleToInsert = insertExampleFunc(exampleSelect.value)
editor.dispatch({
changes: {
from: editor.state.doc.length,
to: editor.state.doc.length,
insert: "\n" + exampleToInsert
}
})
editor.focus()
})
let timerSave;
const waitToSave = 1000;
mathWorker.onmessage = function (oEvent) {
const results = JSON.parse(oEvent.data)
const tabToSave = tabIDs.value;
outputs.innerHTML = "";
results.outputs.forEach(out => {
switch (out.type) {
case "math":
out.text.forEach(e => {
const pre = document.createElement("pre");
if (e.visible) {
const type = e.type;
const value = e.result;
let div
switch (type) {
case "string":
div = document.createElement("code");
div.innerHTML = value;
pre.appendChild(div);
break;
case "any":
div = document.createElement("div");
div.textContent = value;
pre.appendChild(div);
break;
case "error":
div = document.createElement("div");
div.style.color = "red";
div.innerHTML = value;
pre.appendChild(div);
break;
case "plot":
div = document.createElement("div");
try {
Plotly.newPlot(div, e.result.data, e.result.layout, e.result.config)
} catch (error) {
div.innerHTML = 'myError:' + error.toString();
}
pre.appendChild(div);
break;
}
outputs.appendChild(pre);
}
});
break;
case "markdown":
const div = document.createElement("div");
div.innerHTML = md.render(out.text);
outputs.appendChild(div);
break;
}
});
clearTimeout(timerSave);
sessions[lastTab] = editor.state
timerSave = setTimeout(saveSession, waitToSave, tabToSave)
if (results.mathState) {
workerState = results.mathState
}
if (results.parserState) {
parserState = results.parserState
}
};
function createState(ID) {
// Create a new editor state
return EditorState.create({
doc: getSessionText(ID),
extensions: [
basicSetup,
StreamLanguage.define(mathjs(() => workerState, () => parserState)),
EditorView.lineWrapping,
EditorView.updateListener.of(update => {
if (update.docChanged) {
const text = update.state.doc.toString()
clearTimeout(timer);
timer = setTimeout(sendWorkToMathWorker, wait, text);
}
})
]
})
}
function getSessionName(ID) {
return 'localSession' + ID
}
function getSessionText(ID) {
return localStorage.getItem(getSessionName(ID)) || ""
}
function setSessionName(ID) {
const firstLineComment = /^\s*#\s*.*?(\w.*?)\s*(?:\n|$)/
const thisSession = getSessionName(ID);
let noteBookName
if (localStorage.getItem(thisSession)) {
const sessionText = localStorage.getItem(thisSession)
const foundName = firstLineComment.test(sessionText) ? sessionText.match(firstLineComment)[1] : null;
noteBookName = foundName ? foundName : "Notebook " + ID
document.getElementById('tabL' + ID).innerHTML = sessionText.trim() ? (foundName ? (noteBookName.length > 16 ? noteBookName.slice(0, 15).trim() + '…' : noteBookName) : String(ID)) : '.'
document.getElementById('tabL' + ID).title = sessionText.trim() ? noteBookName : 'Empty'
}
else {
document.getElementById('tabL' + ID).innerHTML = '.'
document.getElementById('tabL' + ID).title = 'Empty'
}
return noteBookName
}
function saveSession(sessionID) {
const thisSessionName = getSessionName(sessionID)
const thisSession = sessions[sessionID]
if (thisSession !== null) {
const textToSave = thisSession.doc.toString().replace(/\r\n/g, '\n');
localStorage.setItem(thisSessionName, textToSave)
sessionNames[sessionID] = setSessionName(sessionID)
}
}
function sendWorkToMathWorker(mathExpressoins) {
if (mathExpressoins != "") {
const expressions = mathExpressoins
.replace(/\r?\n/g, '\n')
.trim()
const request = { expr: expressions }
mathWorker.postMessage(JSON.stringify(request))
}
}