-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraft.js
132 lines (100 loc) · 3.28 KB
/
draft.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
export class Draft {
constructor(text) {
this.text = text;
this.extract = text.substring(0,150);
this.uid = Math.random().toString(36).substring(2, 9);
}
static async init(text) {
const draft = new Draft(text);
draft.position = await Draft.count() + 1;
await draft.save();
return draft;
}
static async all() {
const allDrafts = [];
const all = await browser.storage.local.get();
const uids = Object.keys(all)
.filter(key => key.startsWith("drafter-draft"))
.map(key => key.substring("drafter-draft-".length));
for (const uid of uids) {
const draft = await Draft.find(uid);
allDrafts.push(draft);
}
return allDrafts.sort(function (a, b) { return a.position - b.position; });
}
static async find(uid) {
const draftKey = "drafter-draft-" + uid;
const data = await browser.storage.local.get(draftKey);
const draft = new Draft(data[draftKey].text);
draft.uid = data[draftKey].uid;
draft.position = data[draftKey].position;
return draft;
}
static async count() {
const all = await browser.storage.local.get();
return Object.keys(all)
.filter(key => key.startsWith("drafter-draft"))
.length;
}
static async adjustPositions() {
const drafts = await Draft.all();
const active = await Draft.getActive();
for (const draft of drafts) {
draft.position = drafts.indexOf(draft) + 1;
draft.save();
if (active && draft.uid === active.uid) {
await Draft.updateActivePosition(draft.position);
}
}
}
static async getCurrent() {
const current = await browser.storage.local.get("drafter-current");
return current["drafter-current"];
}
static async saveCurrent(content) {
const activeDraft = await Draft.getActive();
if (activeDraft && activeDraft != "") {
const draft = await Draft.find(activeDraft.uid);
await draft.update(content);
}
await browser.storage.local.set({ "drafter-current": content });
}
static async destroyCurrent() {
await browser.storage.local.remove("drafter-current");
await Draft.destroyActive();
}
async save() {
const data = { [`drafter-draft-${this.uid}`]: this };
await browser.storage.local.set(data);
}
async update(content) {
this.text = content;
this.extract = content.substring(0,150);
await this.save();
}
async load() {
await browser.storage.local.set({"drafter-current": this.text});
await this.setActive();
}
async destroy() {
const active = await Draft.getActive();
if (active && this.uid === active.uid) { await Draft.destroyActive(); }
await browser.storage.local.remove("drafter-draft-" + this.uid);
await Draft.adjustPositions();
}
static async getActive() {
const result = await browser.storage.local.get("drafter-active");
return result["drafter-active"];
}
static async updateActivePosition(position) {
const draft = await Draft.getActive();
draft.position = position;
await browser.storage.local.set({"drafter-active": draft});
}
static async destroyActive() {
await browser.storage.local.remove("drafter-active");
}
async setActive() {
await browser.storage.local.set({"drafter-active": this});
}
}