@@ -19,7 +19,6 @@ export default class CssSnippetStore extends Plugin {
19
19
// Start the mutation observer when the plugin is loaded.
20
20
this . injectBrowseButton ( ) ;
21
21
22
-
23
22
// fetching list of snippets
24
23
const url = "https://raw.githubusercontent.com/xavwe/obsidian-css-snippet-store/main/snippets.json"
25
24
try {
@@ -30,9 +29,9 @@ export default class CssSnippetStore extends Plugin {
30
29
return ;
31
30
}
32
31
/*
33
- if (!response.headers.get('content-type')?.includes('application/json')) {
34
- throw new Error("Unexpected content type");
35
- }*/
32
+ if (!response.headers.get('content-type')?.includes('application/json')) {
33
+ throw new Error("Unexpected content type");
34
+ }*/
36
35
this . snippets = await response . json ( ) ;
37
36
} else {
38
37
new Notice ( `No Internet connection...` ) ;
@@ -81,8 +80,6 @@ export default class CssSnippetStore extends Plugin {
81
80
} ) ;
82
81
}
83
82
84
-
85
-
86
83
onunload ( ) {
87
84
// Clean up the mutation observer on plugin unload.
88
85
if ( this . observer ) {
@@ -244,23 +241,30 @@ class CssSnippetStoreModal extends Modal {
244
241
// Prevent click events on buttons inside the card from triggering README modal
245
242
if ( ( event . target as HTMLElement ) . tagName . toLowerCase ( ) === 'button' ) return ;
246
243
244
+ // Create and open modal first with loading indicator
245
+ const readmeModal = new SnippetReadmeModal ( this . app , snippet , "" , this . plugin ) ;
246
+ readmeModal . open ( ) ;
247
+
247
248
const readmeUrl = `https://raw.githubusercontent.com/${ snippet . repo } /refs/heads/main/${ snippet . folder } /README.md` ;
248
249
try {
249
250
if ( await isOnline ( ) ) {
250
251
const response = await fetchWithTimeout ( readmeUrl ) ;
251
252
if ( ! response . ok ) {
252
253
new Notice ( `Could not fetch README: ${ response . statusText } ` ) ;
254
+ readmeModal . close ( ) ;
253
255
return ;
254
256
}
255
257
const readme = await response . text ( ) ;
256
- // Pass the plugin instance (this.plugin)
257
- new SnippetReadmeModal ( this . app , snippet , readme , this . plugin ) . open ( ) ;
258
+ // Update the modal with content
259
+ readmeModal . updateReadmeContent ( readme ) ;
258
260
} else {
259
261
new Notice ( "No Internet connection..." ) ;
262
+ readmeModal . close ( ) ;
260
263
}
261
264
} catch ( error ) {
262
265
console . error ( error ) ;
263
266
new Notice ( `Error fetching README: ${ error . message } ` ) ;
267
+ readmeModal . close ( ) ;
264
268
}
265
269
} ) ;
266
270
@@ -269,7 +273,6 @@ class CssSnippetStoreModal extends Modal {
269
273
} ) ;
270
274
}
271
275
272
-
273
276
onOpen ( ) {
274
277
const { contentEl } = this ;
275
278
contentEl . addClass ( 'snippet-store-modal' ) ;
@@ -313,7 +316,6 @@ class CssSnippetStoreModal extends Modal {
313
316
} ) ;
314
317
}
315
318
316
-
317
319
onClose ( ) {
318
320
const { contentEl } = this ;
319
321
contentEl . empty ( ) ;
@@ -327,7 +329,6 @@ function fetchWithTimeout(resource: RequestInfo, options: RequestInit = {}, time
327
329
] ) ;
328
330
}
329
331
330
-
331
332
export async function isOnline ( timeout = 3000 ) : Promise < boolean > {
332
333
try {
333
334
const controller = new AbortController ( ) ;
@@ -351,40 +352,73 @@ class SnippetReadmeModal extends Modal {
351
352
app : App ,
352
353
private snippet : Snippet ,
353
354
private readmeContent : string ,
354
- private plugin : Plugin // Add reference to the plugin instance
355
+ private plugin : Plugin
355
356
) {
356
357
super ( app ) ;
357
358
}
358
359
360
+ updateReadmeContent ( content : string ) {
361
+ this . readmeContent = content ;
362
+ this . renderContent ( ) ;
363
+ }
364
+
359
365
async onOpen ( ) {
360
366
const { contentEl } = this ;
361
367
contentEl . empty ( ) ;
362
368
contentEl . addClass ( "snippet-readme-modal" ) ;
363
369
this . modalEl . style . width = "80vw" ;
364
370
this . modalEl . style . height = "80vh" ;
365
371
366
- // Rewrite relative image paths to absolute GitHub raw URLs
367
- const adjustedContent = this . rewriteRelativeMediaPaths ( this . readmeContent ) ;
372
+ // Show loading indicator if no content yet
373
+ if ( ! this . readmeContent ) {
374
+ contentEl . createEl ( 'div' , {
375
+ text : 'Loading README...' ,
376
+ cls : 'snippet-readme-loading'
377
+ } ) ;
378
+ return ;
379
+ }
368
380
369
- // Markdown container
370
- const markdownContainer = contentEl . createDiv ( ) ;
381
+ await this . renderContent ( ) ;
382
+ }
371
383
372
- // Render Markdown using Obsidian's renderer
373
- // Use the plugin instance instead of "this"
374
- await MarkdownRenderer . render (
375
- this . app ,
376
- adjustedContent ,
377
- markdownContainer ,
378
- "" ,
379
- this . plugin // Pass the plugin instance which is a Component
380
- ) ;
384
+ async renderContent ( ) {
385
+ if ( ! this . readmeContent ) return ;
381
386
382
- markdownContainer . querySelectorAll ( "img" ) . forEach ( ( img ) => {
383
- img . style . maxWidth = "100%" ;
384
- img . style . height = "auto" ;
385
- img . style . display = "block" ;
386
- img . style . margin = "1rem auto" ; // Optional: center images
387
- } ) ;
387
+ const { contentEl } = this ;
388
+ contentEl . empty ( ) ;
389
+
390
+ try {
391
+ // Rewrite relative image paths to absolute GitHub raw URLs
392
+ const adjustedContent = this . rewriteRelativeMediaPaths ( this . readmeContent ) ;
393
+
394
+ // Markdown container
395
+ const markdownContainer = contentEl . createDiv ( ) ;
396
+
397
+ // Render Markdown using Obsidian's renderer
398
+ await MarkdownRenderer . render (
399
+ this . app ,
400
+ adjustedContent ,
401
+ markdownContainer ,
402
+ "" ,
403
+ this . plugin
404
+ ) ;
405
+
406
+ // Optimize image loading
407
+ markdownContainer . querySelectorAll ( "img" ) . forEach ( ( img ) => {
408
+ img . setAttribute ( "loading" , "lazy" ) ;
409
+ img . style . maxWidth = "100%" ;
410
+ img . style . height = "auto" ;
411
+ img . style . display = "block" ;
412
+ img . style . margin = "1rem auto" ;
413
+ } ) ;
414
+ } catch ( error ) {
415
+ console . error ( "Error rendering README:" , error ) ;
416
+ contentEl . empty ( ) ;
417
+ contentEl . createEl ( 'div' , {
418
+ text : `Error rendering README: ${ error . message } ` ,
419
+ cls : 'snippet-readme-error'
420
+ } ) ;
421
+ }
388
422
}
389
423
390
424
onClose ( ) {
0 commit comments