Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.4 KB
Raw Blame
/**
 * Document Notes Component (library document pages)
 *
 * The document-side twin of components/research_notes.js: on a library
 * document's pages, list the notes referencing it, start a new linked
 * note, and — on the full-text view, whose extracted text is immutable —
 * attach Word-review-style inline comments to passages via the shared
 * LDRAnnotationSurface (components/annotation_surface.js).
 *
 * Reads its target from data-document-id on #document-notes-section
 * (details page) or #ldr-document-text-content (text view). Rendering
 * builds DOM nodes with textContent; names are IIFE-scoped.
 *
 * URL security: every URL here is a same-origin relative path built with
 * encodeURIComponent — no external URLs. URLValidator.isSafeUrl is
 * available if that ever changes.
 */
(function() {
    let documentId = null;
    let loadGeneration = 0;

    // Component-local wrappers over the shared trio (window.NotesShared) —
    // local names kept for shadowing-safety; the bodies live in one place.
    const toast = (message, type) => window.NotesShared.toast(message, type);
    const postJson = (url, body) => window.NotesShared.postJson(url, body);

    function initDocumentNotes() {
        const section = document.getElementById('document-notes-section');
        const textContainer = document.getElementById('ldr-document-text-content');
        documentId =
            (section && section.dataset.documentId) ||
            (textContainer && textContainer.dataset.documentId) ||
            null;
        if (!documentId) return;

        const addBtn = document.getElementById('document-add-note-btn');
        if (addBtn) addBtn.addEventListener('click', addNoteForDocument);

        if (textContainer) {
            // The extracted text is immutable — safe annotation surface.
            const base = `/notes/api/documents/${encodeURIComponent(documentId)}`;
            window.LDRAnnotationSurface.init({
                containerId: 'ldr-document-text-content',
                endpoints: {
                    list: `${base}/annotations`,
                    create: `${base}/annotations`,
                    deleteFor: (noteId) => `${base}/annotations/${encodeURIComponent(noteId)}`
                },
                onChanged: loadDocumentNotes
            });
        }

        loadDocumentNotes();
    }

    async function loadDocumentNotes() {
        const list = document.getElementById('document-notes-list');
        const empty = document.getElementById('document-notes-empty');
        if (!list) return;
        const generation = ++loadGeneration;
        try {
            const response = await safeFetchWithAuth(`/notes/api/documents/${encodeURIComponent(documentId)}/notes`, {
                credentials: 'same-origin'
            });
            const data = await response.json();
            if (generation !== loadGeneration) return;
            if (!data.success) throw new Error(data.error || 'load failed');

            list.replaceChildren();
            const notes = data.notes || [];
            if (empty) empty.style.display = notes.length ? 'none' : 'block';
            for (const note of notes) {
                list.appendChild(window.NotesShared.renderNoteRow(note));
            }
        } catch (error) {
            if (generation !== loadGeneration) return;
            SafeLogger.error('Error loading document notes:', error);
            if (empty) {
                empty.textContent = "Couldn't load notes for this document.";
                empty.style.display = 'block';
            }
        }
    }


    async function addNoteForDocument() {
        const btn = document.getElementById('document-add-note-btn');
        if (btn) btn.disabled = true;
        try {
            const data = await postJson(`/notes/api/documents/${encodeURIComponent(documentId)}/notes`, {});
            window.location.href = `/notes/${encodeURIComponent(data.note_id)}`;
        } catch (error) {
            SafeLogger.error('Error creating note for document:', error);
            toast(error.message || 'Failed to create note', 'error');
            if (btn) btn.disabled = false;
        }
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initDocumentNotes);
    } else {
        initDocumentNotes();
    }

    if (window.__VITEST_TEST__) {
        window.__documentNotesTest = {
            loadDocumentNotes,
            setDocumentId: (value) => { documentId = value; }
        };
    }
})();