-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
321 lines (316 loc) · 11.9 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
*/
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, desc) => {
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && key !== "default")
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
}
return target;
};
var __toModule = (module2) => {
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
};
// main.ts
__export(exports, {
PromptManagerPlugin: () => PromptManagerPlugin
});
var import_obsidian2 = __toModule(require("obsidian"));
// promptView.ts
var import_obsidian = __toModule(require("obsidian"));
var VIEW_TYPE_PROMPT = "prompt-manager-view";
var PromptView = class extends import_obsidian.ItemView {
constructor(leaf, plugin) {
super(leaf);
this.prompts = [];
this.viewType = "list";
this.plugin = plugin;
}
getViewType() {
return VIEW_TYPE_PROMPT;
}
getDisplayText() {
return "Prompt Manager";
}
async onOpen() {
this.containerEl.empty();
this.containerEl.classList.add("prompt-manager-view");
const topContainer = this.containerEl.createDiv("top-container");
this.searchInput = topContainer.createEl("input", { cls: "prompt-search-input" });
this.searchInput.type = "text";
this.searchInput.placeholder = "Search prompts...";
this.searchInput.addEventListener("input", () => {
this.filterPrompts(this.searchInput.value.toLowerCase());
});
this.refreshButton = topContainer.createEl("button", { cls: "prompt-refresh-btn" });
this.refreshButton.setText("\u{1F504} Refresh");
this.refreshButton.onclick = async () => {
await this.refresh();
this.render();
};
const newButton = topContainer.createEl("button", { cls: "prompt-new-btn" });
newButton.setText("New Prompt");
newButton.onclick = () => this.showNewPromptModal();
const toggleViewButton = topContainer.createEl("button", { cls: "prompt-toggle-view-btn" });
toggleViewButton.setText(this.viewType === "board" ? "List View" : "Board View");
toggleViewButton.onclick = () => {
this.viewType = this.viewType === "board" ? "list" : "board";
toggleViewButton.setText(this.viewType === "board" ? "List View" : "Board View");
this.render();
};
await this.refresh();
this.render();
}
render() {
const topContainer = this.containerEl.querySelector(".top-container");
this.containerEl.empty();
this.containerEl.appendChild(topContainer);
const content = this.containerEl.createDiv("prompt-content");
if (this.viewType === "list") {
this.renderList(content);
} else {
this.renderBoard(content);
}
}
async refresh() {
const promptFolderPath = this.plugin.settings.promptFolderPath;
if (!promptFolderPath) {
new import_obsidian.Notice("Please select a prompts folder in the plugin settings.");
return;
}
const folder = this.app.vault.getAbstractFileByPath(promptFolderPath);
if (!folder || !(folder instanceof import_obsidian.TFolder)) {
new import_obsidian.Notice("Invalid prompts folder selected.");
return;
}
const files = this.app.vault.getFiles().filter((file) => file.parent && file.parent.path === folder.path);
const mdFiles = files.filter((file) => file.extension === "md");
this.prompts = await Promise.all(mdFiles.map(async (file) => {
const content = await this.app.vault.read(file);
const { version, details, fullContent } = this.extractPromptData(content);
return {
name: file.basename,
version,
details,
fullContent,
file
};
}));
this.prompts.sort((a, b) => {
const versionA = parseFloat(a.version);
const versionB = parseFloat(b.version);
return versionB - versionA;
});
}
extractPromptData(content) {
const versionMatches = Array.from(content.matchAll(/### Version\s+(\d+(?:\.\d+)?)\n([\s\S]*?)(?=### Version|\z)/g));
if (versionMatches.length === 0) {
return { version: "0.0", details: content.trim(), fullContent: content };
}
const lastVersion = versionMatches[versionMatches.length - 1];
const version = lastVersion[1];
const versionContent = lastVersion[2].trim();
const fullContent = versionMatches.map((v) => `### Version ${v[1]}
${v[2]}`).join("\n\n");
return {
version,
details: versionContent,
fullContent
};
}
renderList(container) {
const list = container.createEl("table", "prompt-list-table");
const header = list.createEl("thead").createEl("tr");
header.createEl("th").setText("Name");
header.createEl("th").setText("Version");
header.createEl("th").setText("Actions");
const body = list.createEl("tbody");
this.prompts.forEach((prompt) => {
const row = body.createEl("tr");
row.createEl("td").setText(prompt.name);
row.createEl("td").setText(prompt.version);
const actionsTd = row.createEl("td");
const editBtn = actionsTd.createEl("button", { cls: "prompt-edit-btn" });
editBtn.setText("Edit");
editBtn.onclick = () => this.openPrompt(prompt.file);
const copyBtn = actionsTd.createEl("button", { cls: "prompt-copy-btn" });
copyBtn.setText("Copy");
copyBtn.onclick = () => {
navigator.clipboard.writeText(prompt.details).then(() => {
new import_obsidian.Notice(`Copied prompt version ${prompt.version} content`);
}).catch((err) => {
console.error("Failed to copy: ", err);
new import_obsidian.Notice("Failed to copy content");
});
};
});
}
renderBoard(container) {
const board = container.createDiv("prompt-board");
this.prompts.forEach((prompt) => {
const card = board.createDiv("prompt-card");
const header = card.createDiv("prompt-header");
header.createEl("h3").setText(prompt.name);
const versionSpan = header.createEl("span", { cls: "prompt-version" });
versionSpan.setText(`v${prompt.version}`);
const details = card.createDiv("prompt-details");
details.setText(prompt.details);
const copyBtn = card.createEl("button", { cls: "prompt-copy-btn" });
copyBtn.setText("Copy");
copyBtn.onclick = (e) => {
e.stopPropagation();
navigator.clipboard.writeText(prompt.details).then(() => {
new import_obsidian.Notice(`Copied prompt version ${prompt.version} content`);
}).catch((err) => {
console.error("Failed to copy: ", err);
new import_obsidian.Notice("Failed to copy content");
});
};
card.onclick = () => this.openPrompt(prompt.file);
});
}
filterPrompts(searchTerm) {
const items = this.containerEl.querySelectorAll(this.viewType === "board" ? ".prompt-card" : ".prompt-list-table tbody tr");
items.forEach((item) => {
var _a;
const text = ((_a = item.textContent) == null ? void 0 : _a.toLowerCase()) || "";
item.style.display = text.includes(searchTerm) ? "" : "none";
});
}
async showNewPromptModal() {
const modal = new NewPromptModal(this.app, async (name) => {
if (name) {
const promptFolderPath = this.plugin.settings.promptFolderPath;
if (!promptFolderPath) {
new import_obsidian.Notice("Please select a prompts folder in the plugin settings.");
return;
}
const folder = this.app.vault.getAbstractFileByPath(promptFolderPath);
if (!folder || !(folder instanceof import_obsidian.TFolder)) {
new import_obsidian.Notice("Invalid prompts folder selected.");
return;
}
const content = `### Version 1.0
New prompt content`;
const file = await this.app.vault.create(name + ".md", content);
await this.refresh();
this.render();
}
});
modal.open();
}
async openPrompt(file) {
const leaf = this.app.workspace.getLeaf();
if (leaf) {
await leaf.openFile(file);
}
}
};
var NewPromptModal = class extends import_obsidian.Modal {
constructor(app, onSubmit) {
super(app);
this.onSubmit = onSubmit;
}
onOpen() {
const { contentEl } = this;
contentEl.empty();
contentEl.createEl("h2").setText("Create New Prompt");
const input = contentEl.createEl("input", { cls: "prompt-name-input" });
input.type = "text";
input.placeholder = "Enter prompt name";
input.autofocus = true;
const buttonContainer = contentEl.createDiv("button-container");
const submitButton = buttonContainer.createEl("button", { cls: "prompt-submit-btn" });
submitButton.setText("Create");
submitButton.onclick = () => {
this.onSubmit(input.value);
this.close();
};
const cancelButton = buttonContainer.createEl("button", { cls: "prompt-cancel-btn" });
cancelButton.setText("Cancel");
cancelButton.onclick = () => this.close();
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
};
// main.ts
var PromptManagerPlugin = class extends import_obsidian2.Plugin {
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, content = "") {
const folderPath = (0, import_obsidian2.normalizePath)(this.settings.promptFolderPath);
const folder = this.app.vault.getAbstractFileByPath(folderPath);
if (!folder) {
await this.app.vault.createFolder(folderPath);
}
const filePath = (0, import_obsidian2.normalizePath)(`${folderPath}/${name}.md`);
await this.app.vault.create(filePath, content);
}
async getPrompts() {
const folderPath = (0, import_obsidian2.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");
}
};
var PromptManagerSettingTab = class extends import_obsidian2.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
new import_obsidian2.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();
}));
}
};