-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
main.ts
639 lines (542 loc) · 17 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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
import {
WorkspaceLeaf,
Plugin,
MarkdownPostProcessorContext,
TFolder,
TFile,
ViewState,
Platform,
MarkdownView,
addIcon,
} from 'obsidian';
import {
DatabaseView,
} from 'views/DatabaseView';
import {
DBFolderSettingTab,
loadServicesThatRequireSettings
} from 'Settings';
import {
DbfAPIInterface
} from 'typings/api';
import { DatabaseSettings, LocalSettings } from 'cdm/SettingsModel';
import StateManager from 'StateManager';
import { around } from 'monkey-around';
import { LOGGER } from 'services/Logger';
import { DatabaseCore, DATABASE_CONFIG, DB_ICONS, DEFAULT_SETTINGS, EMITTERS_GROUPS, YAML_INDENT } from 'helpers/Constants';
import { PreviewDatabaseModeService } from 'services/MarkdownPostProcessorService';
import { unmountComponentAtNode } from 'react-dom';
import { isDatabaseNote } from 'helpers/VaultManagement';
import { getParentWindow } from 'helpers/WindowElement';
import { DatabaseHelperCreationModal } from 'commands/addDatabaseHelper/databaseHelperCreationModal';
import { generateDbConfiguration, generateNewDatabase } from 'helpers/CommandsHelper';
import { registerDateFnLocale, t } from 'lang/helpers';
import ProjectAPI from 'api/obsidian-projects-api';
import { Db } from "services/CoreService";
import { CustomView } from 'views/AbstractView';
interface WindowRegistry {
viewMap: Map<string, CustomView>;
viewStateReceivers: Array<(views: CustomView[]) => void>;
appRoot: HTMLElement;
}
export default class DBFolderPlugin extends Plugin {
/** Plugin-wide default settings. */
public settings: DatabaseSettings;
/** External-facing plugin API */
public api: DbfAPIInterface;
onRegisterProjectView = () => new ProjectAPI(this);
public hover: { linkText: string; sourcePath: string } = {
linkText: null,
sourcePath: null,
};
databaseFileModes: Record<string, string> = {};
_loaded = false;
stateManagers: Map<TFile, StateManager> = new Map();
windowRegistry: Map<Window, WindowRegistry> = new Map();
ribbonIcon: HTMLElement;
statusBarItem: HTMLElement;
async onload(): Promise<void> {
await this.load_settings();
await this.loadServices();
addIcon(DB_ICONS.NAME, DB_ICONS.ICON);
this.registerEvent(
app.workspace.on('window-open', (_: unknown, win: Window) => {
this.mount(win);
})
);
this.registerEvent(
app.workspace.on('window-close', (_: unknown, win: Window) => {
this.unmount(win);
})
);
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new DBFolderSettingTab(this, {
onSettingsChange: async (newSettings) => {
this.settings = newSettings;
await this.saveSettings();
//Force a complete re-render when settings change
this.stateManagers.forEach((stateManager) => {
stateManager.forceRefresh();
});
},
})
);
this.registerView(DatabaseCore.FRONTMATTER_KEY, (leaf) => new DatabaseView(leaf, this));
this.registerEvents();
this.registerCommands();
this.registerMonkeyPatches();
this.addMarkdownPostProcessor();
this.registerLocale();
// Mount an empty component to start; views will be added as we go
this.mount(window);
}
unload(): void {
Promise.all(
app.workspace.getLeavesOfType(DatabaseCore.FRONTMATTER_KEY).map((leaf) => {
this.databaseFileModes[leaf.id] = 'markdown';
return this.setMarkdownView(leaf);
})
).then(() => {
super.unload();
});
}
async onunload() {
LOGGER.info('Unloading DBFolder plugin');
this.windowRegistry.forEach((reg, win) => {
reg.viewStateReceivers.forEach((fn) => fn([]));
this.unmount(win);
});
this.unmount(window);
this.stateManagers.clear();
this.windowRegistry.clear();
this.databaseFileModes = {};
app.workspace.unregisterHoverLinkSource(DatabaseCore.FRONTMATTER_KEY);
}
/** Update plugin settings. */
async updateSettings(settings: Partial<DatabaseSettings>) {
Object.assign(this.settings, settings);
await this.saveData(this.settings);
}
async saveSettings() {
await this.saveData(this.settings);
}
async load_settings(): Promise<void> {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
loadServicesThatRequireSettings(this.settings);
}
async loadServices() {
await Db.init();
}
public registerPriorityCodeblockPostProcessor(
language: string,
priority: number,
processor: (source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => Promise<void>
) {
const registered = this.registerMarkdownCodeBlockProcessor(language, processor);
registered.sortOrder = priority;
}
async setDatabaseView(leaf: WorkspaceLeaf) {
await leaf.setViewState({
type: DatabaseCore.FRONTMATTER_KEY,
state: leaf.view.getState(),
popstate: true,
} as ViewState);
}
viewStateReceivers: Array<(views: CustomView[]) => void> = [];
addView(view: CustomView) {
const win = view.getWindow();
const reg = this.windowRegistry.get(win);
if (!reg) {
return;
}
if (!reg.viewMap.has(view.id)) {
reg.viewMap.set(view.id, view);
}
const file = view.file;
if (this.stateManagers.has(file)) {
this.stateManagers.get(file).registerView(view);
} else {
this.stateManagers.set(
file,
new StateManager(
view,
() => this.stateManagers.delete(file),
() => this.settings
)
);
}
reg.viewStateReceivers.forEach((fn) => fn(this.getDatabaseViews(win)));
}
getStateManager(file: TFile) {
return this.stateManagers.get(file);
}
getStateManagerFromViewID(id: string, win: Window) {
const view = this.getDatabaseView(id, win);
if (!view) {
return null;
}
return this.stateManagers.get(view.file);
}
removeView(view: CustomView) {
const entry = Array.from(this.windowRegistry.entries()).find(([, reg]) => {
return reg.viewMap.has(view.id);
}, []);
if (!entry) {
return;
}
const [win, reg] = entry;
const file = view.file;
if (reg.viewMap.has(view.id)) {
reg.viewMap.delete(view.id);
}
if (this.stateManagers.has(file)) {
this.stateManagers.get(file).unregisterView(view);
reg.viewStateReceivers.forEach((fn) => fn(this.getDatabaseViews(win)));
}
}
unmount(win: Window) {
if (!this.windowRegistry.has(win)) {
return;
}
const reg = this.windowRegistry.get(win);
for (const view of reg.viewMap.values()) {
view.destroy();
}
unmountComponentAtNode(reg.appRoot);
reg.appRoot.remove();
reg.viewMap.clear();
reg.viewStateReceivers.length = 0;
reg.appRoot = null;
this.windowRegistry.delete(win);
}
async setMarkdownView(leaf: WorkspaceLeaf, focus = true) {
await leaf.setViewState(
{
type: 'markdown',
state: leaf.view.getState(),
popstate: true,
} as ViewState,
{ focus }
);
}
getDatabaseViews(win: Window) {
const reg = this.windowRegistry.get(win);
if (reg) {
return Array.from(reg.viewMap.values());
}
return [];
}
getDatabaseView(id: string, win: Window) {
const reg = this.windowRegistry.get(win);
if (reg?.viewMap.has(id)) {
return reg.viewMap.get(id);
}
for (const reg of this.windowRegistry.values()) {
if (reg.viewMap.has(id)) {
return reg.viewMap.get(id);
}
}
return null;
}
mount(win: Window) {
if (this.windowRegistry.has(win)) {
return;
}
const el = win.document.body.createDiv();
this.windowRegistry.set(win, {
viewMap: new Map(),
viewStateReceivers: [],
appRoot: el,
});
}
/**
* Returns the default configuration for a database file.
*/
defaultConfiguration(): string {
const local_settings = this.settings.local_settings;
const defaultConfig = [];
defaultConfig.push("config:");
Object.entries(DEFAULT_SETTINGS.local_settings).forEach(([key, value]) => {
const defaultValue = local_settings[key as keyof LocalSettings] !== undefined ? local_settings[key as keyof LocalSettings] : value;
defaultConfig.push(`${YAML_INDENT}${key}: ${defaultValue}`);
});
defaultConfig.push(DATABASE_CONFIG.END_CENTINEL);
return defaultConfig.join('\n');
}
registerEvents() {
this.registerEvent(
app.workspace.on('file-menu', (menu, file: TFile, source, leaf) => {
// Add a menu item to the folder context menu to create a database
if (file instanceof TFolder) {
menu.addItem((item) => {
item
.setTitle(t("menu_pane_create_db"))
.setIcon(DB_ICONS.NAME)
.onClick(() => generateNewDatabase(
generateDbConfiguration(this.settings.local_settings),
file
));
});
return;
}
if (
!Platform.isMobile &&
file instanceof TFile &&
leaf &&
source === 'sidebar-context-menu' &&
isDatabaseNote(file)
) {
const views = this.getDatabaseViews(
getParentWindow(leaf.view.containerEl)
);
const haveDatabaseView = views.some((view) => {
if (view.file === file) {
view.onPaneMenu(menu, 'more-options', false);
return true;
}
return false;
});
if (!haveDatabaseView) {
menu.addItem((item) => {
item
.setTitle(t("menu_pane_open_as_db_action"))
.setIcon(DB_ICONS.NAME)
.setSection('pane')
.onClick(() => {
this.databaseFileModes[leaf.id || file.path] =
DatabaseCore.FRONTMATTER_KEY;
this.setDatabaseView(leaf);
});
});
return;
}
}
if (
leaf?.view instanceof MarkdownView &&
file instanceof TFile &&
source === 'pane-more-options' &&
isDatabaseNote(file)
) {
menu.addItem((item) => {
item
.setTitle(t("menu_pane_open_as_db_action"))
.setIcon(DB_ICONS.NAME)
.setSection('pane')
.onClick(() => {
this.databaseFileModes[leaf.id || file.path] =
DatabaseCore.FRONTMATTER_KEY;
this.setDatabaseView(leaf);
});
});
}
})
);
/**
* When the Dataview index is ready, trigger the index ready event.
*/
this.registerEvent(
app.metadataCache.on("dataview:index-ready", async () => {
const initView = app.workspace.getActiveViewOfType(DatabaseView);
if (initView) {
await initView.reloadDatabase();
}
/**
* Once the index is ready, we can start listening for metadata changes.
*/
if (this.settings.global_settings.enable_auto_update) {
// Delay the registration of the event to allow the index to be ready entirely
setTimeout(() => {
this.registerEvent(app.metadataCache.on("dataview:metadata-change",
(type, file, oldPath?) => {
const activeView = app.workspace.getActiveViewOfType(DatabaseView);
Array.from(this.windowRegistry.entries()).forEach(async ([, { viewMap }]) => {
// Iterate through all the views and reload the database if the file is the same
viewMap.forEach(async (view) => {
const isActive = activeView && (view.file.path === activeView?.file.path);
view.handleExternalMetadataChange(type, file, isActive, oldPath);
});
});
})
);
}, 2500);
}
}));
/**
* Check when the active view focus changes and update bar status
*/
this.registerEvent(app.workspace.on("active-leaf-change", () => {
const activeView = app.workspace.getActiveViewOfType(DatabaseView);
if (!activeView && this.statusBarItem) {
this.statusBarItem.detach();
this.statusBarItem = null;
}
else if (activeView && this.statusBarItem) {
activeView.handleUpdateStatusBar();
}
}));
}
registerCommands() {
// Creator Helper Command
this.addCommand({
id: 'create-new-database-folder',
name: t("ribbon_icon_title"),
callback: () => new DatabaseHelperCreationModal(this.settings.local_settings).open(),
});
// Active View Go Next Page
this.addCommand({
id: 'active-database-folder-go-next-page',
name: t('active_go_next_page'),
checkCallback: (checking) => {
const activeView = app.workspace.getActiveViewOfType(DatabaseView);
if (!activeView) return false;
if (checking) return true;
activeView.goNextPage();
},
});
// Active View Go Previous Page
this.addCommand({
id: 'active-database-folder-go-previous-page',
name: t('active_go_previous_page'),
checkCallback: (checking) => {
const activeView = app.workspace.getActiveViewOfType(DatabaseView);
if (!activeView) return false;
if (checking) return true;
activeView.goPreviousPage();
},
});
// Active View Add New Row Modal
this.addCommand({
id: 'active-database-folder-add-new-row',
name: t('active_add_new_row'),
checkCallback: (checking) => {
const activeView = app.workspace.getActiveViewOfType(DatabaseView);
if (!activeView) return false;
if (checking) return true;
activeView.addNewRow();
},
});
// Active View Open Settings
this.addCommand({
id: 'active-database-folder-open-settings',
name: t('active_open_settings'),
checkCallback: (checking) => {
const activeView = app.workspace.getActiveViewOfType(DatabaseView);
if (!activeView) return false;
if (checking) return true;
activeView.settingsAction();
},
});
// Active View Enable/Disable Filters
this.addCommand({
id: 'active-database-folder-toggle-filters',
name: t('active_toggle_filters'),
checkCallback: (checking) => {
const activeView = app.workspace.getActiveViewOfType(DatabaseView);
if (!activeView) return false;
if (checking) return true;
activeView.toggleFilters();
},
});
// Active View Open Filters Modal
this.addCommand({
id: 'active-database-folder-open-filters',
name: t('active_open_filters'),
checkCallback: (checking) => {
const activeView = app.workspace.getActiveViewOfType(DatabaseView);
if (!activeView) return false;
if (checking) return true;
activeView.openFilters();
},
});
// Ribbon Icon
this.showRibbonIcon();
}
showRibbonIcon() {
this.ribbonIcon = this.addRibbonIcon(DB_ICONS.NAME, t("ribbon_icon_title"), async () => {
new DatabaseHelperCreationModal(this.settings.local_settings).open()
});
}
/**
* Displays a transcluded .excalidraw image in markdown preview mode
*/
private addMarkdownPostProcessor() {
const previewMode = PreviewDatabaseModeService.getInstance(this);
this.registerMarkdownPostProcessor(previewMode.markdownPostProcessor);
// internal-link quick preview
this.registerEvent(app.workspace.on("quick-preview", previewMode.hoverEvent));
}
/**
* Wrap Obsidian functionalities to add the database support needed
*/
registerMonkeyPatches() {
const self = this;
// Monkey patch to manage hotkey emitters
app.workspace.onLayoutReady(() => {
this.register(
around((app as any).commands, {
executeCommand(next) {
return function (command: any) {
const view = app.workspace.getActiveViewOfType(DatabaseView);
if (view && command?.id) {
view.emitter.emit(EMITTERS_GROUPS.HOTKEY, command.id);
}
return next.call(this, command);
};
},
})
);
});
// Monkey patch WorkspaceLeaf to open Databases with DatabaseView by default
this.register(
around(WorkspaceLeaf.prototype, {
// Databases can be viewed as markdown or Database, and we keep track of the mode
// while the file is open. When the file closes, we no longer need to keep track of it.
detach(next) {
return function () {
const state = this.view?.getState();
if (state?.file && self.databaseFileModes[this.id || state.file]) {
delete self.databaseFileModes[this.id || state.file];
}
return next.apply(this);
};
},
setViewState(next) {
return function (state: ViewState, ...rest: any[]) {
if (
// Don't force Databases mode during shutdown
self._loaded &&
// If we have a markdown file
state.type === 'markdown' &&
state.state?.file &&
// And the current mode of the file is not set to markdown
self.databaseFileModes[this.id || state.state.file] !== 'markdown'
) {
// Then check for the database frontMatterKey
const cache = app.metadataCache.getCache(state.state.file);
if (cache?.frontmatter && cache.frontmatter[DatabaseCore.FRONTMATTER_KEY]) {
// If we have it, force the view type to database
const newState = {
...state,
type: DatabaseCore.FRONTMATTER_KEY,
};
self.databaseFileModes[state.state.file] = DatabaseCore.FRONTMATTER_KEY;
return next.apply(this, [newState, ...rest]);
}
}
return next.apply(this, [state, ...rest]);
};
},
})
);
}
/**
* Register language used in the plugin
*/
async registerLocale() {
registerDateFnLocale();
}
}