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

Commit 323f58b

Browse files
committed
feat: add search functionality to snippet store modal
1 parent bf6b597 commit 323f58b

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

main.ts

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -172,58 +172,84 @@ class CssSnippetStoreModal extends Modal {
172172
this.modalEl.style.maxWidth = '1098px';
173173

174174
contentEl.createEl('h1', { text: 'CSS Snippet Store' });
175-
const grid = contentEl.createEl('div', { cls: 'community-items-container' });
176-
177-
this.snippets.forEach(snippet => {
178-
const card = grid.createDiv({ cls: 'community-item' });
179-
180-
card.createEl('div', { text: snippet.name, cls: 'community-item-name' });
181-
card.createEl('div', { text: `By ${snippet.author}`, cls: 'community-item-author' });
182-
card.createEl('div', { text: snippet.description, cls: 'community-desc' });
183175

184-
const buttonWrapper = card.createEl('div', { cls: 'snippet-store-button-wrapper' });
185-
186-
const button = buttonWrapper.createEl('button', { cls: 'mod-cta' });
176+
// --- Search bar ---
177+
const searchInput = contentEl.createEl('input', {
178+
type: 'text',
179+
placeholder: 'Search snippets...',
180+
});
181+
searchInput.classList.add('snippet-search-input');
182+
searchInput.style.marginBottom = '1rem';
183+
searchInput.style.width = '100%';
184+
searchInput.style.padding = '0.5rem';
187185

188-
// Check if snippet already exists before updating the button text
189-
this.checkSnippetExists(snippet.id).then((exists) => {
190-
if (exists) {
191-
button.textContent = 'Delete';
192-
button.className = 'mod-danger'; // Optionally change the class to indicate danger
186+
const grid = contentEl.createEl('div', { cls: 'community-items-container' });
193187

194-
// Delete snippet logic
195-
button.addEventListener('click', async () => {
196-
await this.uninstall(snippet.id);
197-
// Optionally, reload the modal to update the button text after deletion
198-
this.close();
199-
this.open();
200-
});
201-
} else {
202-
button.textContent = 'Install';
203-
button.className = 'mod-cta';
204-
205-
// Install snippet logic
206-
button.addEventListener('click', async () => {
207-
const url = "https://raw.githubusercontent.com/" + snippet.repo + "/refs/heads/main/" + snippet.folder + "/snippet.css"
208-
try {
209-
if (navigator.onLine) {
210-
const response = await fetch(url);
211-
const code = await response.text();
212-
await this.install(snippet.id, code);
213-
// Optionally, reload the modal to update the button text after installation
188+
// --- Render Function ---
189+
const renderSnippets = (filter: string = "") => {
190+
grid.empty();
191+
const lowerFilter = filter.toLowerCase();
192+
193+
this.snippets
194+
.filter(snippet =>
195+
!filter ||
196+
snippet.name.toLowerCase().includes(lowerFilter) ||
197+
snippet.author.toLowerCase().includes(lowerFilter) ||
198+
snippet.description.toLowerCase().includes(lowerFilter)
199+
)
200+
.forEach(snippet => {
201+
const card = grid.createDiv({ cls: 'community-item' });
202+
203+
card.createEl('div', { text: snippet.name, cls: 'community-item-name' });
204+
card.createEl('div', { text: `By ${snippet.author}`, cls: 'community-item-author' });
205+
card.createEl('div', { text: snippet.description, cls: 'community-desc' });
206+
207+
const buttonWrapper = card.createEl('div', { cls: 'snippet-store-button-wrapper' });
208+
const button = buttonWrapper.createEl('button', { cls: 'mod-cta' });
209+
210+
this.checkSnippetExists(snippet.id).then((exists) => {
211+
if (exists) {
212+
button.textContent = 'Delete';
213+
button.className = 'mod-danger';
214+
215+
button.addEventListener('click', async () => {
216+
await this.uninstall(snippet.id);
214217
this.close();
215218
this.open();
216-
} else {
217-
new Notice(`No Internet connection...`);
218-
return;
219-
}
220-
} catch (error) {
221-
console.error(error);
222-
new Notice(`Error: ${error.message}`);
219+
});
220+
} else {
221+
button.textContent = 'Install';
222+
button.className = 'mod-cta';
223+
224+
button.addEventListener('click', async () => {
225+
const url = `https://raw.githubusercontent.com/${snippet.repo}/refs/heads/main/${snippet.folder}/snippet.css`;
226+
try {
227+
if (navigator.onLine) {
228+
const response = await fetch(url);
229+
const code = await response.text();
230+
await this.install(snippet.id, code);
231+
this.close();
232+
this.open();
233+
} else {
234+
new Notice(`No Internet connection...`);
235+
}
236+
} catch (error) {
237+
console.error(error);
238+
new Notice(`Error: ${error.message}`);
239+
}
240+
});
223241
}
224242
});
225-
}
226-
});
243+
});
244+
};
245+
246+
// Initial rendering
247+
renderSnippets();
248+
249+
// Attach event listener to search input
250+
searchInput.addEventListener('input', () => {
251+
const value = searchInput.value.trim();
252+
renderSnippets(value);
227253
});
228254
}
229255

0 commit comments

Comments
 (0)