-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
303 lines (240 loc) · 7.43 KB
/
renderer.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
300
301
302
303
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// No Node.js APIs are available in this process because
// `nodeIntegration` is turned off. Use `preload.js` to
// selectively enable features needed in the rendering
// process.
const math = require('mathjs');
const { remote } = require("electron");
const Store = require('electron-store');
const fs = require('fs');
const path = require('path');
const Combokeys = require("combokeys");
const { e } = require('mathjs');
var combokeys = new Combokeys(document);
require('combokeys/plugins/global-bind')(combokeys);
const hotkeys = new remote.Menu();
window.addEventListener("DOMContentLoaded", () => {
const minimizeButton = document.getElementById("minimize-btn");
const maxUnmaxButton = document.getElementById("max-unmax-btn");
const closeButton = document.getElementById("close-btn");
minimizeButton.addEventListener("click", e => {
window.minimizeWindow();
});
maxUnmaxButton.addEventListener("click", e => {
const icon = maxUnmaxButton.querySelector("i.far");
window.maxUnmaxWindow();
});
closeButton.addEventListener("click", e => {
window.closeWindow();
});
const expressionElement = document.getElementById("expression");
const answer = document.getElementById("answer");
const cmd = document.getElementById("cmd");
expressionElement.addEventListener("input", expressionListener);
cmd.addEventListener("keydown", commandListener);
combokeys.bindGlobal(['command+e', 'ctrl+e'], () => {document.getElementById("cmd").focus()});
combokeys.bindGlobal(['command+d', 'ctrl+d'], () => {document.getElementById("expression").focus()});
combokeys.bindGlobal(['command+s', 'ctrl+s'], () => {runCmd(["save"])});
combokeys.bindGlobal(['command+o', 'ctrl+o'], () => {runCmd(["open"])});
});
const store = new Store();
var currentFile = "";
var fileSaved = false;
function save(t) {
let filename = currentFile.toString();
if (t.length == 1 && filename === "") {
filename = remote.dialog.showSaveDialogSync(remote.getCurrentWindow(), {
filters: [
{ name: 'Plain Text', extensions: ['txt'] },
{ name: 'All Files', extensions: ['*'] }
]
});
} else if(filename === "") {
filename = t[1];
}
filename = path.resolve(filename);
filename = path.normalize(filename);
let doc = document.getElementById("expression").value;
if (typeof doc !== 'undefined') {
fs.writeFileSync(filename, doc);
currentFile = filename;
fileSaved = true;
updateTitleBar()
}
}
function open(t) {
let filename = "";
if (t.length == 1) {
filename = remote.dialog.showOpenDialogSync(remote.getCurrentWindow(), {
filters: [
{ name: 'Plain Text', extensions: ['txt'] },
{ name: 'All Files', extensions: ['*'] }
]
})
if(typeof(filename) === undefined) {
return
}
filename = filename[0].toString();
} else {
filename = path.resolve(t[1].replace(/['"]+/g, '')); //Regex strips double quotes
}
filename = path.resolve(filename);
filename = path.normalize(filename).toString();
let expr = document.getElementById("expression");
text = Buffer.from(fs.readFileSync(filename)).toString('utf-8');
expr.value = text;
currentFile = filename;
fileSaved = true;
updateTitleBar()
}
function commandListener(e) {
if (e.keyCode === 13) {
const cmd = document.getElementById("cmd").value;
let command = cmd.split(/ +(?=(?:(?:[^"]*"){2})*[^"]*$)/g); //Regex splits by spaces except for those in quotes
runCmd(command);
}
}
function runCmd(command) {
commands = {
"set": {
f: function (t) { store.set(t[1], t[2]) },
h: "Set Config Property"
},
"save": {
f: save,
h: "Save File"
},
"open": {
f: open,
h: "Open File"
}
}
for (key in commands) {
if (command[0] == key) {
commands[key].f(command);
documentEval();
}
}
cmd.value = "";
}
function updateTitleBar() {
let titlebar = document.getElementById("menu-bar");
let menubarPath = document.getElementById("menubar-path");
menubarPath.innerText = currentFile;
if(!fileSaved && currentFile != "") {
menubarPath.innerText = "*" + menubarPath.innerText; //The most important information of a path is on the right, so the paths text is overflowed on the left. To achievee this the text direction is set to rtl which messes with appending in js for some reason?
}
}
function expressionEvaluate(text) {
text = text.split("\n", -1);
let result = [];
let scope = {};
for (let i = 0; i < text.length; i++) {
let n = ""
try {
let r = math.evaluate(text[i], scope);
let precision = 4;
if (typeof store.get("precision") !== 'undefined') {
precision = store.get("precision");
}
if (typeof scope.precision !== 'undefined') {
precision = scope.precision;
}
n = math.format(r, Math.round(precision));
}
catch (error) {
n = "Syntax Error"
}
if (n !== "undefined") {
result[i] = n;
} else {
result[i] = "";
}
}
return result.join("<br>");
}
function algebraEvaluate(text) {
text = text.split("\n", -1);
keywords = {
'solve': 'solve',
'#': 'solve',
'equation': 'eq',
'eq': 'eq',
'!': 'eq',
'simplify': 'simp',
'simp': 'simp',
'@': 'simp',
'expression': 'exp',
'exp': 'exp',
'derive': 'derive',
'der': 'derive',
'\\': 'derive',
}
result = []
parsed = []
scope = {}
for (let i = 0; i < text.length; i++) {
let line = text[i];
let r = "";
if(line == "") {
continue;
}
let keyword = 'exp'
for(let key in keywords) {
if(line.startsWith(key)) {
keyword = keywords[key];
line = line.slice(key.length);
line = line.trim();
break;
}
}
try {
if(keyword == "exp") {
r = math.evaluate(line, scope)
} else if (keyword == "eq") {
} else if (keyword == "simp") {
r = math.simplify(line, scope)
} else if (keyword == "solve") {
} else if (keyword == "derive") {
variable = line.split(' ')[0];
line = line.split(' ').slice(1).join(' ');
r = math.derivative(line, variable);
}
} catch(e) {
console.log(e);
r = "Syntax Error";
}
if(r !== "undefined") {
let precision = 4;
if (typeof store.get("precision") !== 'undefined') {
precision = store.get("precision");
}
if (typeof scope.precision !== 'undefined') {
precision = scope.precision;
}
r = math.format(r, Math.round(precision));
result[i] = r;
} else {
result[i] = "";
}
}
return result.join("<br>");
}
function documentEval() {
let expressionElement = document.getElementById("expression");
let answer = document.getElementById("answer");
updateTitleBar();
expressionElement.rows = expressionElement.value.split("\n").length + 1;
//Todo set workspace using scope
if(store.get("workspace").toLowerCase() == 'alg' || store.get("workspace").toLowerCase() == 'algebra' || store.get("workspace").toLowerCase() == 'algebruh') { //TODO secret algebruh workspace that has seperate functionality
answer.innerHTML = algebraEvaluate(expressionElement.value);
} else {
answer.innerHTML = expressionEvaluate(expressionElement.value);
}
}
function expressionListener() {
fileSaved = false;
documentEval();
}
expressionListener();