-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.js
60 lines (48 loc) · 1.77 KB
/
model.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
import { DEFAULT_CODE } from "./constants.js";
import { LOCALE, LANG } from "./locale.js";
import { Storage } from "./storage.js";
import { Editor } from "./editor.js";
import { Grid } from "./grid.js";
import { encode, decode } from "./utils.js";
export { Model };
/*************************************************************************************************/
/* Model class */
/*************************************************************************************************/
class Model {
constructor() {
this.storage = new Storage();
this.editor = new Editor();
this.grid = new Grid();
}
async init() {
await this.storage.init();
await this.editor.init();
await this.grid.init();
// Load editor's font size.
let fontSize = this.storage.loadMetadata("__editorFontSize__");
fontSize = parseInt(fontSize);
if (fontSize) {
this.editor.editor.setFontSize(fontSize);
}
}
save(name) {
// Save editor's font size.
this.storage.storeMetadata(
"__editorFontSize__", parseInt(this.editor.editor.getFontSize()));
console.log(`Saving code listing: "${name}".`);
const code = this.editor.getCode();
const lang = LANG;
const data = this.grid.dump();
return this.storage.save(name, code, lang, data);
}
makeURL(name) {
let listing = this.save(name);
let data = {};
data[name] = listing;
const encoded = encode(data);
let url = new URL(window.location.href);
url.searchParams.set('name', name);
url.searchParams.set('data', encoded);
return url.toString();
}
}