-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.html
60 lines (53 loc) · 2.43 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- <script src="https://www.desmos.com/api/v1.4/calculator.js?apiKey=dcb31709b452b1cf9dc26972add0fda6"></script> -->
<script type="text/javascript" src="calculator.js"></script>
</head>
<body style="margin: 0;">
<div style="line-height: 46px;">
<input id="file" type="file" accept=".json" style="display: none;"/>
<button id="load">load</button>
<button id="save">save</button>
<input id="title" type="text" value="Untitled">
</div>
<div id="calculator" style="position: absolute; top: 46px; bottom: 0; left: 0; right: 0;"></div>
<script>
window.Calc = Desmos.GraphingCalculator(document.querySelector('#calculator'));
function loadDesmosState(jsonString){
var state = JSON.parse(jsonString);
window.Calc.setState(state);
};
document.querySelector('#file').onchange = function(e){
let file = e.target.files[0];
name = file.name.replace(/\.(json|JSON)$/, '');
document.querySelector('#title').value = name;
document.querySelector('head>title').text = name;
let reader = new FileReader();
reader.onload = function(e){
loadDesmosState(e.target.result);
};
reader.readAsText(file);
};
document.querySelector('#load').onclick = function(e){
document.querySelector('#file').click();
};
function download(content, fileName, contentType) {
let a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([content], {type: contentType}));
a.download = fileName;
a.click();
};
document.querySelector('#save').onclick = function(e){
let state = JSON.stringify(window.Calc.getState());
let title = document.querySelector('#title').value;
download(state, title+'.json', 'text/plain');
};
</script>
</body>
</html>
<!-- TODO: -->
<!-- load multiple scripts / import -->