-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
114 lines (91 loc) · 3.77 KB
/
storage.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
const DB_VERSION = 1;
const DEFAULT_MARKDOWN_TEXT = `**This space is for any of your short-term markdown notes**
You can use GitHub Flavoured Markdown:
- [ ] task lists are supported
- [x] mark items as complete as you finish them
Emojis are supported too! :smile:
Any links become clickable: http://example.com
...[and you can add your own text like this](http://example.com)
Read [the Markdown cheatsheet](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet) for more details!`;
const DEFAULT_PLAINTEXT = `This space is for any of your short-term plain-text notes.
Follow up claim -- call 01234561298. Reference XC12345567.`;
(quickjots => {
// Do nothing on the homepage
if (document.body.classList.contains('homepage')) return;
quickjots.storage = {
MAIN_STORE: 'quickjots',
METADATA_STORE: 'quickjots_metadata',
transact(mode, desiredStore, cb) {
try {
const transaction = this.db.transaction([desiredStore], mode);
transaction.onerror = e => console.error('Failed transaction', e);
const store = transaction.objectStore(desiredStore);
cb(null, transaction, store);
} catch (err) {
cb(err);
}
},
save(key, value, desiredStore, cb) {
this.transact('readwrite', desiredStore, (err, transaction, store) => {
if (err) return cb({ success: false, err });
try {
const request = store.put(
desiredStore === quickjots.storage.MAIN_STORE ?
{ type: key, text: value } : { name: key, value }
);
request.onsuccess = () => cb({ success: true });
request.onerror = () => cb({ success: false });
} catch (e) {
cb({ success: false, err: e });
}
});
},
get(key, desiredStore, cb) {
this.transact('readonly', desiredStore, (err, transaction, store) => {
if (err) return cb({ success: false, err });
const request = store.get(key);
request.onsuccess = () => cb({ success: true, value: request.result });
request.onerror = () => cb({ successs: false });
});
},
delete(key, desiredStore, cb) {
this.transact('readwrite', desiredStore, (err, transaction, store) => {
if (err) return cb({ success: false, err });
try {
const request = store.delete(key);
request.onsuccess = () => cb({ success: true });
request.onerror = () => cb({ success: false });
} catch (e) {
cb({ success: false, err: e });
}
});
}
};
const request = window.indexedDB.open('quickjots', DB_VERSION);
request.onerror = err => console.error('There was an error opening the database', err);
request.onsuccess = () => {
console.info('Database opened successfully');
quickjots.storage.db = request.result;
quickjots.restoreDBContents();
};
request.onupgradeneeded = e => {
const db = e.target.result;
const quickjotsStore = db.createObjectStore(quickjots.storage.MAIN_STORE, {
keyPath: 'type',
});
quickjotsStore.createIndex('text', 'text', { unique: false });
const metadataStore = db.createObjectStore(quickjots.storage.METADATA_STORE, {
keyPath: 'name',
});
metadataStore.createIndex('value', 'value', { unique: false });
const initialMarkdown = { type: 'markdown', text: DEFAULT_MARKDOWN_TEXT };
const initialPlaintext = { type: 'plaintext', text: DEFAULT_PLAINTEXT };
const initalDarkMode = { name: 'dark', value: false };
const records = [initialMarkdown, initialPlaintext];
records.forEach(record => quickjotsStore.put(record));
metadataStore.put(initalDarkMode);
// Show help -- new user
quickjots.toggleHelp(true);
console.info('Initial quickjots schema created');
};
})(window.quickjots = window.quickjots || {});