-
Notifications
You must be signed in to change notification settings - Fork 4
/
websocket_math_with_graph.html
188 lines (173 loc) · 6.33 KB
/
websocket_math_with_graph.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="HandheldFriendly" content="true" />
<title>Websocket Math With Graph</title>
<link rel="stylesheet" href="../assets/style/reset.css" />
<link rel="stylesheet" href="../assets/style/components.css" />
<link rel="stylesheet" href="../assets/style/examples.css" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css"
integrity="sha384-AfEj0r4/OFrOo5t7NnNe46zW/tFgW6x/bCJG8FqQCEo3+Aro6EYUG4+cU+KJWu/X"
crossorigin="anonymous"
/>
<script
defer
src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js"
integrity="sha384-g7c+Jr9ZivxKLnZTDUhnkOnsh30B4H0rpLUpJ4jAIKs4fnJI+sEnkvrMWph2EDg4"
crossorigin="anonymous"
></script>
<script type="text/javascript" src="../assets/desmos/calculator.js"></script>
<script type="text/javascript" src="../../dist/iink.min.js"></script>
<style>
#graph,
#editor {
height: calc(100vh - 166px);
width: 50%;
}
@media only screen and (max-width: 600px) {
#graph,
#editor {
width: 100%;
height: calc(50vh - 83px);
}
}
</style>
</head>
<body>
<div id="result"></div>
<nav class="flex-container wrap between">
<div class="flex-container wrap gap">
<button id="clear" class="nav-btn btn-fab-mini btn-lightBlue" disabled>
<img src="../assets/img/clear.svg" />
</button>
<button id="undo" class="nav-btn btn-fab-mini btn-lightBlue" disabled>
<img src="../assets/img/undo.svg" />
</button>
<button id="redo" class="nav-btn btn-fab-mini btn-lightBlue" disabled>
<img src="../assets/img/redo.svg" />
</button>
</div>
</nav>
<div class="flex-container wrap">
<div id="editor" touch-action="none"></div>
<div id="graph" class="graph"></div>
</div>
<script>
const editorElement = document.getElementById("editor");
const resultElement = document.getElementById("result");
const clearElement = document.getElementById("clear");
const undoElement = document.getElementById("undo");
const redoElement = document.getElementById("redo");
const graphEl = document.getElementById('graph')
const colors = ['#0060A0', '#09E05C', '#E06A09']
const calculator = Desmos.Calculator(graphEl, {
keypad: false,
graphpaper: true,
expressions: false,
settingsMenu: false,
zoomButtons: false,
expressionsTopbar: false,
solutions: false,
border: false,
lockViewport: false,
expressionsCollapsed: true
})
function cleanLatex(latexExport) {
if (latexExport.includes("\\\\")) {
const steps = "\\begin{align*}" + latexExport + "\\end{align*}";
return steps.replace("\\begin{aligned}", "").replace("\\end{aligned}", "").replace(new RegExp("(align.{1})", "g"), "aligned");
}
return latexExport.replace(new RegExp("(align.{1})", "g"), "aligned");
}
function extractExpressions (latex) {
if (!latex) return ['']
const expressions = latex.split('\\\\')
return expressions.map(e => {
return e.replace('\\dfrac', '\\frac')
.replace('\\begin{aligned}', '')
.replace('\\end{aligned}', '')
.replace('\\begin{cases}', '')
.replace('\\end{cases} ', '')
}
)
}
/**
* we expose the editor for use in the integration test
*/
let editor;
/**
* We use a function to wait the server configuration to load
*/
async function loadEditor() {
const res = await fetch("../server-configuration.json");
const conf = await res.json();
const options = {
configuration: {
server: {
...conf,
protocol: "WEBSOCKET"
},
recognition: {
type: "MATH",
math: {
mimeTypes: ["application/x-latex"]
}
}
}
};
/**
* Instanciate editor
* @param {Element} The DOM element to attach the ink paper
* @param {Object} The Editor parameters
*/
editor = new iink.Editor(editorElement, options);
/**
* async initialize editor behaviors
*/
await editor.initialize();
editor.events.addEventListener("changed", (event) => {
undoElement.disabled = !event.detail.canUndo;
redoElement.disabled = !event.detail.canRedo;
clearElement.disabled = !event.detail.canClear;
});
editor.events.addEventListener("exported", (evt) => {
const exports = evt.detail;
resultElement.innerHTML =
event.detail && event.detail["application/vnd.myscript.jiix"] ? event.detail["application/vnd.myscript.jiix"].label : "";
colors.forEach((color, index) => {
calculator.setExpression({ id: index, latex: '', color: colors[index] });
});
if (exports && exports["application/x-latex"]) {
try {
katex.render(cleanLatex(exports["application/x-latex"]), resultElement);
} catch (error) {
resultElement.innerHTML = '<span>' + cleanLatex(exports['application/x-latex']) + '</span>';
}
extractExpressions(exports['application/x-latex']).forEach((val, index) => {
calculator.setExpression({ id: index, latex: val, color: colors[index] });
})
}
});
clearElement.addEventListener("click", async () => {
editor.clear();
});
undoElement.addEventListener("click", () => {
editor.undo();
});
redoElement.addEventListener("click", () => {
editor.redo();
});
window.addEventListener("resize", () => {
editor.resize();
});
};
loadEditor().catch(error => console.error(error));
</script>
</body>
</html>