-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCalculator.html
149 lines (130 loc) · 3.79 KB
/
Calculator.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1">
<link rel="icon" href="favicon.ico">
<title>Text-Field Calculator</title>
<link rel="stylesheet" href="main.css">
<script src="jquery-3.6.0.min.js"></script>
<script src="https://unpkg.com/[email protected]/lib/browser/math.js"></script>
<script>
function getType(o) {
return Object.prototype.toString.call(o).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
}
function insert(base, pos, insert) {
return base.substr(0, pos) + insert + base.substr(pos)
}
// https://stackoverflow.com/a/6234804/8213163
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
math.config({
number: "BigNumber",
precision: 32
});
// Extra functions
math.import({
divmod: (x, n) => math.matrix([Math.floor(x / n), x % n]),
signatures: (f) => f.signatures,
});
$(function() {
var output = $(".output");
var input = $(".input");
input.val(localStorage.getItem("content") ?? "");
function evaluateInput() {
var expression = input.val();
var result;
try {
result = math.evaluate(expression);
} catch(e) {
result = e;
}
var type = getType(result);
if (type == "error") {
var position = result.char;
if (position) {
const escaped = escapeHtml(expression);
const column = position - escaped.lastIndexOf("\n", position - 1) - 2;
const endOfLine = escaped.indexOf("\n", position) == -1 ? escaped.length : escaped.indexOf("\n", position);
const escapedMessage = escapeHtml(result.message);
const message = escapedMessage.substring(0, escapedMessage.length - ` (char ${position})`.length);
output.html(insert(escaped, endOfLine + 1, `<div class='error'>${' '.repeat(column)}^ ${message}</div>`));
} else {
output.html($("<div>").addClass("error").text(result.message));
}
} else {
if (result === undefined) {
result = "";
} else if (result.type === "ResultSet") {
result = result.entries.map(entry => math.format(entry, { notation: "fixed" })).join("\n");
} else {
result = math.format(result, { notation: "fixed" });
}
output.text(result);
}
// Put text in localStorage to persist data
localStorage.setItem("content", expression);
}
input.on("input", function(e) {
evaluateInput();
}).on("keydown", function(e) {
if (e.key == "Tab") {
if (!output.hasClass("error")) {
input.select();
var text = output.text();
if (!document.execCommand("insertText", false, text))
{
input[0].setRangeText(text, 0, input[0].selectionEnd, "end");
}
}
return false;
}
});
evaluateInput();
})
</script>
<style>
body {
display: flex;
flex-direction: column;
min-height: calc(100vh - 20px);
margin: 10px;
}
.input {
width: 100%;
box-sizing: border-box;
height: 30vh;
resize: vertical;
margin-bottom: .5em;
font-size: inherit;
}
.output {
font-family: monospace;
white-space: pre-wrap;
}
.error {
color: rgb(255, 50, 50);
}
</style>
</head>
<body>
<header>
<b>Text-Field Calculator</b>
<a href="Calculator-v2.html">Version 2</a>
</header>
<textarea class="input" autofocus></textarea>
<div class="output-wrap">
<div style="font-weight: bold;">Output:</div><div class="output"></div>
</div>
<footer>
<a href="https://mathjs.org/docs/index.html#documentation">Documentation</a>
<a href="https://github.com/samfundev/TextFieldCalculator">Source</a>
</footer>
</body>
</html>