-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFlatNotepad.ts
170 lines (145 loc) · 4.79 KB
/
FlatNotepad.ts
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
import { Note, Notepad, Section, Trie } from './index';
import { format, parse } from 'date-fns';
import { EncryptionMethod } from './crypto';
import { LAST_MODIFIED_FORMAT } from './date-formats';
export type FlatNotepadOptions = {
lastModified?: Date;
notepadAssets?: string[];
sections?: { [internalRef: string]: FlatSection };
notes?: { [internalRef: string]: Note };
crypto?: EncryptionMethod;
};
export type FlatSection = {
title: string;
internalRef: string;
parentRef?: string;
};
/**
* A FlatNotepad is similar to the {@link Notepad} class, but it stores all the notes/sections
* as in flat structures. FlatNotepads will likely be better for internal use in many situations.
*
* Something to remember is that all operations on this class like addSection, will return a <strong>new</strong>
* object of this class, and not modify the existing one.
*/
export default class FlatNotepad {
public readonly lastModified: string;
public readonly sections: { [internalRef: string]: FlatSection };
public readonly notes: { [internalRef: string]: Note };
public readonly notepadAssets: string[];
public readonly crypto?: EncryptionMethod;
constructor(
public readonly title: string,
opts: FlatNotepadOptions = {}
) {
this.lastModified = format(opts.lastModified || new Date(), LAST_MODIFIED_FORMAT);
this.sections = opts.sections || {};
this.notes = opts.notes || {};
this.notepadAssets = opts.notepadAssets || [];
if (opts.crypto) this.crypto = opts.crypto;
}
static makeFlatSection(title: string, parentRef?: string): FlatSection {
const tmpSection = new Section(title);
return { title: tmpSection.title, internalRef: tmpSection.internalRef, parentRef };
}
public addSection(section: FlatSection): FlatNotepad {
return this.clone({
sections: {
...this.sections,
[section.internalRef]: section
}
});
}
public addNote(note: Note): FlatNotepad {
// Ensure our parent is just a string for the section's internalRef, not the whole Parent object
if (typeof note.parent !== 'string') {
note = note.clone({
parent: (note.parent as Section).internalRef
});
}
return this.clone({
notes: {
...this.notes,
[note.internalRef]: note
}
});
}
public addAsset(uuid: string): FlatNotepad {
return this.clone({
notepadAssets: [
...this.notepadAssets,
uuid
]
});
}
public modified(lastModified: Date = new Date()): FlatNotepad {
return this.clone({
lastModified
});
}
/**
* Unlike the {@link Notepad}, this uses an indexed lookup system. This should be faster than a
* traditional notepad search.
*
* @param {Trie} trie The search trie for the notepad
* @param {string} query Can either be a title-search or a hashtag-search
* @returns {Note[]}
*/
public search(trie: Trie, query: string): Note[] {
return Trie.search(trie, query).map(ref => this.notes[ref]);
}
/**
* This will convert everything into the formal {@link Notepad} structure, however no {@link Asset}s will
* be restored. The client should rebuild the assets after this using the values in {@link notepadAssets}
* @returns {Notepad}
*/
public toNotepad(): Notepad {
const buildSection = (flat: FlatSection): Section => {
let section = new Section(flat.title, [], [], flat.internalRef);
// Restore sub-sections
Object.values(this.sections)
.filter(s => s.parentRef === flat.internalRef)
.map(s => section = section.addSection(buildSection(s)));
// Restore notes
Object.values(this.notes)
.filter(n => n.parent === flat.internalRef)
.map(n => section = section.addNote(n));
return section;
};
let notepad = new Notepad(this.title, {
lastModified: parse(this.lastModified, LAST_MODIFIED_FORMAT, new Date()),
notepadAssets: this.notepadAssets,
crypto: this.crypto
});
// Add all the sections + notes
Object.values(this.sections)
.filter(s => !s.parentRef)
.forEach(s => notepad = notepad.addSection(buildSection(s)));
return notepad;
}
public clone(opts: Partial<FlatNotepadOptions> = {}, title: string = this.title): FlatNotepad {
return new FlatNotepad(title, {
lastModified: parse(this.lastModified, LAST_MODIFIED_FORMAT, new Date()),
sections: this.sections,
notes: this.notes,
notepadAssets: this.notepadAssets,
crypto: this.crypto,
...opts
});
}
public pathFrom(obj: Note | FlatSection): (FlatSection | FlatNotepad)[] {
const parents: FlatSection[] = [];
if (!!(obj as Note).parent) { // This is a note
obj = this.sections[(obj as Note).parent as string];
} else {
const parent = (obj as FlatSection).parentRef;
if (!parent) return [this];
obj = this.sections[(obj as FlatSection).parentRef!];
}
let tmp: FlatSection = obj;
while (true) {
parents.unshift(tmp);
if (!tmp.parentRef) return [ this, ...parents ];
tmp = this.sections[tmp.parentRef];
}
}
}