-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfiles.js
121 lines (99 loc) · 2.57 KB
/
files.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
// :::::::::: ::::::::::: ::: :::::::::: ::::::::
// :+: :+: :+: :+: :+: :+:
// +:+ +:+ +:+ +:+ +:+
// :#::+::# +#+ +#+ +#++:++# +#++:++#++
// +#+ +#+ +#+ +#+ +#+
// #+# #+# #+# #+# #+# #+#
// ### ########### ########## ########## ########
const imageFileTypes = [
'image/apng'
,'image/avif'
,'image/gif'
,'image/jpeg'
,'image/png'
,'image/svg+xml'
,'image/webp'
,'image/bmp'
,'image/x-icon'
,'image/tiff'
]
function isImage (file) {
return imageFileTypes.indexOf(file.type) >= 0;
}
function defaultFilename () {
return 'Noteplace_' + date2str(new Date()) + '.json';
}
// Start file download.
_('#save').addEventListener('click', function () {
_('#modal-input').value = defaultFilename();
_('#modal-save').style.display = '';
_('#exampleModalLabel').innerHTML = 'Save to local file:';
_('#modal-save').onclick = function () {
download(
_('#modal-input').value,
JSON.stringify(saveToG())
);
};
_('#modal-list').innerHTML = '';
}, false);
// save everything to a single object
function saveToG (add_history = true) {
const G = {
T: _View.state.T,
S: _View.state.S,
nodes: (
add_history
? _NODES
: _NODES.filter(node => !node.deleted)
).map(stripNode),
places: stripPlace()
};
if (add_history) {
G.history = _HISTORY;
G.history_current_id = _HISTORY_CURRENT_ID;
}
return G;
}
_G = null;
// load everything from single object
function loadFromG (G) {
console.log('Loading..');
_G = G;
_View.goto(G, false, true);
// delete _PLACES;
if ('places' in G) {
_PLACES = G.places;
} else {
_PLACES = _PLACES_default;
}
fillPlaces();
// applyZoom([1*G.T[0],1*G.T[1]], 1*G.S);
$('.node').remove();
// delete _NODES;
_NODES = [];
gen_DOMId2nodej();
G.nodes.map(stripNode).forEach(node => newNode(node, false, true));
redraw();
if ( 'history' in G ) {
_HISTORY = G.history;
if ( 'history_current_id' in G ){
_HISTORY_CURRENT_ID = G.history_current_id;
} else {
_HISTORY_CURRENT_ID = lastHistoryID();
}
genHistIDMap();
fillHistoryList();
} else {
clearAllHistory();
}
console.log('Loading complete, now ' + _NODES.length + ' nodes');
}
_('#file').oninput = function () {
let fr = new FileReader();
fr.onload = function () {
console.log('Received file..');
loadFromG(JSON.parse(fr.result));
$('#file').value = '';
};
fr.readAsText(this.files[0]);
};