Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit 3888dbb

Browse files
committed
feat: enhance SnippetReadmeModal to support loading indicators and dynamic content updates
1 parent 63a85b8 commit 3888dbb

File tree

1 file changed

+65
-31
lines changed

1 file changed

+65
-31
lines changed

main.ts

Lines changed: 65 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export default class CssSnippetStore extends Plugin {
1919
// Start the mutation observer when the plugin is loaded.
2020
this.injectBrowseButton();
2121

22-
2322
// fetching list of snippets
2423
const url = "https://raw.githubusercontent.com/xavwe/obsidian-css-snippet-store/main/snippets.json"
2524
try {
@@ -30,9 +29,9 @@ export default class CssSnippetStore extends Plugin {
3029
return;
3130
}
3231
/*
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+
}*/
3635
this.snippets = await response.json();
3736
} else {
3837
new Notice(`No Internet connection...`);
@@ -81,8 +80,6 @@ export default class CssSnippetStore extends Plugin {
8180
});
8281
}
8382

84-
85-
8683
onunload() {
8784
// Clean up the mutation observer on plugin unload.
8885
if (this.observer) {
@@ -244,23 +241,30 @@ class CssSnippetStoreModal extends Modal {
244241
// Prevent click events on buttons inside the card from triggering README modal
245242
if ((event.target as HTMLElement).tagName.toLowerCase() === 'button') return;
246243

244+
// Create and open modal first with loading indicator
245+
const readmeModal = new SnippetReadmeModal(this.app, snippet, "", this.plugin);
246+
readmeModal.open();
247+
247248
const readmeUrl = `https://raw.githubusercontent.com/${snippet.repo}/refs/heads/main/${snippet.folder}/README.md`;
248249
try {
249250
if (await isOnline()) {
250251
const response = await fetchWithTimeout(readmeUrl);
251252
if (!response.ok) {
252253
new Notice(`Could not fetch README: ${response.statusText}`);
254+
readmeModal.close();
253255
return;
254256
}
255257
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);
258260
} else {
259261
new Notice("No Internet connection...");
262+
readmeModal.close();
260263
}
261264
} catch (error) {
262265
console.error(error);
263266
new Notice(`Error fetching README: ${error.message}`);
267+
readmeModal.close();
264268
}
265269
});
266270

@@ -269,7 +273,6 @@ class CssSnippetStoreModal extends Modal {
269273
});
270274
}
271275

272-
273276
onOpen() {
274277
const { contentEl } = this;
275278
contentEl.addClass('snippet-store-modal');
@@ -313,7 +316,6 @@ class CssSnippetStoreModal extends Modal {
313316
});
314317
}
315318

316-
317319
onClose() {
318320
const { contentEl } = this;
319321
contentEl.empty();
@@ -327,7 +329,6 @@ function fetchWithTimeout(resource: RequestInfo, options: RequestInit = {}, time
327329
]);
328330
}
329331

330-
331332
export async function isOnline(timeout = 3000): Promise<boolean> {
332333
try {
333334
const controller = new AbortController();
@@ -351,40 +352,73 @@ class SnippetReadmeModal extends Modal {
351352
app: App,
352353
private snippet: Snippet,
353354
private readmeContent: string,
354-
private plugin: Plugin // Add reference to the plugin instance
355+
private plugin: Plugin
355356
) {
356357
super(app);
357358
}
358359

360+
updateReadmeContent(content: string) {
361+
this.readmeContent = content;
362+
this.renderContent();
363+
}
364+
359365
async onOpen() {
360366
const { contentEl } = this;
361367
contentEl.empty();
362368
contentEl.addClass("snippet-readme-modal");
363369
this.modalEl.style.width = "80vw";
364370
this.modalEl.style.height = "80vh";
365371

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+
}
368380

369-
// Markdown container
370-
const markdownContainer = contentEl.createDiv();
381+
await this.renderContent();
382+
}
371383

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;
381386

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+
}
388422
}
389423

390424
onClose() {

0 commit comments

Comments
 (0)