-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
292 lines (262 loc) · 9.93 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import {App, ButtonComponent, Editor, MarkdownView, Plugin, PluginSettingTab, Setting} from 'obsidian';
import {toggleServerTaskStatus, updateFileFromServer} from "./src/updateFileFromServer";
import {FolderSuggest} from "./src/suggest/folderSuggester";
import {migrateSettings} from "./src/settingsMigrator";
import {DEFAULT_SETTINGS, TodoistSettings} from "./src/DefaultSettings";
export default class TodoistPlugin extends Plugin {
settings: TodoistSettings;
hasIntervalFailure: boolean = false;
async onload() {
await this.loadSettings();
this.addCommand({
id: 'toggle-todoist-task',
name: 'Toggle todoist task',
editorCallback: (editor: Editor, view: MarkdownView) => {
toggleServerTaskStatus(editor, this.settings);
// @ts-ignore undocumented but was recommended to use here - https://github.com/obsidianmd/obsidian-releases/pull/768#issuecomment-1038441881
view.app.commands.executeCommandById("editor:toggle-checklist-status")
}
});
this.addCommand({
id: 'todoist-task-pull',
name: 'Replace keyword with todos',
editorCallback: () => {
updateFileFromServer(this.settings, this.app)
}
});
if (this.settings.enableAutomaticReplacement) {
this.registerEvent(this.app.workspace.on('file-open', async () => {
if (this.hasIntervalFailure) {
console.log("Todoist text: not checking for replacement keyword because of previous server " +
"failure. Either use the manual keyword, or restart the app.")
return;
}
try {
await updateFileFromServer(this.settings, this.app)
} catch {
this.hasIntervalFailure = true;
}
}));
}
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new TodoistPluginSettingTab(this.app, this));
/* This is in addition to the on file-open callback. This helps with
1. manually adding the keyword to a new spot in a file
2. when you make a setting change, such as changing your keyword
If this notices a keyword, it should wait at least 2 seconds before updating the text - this avoids a shocking
user experience.
*/
// 5 sec sleep because we want to ensure the file-open event finishes before this loop starts
await new Promise(r => setTimeout(r, 3000));
this.registerInterval(window.setInterval(() => this.updateFileFromServerIfEnabled(), 4 * 1000))
}
async updateFileFromServerIfEnabled() {
if (this.settings.enableAutomaticReplacement && !this.hasIntervalFailure) {
await new Promise(r => setTimeout(r, 2000));
try {
await updateFileFromServer(this.settings, this.app)
}
catch {
this.hasIntervalFailure = true;
}
}
}
onunload() {
}
async loadSettings() {
let storedSettings = await this.loadData() ?? DEFAULT_SETTINGS;
this.settings = migrateSettings(storedSettings);
await this.saveSettings();
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class TodoistPluginSettingTab extends PluginSettingTab {
plugin: TodoistPlugin;
constructor(app: App, plugin: TodoistPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h1', {text: 'Todoist Text'});
containerEl.createEl('a', {text: 'Important - see usage instructions', href: 'https://github.com/wesmoncrief/obsidian-todoist-text/tree/master#readme'});
this.addApiKeySetting(containerEl);
this.addEnableAutomaticReplacementSetting(containerEl);
this.addIncludeSubttasksSetting(containerEl);
this.addKeywordTodoistQuerySetting(containerEl);
this.addExcludedDirectoriesSetting(containerEl);
}
private addEnableAutomaticReplacementSetting(containerEl: HTMLElement) {
new Setting(containerEl)
.setName('Enable automatic replacement of keyword with Todos')
.setDesc("When enabled, any time a keyword is seen in a non-excluded file, it will be automatically" +
" replaced with your Todos whenever the file is opened." +
" When disabled, manually use the 'Replace keyword with todos' command to replace your keyword with Todos.")
.addToggle(t =>
t.setValue(this.plugin.settings.enableAutomaticReplacement)
.onChange(async (value) => {
this.plugin.settings.enableAutomaticReplacement = value;
await this.plugin.saveSettings();
}
));
}
private addIncludeSubttasksSetting(containerEl: HTMLElement) {
new Setting(containerEl)
.setName('Enable Subtasks')
.setDesc("When enabled, any Subtasks associated with Todos meeting filter criteria from your keyword will be shown, indented, under the parent Todo.")
.addToggle(t =>
t.setValue(this.plugin.settings.showSubtasks)
.onChange(async (value) => {
this.plugin.settings.showSubtasks = value;
await this.plugin.saveSettings();
}
));
}
private addExcludedDirectoriesSetting(containerEl: HTMLElement) {
containerEl.createEl('h2', {text: 'Excluded folder'});
const excludedFolderDescription = document.createDocumentFragment();
excludedFolderDescription.append(
"If you use template files (e.g. for daily notes) and you want to use a keyword in that template file, this plugin would replace the keyword in your template file with Todos immediately, rendering the template useless.",
excludedFolderDescription.createEl("br"),
"To prevent this, exclude the folder containing your template file.",
);
new Setting(this.containerEl).setDesc(excludedFolderDescription)
this.plugin.settings.excludedDirectories.forEach(
(dir, index) => {
new Setting(this.containerEl)
.setName("Excluded folder")
.addSearch((cb) => {
new FolderSuggest(this.app, cb.inputEl);
cb.setPlaceholder("Example: folder1/folder2")
.setValue(dir)
.onChange(async (new_folder) => {
this.plugin.settings.excludedDirectories[index] = new_folder;
await this.plugin.saveSettings();
});
})
.addExtraButton(eb => {
eb.setIcon("cross")
.setTooltip("Delete")
.onClick(async () => {
this.plugin.settings.excludedDirectories.splice(
index,
1
);
await this.plugin.saveSettings();
await this.display()
})
});
}
)
new Setting(this.containerEl)
.setName("Add another excluded folder")
.addButton((button: ButtonComponent) => {
button
.setButtonText("+")
.setCta()
.onClick(async () => {
this.plugin.settings.excludedDirectories.push("");
await this.plugin.saveSettings();
this.display();
});
});
}
private addKeywordTodoistQuerySetting(containerEl: HTMLElement) {
// todo add warning/stop if multiple same keywords
containerEl.createEl('h2', {text: 'Keywords and Filter Definitions'});
const filterDescription = document.createDocumentFragment();
filterDescription.append('This plugin will find the specified keyword in a currently open file and replace ' +
'the keyword with your Todos. Your Todos will be pulled from Todoist based on the specified ',
containerEl.createEl("a", null, (link) => {
link.href = "https://todoist.com/help/articles/introduction-to-filters";
link.innerText = "filter definition.";
}),
containerEl.createEl("br"),
"Each keyword you use should be unique."
)
new Setting(containerEl).setDesc(filterDescription);
this.plugin.settings.keywordToTodoistQuery.forEach(
(keywordToTodoistQuery, index) => {
const div = this.containerEl.createEl("div");
div.addClass("todoist-setting-div");
new Setting(containerEl)
.addText(text => text
.setPlaceholder("@@TODOIST_KEYWORD@@")
.setValue(
this.plugin.settings.keywordToTodoistQuery[index].keyword
)
.onChange(async (value) => {
this.plugin.settings.keywordToTodoistQuery[index].keyword = value;
await this.plugin.saveSettings();
})
.inputEl.addClass("todoist-query-setting")
)
.addText(text => text
.setPlaceholder("today|overdue")
.setValue(
this.plugin.settings.keywordToTodoistQuery[index].todoistQuery
)
.onChange(async (value) => {
this.plugin.settings.keywordToTodoistQuery[index].todoistQuery = value;
await this.plugin.saveSettings();
})
.inputEl.addClass("todoist-query-setting")
)
.addExtraButton(eb => {
eb.setIcon("cross")
.setTooltip("Delete")
.onClick(async () => {
this.plugin.settings.keywordToTodoistQuery.splice(
index,
1
);
await this.plugin.saveSettings();
await this.display()
})
})
div.appendChild(this.containerEl.lastChild);
});
new Setting(this.containerEl)
.setName("Add another keyword and Todoist query")
.addButton((button: ButtonComponent) => {
button
.setButtonText("+")
.setCta()
.onClick(async () => {
this.plugin.settings.keywordToTodoistQuery.push({
keyword: "",
todoistQuery: ""
});
await this.plugin.saveSettings();
this.display();
});
});
}
private addApiKeySetting(containerEl: HTMLElement) {
const tokenDescription = document.createDocumentFragment();
tokenDescription.createEl("span", null, (span) => {
span.innerText = 'This is your personal authentication token for Todoist. Be aware that anyone with this token ' +
'could access all of your Todoist data. This is stored in plain text in your .obsidian/plugins folder.' +
' Ensure that you are comfortable with the security implications before proceeding. ' +
'You can get your token from the "API token" section ';
span.createEl("a", null, (link) => {
link.href = "https://todoist.com/prefs/integrations";
link.innerText = "here.";
});
});
new Setting(containerEl)
.setName('API token')
.setDesc(tokenDescription)
.addText(text => text
.setValue(this.plugin.settings.authToken)
.onChange(async (value) => {
this.plugin.settings.authToken = value;
await this.plugin.saveSettings();
// give another chance for auto-updates to happen
this.plugin.hasIntervalFailure = false;
}));
}
}