export function exportSession(s) { return JSON.stringify({ version: 2, savedAt: new Date().toISOString(), fileName: s.fileName, pdfHash: s.pdfHash || '', screen: 'reader', noteModes: s.noteModes, commentary: s.commentary, paper: s.paper, litPapers: s.litPapers, litByPara: s.litByPara, figures: s.figures || [], litByFig: s.litByFig || {}, equations: s.equations || [], references: s.references || [], citeIndex: s.citeIndex || {}, userNotes: s.userNotes || {}, paperNote: s.paperNote || '', }, null, 2); } export function importSession(json) { const s = JSON.parse(json); if ((s.version !== 1 && s.version !== 2) || !s.paper || !s.paper.paragraphs) { throw new Error('could not read that session file'); } // Tolerant defaults for fields absent in older sessions if (!s.figures) s.figures = []; if (!s.litByFig) s.litByFig = {}; if (!s.equations) s.equations = []; if (!s.references) s.references = []; if (!s.citeIndex) s.citeIndex = {}; if (!s.userNotes) s.userNotes = {}; if (!s.paperNote) s.paperNote = ''; return s; } export async function hashPdf(arrayBuffer) { const digest = await crypto.subtle.digest('SHA-256', arrayBuffer); return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, '0')).join(''); }