mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2025-12-21 21:22:25 +00:00
refactor: streamline snippet saving logic and button creation in UIManager
This commit is contained in:
@@ -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);
|
||||
const content = this.parseEditorContent();
|
||||
if (!content) return;
|
||||
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user