mirror of
https://github.com/olehomelchenko/astrolabe-nvc.git
synced 2025-12-21 21:22:23 +00:00
refactior: snippet selection handling and improve auto-save logic
This commit is contained in:
@@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
// Storage limits (5MB in bytes)
|
// Storage limits (5MB in bytes)
|
||||||
const STORAGE_LIMIT_BYTES = 5 * 1024 * 1024;
|
const STORAGE_LIMIT_BYTES = 5 * 1024 * 1024;
|
||||||
const WARNING_THRESHOLD = 0.9; // 90% = 4.5MB
|
|
||||||
|
|
||||||
// Generate unique ID using Date.now() + random numbers
|
// Generate unique ID using Date.now() + random numbers
|
||||||
function generateSnippetId() {
|
function generateSnippetId() {
|
||||||
@@ -331,12 +330,7 @@ function toggleSort(sortType) {
|
|||||||
renderSnippetList();
|
renderSnippetList();
|
||||||
|
|
||||||
// Restore selection if there was one
|
// Restore selection if there was one
|
||||||
if (window.currentSnippetId) {
|
restoreSnippetSelection();
|
||||||
const selectedItem = document.querySelector(`[data-snippet-id="${window.currentSnippetId}"]`);
|
|
||||||
if (selectedItem) {
|
|
||||||
selectedItem.classList.add('selected');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize search controls
|
// Initialize search controls
|
||||||
@@ -404,6 +398,23 @@ function updateClearButton() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper: Get currently selected snippet
|
||||||
|
function getCurrentSnippet() {
|
||||||
|
return window.currentSnippetId ? SnippetStorage.getSnippet(window.currentSnippetId) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper: Restore visual selection state for current snippet
|
||||||
|
function restoreSnippetSelection() {
|
||||||
|
if (window.currentSnippetId) {
|
||||||
|
const item = document.querySelector(`[data-snippet-id="${window.currentSnippetId}"]`);
|
||||||
|
if (item) {
|
||||||
|
item.classList.add('selected');
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Clear current selection and hide meta panel
|
// Clear current selection and hide meta panel
|
||||||
function clearSelection() {
|
function clearSelection() {
|
||||||
window.currentSnippetId = null;
|
window.currentSnippetId = null;
|
||||||
@@ -504,7 +515,7 @@ function autoSaveDraft() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const currentSpec = JSON.parse(editor.getValue());
|
const currentSpec = JSON.parse(editor.getValue());
|
||||||
const snippet = SnippetStorage.getSnippet(window.currentSnippetId);
|
const snippet = getCurrentSnippet();
|
||||||
|
|
||||||
if (snippet) {
|
if (snippet) {
|
||||||
snippet.draftSpec = currentSpec;
|
snippet.draftSpec = currentSpec;
|
||||||
@@ -513,8 +524,7 @@ function autoSaveDraft() {
|
|||||||
// Refresh snippet list to update status light
|
// Refresh snippet list to update status light
|
||||||
renderSnippetList();
|
renderSnippetList();
|
||||||
// Restore selection
|
// Restore selection
|
||||||
const selectedItem = document.querySelector(`[data-snippet-id="${window.currentSnippetId}"]`);
|
restoreSnippetSelection();
|
||||||
if (selectedItem) selectedItem.classList.add('selected');
|
|
||||||
|
|
||||||
// Update button states
|
// Update button states
|
||||||
updateViewModeUI(snippet);
|
updateViewModeUI(snippet);
|
||||||
@@ -530,8 +540,8 @@ function debouncedAutoSave() {
|
|||||||
if (window.isUpdatingEditor) return;
|
if (window.isUpdatingEditor) return;
|
||||||
|
|
||||||
// If viewing published and no draft exists, create draft automatically
|
// If viewing published and no draft exists, create draft automatically
|
||||||
if (currentViewMode === 'published' && window.currentSnippetId) {
|
if (currentViewMode === 'published') {
|
||||||
const snippet = SnippetStorage.getSnippet(window.currentSnippetId);
|
const snippet = getCurrentSnippet();
|
||||||
if (snippet) {
|
if (snippet) {
|
||||||
const hasDraft = JSON.stringify(snippet.spec) !== JSON.stringify(snippet.draftSpec);
|
const hasDraft = JSON.stringify(snippet.spec) !== JSON.stringify(snippet.draftSpec);
|
||||||
if (!hasDraft) {
|
if (!hasDraft) {
|
||||||
@@ -588,13 +598,11 @@ function initializeAutoSave() {
|
|||||||
|
|
||||||
// Save meta fields (name and comment) for the selected snippet
|
// Save meta fields (name and comment) for the selected snippet
|
||||||
function autoSaveMeta() {
|
function autoSaveMeta() {
|
||||||
if (!window.currentSnippetId) return;
|
|
||||||
|
|
||||||
const nameField = document.getElementById('snippet-name');
|
const nameField = document.getElementById('snippet-name');
|
||||||
const commentField = document.getElementById('snippet-comment');
|
const commentField = document.getElementById('snippet-comment');
|
||||||
if (!nameField || !commentField) return;
|
if (!nameField || !commentField) return;
|
||||||
|
|
||||||
const snippet = SnippetStorage.getSnippet(window.currentSnippetId);
|
const snippet = getCurrentSnippet();
|
||||||
if (snippet) {
|
if (snippet) {
|
||||||
snippet.name = nameField.value.trim() || generateSnippetName();
|
snippet.name = nameField.value.trim() || generateSnippetName();
|
||||||
snippet.comment = commentField.value;
|
snippet.comment = commentField.value;
|
||||||
@@ -604,10 +612,7 @@ function autoSaveMeta() {
|
|||||||
renderSnippetList();
|
renderSnippetList();
|
||||||
|
|
||||||
// Restore selection after re-render
|
// Restore selection after re-render
|
||||||
const selectedItem = document.querySelector(`[data-snippet-id="${window.currentSnippetId}"]`);
|
restoreSnippetSelection();
|
||||||
if (selectedItem) {
|
|
||||||
selectedItem.classList.add('selected');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -681,25 +686,6 @@ function deleteSnippet(snippetId) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rename snippet
|
|
||||||
function renameSnippet(snippetId, newName) {
|
|
||||||
const snippet = SnippetStorage.getSnippet(snippetId);
|
|
||||||
if (!snippet) return false;
|
|
||||||
|
|
||||||
snippet.name = newName.trim() || generateSnippetName();
|
|
||||||
SnippetStorage.saveSnippet(snippet);
|
|
||||||
|
|
||||||
// Refresh the list to show new name
|
|
||||||
renderSnippetList();
|
|
||||||
|
|
||||||
// Restore selection if this was the selected snippet
|
|
||||||
if (window.currentSnippetId === snippetId) {
|
|
||||||
document.querySelector(`[data-snippet-id="${snippetId}"]`).classList.add('selected');
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load snippet into editor based on view mode
|
// Load snippet into editor based on view mode
|
||||||
function loadSnippetIntoEditor(snippet) {
|
function loadSnippetIntoEditor(snippet) {
|
||||||
if (!editor) return;
|
if (!editor) return;
|
||||||
@@ -754,10 +740,8 @@ function updateViewModeUI(snippet) {
|
|||||||
|
|
||||||
// Switch view mode
|
// Switch view mode
|
||||||
function switchViewMode(mode) {
|
function switchViewMode(mode) {
|
||||||
if (!window.currentSnippetId) return;
|
|
||||||
|
|
||||||
currentViewMode = mode;
|
currentViewMode = mode;
|
||||||
const snippet = SnippetStorage.getSnippet(window.currentSnippetId);
|
const snippet = getCurrentSnippet();
|
||||||
if (snippet) {
|
if (snippet) {
|
||||||
loadSnippetIntoEditor(snippet);
|
loadSnippetIntoEditor(snippet);
|
||||||
updateViewModeUI(snippet);
|
updateViewModeUI(snippet);
|
||||||
@@ -766,9 +750,7 @@ function switchViewMode(mode) {
|
|||||||
|
|
||||||
// Publish draft to spec
|
// Publish draft to spec
|
||||||
function publishDraft() {
|
function publishDraft() {
|
||||||
if (!window.currentSnippetId) return;
|
const snippet = getCurrentSnippet();
|
||||||
|
|
||||||
const snippet = SnippetStorage.getSnippet(window.currentSnippetId);
|
|
||||||
if (!snippet) return;
|
if (!snippet) return;
|
||||||
|
|
||||||
// Copy draftSpec to spec
|
// Copy draftSpec to spec
|
||||||
@@ -777,17 +759,14 @@ function publishDraft() {
|
|||||||
|
|
||||||
// Refresh UI
|
// Refresh UI
|
||||||
renderSnippetList();
|
renderSnippetList();
|
||||||
const selectedItem = document.querySelector(`[data-snippet-id="${window.currentSnippetId}"]`);
|
restoreSnippetSelection();
|
||||||
if (selectedItem) selectedItem.classList.add('selected');
|
|
||||||
|
|
||||||
updateViewModeUI(snippet);
|
updateViewModeUI(snippet);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Revert draft to published spec
|
// Revert draft to published spec
|
||||||
function revertDraft() {
|
function revertDraft() {
|
||||||
if (!window.currentSnippetId) return;
|
const snippet = getCurrentSnippet();
|
||||||
|
|
||||||
const snippet = SnippetStorage.getSnippet(window.currentSnippetId);
|
|
||||||
if (!snippet) return;
|
if (!snippet) return;
|
||||||
|
|
||||||
if (confirm('Revert all draft changes to last published version? This cannot be undone.')) {
|
if (confirm('Revert all draft changes to last published version? This cannot be undone.')) {
|
||||||
@@ -802,8 +781,7 @@ function revertDraft() {
|
|||||||
|
|
||||||
// Refresh UI
|
// Refresh UI
|
||||||
renderSnippetList();
|
renderSnippetList();
|
||||||
const selectedItem = document.querySelector(`[data-snippet-id="${window.currentSnippetId}"]`);
|
restoreSnippetSelection();
|
||||||
if (selectedItem) selectedItem.classList.add('selected');
|
|
||||||
|
|
||||||
updateViewModeUI(snippet);
|
updateViewModeUI(snippet);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user