diff --git a/src/UIManager.js b/src/UIManager.js index 6812501..7463db9 100644 --- a/src/UIManager.js +++ b/src/UIManager.js @@ -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) { diff --git a/src/main.js b/src/main.js index 8e2e4fd..4292f37 100644 --- a/src/main.js +++ b/src/main.js @@ -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); - } - } - }); });