generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
131 lines (111 loc) · 3.34 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
import {
App,
Plugin,
PluginSettingTab,
Setting,
TFile,
Notice,
} from "obsidian";
import { BindModal, MOCFile } from "./BindModal";
import { insertLink } from "./insertLink";
type MOCModalBinderSettings = {
autoBind: boolean;
};
const DEFAULT_SETTINGS: MOCModalBinderSettings = {
autoBind: true,
};
export default class MOCModalBinder extends Plugin {
settings: MOCModalBinderSettings;
async onload() {
await this.loadSettings();
// Register command to open MOC selection modal
this.addCommand({
id: "open-moc-selector",
name: "Open MOC Selector",
callback: () => {
const activeFile = this.app.workspace.getActiveFile();
if (activeFile) {
this.openMOCSelector(activeFile);
}
},
});
// Register file creation event handler if autoBind is enabled
this.registerEvent(
this.app.vault.on("create", (file) => {
if (file instanceof TFile && this.settings.autoBind === true) {
this.openMOCSelector(file);
}
})
);
// Add settings tab
this.addSettingTab(new MOCModalBinderSettingTab(this.app, this));
}
async openMOCSelector(file: TFile) {
// Get all files with MOC tag
const mocFiles = this.app.vault.getMarkdownFiles().filter((f) => {
const cache = this.app.metadataCache.getFileCache(f);
return cache?.frontmatter?.tags?.includes("MOC");
});
if (mocFiles.length === 0) {
console.warn("No MOC files found");
return;
}
// Map to MOCFile format
const mappedFiles: MOCFile[] = mocFiles.map((f) => {
const cache = this.app.metadataCache.getFileCache(f);
return {
file: f,
tags:
cache?.frontmatter?.tags?.filter((t: string) => t !== "MOC") || [],
selected: false,
};
});
new BindModal(this.app, {
files: mappedFiles,
onSelect: async () => {
const selectedFiles = mappedFiles.filter((f) => f.selected);
// Add link to selected MOC files
for (const mocFile of selectedFiles) {
await insertLink(mocFile.file, file);
}
// Add tags to new file
const allTags = selectedFiles.flatMap((f) => f.tags);
await this.app.fileManager.processFrontMatter(file, (frontmatter) => {
const currentTags = frontmatter.tags || [];
const uniqueTags = [...new Set([...currentTags, ...allTags])];
frontmatter.tags = uniqueTags;
});
},
targetFilename: file.basename,
}).open();
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class MOCModalBinderSettingTab extends PluginSettingTab {
plugin: MOCModalBinder;
constructor(app: App, plugin: MOCModalBinder) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName("Auto Bind")
.setDesc("Automatically show MOC selector modal when creating new files")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.autoBind)
.onChange(async (value) => {
this.plugin.settings.autoBind = value;
await this.plugin.saveSettings();
})
);
}
}