/** * Preview Module * Opens file preview in a separate popup window */ (function() { 'use strict'; let currentBlobUrl = null; let currentFile = null; let currentRowIndex = null; let previewWindow = null; // File type mappings (extensions stored without leading dot, matching shared/zddc.js) const IMAGE_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'svg', 'webp', 'bmp', 'ico']; const TEXT_EXTENSIONS = ['txt', 'md', 'json', 'xml', 'csv', 'log', 'html', 'css', 'js', 'ts', 'py', 'sh', 'bat', 'yaml', 'yml', 'ini', 'cfg', 'conf']; const PDF_EXTENSIONS = ['pdf']; const ZIP_EXTENSIONS = ['zip']; // Cache for lazily loaded CDN libraries const loadedLibraries = new Map(); /** * Lazily load a script from CDN. Returns a promise that resolves when loaded. * Caches the promise so subsequent calls return immediately. */ function loadLibrary(url) { if (loadedLibraries.has(url)) return loadedLibraries.get(url); const promise = new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = url; script.onload = resolve; script.onerror = () => reject(new Error(`Failed to load library: ${url}`)); document.head.appendChild(script); }); loadedLibraries.set(url, promise); return promise; } /** * Initialize preview module */ function init() { // Listen for row focused events from selection module document.addEventListener('rowfocused', handleRowFocused); // Set up toggle button to open/close preview window const toggleBtn = document.getElementById('togglePreviewBtn'); if (toggleBtn) { toggleBtn.addEventListener('click', () => { if (previewWindow && !previewWindow.closed) { // Close preview window previewWindow.close(); previewWindow = null; toggleBtn.classList.remove('preview-active'); } else if (currentFile) { openPreviewWindow(currentFile); toggleBtn.classList.add('preview-active'); } }); } } /** * Handle row focused event */ function handleRowFocused(e) { const { rowIndex, file } = e.detail; currentRowIndex = rowIndex; if (file && file !== currentFile) { currentFile = file; // Update preview window if open if (previewWindow && !previewWindow.closed) { openPreviewWindow(file); } } } /** * Open preview in a separate popup window */ async function openPreviewWindow(file) { if (!file) return; currentFile = file; try { const blob = await getFileBlob(file); // Clean up previous blob URL if (currentBlobUrl) { URL.revokeObjectURL(currentBlobUrl); } currentBlobUrl = URL.createObjectURL(blob); const fileName = zddc.joinExtension(file.originalFilename, file.extension); // Build preview HTML with toolbar const previewHtml = `