generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.ts
115 lines (90 loc) · 2.6 KB
/
main.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
import { addIcons } from "icons";
import {
App,
Notice,
Plugin,
PluginSettingTab,
Setting,
TFile,
} from "obsidian";
import { RecentNotes, File } from "recent-notes-interface";
import { VIEW_TYPE_REVIEW_NOTES, ReviewNotesView } from "review-notes-view";
// Remember to rename these classes and interfaces!
interface PluginSettings {
setting: string;
}
const DEFAULT_DATA: RecentNotes = {
files: [],
};
export default class RecentNotesPlugin extends Plugin {
settings: PluginSettings;
recentNotes: RecentNotes;
reviewNotesView: ReviewNotesView;
async onload() {
await this.loadRecentNotes();
// handles creation of new file and modification to existing file
this.registerEvent(this.app.vault.on("modify", this.savePath));
this.registerEvent(this.app.vault.on("rename", this.renamePath));
// register custom view
this.registerView(VIEW_TYPE_REVIEW_NOTES, (leaf) => {
this.reviewNotesView = new ReviewNotesView(
leaf,
this.recentNotes,
this
);
this.registerEvent(this.app.vault.on("create", this.savePath));
return this.reviewNotesView;
});
addIcons();
this.addRibbonIcon("file", "View Recent Notes", () => {
this.activateView();
});
}
async activateView() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE_REVIEW_NOTES);
await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE_REVIEW_NOTES,
active: true,
});
this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_TYPE_REVIEW_NOTES)[0]
);
}
async onunload() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE_REVIEW_NOTES);
}
savePath = async (file: TFile): Promise<void> => {
// folder creation
if (file.basename === undefined) {
return;
}
this.recentNotes.files.findIndex(({ path }) => file.path === path) ===
-1
? this.recentNotes.files.push({
path: file.path,
filename: file.basename,
// eslint-disable-next-line no-mixed-spaces-and-tabs
})
: undefined;
await this.saveRecentNotes();
};
renamePath = async (file: TFile, previousPath: string): Promise<void> => {
const note = this.recentNotes.files.find(
(item) => item.path === previousPath
);
if (note) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
note.path = file.path;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
note.filename = file.basename;
await this.saveRecentNotes();
}
};
async loadRecentNotes() {
this.recentNotes = Object.assign(DEFAULT_DATA, await super.loadData());
}
async saveRecentNotes() {
await this.saveData(this.recentNotes);
this.reviewNotesView.onOpen();
}
}