-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.ts
More file actions
295 lines (267 loc) · 9.57 KB
/
main.ts
File metadata and controls
295 lines (267 loc) · 9.57 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
/**
* @file main.ts
* @description Plugin entry point for Abstract Folder.
* @author Erfan Rahmani
* @license GPL-3.0
* @copyright 2025 Erfan Rahmani
*/
import { Plugin, WorkspaceLeaf, Notice } from 'obsidian';
import { Logger } from './src/utils/logger';
import { AbstractFolderPluginSettings, DEFAULT_SETTINGS } from './src/settings';
import { FolderIndexer } from './src/indexer';
import { MetricsManager } from './src/metrics-manager';
import { AbstractFolderView, VIEW_TYPE_ABSTRACT_FOLDER } from './src/view';
import { CreateAbstractChildModal, ParentPickerModal, ChildFileType, FolderSelectionModal, ConversionOptionsModal, DestinationPickerModal, NewFolderNameModal, SimulationModal, ScopeSelectionModal, CreateEditGroupModal } from './src/ui/modals';
import { ManageGroupsModal } from './src/ui/modals/manage-groups-modal';
import { AbstractFolderSettingTab } from './src/ui/settings-tab';
import { createAbstractChildFile } from './src/utils/file-operations';
import { convertFoldersToPluginFormat, generateFolderStructurePlan, executeFolderGeneration } from './src/utils/conversion';
import { TFolder, TFile } from 'obsidian';
import { Group } from './src/types';
import './src/styles/index.css';
export default class AbstractFolderPlugin extends Plugin {
settings: AbstractFolderPluginSettings;
indexer: FolderIndexer;
metricsManager: MetricsManager;
ribbonIconEl: HTMLElement | null = null;
async onload() {
Logger.debug("Starting onload...");
await this.loadSettings();
this.indexer = new FolderIndexer(this.app, this.settings, this);
this.metricsManager = new MetricsManager(this.app, this.indexer, this);
Logger.debug("Initializing indexer...");
this.indexer.initializeIndexer();
this.registerView(
VIEW_TYPE_ABSTRACT_FOLDER,
(leaf) => new AbstractFolderView(leaf, this.indexer, this.settings, this, this.metricsManager)
);
this.updateRibbonIconVisibility();
this.addCommand({
id: "open-view",
name: "Open view",
callback: () => {
this.activateView().catch(console.error);
},
});
this.addCommand({
id: "focus-active-file",
name: "Toggle focus on active file in abstract tree",
callback: () => {
const activeFile = this.app.workspace.getActiveFile();
if (!activeFile) {
new Notice("No active file to focus.");
return;
}
this.activateView().then(() => {
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_ABSTRACT_FOLDER);
if (leaves.length > 0) {
const view = leaves[0].view as AbstractFolderView;
view.focusFile(activeFile.path);
}
}).catch(console.error);
}
});
this.addCommand({
id: "focus-search",
name: "Focus search bar",
callback: () => {
this.activateView().then(() => {
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_ABSTRACT_FOLDER);
if (leaves.length > 0) {
const view = leaves[0].view as AbstractFolderView;
view.focusSearch();
}
}).catch(console.error);
}
});
this.addCommand({
id: "create-child",
name: "Create abstract child",
callback: () => {
new CreateAbstractChildModal(this.app, this.settings, (childName: string, childType: ChildFileType) => {
new ParentPickerModal(this.app, (parentFile) => {
createAbstractChildFile(this.app, this.settings, childName, parentFile, childType, this.indexer)
.catch(console.error);
}).open();
}).open();
},
});
this.addCommand({
id: "manage-groups",
name: "Manage groups",
callback: () => {
new ManageGroupsModal(this.app, this.settings, (updatedGroups: Group[], activeGroupId: string | null) => {
this.settings.groups = updatedGroups;
this.settings.activeGroupId = activeGroupId;
this.saveSettings().then(() => {
this.app.workspace.trigger('abstract-folder:group-changed');
}).catch(console.error);
}).open();
},
});
this.addCommand({
id: "create-group-with-active-file",
name: "Create group with active file",
callback: () => {
const activeFile = this.app.workspace.getActiveFile();
if (!activeFile) {
new Notice("No active file to create a group from.");
return;
}
const prefilledGroup: Group = {
id: Math.random().toString(36).substring(2, 15),
name: activeFile.basename,
parentFolders: [activeFile.path],
};
new CreateEditGroupModal(this.app, this.settings, prefilledGroup, (updatedGroup: Group) => {
this.settings.groups.push(updatedGroup);
this.settings.activeGroupId = updatedGroup.id;
this.saveSettings().then(() => {
this.app.workspace.trigger('abstract-folder:group-changed');
this.activateView().catch(console.error);
}).catch(console.error);
}).open();
}
});
this.addCommand({
id: "clear-active-group",
name: "Clear active group",
callback: () => {
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_ABSTRACT_FOLDER);
if (leaves.length > 0) {
const view = leaves[0].view as AbstractFolderView;
view.clearActiveGroup();
} else if (this.settings.activeGroupId) {
this.settings.activeGroupId = null;
this.saveSettings().then(() => {
new Notice("Active group cleared.");
this.app.workspace.trigger('abstract-folder:group-changed');
}).catch(console.error);
} else {
new Notice("No active group to clear.");
}
},
});
this.addCommand({
id: "convert-folder-to-plugin",
name: "Convert folder structure to plugin format",
callback: () => {
new FolderSelectionModal(this.app, (folder: TFolder) => {
new ConversionOptionsModal(this.app, folder, (options) => {
convertFoldersToPluginFormat(this.app, this.settings, folder, options)
.catch(console.error);
}).open();
}).open();
}
});
this.addCommand({
id: "create-folders-from-plugin",
name: "Create folder structure from plugin format",
callback: () => {
new ScopeSelectionModal(this.app, (scope) => {
new DestinationPickerModal(this.app, (parentFolder: TFolder) => {
new NewFolderNameModal(this.app, parentFolder, (destinationPath: string, placeIndexFileInside: boolean) => {
// Automatically add the export folder to excluded paths if not already present
if (!this.settings.excludedPaths.includes(destinationPath)) {
this.settings.excludedPaths.push(destinationPath);
this.saveSettings().then(() => {
this.indexer.updateSettings(this.settings);
}).catch(console.error);
}
const rootScope = (scope instanceof TFile) ? scope : undefined;
const plan = generateFolderStructurePlan(this.app, this.settings, this.indexer, destinationPath, placeIndexFileInside, rootScope);
new SimulationModal(this.app, plan.conflicts, (resolvedConflicts) => {
executeFolderGeneration(this.app, plan).catch((error) => console.error(error));
}).open();
}).open();
}).open();
}).open();
}
});
this.addSettingTab(new AbstractFolderSettingTab(this.app, this));
this.app.workspace.onLayoutReady(() => {
this.metricsManager.applyDecay();
this.indexer.rebuildGraphAndTriggerUpdate();
if (this.settings.startupOpen) {
this.activateView().catch(console.error);
}
});
}
onunload() {
Logger.debug("Starting onunload...");
this.indexer.onunload();
Logger.debug("Saving metrics...");
void this.metricsManager.saveMetrics();
if (this.ribbonIconEl) {
this.ribbonIconEl.remove();
this.ribbonIconEl = null;
}
}
async activateView() {
let leaf: WorkspaceLeaf | null = null;
const leaves = this.app.workspace.getLeavesOfType(VIEW_TYPE_ABSTRACT_FOLDER);
if (leaves.length > 0) {
// If a leaf of our view type already exists, use it
leaf = leaves[0];
} else {
// No existing leaf found, create a new one
const side = this.settings.openSide;
if (side === 'left') {
leaf = this.app.workspace.getLeftLeaf(false);
if (!leaf) {
leaf = this.app.workspace.getLeftLeaf(true);
}
} else { // right
leaf = this.app.workspace.getRightLeaf(false);
if (!leaf) {
leaf = this.app.workspace.getRightLeaf(true);
}
}
}
if (leaf) {
await leaf.setViewState({
type: VIEW_TYPE_ABSTRACT_FOLDER,
active: true,
});
if (leaf instanceof WorkspaceLeaf) {
void this.app.workspace.revealLeaf(leaf);
}
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, (await this.loadData()) as AbstractFolderPluginSettings);
// Migration: Ensure multiple property name arrays are initialized from legacy settings
if (this.settings.propertyName && (!this.settings.parentPropertyNames || this.settings.parentPropertyNames.length === 0)) {
this.settings.parentPropertyNames = [this.settings.propertyName];
}
if (this.settings.childrenPropertyName && (!this.settings.childrenPropertyNames || this.settings.childrenPropertyNames.length === 0)) {
this.settings.childrenPropertyNames = [this.settings.childrenPropertyName];
}
// Cleanup: Remove legacy "views" field if it exists in the data.json as it causes issues in newer versions
// and is no longer part of the settings interface.
const settingsRecord = this.settings as unknown as Record<string, unknown>;
if (settingsRecord.views) {
Logger.debug("Found legacy 'views' field in settings, removing it.");
delete settingsRecord.views;
await this.saveSettings();
}
}
async saveSettings() {
await this.saveData(this.settings);
this.updateRibbonIconVisibility();
}
updateRibbonIconVisibility() {
if (this.settings.showRibbonIcon) {
if (!this.ribbonIconEl) {
this.ribbonIconEl = this.addRibbonIcon("folder-tree", "Open abstract folders", () => {
this.activateView().catch(console.error);
});
}
} else {
if (this.ribbonIconEl) {
this.ribbonIconEl.remove();
this.ribbonIconEl = null;
}
}
}
}