-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathload-note.ts
More file actions
57 lines (42 loc) · 1.9 KB
/
Copy pathload-note.ts
File metadata and controls
57 lines (42 loc) · 1.9 KB
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
import * as vscode from 'vscode';
import { JournalController, Input, NoteInput } from '../../shared/model/index';
import { fileExists } from '../../shared/index';
/**
* Feature responsible for creating (if needed) and loading notes given a user input as title.
* (extracted as feature to enable direct unit tests)
*/
export class LoadNotes {
constructor(public input: NoteInput, public ctrl: JournalController) {
}
public async load(): Promise<vscode.TextDocument> {
let path = await this.ctrl.parser.resolveNotePathForInput(this.input);
return this.loadWithPath(path);
}
public async loadWithPath(path: string): Promise<vscode.TextDocument> {
let content: string = await this.ctrl.inject.formatNote(this.input);
let document : vscode.TextDocument = await this.loadNote(path, content);
// inject reference to new note in the target day's journal page (offset from input, defaults to today)
const entryInput = new Input(this.input.offset);
entryInput.date = this.input.date;
entryInput.scope = this.input.scope;
await this.ctrl.reader.loadEntryForInput(entryInput)
.catch(reason => this.ctrl.logger.error("Failed to load target day's page for injecting link to note.", reason));
return document;
}
/**
* Creates or loads a note
*
* @param {string} path
* @param {string} content
* @returns {Promise<vscode.TextDocument>}
* @memberof Writer
*/
public async loadNote(path: string, content: string): Promise<vscode.TextDocument> {
this.ctrl.logger.trace("Entering loadNote() in features/load-note.ts for path: ", path);
const exists = await fileExists(this.ctrl.fs, path);
if (exists) {
return this.ctrl.ui.openDocument(path);
}
return this.ctrl.writer.createSaveLoadTextDocument(path, content);
}
}