-
Notifications
You must be signed in to change notification settings - Fork 17
/
localStorage.js
252 lines (219 loc) · 7 KB
/
localStorage.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import * as THREE from 'three';
import {
checkIfSubStringIsMatrix4,
checkIfSubStringIsVector3,
} from '@ud-viz/utils_shared';
import { createLabelInput } from './html';
/**
* If there was a matrix4 recorded in localStorage restore it matrix4 + when tab is closing record current matrix4
* /!\ calling this function make window reference the matrix4 avoiding it to be GC and could cause memory leak
*
* @param {THREE.Matrix4} matrix4 - matrix4 to track
* @param {string} key - key of the item in localstorage
* @returns {boolean} true if matrix4 has been setted with localStorage
*/
export function localStorageSetMatrix4(matrix4, key) {
// listen the close tab event
window.addEventListener('beforeunload', () => {
const matrix4String = matrix4.toArray().toString();
if (checkIfSubStringIsMatrix4(matrix4String.split(','))) {
localStorage.setItem(key, matrix4String);
}
});
// check if there was a previous matrix4
const storedMatrixArrayString = localStorage.getItem(key);
if (
typeof storedMatrixArrayString === 'string' &&
checkIfSubStringIsMatrix4(storedMatrixArrayString.split(','))
) {
let error = false;
try {
const storedMatrixArray = storedMatrixArrayString
.split(',')
.map((x) => parseFloat(x));
matrix4.fromArray(storedMatrixArray);
return true;
} catch (e) {
error = true;
console.error(e);
}
return !error;
}
return false;
}
/**
* If there was a vector 3 recorded in localStorage within the key passed copy values in vector3 + when tab is closing record current vector3 values
* /!\ calling this function make window reference the vector3 avoiding it to be GC and could cause memory leak
*
* @param {THREE.Vector3} vector3 - vector 3 to track
* @param {string} key - key of the item in localstorage
* @returns {boolean} true if vector3 has been setted with localStorage
*/
export function localStorageSetVector3(vector3, key) {
if (!vector3) throw new Error('no vector3');
if (!key) throw new Error('no key');
// listen the close tab event
window.addEventListener('beforeunload', () => {
const vector3String = vector3.toArray().toString();
if (checkIfSubStringIsVector3(vector3String.split(','))) {
localStorage.setItem(key, vector3String);
}
});
// check if there was a previous vector3 register within this key
const storedVector3String = localStorage.getItem(key);
if (
typeof storedVector3String === 'string' &&
checkIfSubStringIsVector3(storedVector3String.split(','))
) {
let error = false;
try {
const storedVector3Array = storedVector3String
.split(',')
.map((x) => parseFloat(x));
const storedVector3 = new THREE.Vector3().fromArray(storedVector3Array);
vector3.copy(storedVector3);
} catch (e) {
error = true;
console.error(e);
}
return !error;
}
return false;
}
/**
*
* @param {string} keyLocalStorage - the key of the item in localstorage
* @param {string} summaryText - what text to display
* @param {HTMLElement|null} [parent=null] - where to append result (optional)
* @returns {HTMLElement} - the html element created
*/
export const createLocalStorageDetails = (
keyLocalStorage,
summaryText,
parent = null
) => {
const details = document.createElement('details');
if (parent) parent.appendChild(details);
const summary = document.createElement('summary');
summary.innerText = summaryText;
details.appendChild(summary);
const item = localStorage.getItem(keyLocalStorage)
? JSON.parse(localStorage.getItem(keyLocalStorage))
: null;
if (item && item.opened) details.open = true;
window.addEventListener('beforeunload', () => {
localStorage.setItem(
keyLocalStorage,
JSON.stringify({ opened: details.open })
);
});
return details;
};
/**
*
* @param {string} keyLocalStorage - the key of the item in localstorage
* @param {string} labelText - what text to display
* @param {HTMLElement} inputParent - where to append
* @param {boolean} defaultValue - value to initialize with
* @returns {HTMLElement} - the html element created
*/
export const createLocalStorageCheckbox = (
keyLocalStorage,
labelText,
inputParent,
defaultValue = false
) => {
const { input, parent } = createLabelInput(labelText, 'checkbox');
inputParent.appendChild(parent);
const item = localStorage.getItem(keyLocalStorage)
? JSON.parse(localStorage.getItem(keyLocalStorage))
: null;
if (item) {
input.checked = item.checked;
} else {
input.checked = defaultValue;
}
window.addEventListener('beforeunload', () => {
localStorage.setItem(
keyLocalStorage,
JSON.stringify({ checked: input.checked })
);
});
return input;
};
/**
*
* @param {string} keyLocalStorage - the key of the item in localstorage
* @param {string} labelText - what text to display
* @param {HTMLElement} inputParent - where to append
* @param {object} options - options of the slider
* @param {number} options.min - min of the slider
* @param {number} options.max - max of the slider
* @param {number} options.step - step of the slider
* @returns {HTMLElement} - the html element created
*/
export const createLocalStorageSlider = (
keyLocalStorage,
labelText,
inputParent,
options = {}
) => {
const { input, parent } = createLabelInput(labelText, 'range');
inputParent.appendChild(parent);
input.min = options.min || 0;
input.max = options.max || 1;
input.step = options.step || 0.1;
const item = localStorage.getItem(keyLocalStorage)
? JSON.parse(localStorage.getItem(keyLocalStorage))
: null;
if (item) {
input.value = item.value;
} else {
input.value = options.defaultValue || input.max;
}
window.addEventListener('beforeunload', () => {
localStorage.setItem(
keyLocalStorage,
JSON.stringify({ value: input.value })
);
});
return input;
};
/**
*
* @param {string} keyLocalStorage - the key of the item in localstorage
* @param {string} labelText - what text to display
* @param {HTMLElement} inputParent - where to append
* @param {object} options - options of the slider
* @param {number} options.min - min of the slider
* @param {number} options.max - max of the slider
* @param {number} options.step - step of the slider
* @returns {HTMLElement} - the html element created
*/
export const createLocalStorageNumberInput = (
keyLocalStorage,
labelText,
inputParent,
options = {}
) => {
const { input, parent } = createLabelInput(labelText, 'number');
inputParent.appendChild(parent);
input.min = options.min || 0;
input.max = options.max || 1;
input.step = options.step || 0.1;
const item = localStorage.getItem(keyLocalStorage)
? JSON.parse(localStorage.getItem(keyLocalStorage))
: null;
if (item) {
input.value = item.value;
} else {
input.value = options.defaultValue || input.max;
}
window.addEventListener('beforeunload', () => {
localStorage.setItem(
keyLocalStorage,
JSON.stringify({ value: input.value })
);
});
return input;
};