-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromptView.ts
305 lines (252 loc) · 10.7 KB
/
promptView.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
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
import { ItemView, WorkspaceLeaf, TFile, App, Modal, Notice, Vault, TAbstractFile, TFolder } from 'obsidian';
import { PromptManagerPlugin } from './main';
export const VIEW_TYPE_PROMPT = 'prompt-manager-view';
interface PromptData {
name: string;
version: string;
details: string;
fullContent: string;
file: TFile;
}
export class PromptView extends ItemView {
plugin: PromptManagerPlugin;
prompts: PromptData[] = [];
viewType: 'board' | 'list' = 'list'; // 默认为列表视图
containerEl: HTMLElement;
searchInput?: HTMLInputElement;
refreshButton?: HTMLButtonElement;
constructor(leaf: WorkspaceLeaf, plugin: PromptManagerPlugin) {
super(leaf);
this.plugin = plugin;
}
getViewType(): string {
return VIEW_TYPE_PROMPT;
}
getDisplayText(): string {
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('🔄 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();
}
private 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 Notice('Please select a prompts folder in the plugin settings.');
return;
}
const folder = this.app.vault.getAbstractFileByPath(promptFolderPath);
if (!folder || !(folder instanceof TFolder)) {
new 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: TFile) => file.extension === 'md');
this.prompts = await Promise.all(
mdFiles.map(async (file: TFile) => {
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;
});
}
private extractPromptData(content: string): { version: string, details: string, fullContent: string } {
// 匹配所有的 Version 标题
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]}\n${v[2]}`).join('\n\n');
return {
version,
details: versionContent,
fullContent
};
}
private renderList(container: HTMLElement) {
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 Notice(`Copied prompt version ${prompt.version} content`);
})
.catch(err => {
console.error('Failed to copy: ', err);
new Notice('Failed to copy content');
});
};
});
}
private renderBoard(container: HTMLElement) {
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(); // 防止触发card的点击事件
navigator.clipboard.writeText(prompt.details)
.then(() => {
new Notice(`Copied prompt version ${prompt.version} content`);
})
.catch(err => {
console.error('Failed to copy: ', err);
new Notice('Failed to copy content');
});
};
card.onclick = () => this.openPrompt(prompt.file);
});
}
private filterPrompts(searchTerm: string) {
const items = this.containerEl.querySelectorAll(
this.viewType === 'board' ? '.prompt-card' : '.prompt-list-table tbody tr'
);
items.forEach((item: HTMLElement) => {
const text = item.textContent?.toLowerCase() || '';
item.style.display = text.includes(searchTerm) ? '' : 'none';
});
}
private async showNewPromptModal() {
const modal = new NewPromptModal(this.app, async (name) => {
if (name) {
const promptFolderPath = this.plugin.settings.promptFolderPath;
if (!promptFolderPath) {
new Notice('Please select a prompts folder in the plugin settings.');
return;
}
const folder = this.app.vault.getAbstractFileByPath(promptFolderPath);
if (!folder || !(folder instanceof TFolder)) {
new Notice('Invalid prompts folder selected.');
return;
}
const content = `### Version 1.0\n\nNew prompt content`;
const file = await this.app.vault.create(name + '.md', content);
await this.refresh();
this.render();
}
});
modal.open();
}
private async openPrompt(file: TFile) {
const leaf = this.app.workspace.getLeaf();
if (leaf) {
await leaf.openFile(file);
}
}
}
class NewPromptModal extends Modal {
private result: string;
private onSubmit: (name: string) => void;
constructor(app: App, onSubmit: (name: string) => void) {
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();
}
}