-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
127 lines (101 loc) · 4.06 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
116
117
118
119
120
121
122
123
124
125
126
127
import { Plugin, TFolder, normalizePath, WorkspaceLeaf, moment } from 'obsidian';
import { SideBarView, VIEW_TYPE_SIDE_BAR } from "./views/SideBar";
import { JournalystSettingsTab } from "./views/Settings";
interface JournalystPluginSettings {
rootDirectory: string;
}
const DEFAULT_SETTINGS: JournalystPluginSettings = {
rootDirectory: '/'
}
export default class JournalystPlugin extends Plugin {
settings: JournalystPluginSettings;
journals: TFolder[] = [];
async onload() {
await this.loadSettings();
this.addSettingTab(new JournalystSettingsTab(this.app, this));
const ribbonIconEl = this.addRibbonIcon('book-copy', 'Go to Journalyst view', () => {
this.activateView();
});
this.app.workspace.onLayoutReady(() => {
const rootFolder = this.app.vault.getAbstractFileByPath(this.settings.rootDirectory);
if (rootFolder instanceof TFolder === false) {
return;
}
rootFolder.children.forEach(child => {
if (child instanceof TFolder === false) {
return;
}
this.journals.push(child);
this.addCommand({
id: 'create-journal-' + child.name,
name: 'Create new journal in ' + child.name,
callback: () => {
const todaysDate = moment().format('YYYY-MM-DD');
const newFileName = todaysDate + '.md';
const fullPath = normalizePath(child.path + '/' + newFileName);
this.app.vault.create(fullPath, '---\ntitle: ' + todaysDate + '\n---\n')
}
})
})
this.registerView(
VIEW_TYPE_SIDE_BAR,
(leaf) => new SideBarView(leaf, this)
);
})
this.registerEvent(
this.app.vault.on('create', (item) => this.onItemChange())
);
this.registerEvent(
this.app.vault.on('delete', (item) => this.onItemChange())
);
this.registerEvent(
this.app.vault.on('rename', (item) => this.onItemChange())
);
};
onunload() {}
private onItemChange() {
const rootFolder = this.app.vault.getAbstractFileByPath(this.settings.rootDirectory);
if (rootFolder instanceof TFolder === false) {
return;
}
this.journals = []
rootFolder.children.forEach(child => {
if (child instanceof TFolder === false) {
return;
}
this.journals.push(child);
this.addCommand({
id: 'create-journal-' + child.name,
name: 'Create new journal in ' + child.name,
callback: () => {
const todaysDate = moment().format('YYYY-MM-DD');
const newFileName = todaysDate + '.md';
const fullPath = normalizePath(child.path + '/' + newFileName);
this.app.vault.create(fullPath, '---\ntitle: ' + todaysDate + '\n---\n')
}
})
})
}
async activateView() {
const { workspace } = this.app;
let leaf: WorkspaceLeaf | null = null;
const leaves = workspace.getLeavesOfType(VIEW_TYPE_SIDE_BAR);
if (leaves.length > 0) {
// A leaf with our view already exists, use that
leaf = leaves[0];
} else {
// Our view could not be found in the workspace, create a new leaf
// in the right sidebar for it
leaf = workspace.getRightLeaf(false);
await leaf.setViewState({ type: VIEW_TYPE_SIDE_BAR, active: true });
}
// "Reveal" the leaf in case it is in a collapsed sidebar
workspace.revealLeaf(leaf);
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}