From 9bb476c1358b12d2a75858240dbd97a88541c7db Mon Sep 17 00:00:00 2001 From: Oleh Omelchenko Date: Sun, 19 Jan 2025 03:34:52 +0200 Subject: [PATCH] refactor: remove draft management from SnippetManager and streamline snippet handling --- src/SnippetManager.js | 55 ++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/src/SnippetManager.js b/src/SnippetManager.js index a264519..25bda4f 100644 --- a/src/SnippetManager.js +++ b/src/SnippetManager.js @@ -1,50 +1,32 @@ +import { defaultSnippets } from './config.js'; + export class SnippetManager { constructor() { this.currentSnippetId = null; this.hasUnsavedChanges = false; this.isDraftVersion = false; - this.drafts = new Map(); // Store draft versions this.readOnlyMode = false; this.loadSnippets(); - this.loadDrafts(); this.setupUI(); } hasDraftChanges(id) { - if (!this.drafts.has(id)) return false; const snippet = this.snippets.find(s => s.id === id); - const draft = this.drafts.get(id); - return JSON.stringify(snippet.content) !== JSON.stringify(draft); + return snippet && snippet.draft !== undefined; } loadSnippets() { - // Try to load from localStorage const stored = localStorage.getItem('vegaSnippets'); this.snippets = stored ? JSON.parse(stored) : defaultSnippets; - - // Initialize localStorage if empty if (!stored) { this.saveToStorage(); } } - loadDrafts() { - const stored = localStorage.getItem('vegaDrafts'); - if (stored) { - const draftsObj = JSON.parse(stored); - this.drafts = new Map(Object.entries(draftsObj)); - } - } - saveToStorage() { localStorage.setItem('vegaSnippets', JSON.stringify(this.snippets)); } - saveDraftsToStorage() { - const draftsObj = Object.fromEntries(this.drafts); - localStorage.setItem('vegaDrafts', JSON.stringify(draftsObj)); - } - renderSnippetList() { const container = document.getElementById('snippet-list'); container.innerHTML = ''; @@ -66,11 +48,9 @@ export class SnippetManager { this.currentSnippetId = id; const hasChanges = this.hasDraftChanges(id); - // Default to draft if available, unless explicitly specified this.isDraftVersion = forceDraft !== null ? forceDraft : hasChanges; - - const content = this.isDraftVersion && this.drafts.has(id) ? - this.drafts.get(id) : + const content = this.isDraftVersion && snippet.draft ? + snippet.draft : snippet.content; this.editor.setValue(JSON.stringify(content, null, 2)); @@ -107,15 +87,17 @@ export class SnippetManager { try { const content = JSON.parse(this.editor.getValue()); - const currentSnippet = this.snippets.find(s => s.id === this.currentSnippetId); + const snippetIndex = this.snippets.findIndex(s => s.id === this.currentSnippetId); - // Only save draft if content is different from saved version - if (JSON.stringify(content) !== JSON.stringify(currentSnippet.content)) { - this.drafts.set(this.currentSnippetId, content); - this.isDraftVersion = true; - this.saveDraftsToStorage(); - this.renderSnippetList(); - this.updateVersionSwitch(); + 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.saveToStorage(); + this.renderSnippetList(); + this.updateVersionSwitch(); + } } } catch (e) { console.error('Invalid JSON in editor'); @@ -131,8 +113,7 @@ export class SnippetManager { if (snippetIndex !== -1) { this.snippets[snippetIndex].content = content; - this.drafts.delete(this.currentSnippetId); // Remove draft after saving - this.saveDraftsToStorage(); + delete this.snippets[snippetIndex].draft; // Remove draft after saving this.saveToStorage(); this.hasUnsavedChanges = false; this.isDraftVersion = false; @@ -191,8 +172,8 @@ export class SnippetManager { if (confirm('Editing the saved version will overwrite your draft. Continue?')) { this.readOnlyMode = false; this.isDraftVersion = true; - this.drafts.delete(this.currentSnippetId); - this.saveDraftsToStorage(); + delete this.snippets.find(s => s.id === this.currentSnippetId).draft; + this.saveToStorage(); this.updateVersionSwitch(); this.renderSnippetList(); } else {