generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
167 lines (142 loc) · 5.89 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
import {App, EditorPosition, MarkdownView, Plugin, PluginSettingTab, Setting, TFile} from 'obsidian';
interface RandomTodoPluginSettings {
todoPattern: string;
showStatusBar: boolean;
}
const DEFAULT_SETTINGS: RandomTodoPluginSettings = {
todoPattern: '(^|\\s)\\.\\.\\.(\\s|$)',
showStatusBar: false
}
export default class RandomTodoPlugin extends Plugin {
settings: RandomTodoPluginSettings;
statusBarItem: HTMLElement;
todoPattern: RegExp;
// file name -> (last updated, positions of todos)
fileCache = new Map<string, [number, Array<EditorPosition>]>(null);
getRandomFileWithTodo = async (): Promise<string> => {
const markdownFiles = this.app.vault.getMarkdownFiles();
markdownFiles.shuffle();
for (const markdownFile of markdownFiles) {
const positions = await this.collectTodosFromFile(markdownFile);
if (positions.length > 0) {
return markdownFile.path;
}
}
}
getRandomTodoItem = async (): Promise<[string, EditorPosition]> => {
const markdownFiles = this.app.vault.getMarkdownFiles();
const collectedLinks = Array<[string, EditorPosition]>();
for (const markdownFile of markdownFiles) {
const positions = await this.collectTodosFromFile(markdownFile);
for (const position of positions) {
collectedLinks.push([markdownFile.path, position])
}
}
const pos = this.randomInt(collectedLinks.length);
return collectedLinks[pos];
}
collectTodosFromFile = async (file: TFile): Promise<Array<EditorPosition>> => {
const [mtime, cachedPositions] = this.fileCache.get(file.path) || [0, null];
if (!cachedPositions && mtime !== file.stat.mtime) {
const contents = await this.app.vault.cachedRead(file);
const lines = contents.split('\n');
const positions: Array<EditorPosition> = lines.map((value, index) => ({
line: index,
ch: value.search(this.todoPattern)
})).filter(value => value.ch != -1);
this.fileCache.set(file.path, [file.stat.mtime, positions])
return positions;
}
return cachedPositions;
}
randomInt = (max: number) => Math.floor(Math.random() * max);
async onload() {
console.log('loading plugin');
await this.loadSettings();
this.todoPattern = new RegExp(this.settings.todoPattern, 'g');
if (this.settings.showStatusBar) {
this.statusBarItem = this.addStatusBarItem();
this.app.workspace.on("file-open", (file) => {
this.app.vault.cachedRead(file).then(contents => {
const N = [...contents.matchAll(this.todoPattern)].length;
const text = N > 0 ? N + ' to-do items' : '';
this.statusBarItem.setText(text);
})
});
this.app.vault.on("modify", (file: TFile) => {
this.app.vault.cachedRead(file).then(contents => {
const N = [...contents.matchAll(this.todoPattern)].length;
const text = N > 0 ? N + ' to-do items' : '';
this.statusBarItem.setText(text);
})
});
}
this.addCommand({
id: 'open-random-todo-file',
name: 'Random Todo: File',
callback: () => {
this.getRandomFileWithTodo().then(link => {
this.app.workspace.openLinkText(link, '');
})
}
});
this.addCommand({
id: 'open-random-todo-item',
name: 'Random Todo: Item',
callback: () => {
this.getRandomTodoItem().then(([link, position]) => {
this.app.workspace.openLinkText(link, '').then(() => {
this.app.workspace.getActiveViewOfType(MarkdownView).editor.setCursor(position);
});
})
}
});
this.addSettingTab(new RandomTodoPluginSettingTab(this.app, this));
}
onunload() {
console.log('unloading plugin');
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class RandomTodoPluginSettingTab extends PluginSettingTab {
plugin: RandomTodoPlugin;
constructor(app: App, plugin: RandomTodoPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for Random To-Do Plugin'});
new Setting(containerEl)
.setName('To-do item pattern')
.setDesc('Regular expression which a to-do item should match')
.addText(text => text
.setPlaceholder(DEFAULT_SETTINGS.todoPattern)
.setValue(this.plugin.settings.todoPattern)
.onChange(async (value) => {
this.plugin.todoPattern = new RegExp(value, 'g');
this.plugin.settings.todoPattern = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('To-do item count view')
.setDesc('Show/hide todo count in Status Bar')
.addToggle(component => component
.setValue(this.plugin.settings.showStatusBar)
.onChange(async (value) => {
if (value) {
this.plugin.statusBarItem.show()
} else {
this.plugin.statusBarItem.hide()
}
this.plugin.settings.showStatusBar = value;
await this.plugin.saveSettings();
}));
}
}