mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2025-12-21 21:22:25 +00:00
feat: implement snippet import/export functionality in UIManager
This commit is contained in:
@@ -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) {
|
||||
|
||||
25
src/main.js
25
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user