mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2025-12-21 21:22:25 +00:00
refactor: remove draft management from SnippetManager and streamline snippet handling
This commit is contained in:
@@ -1,50 +1,32 @@
|
|||||||
|
import { defaultSnippets } from './config.js';
|
||||||
|
|
||||||
export class SnippetManager {
|
export class SnippetManager {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.currentSnippetId = null;
|
this.currentSnippetId = null;
|
||||||
this.hasUnsavedChanges = false;
|
this.hasUnsavedChanges = false;
|
||||||
this.isDraftVersion = false;
|
this.isDraftVersion = false;
|
||||||
this.drafts = new Map(); // Store draft versions
|
|
||||||
this.readOnlyMode = false;
|
this.readOnlyMode = false;
|
||||||
this.loadSnippets();
|
this.loadSnippets();
|
||||||
this.loadDrafts();
|
|
||||||
this.setupUI();
|
this.setupUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
hasDraftChanges(id) {
|
hasDraftChanges(id) {
|
||||||
if (!this.drafts.has(id)) return false;
|
|
||||||
const snippet = this.snippets.find(s => s.id === id);
|
const snippet = this.snippets.find(s => s.id === id);
|
||||||
const draft = this.drafts.get(id);
|
return snippet && snippet.draft !== undefined;
|
||||||
return JSON.stringify(snippet.content) !== JSON.stringify(draft);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadSnippets() {
|
loadSnippets() {
|
||||||
// Try to load from localStorage
|
|
||||||
const stored = localStorage.getItem('vegaSnippets');
|
const stored = localStorage.getItem('vegaSnippets');
|
||||||
this.snippets = stored ? JSON.parse(stored) : defaultSnippets;
|
this.snippets = stored ? JSON.parse(stored) : defaultSnippets;
|
||||||
|
|
||||||
// Initialize localStorage if empty
|
|
||||||
if (!stored) {
|
if (!stored) {
|
||||||
this.saveToStorage();
|
this.saveToStorage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loadDrafts() {
|
|
||||||
const stored = localStorage.getItem('vegaDrafts');
|
|
||||||
if (stored) {
|
|
||||||
const draftsObj = JSON.parse(stored);
|
|
||||||
this.drafts = new Map(Object.entries(draftsObj));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
saveToStorage() {
|
saveToStorage() {
|
||||||
localStorage.setItem('vegaSnippets', JSON.stringify(this.snippets));
|
localStorage.setItem('vegaSnippets', JSON.stringify(this.snippets));
|
||||||
}
|
}
|
||||||
|
|
||||||
saveDraftsToStorage() {
|
|
||||||
const draftsObj = Object.fromEntries(this.drafts);
|
|
||||||
localStorage.setItem('vegaDrafts', JSON.stringify(draftsObj));
|
|
||||||
}
|
|
||||||
|
|
||||||
renderSnippetList() {
|
renderSnippetList() {
|
||||||
const container = document.getElementById('snippet-list');
|
const container = document.getElementById('snippet-list');
|
||||||
container.innerHTML = '';
|
container.innerHTML = '';
|
||||||
@@ -66,11 +48,9 @@ export class SnippetManager {
|
|||||||
this.currentSnippetId = id;
|
this.currentSnippetId = id;
|
||||||
const hasChanges = this.hasDraftChanges(id);
|
const hasChanges = this.hasDraftChanges(id);
|
||||||
|
|
||||||
// Default to draft if available, unless explicitly specified
|
|
||||||
this.isDraftVersion = forceDraft !== null ? forceDraft : hasChanges;
|
this.isDraftVersion = forceDraft !== null ? forceDraft : hasChanges;
|
||||||
|
const content = this.isDraftVersion && snippet.draft ?
|
||||||
const content = this.isDraftVersion && this.drafts.has(id) ?
|
snippet.draft :
|
||||||
this.drafts.get(id) :
|
|
||||||
snippet.content;
|
snippet.content;
|
||||||
|
|
||||||
this.editor.setValue(JSON.stringify(content, null, 2));
|
this.editor.setValue(JSON.stringify(content, null, 2));
|
||||||
@@ -107,15 +87,17 @@ export class SnippetManager {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const content = JSON.parse(this.editor.getValue());
|
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 (snippetIndex !== -1) {
|
||||||
if (JSON.stringify(content) !== JSON.stringify(currentSnippet.content)) {
|
const currentSnippet = this.snippets[snippetIndex];
|
||||||
this.drafts.set(this.currentSnippetId, content);
|
if (JSON.stringify(content) !== JSON.stringify(currentSnippet.content)) {
|
||||||
this.isDraftVersion = true;
|
this.snippets[snippetIndex].draft = content;
|
||||||
this.saveDraftsToStorage();
|
this.isDraftVersion = true;
|
||||||
this.renderSnippetList();
|
this.saveToStorage();
|
||||||
this.updateVersionSwitch();
|
this.renderSnippetList();
|
||||||
|
this.updateVersionSwitch();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Invalid JSON in editor');
|
console.error('Invalid JSON in editor');
|
||||||
@@ -131,8 +113,7 @@ export class SnippetManager {
|
|||||||
|
|
||||||
if (snippetIndex !== -1) {
|
if (snippetIndex !== -1) {
|
||||||
this.snippets[snippetIndex].content = content;
|
this.snippets[snippetIndex].content = content;
|
||||||
this.drafts.delete(this.currentSnippetId); // Remove draft after saving
|
delete this.snippets[snippetIndex].draft; // Remove draft after saving
|
||||||
this.saveDraftsToStorage();
|
|
||||||
this.saveToStorage();
|
this.saveToStorage();
|
||||||
this.hasUnsavedChanges = false;
|
this.hasUnsavedChanges = false;
|
||||||
this.isDraftVersion = false;
|
this.isDraftVersion = false;
|
||||||
@@ -191,8 +172,8 @@ export class SnippetManager {
|
|||||||
if (confirm('Editing the saved version will overwrite your draft. Continue?')) {
|
if (confirm('Editing the saved version will overwrite your draft. Continue?')) {
|
||||||
this.readOnlyMode = false;
|
this.readOnlyMode = false;
|
||||||
this.isDraftVersion = true;
|
this.isDraftVersion = true;
|
||||||
this.drafts.delete(this.currentSnippetId);
|
delete this.snippets.find(s => s.id === this.currentSnippetId).draft;
|
||||||
this.saveDraftsToStorage();
|
this.saveToStorage();
|
||||||
this.updateVersionSwitch();
|
this.updateVersionSwitch();
|
||||||
this.renderSnippetList();
|
this.renderSnippetList();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user