generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
125 lines (104 loc) · 3.25 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
import { App, Notice, Plugin, PluginSettingTab, Setting } from "obsidian";
interface RandomNoteSettings {
destinationFolder: string;
cutOffTime: string;
}
const DEFAULT_SETTINGS: RandomNoteSettings = {
destinationFolder: "",
cutOffTime: "",
};
export default class RandomNotePlugin extends Plugin {
settings: RandomNoteSettings;
async onload() {
await this.loadSettings();
// creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon(
"dice",
"Condtional Random Note",
() => {
// gets all files from the obsidian vault
const files = this.app.vault.getMarkdownFiles();
// gets all files containing path set
const FolderFiles = files.filter((word) =>
word.path.includes(this.settings.destinationFolder)
);
// gets files that has a later time stamp then set in days
const unixTime =
Number(this.settings.cutOffTime) * 24 * 60 * 60;
const ts = Math.floor(Date.now() / 1000);
const timeFiles = FolderFiles.filter(
(file) => ts - unixTime > file.stat.ctime / 1000
);
// picks a random note
const fileOpen =
timeFiles[Math.floor(Math.random() * FolderFiles.length)];
// opens the file
this.app.workspace.openLinkText(fileOpen.basename, "");
// indicating success
new Notice("Random note generated");
}
);
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new RandomNoteSettingTab(this.app, this));
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
this.registerInterval(
window.setInterval(() => console.log("setInterval"), 5 * 60 * 1000)
);
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class RandomNoteSettingTab extends PluginSettingTab {
plugin: RandomNotePlugin;
constructor(app: App, plugin: RandomNotePlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
// setting for folder path
new Setting(containerEl)
.setName("Destination folder path")
.setDesc(
"Set a folder path, all notes selected will be within that folder"
)
.addText((text) =>
text
.setPlaceholder("Project/Errands")
.setValue(this.plugin.settings.destinationFolder)
.onChange(async (value) => {
this.plugin.settings.destinationFolder = value;
await this.plugin.saveSettings();
})
);
// setting for time stamp
new Setting(containerEl)
.setName("Cutoff time")
.setDesc(
"Specify a time x (days), all notes selected will be x days prior to today (currently for MacOS and Windows)"
)
.addText((text) =>
text
.setPlaceholder("7")
.setValue(this.plugin.settings.cutOffTime)
.onChange(async (value) => {
if (typeof Number(value) == "number") {
this.plugin.settings.cutOffTime = value;
await this.plugin.saveSettings();
} else {
new Notice("Invalid input, please try again");
}
})
);
}
}