-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTranslators.ts
33 lines (26 loc) · 1.01 KB
/
Translators.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
import { Note, Notepad, Section } from './index';
import { parse } from 'date-fns';
export namespace Translators {
export namespace Json {
export function toNotepad(json: string): Notepad {
const jsonObj: Notepad = JSON.parse(json);
let notepad = new Notepad(jsonObj.title, {
lastModified: parse(jsonObj.lastModified),
notepadAssets: jsonObj.notepadAssets || []
});
// Restore sections
jsonObj.sections.forEach(section => notepad = notepad.addSection(restoreSection(section)));
// TODO: Restore assets
return notepad;
}
function restoreSection(section: Section): Section {
let restored = new Section(section.title).clone({ internalRef: section.internalRef });
section.sections.forEach(s => restored = restored.addSection(restoreSection(s)));
section.notes.forEach(n => restored = restored.addNote(restoreNote(n)));
return restored;
}
function restoreNote(note: Note) {
return new Note(note.title, note.time, note.elements, note.bibliography, note.internalRef);
}
}
}