-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
458 lines (385 loc) · 16.3 KB
/
main.js
File metadata and controls
458 lines (385 loc) · 16.3 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
const { Plugin, ItemView, WorkspaceLeaf, PluginSettingTab, Setting, TFolder, Modal, Notice, normalizePath } = require('obsidian');
class PlotMasterPlugin extends Plugin {
DEFAULT_SETTINGS = {
worksFolder: 'Works',
plotPointsFolder: 'PlotPoints',
charactersFolder: 'Characters',
tagFilter: '',
showStatus: true,
enableVisualization: false
}
async onload() {
console.log('Loading PlotMaster Plugin');
await this.loadSettings();
await this.createFolderIfNotExists(this.settings.worksFolder);
this.addRibbonIcon('book', 'PlotMaster', () => {
this.activateView();
});
this.addCommand({
id: 'create-work',
name: 'Create new work',
callback: () => this.createWork()
});
this.addCommand({
id: 'create-plot-point',
name: 'Create plot point',
callback: () => this.createPlotPoint()
});
this.addCommand({
id: 'create-character',
name: 'Create character',
callback: () => this.createCharacter()
});
this.registerView(
'plotmaster-view',
(leaf) => new PlotMasterView(leaf, this)
);
this.addSettingTab(new PlotMasterSettingTab(this.app, this));
}
async createFolderIfNotExists(folderPath) {
const { vault } = this.app;
const normalizedPath = normalizePath(folderPath);
if (!(await vault.adapter.exists(normalizedPath))) {
await vault.createFolder(normalizedPath);
}
}
async activateView() {
const { workspace } = this.app;
let leaf = workspace.getLeavesOfType('plotmaster-view')[0];
if (!leaf) {
leaf = workspace.getRightLeaf(false);
await leaf.setViewState({ type: 'plotmaster-view' });
}
workspace.revealLeaf(leaf);
}
async createWork() {
const workName = await this.promptForWorkName();
if (!workName) return;
const workFolder = normalizePath(`${this.settings.worksFolder}/${workName}`);
await this.createFolderIfNotExists(workFolder);
await this.createFolderIfNotExists(normalizePath(`${workFolder}/${this.settings.plotPointsFolder}`));
await this.createFolderIfNotExists(normalizePath(`${workFolder}/${this.settings.charactersFolder}`));
const workFile = await this.app.vault.create(
normalizePath(`${workFolder}/${workName}.md`),
'---\ntitle: ' + workName + '\nsummary: \ngenre: \n---\n\n'
);
this.app.workspace.activeLeaf.openFile(workFile);
}
async promptForWorkName() {
const modal = new WorkNameModal(this.app);
return new Promise((resolve) => {
modal.onClose = () => resolve(modal.workName);
modal.open();
});
}
async createPlotPoint() {
const workFolder = await this.selectWorkFolder();
if (!workFolder) return;
const plotPoint = await this.app.vault.create(
normalizePath(`${workFolder}/${this.settings.plotPointsFolder}/${Date.now()}.md`),
'---\ntitle: \nscene: \nstatus: planning\n---\n\n'
);
this.app.workspace.activeLeaf.openFile(plotPoint);
}
async createCharacter() {
const workFolder = await this.selectWorkFolder();
if (!workFolder) return;
const workPath = typeof workFolder === 'string' ? workFolder : workFolder.path;
const characterPath = normalizePath(`${workPath}/${this.settings.charactersFolder}/${Date.now()}.md`);
const character = await this.app.vault.create(
characterPath,
'---\nname: \nrole: \nbackground: \npersonality: \n---\n\n'
);
this.app.workspace.activeLeaf.openFile(character);
}
async selectWorkFolder() {
const worksFolder = this.app.vault.getAbstractFileByPath(this.settings.worksFolder);
if (!(worksFolder instanceof TFolder)) {
new Notice('Works folder not found');
return null;
}
const works = worksFolder.children.filter(child => child instanceof TFolder);
if (works.length === 0) {
new Notice('No works found. Please create a work first.');
return null;
}
const modal = new WorkSelectorModal(this.app, works);
const selectedWork = await new Promise((resolve) => {
modal.onClose = () => resolve(modal.selectedWork);
modal.open();
});
return selectedWork ? selectedWork.path : null;
}
async loadSettings() {
this.settings = Object.assign({}, this.DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
onunload() {
console.log('Unloading PlotMaster Plugin');
}
}
class PlotMasterView extends ItemView {
constructor(leaf, plugin) {
super(leaf);
this.plugin = plugin;
}
getViewType() {
return 'plotmaster';
}
getDisplayText() {
return 'Plot Master';
}
async onOpen() {
const container = this.containerEl.children[1];
container.empty();
const worksEl = container.createEl('div');
worksEl.createEl('h3', { text: 'Works' });
const works = await this.getWorks();
this.renderWorks(worksEl, works);
if (this.plugin.settings.enableVisualization) {
this.renderVisualization(container, works);
}
}
async getWorks() {
const worksFolder = this.app.vault.getAbstractFileByPath(this.plugin.settings.worksFolder);
if (!(worksFolder instanceof TFolder)) return [];
return worksFolder.children.filter(child => child instanceof TFolder);
}
renderWorks(containerEl, works) {
const ul = containerEl.createEl('ul');
for (let work of works) {
const li = ul.createEl('li');
const link = li.createEl('a', { text: work.name, href: work.path });
link.addEventListener('click', (event) => {
event.preventDefault();
this.renderWorkDetails(containerEl, work);
});
}
}
async renderWorkDetails(containerEl, work) {
containerEl.empty();
containerEl.createEl('h3', { text: work.name });
const plotPointsEl = containerEl.createEl('div');
plotPointsEl.createEl('h4', { text: 'Plot points' });
const plotPoints = await this.getPlotPoints(work);
this.renderPlotPoints(plotPointsEl, plotPoints);
const charactersEl = containerEl.createEl('div');
charactersEl.createEl('h4', { text: 'Characters' });
const characters = await this.getCharacters(work);
this.renderCharacters(charactersEl, characters);
if (this.plugin.settings.enableVisualization) {
this.renderVisualization(containerEl, [work]);
}
}
async getPlotPoints(work) {
const plotPointsFolder = work.children.find(child => child.name === this.plugin.settings.plotPointsFolder);
if (!(plotPointsFolder instanceof TFolder)) return [];
let plotPoints = plotPointsFolder.children;
if (this.plugin.settings.tagFilter) {
const tags = this.plugin.settings.tagFilter.split(',').map(tag => tag.trim());
plotPoints = await this.filterFilesByTags(plotPoints, tags);
}
return plotPoints;
}
async getCharacters(work) {
const charactersFolder = work.children.find(child => child.name === this.plugin.settings.charactersFolder);
if (!(charactersFolder instanceof TFolder)) return [];
let characters = charactersFolder.children;
if (this.plugin.settings.tagFilter) {
const tags = this.plugin.settings.tagFilter.split(',').map(tag => tag.trim());
characters = await this.filterFilesByTags(characters, tags);
}
return characters;
}
async filterFilesByTags(files, tags) {
const filteredFiles = [];
for (const file of files) {
const content = await this.app.vault.read(file);
const fileTags = this.getTagsFromContent(content);
if (tags.some(tag => fileTags.includes(tag))) {
filteredFiles.push(file);
}
}
return filteredFiles;
}
getTagsFromContent(content) {
const frontmatter = this.app.metadataCache.getFileCache(content).frontmatter;
return frontmatter && frontmatter.tags ? frontmatter.tags : [];
}
renderPlotPoints(containerEl, plotPoints) {
const ul = containerEl.createEl('ul');
for (let plot of plotPoints) {
const li = ul.createEl('li');
const link = li.createEl('a', { text: plot.basename, href: plot.path });
if (this.plugin.settings.showStatus) {
const status = this.getStatusFromFile(plot);
li.createEl('span', { text: ` [${status}]`, cls: `status-${status}` });
}
link.addEventListener('click', (event) => {
event.preventDefault();
this.app.workspace.activeLeaf.openFile(plot);
});
}
}
renderCharacters(containerEl, characters) {
const ul = containerEl.createEl('ul');
for (let character of characters) {
const li = ul.createEl('li');
const link = li.createEl('a', { text: character.basename, href: character.path });
link.addEventListener('click', (event) => {
event.preventDefault();
this.app.workspace.activeLeaf.openFile(character);
});
}
}
getStatusFromFile(file) {
const cache = this.app.metadataCache.getFileCache(file);
return cache && cache.frontmatter && cache.frontmatter.status ? cache.frontmatter.status : 'unknown';
}
renderVisualization(containerEl, works) {
const visualizationEl = containerEl.createEl('div', { cls: 'plotmaster-visualization' });
visualizationEl.createEl('h3', { text: 'Story visualization' });
works.forEach(async (work) => {
const workEl = visualizationEl.createEl('div', { cls: 'plotmaster-work-container' });
const workHeader = workEl.createEl('div', { cls: 'plotmaster-work-header' });
workHeader.createEl('h4', { text: work.name, cls: 'plotmaster-work-title' });
const contentEl = workEl.createEl('div', { cls: 'plotmaster-work-content' });
const plotPointsEl = contentEl.createEl('div', { cls: 'plotmaster-column plotmaster-plotpoints' });
plotPointsEl.createEl('h5', { text: 'Plot points' });
const charactersEl = contentEl.createEl('div', { cls: 'plotmaster-column plotmaster-characters' });
charactersEl.createEl('h5', { text: 'Characters' });
const plotPoints = await this.getPlotPoints(work);
const characters = await this.getCharacters(work);
plotPoints.forEach((plot) => {
plotPointsEl.createEl('div', {
cls: 'plotmaster-item plotmaster-plot',
text: plot.basename
});
});
characters.forEach((character) => {
charactersEl.createEl('div', {
cls: 'plotmaster-item plotmaster-character',
text: character.basename
});
});
});
}
async onClose() {
// Nothing to clean up.
}
}
class PlotMasterSettingTab extends PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
let { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('Works folder')
.setDesc('Folder path for works')
.addText(text => text
.setPlaceholder('Works')
.setValue(this.plugin.settings.worksFolder)
.onChange(async (value) => {
this.plugin.settings.worksFolder = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Plot points folder')
.setDesc('Folder name for plot points within each work')
.addText(text => text
.setPlaceholder('PlotPoints')
.setValue(this.plugin.settings.plotPointsFolder)
.onChange(async (value) => {
this.plugin.settings.plotPointsFolder = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Characters folder')
.setDesc('Folder name for characters within each work')
.addText(text => text
.setPlaceholder('Characters')
.setValue(this.plugin.settings.charactersFolder)
.onChange(async (value) => {
this.plugin.settings.charactersFolder = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Tag filter')
.setDesc('Filter plot points and characters by tag (leave empty for no filter)')
.addText(text => text
.setPlaceholder('tag1, tag2')
.setValue(this.plugin.settings.tagFilter)
.onChange(async (value) => {
this.plugin.settings.tagFilter = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Show status')
.setDesc('Show status indicators for plot points')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.showStatus)
.onChange(async (value) => {
this.plugin.settings.showStatus = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Enable visualization')
.setDesc('Enable graph visualization for works, plot points, and characters')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.enableVisualization)
.onChange(async (value) => {
this.plugin.settings.enableVisualization = value;
await this.plugin.saveSettings();
}));
}
}
class WorkNameModal extends Modal {
constructor(app) {
super(app);
this.workName = null;
}
onOpen() {
const { contentEl } = this;
contentEl.createEl('h2', { text: 'Enter work name' });
const input = contentEl.createEl('input', { type: 'text' });
const submitButton = contentEl.createEl('button', { text: 'Create' });
submitButton.addEventListener('click', () => {
this.workName = input.value;
this.close();
});
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
class WorkSelectorModal extends Modal {
constructor(app, works) {
super(app);
this.works = works;
this.selectedWork = null;
}
onOpen() {
const { contentEl } = this;
contentEl.createEl('h2', { text: 'Select work' });
const select = contentEl.createEl('select');
this.works.forEach(work => {
select.createEl('option', { text: work.name, value: work.path });
});
const submitButton = contentEl.createEl('button', { text: 'Select' });
submitButton.addEventListener('click', () => {
this.selectedWork = this.works.find(work => work.path === select.value);
this.close();
});
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
module.exports = PlotMasterPlugin;