-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameIndexer.ts
More file actions
538 lines (457 loc) · 20 KB
/
FrameIndexer.ts
File metadata and controls
538 lines (457 loc) · 20 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
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
import { TFile, Vault } from "obsidian";
import { ExcalidrawDecompressor } from "ExcalidrawDecompressor";
export interface FrameInfo{
name: string;
id: string;
index: number; // Position in the elements array - used for ordering (newer frames have higher index)
}
export interface FileFrameMap {
[fileName: string]: FrameInfo[];
}
/**
* Cache entry to store file content hash and frames
*/
interface CacheEntry {
contentHash: string;
frames: FrameInfo[];
lastModified: number;
}
/**
* Enhanced FrameIndexer with robust error handling and performance optimization
* Day 6: Polish & Test - Handles malformed files gracefully
*/
export class FrameIndexer{
private vault: Vault;
private frameIndex: FileFrameMap = {};
// Day 5: Performance caching system
private frameCache: Map<string, CacheEntry> = new Map();
private isInitialized: boolean = false;
constructor(vault: Vault) {
this.vault = vault;
}
/**
* Scan all Excalidraw files with enhanced error handling
* Handles nested folders, broken files, and various edge cases
*/
async scanAllExcalidrawFiles(): Promise<void>{
try {
// console.log('🔍 Starting comprehensive Excalidraw file scan with caching...');
const markdownFiles = this.vault.getMarkdownFiles();
const excalidrawFiles = markdownFiles.filter(file => {
// Enhanced validation for robust file filtering
return file &&
file.path &&
file.path.endsWith('.excalidraw.md') &&
file.stat &&
file.basename;
});
// console.log(`Found ${excalidrawFiles.length} valid Excalidraw files`);
// Clear the frame index but keep cache for performance
this.frameIndex = {};
let cacheHits = 0;
let cacheMisses = 0;
let skippedFiles = 0;
let processedFiles = 0;
for (const file of excalidrawFiles) {
try {
const wasFromCache = await this.extractFramesFromFileWithCache(file);
if (wasFromCache) {
cacheHits++;
} else {
cacheMisses++;
}
processedFiles++;
} catch (error) {
console.error(`❌ Failed to process file ${file.path}:`, error);
skippedFiles++;
// Continue processing other files - don't fail entire scan
}
}
// console.log(`📊 Scan complete:`);
// console.log(` ✅ Processed: ${processedFiles} files`);
// console.log(` 💾 Cache hits: ${cacheHits}`);
// console.log(` 🔄 Cache misses: ${cacheMisses}`);
// console.log(` ⚠️ Skipped: ${skippedFiles} files`);
// console.log('📜 Final frame index:', this.frameIndex);
this.isInitialized = true;
} catch (error) {
console.error('❌ Critical error during file scan:', error);
this.isInitialized = false;
throw new Error(`Failed to scan Excalidraw files: ${error.message}`);
}
}
/**
* Extract frames from file with enhanced caching and error handling
* Returns true if loaded from cache, false if file was processed
*/
private async extractFramesFromFileWithCache(file: TFile): Promise<boolean> {
try {
// Enhanced file validation
if (!file || !file.path || !file.stat || !file.basename) {
console.warn(`⚠️ Invalid file object for ${file?.path || 'unknown'}`);
return false;
}
const filePath = file.path;
const fileStats = file.stat;
const currentModTime = fileStats.mtime;
// Check if we have a valid cache entry
const cacheEntry = this.frameCache.get(filePath);
if (cacheEntry && cacheEntry.lastModified === currentModTime) {
// console.log(`💾 Cache hit for ${file.basename}`);
// Use cached frames
if (cacheEntry.frames.length > 0) {
this.frameIndex[file.basename] = cacheEntry.frames;
// console.log(`📃 ${file.basename}: [${cacheEntry.frames.map(f => f.name).join(', ')}] (cached)`);
} else {
// console.log(`📄 ${file.basename}: No frames (cached)`);
}
return true; // From cache
}
// console.log(`🔄 Cache miss for ${file.basename}, processing file...`);
// Read and process the file with enhanced error handling
const content = await this.vault.read(file);
if (!content || content.trim().length === 0) {
console.warn(`⚠️ Empty file: ${file.basename}`);
// Cache empty result to avoid re-processing
this.frameCache.set(filePath, {
contentHash: '',
frames: [],
lastModified: currentModTime
});
return false;
}
const frames = this.parseFramesFromContent(content, file.basename);
const contentHash = this.generateContentHash(content);
// Update cache
this.frameCache.set(filePath, {
contentHash,
frames,
lastModified: currentModTime
});
// Update frame index
if (frames.length > 0) {
this.frameIndex[file.basename] = frames;
// console.log(`📃 ${file.basename}: [${frames.map(f => f.name).join(', ')}] (processed)`);
} else {
// console.log(`📄 ${file.basename}: No frames found`);
}
return false; // Not from cache
} catch (error) {
console.error(`❌ Error processing file ${file?.path || 'unknown'}:`, error);
// Try to maintain previous cache entry if available
const cacheEntry = this.frameCache.get(file?.path);
if (cacheEntry) {
// console.log(`🔄 Using previous cache entry for ${file?.basename}`);
if (cacheEntry.frames.length > 0) {
this.frameIndex[file.basename] = cacheEntry.frames;
}
return true;
}
return false;
}
}
/**
* Generate a simple hash for content change detection
*/
private generateContentHash(content: string): string {
let hash = 0;
for (let i = 0; i < content.length; i++) {
const char = content.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32-bit integer
}
return hash.toString();
}
/**
* Parse frames from Excalidraw file content with comprehensive error handling
* Supports both compressed and uncompressed formats
* Handles malformed files gracefully
*/
private parseFramesFromContent(content: string, filename?: string): FrameInfo[] {
try {
// Validate input
if (!content || typeof content !== 'string' || content.trim().length === 0) {
console.warn(`⚠️ Invalid or empty content provided for parsing${filename ? ` in ${filename}` : ''}`);
return [];
}
// Look for compressed JSON first
let jsonMatch = content.match(/```compressed-json\n([\s\S]*?)\n```/);
let isCompressed = true;
// Fallback to regular JSON
if (!jsonMatch) {
jsonMatch = content.match(/```json\n([\s\S]*?)\n```/);
isCompressed = false;
}
// Check for alternative JSON block formats (generic code blocks)
if (!jsonMatch) {
jsonMatch = content.match(/```\n([\s\S]*?)\n```/);
if (jsonMatch && jsonMatch[1].trim().startsWith('{')) {
isCompressed = false;
// console.log(`🔍 Found generic code block with JSON-like content${filename ? ` in ${filename}` : ''}`);
}
}
if (!jsonMatch) {
// console.log(`📄 No JSON block found${filename ? ` in ${filename}` : ''} - may be frame-less Excalidraw file`);
return [];
}
// console.log(`✅ ${isCompressed ? 'Compressed' : 'Regular'} JSON block found${filename ? ` in ${filename}` : ''}`);
let excalidrawData;
if (isCompressed) {
// console.log('🗜️ Compressed format detected - decompressing...');
try {
excalidrawData = ExcalidrawDecompressor.decompress(jsonMatch[1]);
// console.log('✅ Successfully decompressed data');
} catch (error) {
console.error(`❌ Decompression failed${filename ? ` for ${filename}` : ''}:`, error.message);
// Try treating as regular JSON as fallback
try {
// console.log('🔄 Trying fallback: parsing as regular JSON...');
excalidrawData = JSON.parse(jsonMatch[1]);
// console.log('✅ Fallback successful - was actually uncompressed');
} catch (fallbackError) {
console.error(`❌ Both compression and JSON parsing failed${filename ? ` for ${filename}` : ''}`);
return [];
}
}
} else {
// console.log('📄 Regular JSON format detected');
try {
excalidrawData = JSON.parse(jsonMatch[1]);
// console.log('✅ Successfully parsed JSON');
} catch (error) {
console.error(`❌ JSON parsing failed${filename ? ` for ${filename}` : ''}:`, error.message);
// Try decompression as fallback
try {
// console.log('🔄 Trying fallback: decompression...');
excalidrawData = ExcalidrawDecompressor.decompress(jsonMatch[1]);
// console.log('✅ Fallback successful - was actually compressed');
} catch (fallbackError) {
console.error(`❌ Both JSON parsing and decompression failed${filename ? ` for ${filename}` : ''}`);
return [];
}
}
}
// Validate the parsed data structure
if (!excalidrawData || typeof excalidrawData !== 'object') {
console.error(`❌ Invalid Excalidraw data structure${filename ? ` in ${filename}` : ''}`);
return [];
}
// Extract frames with enhanced error handling
return this.extractFramesFromExcalidrawData(excalidrawData, filename);
} catch (error) {
console.error(`❌ Critical error during frame parsing${filename ? ` for ${filename}` : ''}:`, error);
return [];
}
}
/**
* Extract frames from parsed Excalidraw data with robust error handling
* Supports comments above frame blocks for enhanced documentation
*/
private extractFramesFromExcalidrawData(excalidrawData: any, filename?: string): FrameInfo[] {
try {
// Check for elements array
if (!excalidrawData.elements || !Array.isArray(excalidrawData.elements)) {
// console.log(`📄 No elements array found${filename ? ` in ${filename}` : ''} - empty or frame-less drawing`);
return [];
}
const frames: FrameInfo[] = [];
const elements = excalidrawData.elements;
// console.log(`🔍 Processing ${elements.length} elements${filename ? ` in ${filename}` : ''}...`);
for (let i = 0; i < elements.length; i++) {
try {
const element = elements[i];
// Validate element structure
if (!element || typeof element !== 'object') {
continue;
}
// Check if it's a frame element
if (element.type === 'frame' && element.name && typeof element.name === 'string') {
const frameName = element.name.trim();
const frameId = element.id || `frame_${i}`;
// Skip empty frame names
if (frameName.length === 0) {
console.warn(`⚠️ Skipping frame with empty name at index ${i}`);
continue;
}
// Look for comments above this frame (future enhancement)
const frameComment = this.findFrameComment(elements, i);
frames.push({
name: frameName,
id: frameId,
index: i // Store the position in elements array for ordering
});
// console.log(`🖼️ Found frame: "${frameName}" (${frameId})${frameComment ? ` - ${frameComment}` : ''}`);
}
} catch (elementError) {
console.warn(`⚠️ Error processing element ${i}${filename ? ` in ${filename}` : ''}:`, elementError);
// Continue processing other elements
}
}
// console.log(`✅ Extracted ${frames.length} frames total${filename ? ` from ${filename}` : ''}`);
return frames;
} catch (error) {
console.error(`❌ Error extracting frames from Excalidraw data${filename ? ` for ${filename}` : ''}:`, error);
return [];
}
}
/**
* Find comment text above a frame (future enhancement for better documentation)
*/
private findFrameComment(elements: any[], frameIndex: number): string | null {
try {
// Look for text elements positioned above the frame
// This is a placeholder for future enhancement
return null;
} catch (error) {
return null;
}
}
getFrameIndex(): FileFrameMap{
return this.frameIndex;
}
getFramesForFile(fileName: string): FrameInfo[] {
return this.frameIndex[fileName] || [];
}
/**
* Handle file modification event with enhanced error handling
*/
async handleFileModification(file: TFile): Promise<void> {
if (!file?.path?.endsWith('.excalidraw.md')) {
return;
}
// console.log(`🔄 File modified: ${file.basename}, updating cache...`);
try {
// Force re-processing by removing from cache first
this.frameCache.delete(file.path);
// Re-extract frames
await this.extractFramesFromFileWithCache(file);
// console.log(`✅ Successfully updated cache for ${file.basename}`);
} catch (error) {
console.error(`❌ Error handling file modification for ${file.path}:`, error);
}
}
/**
* Handle file deletion event with enhanced cleanup
*/
handleFileDeletion(file: TFile): void {
if (!file?.path?.endsWith('.excalidraw.md')) {
return;
}
// console.log(`🗑️ File deleted: ${file.basename}, cleaning up cache...`);
try {
// Remove from cache
this.frameCache.delete(file.path);
// Remove from frame index
if (file.basename) {
delete this.frameIndex[file.basename];
}
// console.log(`✅ Successfully cleaned up cache for ${file.basename}`);
} catch (error) {
console.error(`❌ Error during file deletion cleanup for ${file.path}:`, error);
}
}
/**
* Handle file rename event with enhanced state management
*/
async handleFileRename(file: TFile, oldPath: string): Promise<void> {
if (!file?.path?.endsWith('.excalidraw.md') && !oldPath.endsWith('.excalidraw.md')) {
return;
}
// console.log(`📝 File renamed: ${oldPath} → ${file.path}`);
try {
// Get old basename
const oldBasename = oldPath.split('/').pop()?.replace('.excalidraw.md', '') || '';
// If old file was an Excalidraw file, clean it up
if (oldPath.endsWith('.excalidraw.md')) {
this.frameCache.delete(oldPath);
if (oldBasename) {
delete this.frameIndex[oldBasename];
}
// console.log(`🧹 Cleaned up old file: ${oldBasename}`);
}
// If new file is an Excalidraw file, process it
if (file.path.endsWith('.excalidraw.md')) {
await this.extractFramesFromFileWithCache(file);
// console.log(`✅ Processed new file: ${file.basename}`);
}
// console.log(`✅ Successfully handled rename for ${file.basename}`);
} catch (error) {
console.error(`❌ Error handling file rename from ${oldPath} to ${file.path}:`, error);
}
}
/**
* Get cache statistics for debugging and monitoring
*/
getCacheStats(): {
size: number;
files: string[];
memoryUsage: string;
frameCount: number;
} {
try {
const totalFrames = Object.values(this.frameIndex).reduce((sum, frames) => sum + frames.length, 0);
return {
size: this.frameCache.size,
files: Array.from(this.frameCache.keys()),
memoryUsage: `~${Math.round(this.frameCache.size * 0.1)}KB`,
frameCount: totalFrames
};
} catch (error) {
console.error('❌ Error getting cache stats:', error);
return {
size: 0,
files: [],
memoryUsage: 'Unknown',
frameCount: 0
};
}
}
/**
* Clear all caches with enhanced cleanup
*/
clearCache(): void {
try {
// console.log('🧹 Clearing frame cache...');
this.frameCache.clear();
// console.log('✅ Frame cache cleared successfully');
} catch (error) {
console.error('❌ Error clearing cache:', error);
}
}
/**
* Check if the indexer has been properly initialized
*/
isReady(): boolean {
return this.isInitialized;
}
/**
* Get comprehensive diagnostic information
*/
getDiagnostics(): {
isInitialized: boolean;
totalFiles: number;
totalFrames: number;
cacheSize: number;
fileList: string[];
} {
try {
const totalFrames = Object.values(this.frameIndex).reduce((sum, frames) => sum + frames.length, 0);
return {
isInitialized: this.isInitialized,
totalFiles: Object.keys(this.frameIndex).length,
totalFrames,
cacheSize: this.frameCache.size,
fileList: Object.keys(this.frameIndex)
};
} catch (error) {
console.error('❌ Error getting diagnostics:', error);
return {
isInitialized: false,
totalFiles: 0,
totalFrames: 0,
cacheSize: 0,
fileList: []
};
}
}
}