import { appState } from './state/app-state.js'; import { listMdFiles } from './services/files.js'; import { renderSidebar } from './ui/sidebar.js'; import { loadPage } from './ui/reader.js'; import { saveRecent, renderRecents } from './ui/home.js'; const viewHome = document.getElementById('view-home'); const viewReader = document.getElementById('view-reader'); const sidebarEl = document.querySelector('#sidebar'); const content = document.querySelector('#content'); const win = window.__TAURI__.window.getCurrentWindow(); const titlebarTitle = document.getElementById('titlebar-title'); export function showHome() { appState.currentPath = null; appState.currentMode = null; sidebarEl.innerHTML = ''; win.setTitle('Pena — Markdown Viewer'); titlebarTitle.textContent = 'Pena — Markdown Viewer'; viewReader.classList.add('hidden'); viewHome.classList.remove('hidden'); renderRecents(openPath); } export function showReader() { viewHome.classList.add('hidden'); viewReader.classList.remove('hidden'); } export async function openPath(path, mode) { appState.currentPath = path; appState.currentMode = mode; saveRecent(path, mode); if (mode === 'file') { showReader(); const sep = path.includes('\\') ? '\\' : '/'; const parentDir = path.split(sep).slice(0, -1).join(sep); try { const files = await listMdFiles(parentDir); sidebarEl.classList.remove('hidden'); content.style.marginLeft = ''; renderSidebar(parentDir, files, path, filePath => loadPage(filePath, appState.currentMode, sidebarEl)); } catch { sidebarEl.innerHTML = ''; sidebarEl.classList.add('hidden'); content.style.marginLeft = '0'; } await loadPage(path, appState.currentMode, sidebarEl); return; } // mode === 'dir' sidebarEl.classList.remove('hidden'); content.style.marginLeft = ''; try { const files = await listMdFiles(path); showReader(); const sep = path.includes('\\') ? '\\' : '/'; const home = files.find(f => { const name = f.split(sep).pop(); return name === 'Home.md' || name === 'home.md'; }); const firstFile = home ?? files[0]; renderSidebar(path, files, firstFile, filePath => loadPage(filePath, appState.currentMode, sidebarEl)); await loadPage(firstFile, appState.currentMode, sidebarEl); } catch (err) { showReader(); content.innerHTML = `
Impossible d'ouvrir le dossier : ${err}
`; } }