-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
167 lines (143 loc) · 3.79 KB
/
main.js
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
const obsidian = require('obsidian');
const DEFAULT_SETTINGS = {
folder: ''
}
const MAIN_COMMAND_NAME = 'Open random note';
const CONFIGURED_FOLDER_COMMAND_NAME = MAIN_COMMAND_NAME + ' in configured folder';
const CURRENT_FOLDER_COMMAND_NAME = MAIN_COMMAND_NAME + ' in current folder';
const RIBBON_COMMAND_STRING = MAIN_COMMAND_NAME + ' (⇧ to restrict to current folder, or ⎇ to restrict to configured folder)';
class RandomInFolderPlugin extends obsidian.Plugin {
async onload() {
await this.loadSettings();
this.addRibbonIcon('dice', RIBBON_COMMAND_STRING, (evt) => {
if (evt.shiftKey) {
this.currentFolderAction();
} else if(evt.altKey) {
this.configuredFolderAction();
} else {
this.mainAction();
}
});
this.addSettingTab(new RandomInFolderSettingsTab(this.app, this));
this.addCommand({
id: 'random-note-in-configured-folder',
name: CONFIGURED_FOLDER_COMMAND_NAME,
callback: () => this.configuredFolderAction(),
hotkeys: [
{
modifiers:['Mod', 'Alt'],
key: 'r',
}
]
});
this.addCommand({
id: 'random-note',
name: MAIN_COMMAND_NAME,
callback: () => this.mainAction(),
hotkeys: [
{
modifiers:['Mod'],
key: 'r',
}
]
})
this.addCommand({
id: 'random-note-in-current-folder',
name: CURRENT_FOLDER_COMMAND_NAME,
checkCallback: (checking) => {
if (checking) {
if (this.folderOfActiveLeaf()) return true;
return false;
}
this.currentFolderAction();
return true;
},
hotkeys: [
{
modifiers:['Mod', 'Shift'],
key: 'r',
}
]
});
}
mainAction() {
const folder = this.app.vault.getAbstractFileByPath('/');
this.navigateToRandomNoteInFolder(folder);
}
configuredFolderAction() {
const folder = this.app.vault.getAbstractFileByPath(this.settings.folder || '/');
this.navigateToRandomNoteInFolder(folder);
}
currentFolderAction() {
const folder = this.folderOfActiveLeaf();
this.navigateToRandomNoteInFolder(folder);
}
folderOfActiveLeaf() {
if (!this.app.workspace.activeLeaf) {
return null;
}
const activeLeaf = this.app.workspace.activeLeaf;
if (!activeLeaf.view.file) {
return null;
}
return activeLeaf.view.file.parent;
}
navigateToRandomNoteInFolder(folder) {
if (!folder || !folder.children) {
new Notice('Invalid folder.');
return;
}
const randomChild = this.randomFileInFolder(folder);
if (!randomChild) {
new Notice('No files in that folder.');
return;
}
this.app.workspace.activeLeaf.openFile(randomChild);
}
descendantFilesInFolder(folder) {
const files = [];
for (const item of folder.children) {
if (item.children) {
//Recurse into the sub-directory
files.push(...this.descendantFilesInFolder(item));
} else {
files.push(item);
}
}
return files;
}
randomFileInFolder(folder) {
const fileChildren = this.descendantFilesInFolder(folder);
if (fileChildren.length == 0) {
return null;
}
return fileChildren[Math.floor(Math.random()*fileChildren.length)]
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class RandomInFolderSettingsTab extends obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
let {containerEl} = this;
containerEl.empty();
new obsidian.Setting(containerEl)
.setName('Configured folder')
.setDesc('The folder to use for the \'' + CONFIGURED_FOLDER_COMMAND_NAME +'\' option')
.addText(text => text
.setValue(this.plugin.settings.folder)
.setPlaceholder('Example: foldername')
.onChange(async (value) => {
this.plugin.settings.folder = value;
await this.plugin.saveSettings();
}));
}
}
module.exports = RandomInFolderPlugin;