File size: 8,968 Bytes
a86d063
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
Document viewer component for displaying PDFs with citation highlighting
"""
from typing import List, Dict, Optional
import json


class DocumentViewer:
    """Handle document viewing with citation highlighting"""
    
    def __init__(self):
        """Initialize document viewer"""
        self.current_document = None
        self.current_highlights = []
    
    def render_document(self, html_content: str, filename: str, highlight_paragraphs: List[str] = None) -> str:
        """
        Render document with optional paragraph highlighting
        
        Args:
            html_content: HTML content of the document
            filename: Name of the document
            highlight_paragraphs: List of paragraph IDs to highlight
            
        Returns:
            Enhanced HTML with highlighting
        """
        if not html_content:
            return self._render_empty_state()
        
        # Add highlighting script and marks
        if highlight_paragraphs:
            for para_id in highlight_paragraphs:
                # Add highlighted class to specific paragraphs
                html_content = html_content.replace(
                    f'<p class="paragraph" id="{para_id}"',
                    f'<p class="paragraph highlighted-citation" id="{para_id}"'
                )
        
        # Wrap with viewer container and add controls
        enhanced_html = f"""
        <div class="document-viewer-wrapper">
            {self._create_viewer_controls(filename)}
            <div class="document-viewer-content" id="doc-viewer-content">
                {html_content}
            </div>
        </div>
        
        <script>
        // Auto-scroll to first highlighted paragraph
        document.addEventListener('DOMContentLoaded', function() {{
            const firstHighlight = document.querySelector('.highlighted-citation');
            if (firstHighlight) {{
                setTimeout(function() {{
                    firstHighlight.scrollIntoView({{ behavior: 'smooth', block: 'center' }});
                }}, 300);
            }}
        }});
        
        // Smooth scroll when clicking citation links
        function scrollToParagraph(paraId) {{
            const element = document.getElementById(paraId);
            if (element) {{
                // Remove previous highlights
                document.querySelectorAll('.highlighted-citation').forEach(el => {{
                    el.classList.remove('highlighted-citation');
                }});
                
                // Add highlight to clicked citation
                element.classList.add('highlighted-citation');
                
                // Scroll to element
                element.scrollIntoView({{ behavior: 'smooth', block: 'center' }});
                
                // Flash effect
                element.style.animation = 'none';
                setTimeout(() => {{
                    element.style.animation = 'highlight-flash 1s ease';
                }}, 10);
            }}
        }}
        </script>
        """
        
        return enhanced_html
    
    def _create_viewer_controls(self, filename: str) -> str:
        """
        Create viewer control bar
        
        Args:
            filename: Current document filename
            
        Returns:
            HTML for controls
        """
        return f"""
        <div class="viewer-controls">
            <div class="viewer-title">
                <span class="doc-icon">πŸ“„</span>
                <span class="doc-name">{filename}</span>
            </div>
            <div class="viewer-actions">
                <button class="viewer-btn" onclick="document.getElementById('doc-viewer-content').style.fontSize='0.9em';" title="Zoom Out">
                    πŸ”βˆ’
                </button>
                <button class="viewer-btn" onclick="document.getElementById('doc-viewer-content').style.fontSize='1em';" title="Reset Zoom">
                    πŸ”
                </button>
                <button class="viewer-btn" onclick="document.getElementById('doc-viewer-content').style.fontSize='1.1em';" title="Zoom In">
                    πŸ”+
                </button>
            </div>
        </div>
        """
    
    def _render_empty_state(self) -> str:
        """
        Render empty state when no document is selected
        
        Returns:
            HTML for empty state
        """
        return """
        <div class="document-viewer-empty">
            <div class="empty-state-icon">πŸ“„</div>
            <h3>Tidak Ada Dokumen</h3>
            <p>Upload dokumen PDF untuk melihat konten dan sitasi di sini.</p>
        </div>
        """
    
    def create_citation_link(self, filename: str, paragraph_ids: List[str], snippet: str, page: int = None) -> str:
        """
        Create clickable citation link for chat
        
        Args:
            filename: Source document filename
            paragraph_ids: List of paragraph IDs this citation refers to
            snippet: Text snippet to show
            page: Page number (optional)
            
        Returns:
            HTML citation link
        """
        para_id = paragraph_ids[0] if paragraph_ids else "unknown"
        page_info = f" (Hal. {page})" if page else ""
        
        citation_html = f"""
        <div class="citation-card" onclick="scrollToParagraph('{para_id}')">
            <div class="citation-header">
                <strong>πŸ“„ {filename}{page_info}</strong>
            </div>
            <div class="citation-snippet">
                "{snippet[:150]}..."
            </div>
        </div>
        """
        
        return citation_html
    
    def format_sources_with_links(self, sources: List[Dict]) -> tuple[str, List[str]]:
        """
        Format sources as interactive citations
        
        Args:
            sources: List of source metadata from RAG pipeline
            
        Returns:
            Tuple of (HTML string, list of paragraph IDs to highlight)
        """
        if not sources:
            return "", []
        
        all_paragraph_ids = []
        html = "<div class='sources-container'>"
        html += "<h4 class='sources-title'>πŸ“š Sumber Referensi:</h4>"
        
        for i, source in enumerate(sources, 1):
            filename = source.get('filename', 'Unknown')
            chunk_text = source.get('chunk_text', '')
            
            # Parse paragraph IDs if available
            paragraph_ids_str = source.get('paragraph_ids', '[]')
            try:
                if isinstance(paragraph_ids_str, str):
                    paragraph_ids = json.loads(paragraph_ids_str)
                else:
                    paragraph_ids = paragraph_ids_str if isinstance(paragraph_ids_str, list) else []
            except:
                paragraph_ids = []
            
            # Parse pages
            pages_str = source.get('pages', '[]')
            try:
                if isinstance(pages_str, str):
                    pages = json.loads(pages_str)
                else:
                    pages = pages_str if isinstance(pages_str, list) else []
            except:
                pages = []
            
            page = pages[0] if pages else None
            
            # Track all paragraph IDs for highlighting
            all_paragraph_ids.extend(paragraph_ids)
            
            # Create citation link
            html += self.create_citation_link(
                filename=filename,
                paragraph_ids=paragraph_ids,
                snippet=chunk_text,
                page=page
            )
        
        html += "</div>"
        
        return html, list(set(all_paragraph_ids))  # Return unique paragraph IDs
    
    def create_document_selector(self, documents: List[Dict], current_doc: str = None) -> str:
        """
        Create dropdown selector for documents
        
        Args:
            documents: List of document metadata
            current_doc: Currently selected document filename
            
        Returns:
            HTML for document selector
        """
        if not documents:
            return "<p class='no-docs-message'>Belum ada dokumen yang tersedia</p>"
        
        html = """
        <div class="document-selector">
            <label for="doc-select">Pilih Dokumen:</label>
            <select id="doc-select" class="doc-select-dropdown">
        """
        
        for doc in documents:
            filename = doc.get('filename', 'Unknown')
            num_pages = doc.get('num_pages', 0)
            selected = 'selected' if filename == current_doc else ''
            
            html += f"""
                <option value="{filename}" {selected}>
                    {filename} ({num_pages} hal.)
                </option>
            """
        
        html += """
            </select>
        </div>
        """
        
        return html