refactor: consolidate UI updates and snippet saving into dedicated methods

This commit is contained in:
2025-01-19 15:19:30 +02:00
parent a8b2fa4be8
commit 711fe89139

View File

@@ -43,12 +43,9 @@ export class SnippetManager {
snippet.content; snippet.content;
this.editorManager.setValue(content); this.editorManager.setValue(content);
// Set hasUnsavedChanges to true if we're viewing a draft version
this.hasUnsavedChanges = this.isDraftVersion; this.hasUnsavedChanges = this.isDraftVersion;
this.updateReadOnlyState(); this.updateReadOnlyState();
this.uiManager.updateSaveButton(this.hasUnsavedChanges); this.updateUI();
this.uiManager.updateVersionSwitch(this.currentSnippetId, this.isDraftVersion, hasChanges);
this.uiManager.renderSnippetList(this.snippets, this.currentSnippetId);
this.visualizationManager.updateVisualization(content); this.visualizationManager.updateVisualization(content);
} }
} }
@@ -73,7 +70,7 @@ export class SnippetManager {
}; };
this.snippets.push(newSnippet); this.snippets.push(newSnippet);
this.storageManager.saveSnippets(this.snippets); this.saveSnippetsAndUpdateUI();
this.loadSnippet(id); this.loadSnippet(id);
} }
@@ -89,9 +86,7 @@ export class SnippetManager {
if (JSON.stringify(content) !== JSON.stringify(currentSnippet.content)) { if (JSON.stringify(content) !== JSON.stringify(currentSnippet.content)) {
this.snippets[snippetIndex].draft = content; this.snippets[snippetIndex].draft = content;
this.isDraftVersion = true; this.isDraftVersion = true;
this.storageManager.saveSnippets(this.snippets); this.saveSnippetsAndUpdateUI();
this.uiManager.renderSnippetList(this.snippets, this.currentSnippetId);
this.uiManager.updateVersionSwitch(this.currentSnippetId, this.isDraftVersion, this.hasDraftChanges(this.currentSnippetId));
this.visualizationManager.updateVisualization(content); this.visualizationManager.updateVisualization(content);
} }
} }
@@ -109,13 +104,10 @@ export class SnippetManager {
if (snippetIndex !== -1) { if (snippetIndex !== -1) {
this.snippets[snippetIndex].content = content; this.snippets[snippetIndex].content = content;
delete this.snippets[snippetIndex].draft; // Remove draft after saving delete this.snippets[snippetIndex].draft;
this.storageManager.saveSnippets(this.snippets);
this.hasUnsavedChanges = false; this.hasUnsavedChanges = false;
this.isDraftVersion = false; this.isDraftVersion = false;
this.uiManager.updateSaveButton(this.hasUnsavedChanges); this.saveSnippetsAndUpdateUI();
this.uiManager.updateVersionSwitch(this.currentSnippetId, this.isDraftVersion, this.hasDraftChanges(this.currentSnippetId));
this.uiManager.renderSnippetList(this.snippets, this.currentSnippetId);
} }
} catch (e) { } catch (e) {
alert('Invalid JSON in editor'); alert('Invalid JSON in editor');
@@ -126,22 +118,7 @@ export class SnippetManager {
this.readOnlyMode = false; this.readOnlyMode = false;
this.isDraftVersion = true; this.isDraftVersion = true;
delete this.snippets.find(s => s.id === this.currentSnippetId).draft; delete this.snippets.find(s => s.id === this.currentSnippetId).draft;
this.storageManager.saveSnippets(this.snippets); this.saveSnippetsAndUpdateUI();
this.uiManager.updateVersionSwitch(this.currentSnippetId, this.isDraftVersion, this.hasDraftChanges(this.currentSnippetId));
this.uiManager.renderSnippetList(this.snippets, this.currentSnippetId);
}
// Remove these methods as they're now in UIManager
updateSaveButton() {
this.uiManager.updateSaveButton(this.hasUnsavedChanges);
}
updateVersionSwitch() {
this.uiManager.updateVersionSwitch(
this.currentSnippetId,
this.isDraftVersion,
this.hasDraftChanges(this.currentSnippetId)
);
} }
updateReadOnlyState() { updateReadOnlyState() {
@@ -153,7 +130,7 @@ export class SnippetManager {
deleteSnippet(id) { deleteSnippet(id) {
if (confirm('Are you sure you want to delete this snippet?')) { if (confirm('Are you sure you want to delete this snippet?')) {
this.snippets = this.snippets.filter(s => s.id !== id); this.snippets = this.snippets.filter(s => s.id !== id);
this.storageManager.saveSnippets(this.snippets); this.saveSnippetsAndUpdateUI();
if (this.currentSnippetId === id) { if (this.currentSnippetId === id) {
this.currentSnippetId = null; this.currentSnippetId = null;
@@ -163,8 +140,6 @@ export class SnippetManager {
this.editorManager.setValue(''); this.editorManager.setValue('');
} }
} }
this.uiManager.renderSnippetList(this.snippets, this.currentSnippetId);
} }
} }
@@ -175,8 +150,18 @@ export class SnippetManager {
const newName = prompt('Enter new name:', snippet.name); const newName = prompt('Enter new name:', snippet.name);
if (newName && newName.trim() !== '') { if (newName && newName.trim() !== '') {
snippet.name = newName.trim(); snippet.name = newName.trim();
this.storageManager.saveSnippets(this.snippets); this.saveSnippetsAndUpdateUI();
this.uiManager.renderSnippetList(this.snippets, this.currentSnippetId);
} }
} }
saveSnippetsAndUpdateUI() {
this.storageManager.saveSnippets(this.snippets);
this.updateUI();
}
updateUI() {
this.uiManager.updateSaveButton(this.hasUnsavedChanges);
this.uiManager.updateVersionSwitch(this.currentSnippetId, this.isDraftVersion, this.hasDraftChanges(this.currentSnippetId));
this.uiManager.renderSnippetList(this.snippets, this.currentSnippetId);
}
} }