diff --git a/src/SnippetManager.js b/src/SnippetManager.js index 3c03e24..6926e0a 100644 --- a/src/SnippetManager.js +++ b/src/SnippetManager.js @@ -77,40 +77,43 @@ export class SnippetManager { saveDraft() { if (!this.currentSnippetId) return; - try { - const content = JSON.parse(this.editorManager.getValue()); - const snippetIndex = this.snippets.findIndex(s => s.id === this.currentSnippetId); - - if (snippetIndex !== -1) { - const currentSnippet = this.snippets[snippetIndex]; - if (JSON.stringify(content) !== JSON.stringify(currentSnippet.content)) { - this.snippets[snippetIndex].draft = content; - this.isDraftVersion = true; - this.saveSnippetsAndUpdateUI(); - this.visualizationManager.updateVisualization(content); - } + const content = this.parseEditorContent(); + if (!content) return; + + const snippetIndex = this.snippets.findIndex(s => s.id === this.currentSnippetId); + if (snippetIndex !== -1) { + const currentSnippet = this.snippets[snippetIndex]; + if (JSON.stringify(content) !== JSON.stringify(currentSnippet.content)) { + this.snippets[snippetIndex].draft = content; + this.isDraftVersion = true; + this.saveSnippetsAndUpdateUI(); + this.visualizationManager.updateVisualization(content); } - } catch (e) { - console.error('Invalid JSON in editor'); } } saveCurrentSnippet() { if (!this.currentSnippetId) return; - try { - const content = JSON.parse(this.editorManager.getValue()); - const snippetIndex = this.snippets.findIndex(s => s.id === this.currentSnippetId); + const content = this.parseEditorContent(); + if (!content) return; - if (snippetIndex !== -1) { - this.snippets[snippetIndex].content = content; - delete this.snippets[snippetIndex].draft; - this.hasUnsavedChanges = false; - this.isDraftVersion = false; - this.saveSnippetsAndUpdateUI(); - } + const snippetIndex = this.snippets.findIndex(s => s.id === this.currentSnippetId); + if (snippetIndex !== -1) { + this.snippets[snippetIndex].content = content; + delete this.snippets[snippetIndex].draft; + this.hasUnsavedChanges = false; + this.isDraftVersion = false; + this.saveSnippetsAndUpdateUI(); + } + } + + parseEditorContent() { + try { + return JSON.parse(this.editorManager.getValue()); } catch (e) { - alert('Invalid JSON in editor'); + console.error('Invalid JSON in editor'); + return null; } } diff --git a/src/UIManager.js b/src/UIManager.js index 7463db9..1d7fee9 100644 --- a/src/UIManager.js +++ b/src/UIManager.js @@ -51,29 +51,29 @@ export class UIManager { const buttonsDiv = document.createElement('div'); buttonsDiv.className = 'snippet-buttons'; - const editButton = document.createElement('button'); - editButton.className = 'edit-snippet'; - editButton.innerHTML = '✏️'; - editButton.onclick = (e) => { + buttonsDiv.appendChild(this.createButton('✏️', 'edit-snippet', (e) => { e.stopPropagation(); this.snippetManager.renameSnippet(snippet.id); - }; - buttonsDiv.appendChild(editButton); + })); - const deleteButton = document.createElement('button'); - deleteButton.className = 'delete-snippet'; - deleteButton.innerHTML = '❌'; - deleteButton.onclick = (e) => { + buttonsDiv.appendChild(this.createButton('❌', 'delete-snippet', (e) => { e.stopPropagation(); this.snippetManager.deleteSnippet(snippet.id); - }; - buttonsDiv.appendChild(deleteButton); + })); div.appendChild(buttonsDiv); container.appendChild(div); }); } + createButton(innerHTML, className, onClick) { + const button = document.createElement('button'); + button.className = className; + button.innerHTML = innerHTML; + button.onclick = onClick; + return button; + } + updateSaveButton(hasUnsavedChanges) { const saveButton = document.getElementById('save-snippet'); saveButton.disabled = !hasUnsavedChanges;