-
Notifications
You must be signed in to change notification settings - Fork 3
/
dlock.js
49 lines (49 loc) · 1.81 KB
/
dlock.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
//desmos lock by @JohnDoesStuff
if (window.location.href.includes("desmos.com/calculator")) {
if (typeof Calc != "undefined") { //thanks u/SlimRunner
var DLock = {};
DLock.getLock = function() { //gets the array of all lockable variables
var expressions = Calc.getState().expressions.list;
for (var i = 0; i < expressions.length; i++) {
if (expressions[i].latex) if (expressions[i].latex.startsWith("l_{ock}")) return expressions[i].id;
}
}
DLock.lastSelectedExpression = false;
DLock.set = function() {
if (Calc.isAnyExpressionSelected) DLock.lastSelectedExpression = Calc.selectedExpressionId;
var selected = DLock.lastSelectedExpression;
if (selected === false) {
window.alert("Please select an expression");
return
}
var id = DLock.getLock();
var lock = Calc.expressionAnalysis[id];
var values = lock.evaluation.value;
var vars = DLock.getExpression(id).latex.split("[")[1].split("\\right]")[0].split(",");
var expr = DLock.getExpression(selected);
var currentLatex = expr.latex;
for (var i = 0; i < vars.length; i++) {
currentLatex = currentLatex.split(vars[i]).join("\\left(" + values[i] + "\\right)");
}
expr.latex = currentLatex;
expr.id = "dlock" + (new Date()).getTime();
Calc.setExpression(expr);
}
DLock.getExpression = function(id) {
var expressions = Calc.getState().expressions.list;
for (var i = 0; i < expressions.length; i++) {
if (expressions[i].id === id) return expressions[i];
}
}
DLock.handler = function(e) {
if (e.altKey && ((e.code == "KeyL") || (e.key == "l"))) {
DLock.set();
}
}
document.addEventListener('keyup', DLock.handler);
} else {
window.alert("uh oh, something went wrong")
}
} else {
window.alert("this only works on desmos.com/calculator :v")
}