-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
365 lines (323 loc) · 10.4 KB
/
main.ts
File metadata and controls
365 lines (323 loc) · 10.4 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
import { Plugin, TFile, Notice } from "obsidian";
import { FrameIndexer } from "FrameIndexer";
import { EditorExtension } from "EditorExtension";
import { ExcalinkSettings, DEFAULT_SETTINGS } from "settings";
import { ExcalinkSettingTab } from "ExcalinkSettingTab";
/**
* Excalink Plugin - Day 7: Settings & Release
* Enhanced with comprehensive settings and user configuration
*/
export default class Excalink extends Plugin{
public settings: ExcalinkSettings;
public frameIndexer: FrameIndexer;
private editorExtension: EditorExtension | null;
private isInitialized = false;
async onload(): Promise<void> {
try {
// Load settings first
await this.loadSettings();
// Add settings tab
this.addSettingTab(new ExcalinkSettingTab(this.app, this));
// Only initialize core functionality if enabled
if (this.settings.enableFrameSuggestions) {
await this.initializeCore();
}
this.isInitialized = true;
} catch (error) {
console.error('❌ Critical error during plugin loading:', error);
new Notice('Excalink: Plugin failed to load. Check console for details.');
}
}
/**
* Load plugin settings with fallbacks
*/
async loadSettings(): Promise<void> {
try {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
} catch (error) {
console.error('❌ Error loading settings, using defaults:', error);
this.settings = { ...DEFAULT_SETTINGS };
}
}
/**
* Save plugin settings
*/
async saveSettings(): Promise<void> {
try {
await this.saveData(this.settings);
} catch (error) {
console.error('❌ Error saving settings:', error);
throw error;
}
}
/**
* Initialize core plugin functionality
*/
async initializeCore(): Promise<void> {
try {
// Initialize frame indexer with enhanced error handling
try {
this.frameIndexer = new FrameIndexer(this.app.vault);
await this.frameIndexer.scanAllExcalidrawFiles();
} catch (error) {
console.error('❌ Failed to initialize frame indexer:', error);
new Notice('Excalink: Failed to scan Excalidraw files. Plugin will work with limited functionality.');
// Create a fallback frame indexer to prevent crashes
this.frameIndexer = new FrameIndexer(this.app.vault);
}
// Initialize editor extension with validation
try {
if (!this.app) {
throw new Error('App instance not available');
}
this.editorExtension = new EditorExtension(this.frameIndexer, this.app, this);
this.registerEditorExtension(this.editorExtension.getExtension());
} catch (error) {
console.error('❌ Failed to initialize editor extension:', error);
new Notice('Excalink: Failed to initialize editor integration. Please restart Obsidian.');
return;
}
// Register file change event listeners for caching (if enabled)
if (this.settings.enableFileWatching) {
try {
this.setupFileChangeListeners();
} catch (error) {
console.error('❌ Failed to setup file change listeners:', error);
console.warn('⚠️ File watching disabled - frames will not auto-update');
}
}
// Add debug commands with error handling
try {
this.setupDebugCommands();
} catch (error) {
console.error('❌ Failed to register debug commands:', error);
}
} catch (error) {
console.error('❌ Error initializing core functionality:', error);
throw error;
}
}
/**
* Disable core plugin functionality
*/
disableCore(): void {
try {
// The editor extension will be automatically cleaned up by Obsidian
// when we unregister it, but we need to set our reference to null
this.editorExtension = null;
} catch (error) {
console.error('❌ Error disabling core functionality:', error);
}
}
/**
* Force rescan of all files (public method for settings)
*/
async forceRescan(): Promise<void> {
try {
if (!this.frameIndexer) {
throw new Error('Frame indexer not initialized');
}
this.frameIndexer.clearCache();
await this.frameIndexer.scanAllExcalidrawFiles();
const stats = this.frameIndexer.getCacheStats();
const message = `Rescan complete! Found ${stats.frameCount} frames in ${stats.size} files.`;
if (this.settings.showDiagnosticsInNotices) {
new Notice(message, 3000);
}
} catch (error) {
console.error('❌ Error during force rescan:', error);
throw error;
}
}
/**
* Setup debug commands for monitoring and troubleshooting
* Day 7: Enhanced with settings integration
*/
private setupDebugCommands(): void {
// Cache statistics command
this.addCommand({
id: 'show-cache-stats',
name: 'Show cache statistics',
callback: () => {
try {
const stats = this.frameIndexer.getCacheStats();
if (this.settings.enableDebugLogging) {
console.log('📊 Cache Statistics:', stats);
}
const message = `Cache statistics:\n` +
`Files: ${stats.size}\n` +
`Frames: ${stats.frameCount}\n` +
`Memory: ${stats.memoryUsage}`;
if (this.settings.showDiagnosticsInNotices) {
new Notice(message, 5000);
}
} catch (error) {
console.error('❌ Error getting cache stats:', error);
new Notice('Error retrieving cache statistics');
}
}
});
// Full diagnostics command
this.addCommand({
id: 'show-diagnostics',
name: 'Show plugin diagnostics',
callback: () => {
try {
const diagnostics = this.frameIndexer.getDiagnostics();
if (this.settings.enableDebugLogging) {
console.log('🔍 Plugin Diagnostics:', diagnostics);
}
const message = `Plugin diagnostics:\n` +
`Initialized: ${diagnostics.isInitialized ? '✅' : '❌'}\n` +
`Files: ${diagnostics.totalFiles}\n` +
`Frames: ${diagnostics.totalFrames}\n` +
`Cache: ${diagnostics.cacheSize} entries`;
if (this.settings.showDiagnosticsInNotices) {
new Notice(message, 7000);
}
} catch (error) {
console.error('❌ Error getting diagnostics:', error);
new Notice('Error retrieving diagnostics');
}
}
});
// Force rescan command
this.addCommand({
id: 'force-rescan',
name: 'Force rescan all files',
callback: async () => {
try {
if (this.settings.enableDebugLogging) {
console.log('🔄 Force rescanning all Excalidraw files...');
}
new Notice('Rescanning Excalidraw files...');
await this.forceRescan();
} catch (error) {
console.error('❌ Error during force rescan:', error);
new Notice('Error during rescan. Check console for details.');
}
}
});
// Comprehensive testing command (Day 7)
this.addCommand({
id: 'run-comprehensive-tests',
name: 'Run comprehensive tests',
callback: () => {
try {
if (this.settings.enableDebugLogging) {
console.log('🔬 Starting comprehensive plugin tests...');
}
new Notice('Running comprehensive tests...');
if (!this.editorExtension) {
throw new Error('Editor extension not initialized');
}
const allTestsPassed = this.editorExtension.runComprehensiveTests();
const message = allTestsPassed ?
'✅ All tests passed!' :
'❌ Some tests failed. Check console for details.';
new Notice(message, 4000);
} catch (error) {
console.error('❌ Error running comprehensive tests:', error);
new Notice('Error running tests. Check console for details.');
}
}
});
}
/**
* Setup file change event listeners (Day 5)
* Day 6: Enhanced with comprehensive error handling and validation
*/
private setupFileChangeListeners(): void {
try {
// Listen for file modifications with enhanced error handling
this.registerEvent(
this.app.vault.on('modify', async (file) => {
try {
if (file instanceof TFile && file.path && file.path.endsWith('.excalidraw.md')) {
await this.frameIndexer.handleFileModification(file);
}
} catch (error) {
console.error(`❌ Error handling file modification for ${file?.path || 'unknown'}:`, error);
}
})
);
// Listen for file deletions with validation
this.registerEvent(
this.app.vault.on('delete', (file) => {
try {
if (file instanceof TFile && file.path && file.path.endsWith('.excalidraw.md')) {
this.frameIndexer.handleFileDeletion(file);
}
} catch (error) {
console.error(`❌ Error handling file deletion for ${file?.path || 'unknown'}:`, error);
}
})
);
// Listen for file renames with comprehensive validation
this.registerEvent(
this.app.vault.on('rename', async (file, oldPath) => {
try {
if (file instanceof TFile &&
oldPath &&
typeof oldPath === 'string' &&
(file.path.endsWith('.excalidraw.md') || oldPath.endsWith('.excalidraw.md'))) {
await this.frameIndexer.handleFileRename(file, oldPath);
}
} catch (error) {
console.error(`❌ Error handling file rename from ${oldPath || 'unknown'} to ${file?.path || 'unknown'}:`, error);
}
})
);
// console.log('✅ File change listeners registered successfully');
} catch (error) {
console.error('❌ Error setting up file change listeners:', error);
throw error;
}
}
/**
* Plugin unload with enhanced cleanup
* Day 7: Ensures all resources are properly released
*/
onunload(): void {
try {
if (this.settings.enableDebugLogging) {
console.log('👋 Excalink plugin unloading...');
}
// Clean up frame indexer
if (this.frameIndexer) {
try {
this.frameIndexer.clearCache();
} catch (error) {
console.warn('⚠️ Error clearing frame indexer cache:', error);
}
}
// Clean up editor extension
if (this.editorExtension) {
try {
// The extension will be automatically cleaned up by Obsidian
if (this.settings.enableDebugLogging) {
console.log('✅ Editor extension cleanup handled by Obsidian');
}
} catch (error) {
console.warn('⚠️ Error during editor extension cleanup:', error);
}
}
this.isInitialized = false;
if (this.settings.enableDebugLogging) {
console.log('✅ Excalink plugin unloaded successfully');
}
} catch (error) {
console.error('❌ Error during plugin unload:', error);
}
}
/**
* Check if plugin is properly initialized
* Day 6: Public method for debugging and validation
*/
public isReady(): boolean {
return this.isInitialized &&
!!this.frameIndexer &&
this.frameIndexer.isReady() &&
!!this.editorExtension;
}
}