generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.ts
92 lines (81 loc) · 2.95 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
import { App, Plugin, PluginManifest, TFile, WorkspaceLeaf } from 'obsidian';
import { VIEW_TYPE_TODO } from './constants';
import { TodoItemView, TodoItemViewProps } from './ui/TodoItemView';
import { TodoItem, TodoItemStatus } from './model/TodoItem';
import { TodoIndex } from './model/TodoIndex';
import { TodoPluginSettings, DEFAULT_SETTINGS } from './model/TodoPluginSettings';
import { SettingsTab } from './ui/SettingsTab';
import { DateFormatter } from 'util/DateFormatter';
import { DateTime } from 'luxon';
export default class TodoPlugin extends Plugin {
private dateFormatter: DateFormatter;
private todoIndex: TodoIndex;
private view: TodoItemView;
private settings: TodoPluginSettings;
constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
this.todoIndex = new TodoIndex(this.app.vault, DEFAULT_SETTINGS, this.tick.bind(this));
}
async onload(): Promise<void> {
this.settings = Object.assign(DEFAULT_SETTINGS, (await this.loadData()) ?? {});
this.dateFormatter = new DateFormatter(this.settings.dateFormat);
this.addSettingTab(new SettingsTab(this.app, this));
this.registerView(VIEW_TYPE_TODO, (leaf: WorkspaceLeaf) => {
const todos: TodoItem[] = [];
const props = {
todos: todos,
formatDate: (date: DateTime) => {
return this.dateFormatter.formatDate(date);
},
openFile: (filePath: string) => {
const file = this.app.vault.getAbstractFileByPath(filePath) as TFile;
if (this.settings.openFilesInNewLeaf && this.app.workspace.getActiveFile()) {
this.app.workspace.splitActiveLeaf().openFile(file);
} else {
this.app.workspace.getUnpinnedLeaf().openFile(file);
}
},
toggleTodo: (todo: TodoItem, newStatus: TodoItemStatus) => {
this.todoIndex.setStatus(todo, newStatus);
},
};
this.view = new TodoItemView(leaf, props);
return this.view;
});
this.app.workspace.onLayoutReady(() => {
this.initLeaf();
this.triggerIndex();
});
}
onunload(): void {
this.app.workspace.getLeavesOfType(VIEW_TYPE_TODO).forEach((leaf) => leaf.detach());
}
initLeaf(): void {
if (this.app.workspace.getLeavesOfType(VIEW_TYPE_TODO).length) {
return;
}
this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE_TODO,
});
}
getSettings(): TodoPluginSettings {
return this.settings;
}
async updateSettings(settings: TodoPluginSettings): Promise<void> {
this.settings = settings;
this.dateFormatter = new DateFormatter(this.settings.dateFormat);
await this.saveData(this.settings);
this.todoIndex.setSettings(settings);
}
private async triggerIndex(): Promise<void> {
await this.todoIndex.initialize();
}
tick(todos: TodoItem[]): void {
this.view.setProps((currentProps: TodoItemViewProps) => {
return {
...currentProps,
todos: todos,
};
});
}
}