diff --git a/frontend/js/backups.js b/frontend/js/backups.js index f6e00cf..79c0552 100644 --- a/frontend/js/backups.js +++ b/frontend/js/backups.js @@ -23,8 +23,8 @@ const state = { path: null, backups: [], overlay: null, - leftVersion: null, // timestamp of backup shown on left - rightVersion: null, // timestamp of backup shown on right, or null for current + leftVersion: null, + rightVersion: null, diffText: '', }; @@ -32,11 +32,6 @@ const state = { // Public API // --------------------------------------------------------------------------- -/** - * Open the backup/diff viewer for a file. - * @param {string} vault - Vault name - * @param {string} path - Relative file path - */ export async function openBackupViewer(vault, path) { state.vault = vault; state.path = path; @@ -45,16 +40,13 @@ export async function openBackupViewer(vault, path) { state.rightVersion = null; state.diffText = ''; - // Create and show overlay const overlay = createOverlay(); document.body.appendChild(overlay); state.overlay = overlay; requestAnimationFrame(() => overlay.classList.add('active')); - // Show loading state setBody(renderLoading()); - // Fetch backups try { const data = await api(`/api/file/${encodeURIComponent(vault)}/backups?path=${encodeURIComponent(path)}`); state.backups = data.backups || []; @@ -69,14 +61,9 @@ export async function openBackupViewer(vault, path) { return; } - // Auto-select: left = most recent, right = current state.leftVersion = state.backups[0].timestamp; state.rightVersion = null; - - // Render version selectors renderMainView(); - - // Load initial diff await loadDiff(); } @@ -84,16 +71,6 @@ export async function openBackupViewer(vault, path) { // DOM helpers // --------------------------------------------------------------------------- -// Global click handler: catches close button clicks anywhere in the document -document.addEventListener('click', function(e) { - const btn = e.target.closest ? e.target.closest('.backup-close-btn') : null; - if (btn && state.overlay) { - e.stopPropagation(); - e.preventDefault(); - close(); - } -}, true); // capture phase — fires before other handlers - function createOverlay() { const overlay = document.createElement('div'); overlay.className = 'obsigate-modal-overlay backup-viewer-overlay'; @@ -108,25 +85,33 @@ function createOverlay() { // Keyboard: Escape closes the viewer _escHandler = (e) => { - if (e.key === 'Escape' && state.overlay) { - close(); - } + if (e.key === 'Escape' && state.overlay) close(); }; document.addEventListener('keydown', _escHandler); return overlay; } +/** Replace panel content and re-bind the close button. */ function setBody(html) { const panel = document.getElementById('backup-viewer-panel'); - if (panel) panel.innerHTML = html; + if (!panel) return; + panel.innerHTML = html; + safeCreateIcons(); + // Bind close button — same pattern as AdminPanel + const closeBtn = panel.querySelector('.backup-close-btn'); + if (closeBtn) { + closeBtn.addEventListener('click', (e) => { + e.stopPropagation(); + close(); + }); + } } let _escHandler = null; function close() { if (!state.overlay) return; - // Remove keyboard listener if (_escHandler) { document.removeEventListener('keydown', _escHandler); _escHandler = null; @@ -138,18 +123,18 @@ function close() { }, 200); } -// Expose globally for inline onclick handlers -window._backupViewerClose = close; - // --------------------------------------------------------------------------- // Rendering // --------------------------------------------------------------------------- +/** Close button HTML — same pattern as AdminPanel editor-btn */ +const CLOSE_BTN = ''; + function renderLoading() { return `
${escapeHtml(msg)}
@@ -171,10 +156,10 @@ function renderError(msg) { } function renderEmpty() { - return \` + return `Aucun backup disponible pour ce fichier.
@@ -205,7 +190,7 @@ function renderMainView() {${escapeHtml(line)}