feat: implement snippet import/export functionality in UIManager

This commit is contained in:
2025-01-19 17:47:51 +02:00
parent 55b60749ee
commit c474f8cdc2
2 changed files with 19 additions and 25 deletions

View File

@@ -10,6 +10,25 @@ export class UIManager {
document.getElementById('version-switch').onclick = () => {
this.snippetManager.loadSnippet(this.snippetManager.currentSnippetId, !this.snippetManager.isDraftVersion);
};
document.getElementById('export-snippets').onclick = () => this.snippetManager.storageManager.exportSnippets();
document.getElementById('import-snippets').onclick = () => document.getElementById('import-file').click();
document.getElementById('import-file').onchange = (e) => this.handleImport(e);
}
async handleImport(e) {
if (e.target.files.length > 0) {
try {
const snippets = await this.snippetManager.storageManager.importSnippets(e.target.files[0]);
this.snippetManager.snippets = snippets;
this.renderSnippetList(snippets, this.snippetManager.currentSnippetId);
if (snippets.length > 0) {
this.snippetManager.loadSnippet(snippets[0].id);
}
e.target.value = ''; // Reset file input
} catch (err) {
alert(err.message);
}
}
}
renderSnippetList(snippets, currentSnippetId) {

View File

@@ -40,30 +40,5 @@ require(['vs/editor/editor.main'], async function () {
const resizer = new PanelResizer(snippetManager);
window.editor = editor;
snippetManager.setEditor(editor);
document.getElementById('export-snippets').addEventListener('click', () => {
snippetManager.storageManager.exportSnippets();
});
document.getElementById('import-snippets').addEventListener('click', () => {
document.getElementById('import-file').click();
});
document.getElementById('import-file').addEventListener('change', async (e) => {
if (e.target.files.length > 0) {
try {
const snippets = await snippetManager.storageManager.importSnippets(e.target.files[0]);
snippetManager.snippets = snippets;
snippetManager.uiManager.renderSnippetList(snippets, snippetManager.currentSnippetId);
if (snippets.length > 0) {
snippetManager.loadSnippet(snippets[0].id);
}
e.target.value = ''; // Reset file input
} catch (err) {
alert(err.message);
}
}
});
});