feat: add export and import functionality for snippets with UI controls

This commit is contained in:
2025-01-19 15:12:05 +02:00
parent f14f76e2f0
commit a8b2fa4be8
4 changed files with 72 additions and 0 deletions

View File

@@ -13,4 +13,31 @@ export class StorageManager {
saveSnippets(snippets) {
localStorage.setItem(this.SNIPPETS_KEY, JSON.stringify(snippets));
}
exportSnippets() {
const snippets = this.loadSnippets();
const blob = new Blob([JSON.stringify(snippets, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'astrolabe-snippets.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
async importSnippets(file) {
try {
const text = await file.text();
const snippets = JSON.parse(text);
if (!Array.isArray(snippets)) {
throw new Error('Invalid snippets format');
}
this.saveSnippets(snippets);
return snippets;
} catch (err) {
throw new Error('Failed to import snippets: ' + err.message);
}
}
}