-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
89 lines (75 loc) · 2.79 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
import { App, Plugin, PluginSettingTab, Setting, normalizePath } from 'obsidian';
import { PromptView } from './promptView';
export class PromptManagerPlugin extends Plugin {
settings: { promptFolderPath: string };
async onload() {
await this.loadSettings();
this.registerView('prompt-manager', (leaf) => new PromptView(leaf, this));
this.addCommand({
id: 'show-prompt-view',
name: 'Show Prompt Manager',
callback: () => {
this.activateView();
}
});
this.addSettingTab(new PromptManagerSettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, { promptFolderPath: 'prompts' }, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async activateView() {
const { workspace } = this.app;
let leaf = workspace.getLeavesOfType('prompt-manager')[0];
if (!leaf) {
const newLeaf = workspace.getRightLeaf(false);
if (newLeaf) {
leaf = newLeaf;
await leaf.setViewState({ type: 'prompt-manager', active: true });
}
}
if (leaf) {
workspace.revealLeaf(leaf);
}
}
async createPrompt(name: string, content: string = "") {
const folderPath = normalizePath(this.settings.promptFolderPath);
const folder = this.app.vault.getAbstractFileByPath(folderPath);
if (!folder) {
await this.app.vault.createFolder(folderPath);
}
const filePath = normalizePath(`${folderPath}/${name}.md`);
await this.app.vault.create(filePath, content);
}
async getPrompts() {
const folderPath = normalizePath(this.settings.promptFolderPath);
const folder = this.app.vault.getAbstractFileByPath(folderPath);
if (!folder) {
return [];
}
return this.app.vault.getFiles().filter(file => file.parent && file.parent.path === folder.path && file.extension === 'md');
}
}
class PromptManagerSettingTab extends PluginSettingTab {
plugin: PromptManagerPlugin;
constructor(app: App, plugin: PromptManagerPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('Prompts Folder Path')
.setDesc('Select the folder where prompts are stored.')
.addText(text => text
.setValue(this.plugin.settings.promptFolderPath)
.onChange(async (value) => {
this.plugin.settings.promptFolderPath = value;
await this.plugin.saveSettings();
})
);
}
}