-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
213 lines (176 loc) · 4.88 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { App, Plugin, PluginSettingTab, Setting, WorkspaceLeaf, ItemView, ToggleComponent, TFile, MetadataCache } from 'obsidian';
import * as yaml from 'js-yaml';
const HABIT_TRACKER_VIEW_TYPE = 'kikijiki-habit-tracker-view';
interface KikijikiHabitTrackerSettings {
tagPrefix: string;
habits: string[];
}
const DEFAULT_SETTINGS: KikijikiHabitTrackerSettings = {
tagPrefix: 'habit',
habits: []
}
export default class KikijikiHabitTracker extends Plugin {
settings: KikijikiHabitTrackerSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new KikijikiHabitTrackerSettingTab(this.app, this));
this.registerView(
HABIT_TRACKER_VIEW_TYPE,
(leaf) => new HabitTrackerView(leaf, this)
);
this.addCommand({
id: 'open-panel',
name: 'Open panel',
callback: () => {
this.activateView();
}
});
this.registerEvent(
this.app.workspace.on('active-leaf-change', () => {
const view = this.app.workspace.getLeavesOfType(HABIT_TRACKER_VIEW_TYPE)[0]?.view as HabitTrackerView;
if (view) {
view.render();
}
})
);
}
async activateView() {
let rightLeaf = this.app.workspace.getRightLeaf(false);
if (!rightLeaf) {
rightLeaf = this.app.workspace.getRightLeaf(true);
}
if (rightLeaf) {
rightLeaf.setViewState({
type: HABIT_TRACKER_VIEW_TYPE,
active: true,
});
this.app.workspace.revealLeaf(rightLeaf);
}
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class KikijikiHabitTrackerSettingTab extends PluginSettingTab {
plugin: KikijikiHabitTracker;
constructor(app: App, plugin: KikijikiHabitTracker) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('Tag prefix')
.setDesc('Prefix for tags, the final tag will be <prefix>/<habit>.')
.addText(text => text
.setPlaceholder('Enter tag prefix')
.setValue(this.plugin.settings.tagPrefix)
.onChange(async (value) => {
this.plugin.settings.tagPrefix = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Habits')
.setDesc('List of habits that will appear in the panel.');
this.plugin.settings.habits.forEach((habit, index) => {
new Setting(containerEl)
.setName(`Habit ${index + 1}`)
.addText(text => text
.setValue(habit)
.onChange(async (value) => {
this.plugin.settings.habits[index] = value;
await this.plugin.saveSettings();
}))
.addButton(button => {
button.setButtonText('Remove');
button.onClick(async () => {
this.plugin.settings.habits.splice(index, 1);
await this.plugin.saveSettings();
this.display();
});
});
});
new Setting(containerEl)
.addButton(button => {
button.setButtonText('Add habit');
button.onClick(() => {
this.plugin.settings.habits.push('');
this.display();
});
});
}
}
class HabitTrackerView extends ItemView {
plugin: KikijikiHabitTracker;
private settings: Setting[] = [];
private currentFile: string | null = null;
constructor(leaf: WorkspaceLeaf, plugin: KikijikiHabitTracker) {
super(leaf);
this.plugin = plugin;
}
getViewType() {
return HABIT_TRACKER_VIEW_TYPE;
}
getDisplayText() {
return 'Habit tracker';
}
getIcon() {
return "checkbox-glyph";
}
async onOpen() {
this.render();
}
async onClose() {
this.clearSettings();
}
private clearSettings() {
this.settings.forEach(setting => setting.settingEl.remove());
this.settings = [];
}
async render() {
const { contentEl } = this;
const activeFile = this.app.workspace.getActiveFile();
if (activeFile?.path === this.currentFile) {
return;
}
this.currentFile = activeFile?.path ?? null;
contentEl.empty();
this.clearSettings();
if (!activeFile) {
contentEl.setText('No file is open');
return;
}
const cache = this.app.metadataCache.getFileCache(activeFile);
const frontmatter = cache?.frontmatter || {};
const existingTags = frontmatter.tags || [];
this.plugin.settings.habits.forEach(habit => {
const tag = `${this.plugin.settings.tagPrefix}/${habit}`;
const setting = new Setting(contentEl)
.setName(habit)
.addToggle(toggle => {
toggle.setValue(existingTags.includes(tag));
toggle.onChange(this.createToggleHandler(activeFile, tag));
});
this.settings.push(setting);
});
}
private createToggleHandler(file: TFile, tag: string) {
return async (value: boolean) => {
await this.app.fileManager.processFrontMatter(file, (frontmatter) => {
let tags = frontmatter.tags || [];
if (value && !tags.includes(tag)) {
tags.push(tag);
} else if (!value && tags.includes(tag)) {
tags = tags.filter((t: string) => t !== tag);
}
frontmatter.tags = tags;
});
};
}
}