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() {
|
saveDraft() {
|
||||||
if (!this.currentSnippetId) return;
|
if (!this.currentSnippetId) return;
|
||||||
|
|
||||||
try {
|
const content = this.parseEditorContent();
|
||||||
const content = JSON.parse(this.editorManager.getValue());
|
if (!content) return;
|
||||||
const snippetIndex = this.snippets.findIndex(s => s.id === this.currentSnippetId);
|
|
||||||
|
|
||||||
if (snippetIndex !== -1) {
|
const snippetIndex = this.snippets.findIndex(s => s.id === this.currentSnippetId);
|
||||||
const currentSnippet = this.snippets[snippetIndex];
|
if (snippetIndex !== -1) {
|
||||||
if (JSON.stringify(content) !== JSON.stringify(currentSnippet.content)) {
|
const currentSnippet = this.snippets[snippetIndex];
|
||||||
this.snippets[snippetIndex].draft = content;
|
if (JSON.stringify(content) !== JSON.stringify(currentSnippet.content)) {
|
||||||
this.isDraftVersion = true;
|
this.snippets[snippetIndex].draft = content;
|
||||||
this.saveSnippetsAndUpdateUI();
|
this.isDraftVersion = true;
|
||||||
this.visualizationManager.updateVisualization(content);
|
this.saveSnippetsAndUpdateUI();
|
||||||
}
|
this.visualizationManager.updateVisualization(content);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
console.error('Invalid JSON in editor');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
saveCurrentSnippet() {
|
saveCurrentSnippet() {
|
||||||
if (!this.currentSnippetId) return;
|
if (!this.currentSnippetId) return;
|
||||||
|
|
||||||
try {
|
const content = this.parseEditorContent();
|
||||||
const content = JSON.parse(this.editorManager.getValue());
|
if (!content) return;
|
||||||
const snippetIndex = this.snippets.findIndex(s => s.id === this.currentSnippetId);
|
|
||||||
|
|
||||||
if (snippetIndex !== -1) {
|
const snippetIndex = this.snippets.findIndex(s => s.id === this.currentSnippetId);
|
||||||
this.snippets[snippetIndex].content = content;
|
if (snippetIndex !== -1) {
|
||||||
delete this.snippets[snippetIndex].draft;
|
this.snippets[snippetIndex].content = content;
|
||||||
this.hasUnsavedChanges = false;
|
delete this.snippets[snippetIndex].draft;
|
||||||
this.isDraftVersion = false;
|
this.hasUnsavedChanges = false;
|
||||||
this.saveSnippetsAndUpdateUI();
|
this.isDraftVersion = false;
|
||||||
}
|
this.saveSnippetsAndUpdateUI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parseEditorContent() {
|
||||||
|
try {
|
||||||
|
return JSON.parse(this.editorManager.getValue());
|
||||||
} catch (e) {
|
} 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');
|
const buttonsDiv = document.createElement('div');
|
||||||
buttonsDiv.className = 'snippet-buttons';
|
buttonsDiv.className = 'snippet-buttons';
|
||||||
|
|
||||||
const editButton = document.createElement('button');
|
buttonsDiv.appendChild(this.createButton('✏️', 'edit-snippet', (e) => {
|
||||||
editButton.className = 'edit-snippet';
|
|
||||||
editButton.innerHTML = '✏️';
|
|
||||||
editButton.onclick = (e) => {
|
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
this.snippetManager.renameSnippet(snippet.id);
|
this.snippetManager.renameSnippet(snippet.id);
|
||||||
};
|
}));
|
||||||
buttonsDiv.appendChild(editButton);
|
|
||||||
|
|
||||||
const deleteButton = document.createElement('button');
|
buttonsDiv.appendChild(this.createButton('❌', 'delete-snippet', (e) => {
|
||||||
deleteButton.className = 'delete-snippet';
|
|
||||||
deleteButton.innerHTML = '❌';
|
|
||||||
deleteButton.onclick = (e) => {
|
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
this.snippetManager.deleteSnippet(snippet.id);
|
this.snippetManager.deleteSnippet(snippet.id);
|
||||||
};
|
}));
|
||||||
buttonsDiv.appendChild(deleteButton);
|
|
||||||
|
|
||||||
div.appendChild(buttonsDiv);
|
div.appendChild(buttonsDiv);
|
||||||
container.appendChild(div);
|
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) {
|
updateSaveButton(hasUnsavedChanges) {
|
||||||
const saveButton = document.getElementById('save-snippet');
|
const saveButton = document.getElementById('save-snippet');
|
||||||
saveButton.disabled = !hasUnsavedChanges;
|
saveButton.disabled = !hasUnsavedChanges;
|
||||||
|
|||||||
Reference in New Issue
Block a user