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

Commit a0faf0f

Browse files
committed
feat: add uninstall and snippet existence check functionality
1 parent b5f7091 commit a0faf0f

File tree

1 file changed

+71
-29
lines changed

1 file changed

+71
-29
lines changed

main.ts

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -136,52 +136,94 @@ class CssSnippetStoreModal extends Modal {
136136
}
137137
}
138138

139+
async uninstall(name: string) {
140+
const vault = this.app.vault;
141+
const snippetFolderPath = '.obsidian/snippets';
142+
const fileName = name + '.css';
143+
const fullPath = `${snippetFolderPath}/${fileName}`;
144+
145+
try {
146+
// Check if the file exists
147+
if (await vault.adapter.exists(fullPath)) {
148+
await vault.adapter.remove(fullPath);
149+
new Notice(`Snippet "${fileName}" deleted.`);
150+
} else {
151+
new Notice(`Snippet "${fileName}" does not exist.`);
152+
}
153+
} catch (err) {
154+
console.error('Failed to delete snippet:', err);
155+
new Notice('Failed to delete snippet. See console for details.');
156+
}
157+
}
158+
159+
async checkSnippetExists(name: string): Promise<boolean> {
160+
const vault = this.app.vault;
161+
const snippetFolderPath = '.obsidian/snippets';
162+
const fileName = name + '.css';
163+
const fullPath = `${snippetFolderPath}/${fileName}`;
164+
return await vault.adapter.exists(fullPath);
165+
}
166+
139167
onOpen() {
140168
const { contentEl } = this;
141169
contentEl.createEl('h1', { text: 'CSS Snippet Store' });
142170
const grid = contentEl.createEl('div', { cls: 'snippet-store-grid'});
143171

144-
this.snippets.forEach(snippet => {
172+
this.snippets.forEach(snippet => {
145173
const card = grid.createDiv({ cls: 'community-item' });
146174

147175
card.createEl('div', { text: snippet.name, cls: 'community-item-name' });
148176
card.createEl('div', { text: `By ${snippet.author}`, cls: 'community-item-author' });
149177
card.createEl('div', { text: snippet.description, cls: 'community-desc' });
150178

151-
const buttonWrapper = card.createEl('div', { cls: 'snippet-store-button-wrapper'});
152-
153-
const button = buttonWrapper.createEl('button', { text: 'Install', cls: 'mod-cta' });
154-
155-
// Attach event listener
156-
button.addEventListener('click', async () => {
157-
const url = "https://raw.githubusercontent.com/" + snippet.repo + "/refs/heads/main/" + snippet.folder + "/snippet.css"
158-
try {
159-
if (navigator.onLine) {
160-
const response = await fetch(url);
161-
const code = await response.text();
162-
await this.install(snippet.id, code);
163-
} else {
164-
new Notice(`No Internet connection...`);
165-
return;
166-
}
167-
} catch (error) {
168-
console.error(error);
169-
new Notice(`Error: ${error.message}`);
179+
const buttonWrapper = card.createEl('div', { cls: 'snippet-store-button-wrapper' });
180+
181+
const button = buttonWrapper.createEl('button', { cls: 'mod-cta' });
182+
183+
// Check if snippet already exists before updating the button text
184+
this.checkSnippetExists(snippet.id).then((exists) => {
185+
if (exists) {
186+
button.textContent = 'Delete';
187+
button.className = 'mod-danger'; // Optionally change the class to indicate danger
188+
189+
// Delete snippet logic
190+
button.addEventListener('click', async () => {
191+
await this.uninstall(snippet.id);
192+
// Optionally, reload the modal to update the button text after deletion
193+
this.close();
194+
this.open();
195+
});
196+
} else {
197+
button.textContent = 'Install';
198+
button.className = 'mod-cta';
199+
200+
// Install snippet logic
201+
button.addEventListener('click', async () => {
202+
const url = "https://raw.githubusercontent.com/" + snippet.repo + "/refs/heads/main/" + snippet.folder + "/snippet.css"
203+
try {
204+
if (navigator.onLine) {
205+
const response = await fetch(url);
206+
const code = await response.text();
207+
await this.install(snippet.id, code);
208+
// Optionally, reload the modal to update the button text after installation
209+
this.close();
210+
this.open();
211+
} else {
212+
new Notice(`No Internet connection...`);
213+
return;
214+
}
215+
} catch (error) {
216+
console.error(error);
217+
new Notice(`Error: ${error.message}`);
218+
}
219+
});
170220
}
171221
});
172-
173-
if (snippet.source) {
174-
const sourceBtn = buttonWrapper.createEl('a', {
175-
href: snippet.source,
176-
cls: 'snippet-link'
177-
});
178-
sourceBtn.createEl('button', { text: 'Source', cls: '' });
179-
}
180222
});
181223
}
182224

183225
onClose() {
184226
const { contentEl } = this;
185227
contentEl.empty();
186228
}
187-
}
229+
}

0 commit comments

Comments
 (0)