Upload static/app.js with huggingface_hub
Browse files- static/app.js +73 -0
static/app.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Doc Analyzer - PDF Rendering and Theme Toggle
|
| 2 |
+
|
| 3 |
+
// Theme toggle functionality
|
| 4 |
+
function initTheme() {
|
| 5 |
+
const savedTheme = localStorage.getItem('theme') || 'dark';
|
| 6 |
+
document.documentElement.setAttribute('data-theme', savedTheme);
|
| 7 |
+
|
| 8 |
+
const themeToggle = document.querySelector('.theme-toggle');
|
| 9 |
+
if (themeToggle) {
|
| 10 |
+
themeToggle.addEventListener('click', () => {
|
| 11 |
+
const currentTheme = document.documentElement.getAttribute('data-theme');
|
| 12 |
+
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
|
| 13 |
+
document.documentElement.setAttribute('data-theme', newTheme);
|
| 14 |
+
localStorage.setItem('theme', newTheme);
|
| 15 |
+
});
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
// PDF rendering functionality
|
| 20 |
+
async function renderPDF(pdfUrl, canvasId) {
|
| 21 |
+
const canvas = document.getElementById(canvasId);
|
| 22 |
+
if (!canvas) return;
|
| 23 |
+
|
| 24 |
+
const ctx = canvas.getContext('2d');
|
| 25 |
+
|
| 26 |
+
try {
|
| 27 |
+
// Load PDF.js from CDN
|
| 28 |
+
const pdfjsLib = await import('https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.mjs');
|
| 29 |
+
pdfjsLib.GlobalWorkerOptions.workerSrc = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.mjs';
|
| 30 |
+
|
| 31 |
+
// Load the PDF document
|
| 32 |
+
const loadingTask = pdfjsLib.getDocument(pdfUrl);
|
| 33 |
+
const pdf = await loadingTask.promise;
|
| 34 |
+
|
| 35 |
+
// Get first page
|
| 36 |
+
const page = await pdf.getPage(1);
|
| 37 |
+
|
| 38 |
+
// Calculate scale to fit canvas width
|
| 39 |
+
const viewport = page.getViewport({ scale: 1 });
|
| 40 |
+
const scale = canvas.width / viewport.width;
|
| 41 |
+
const scaledViewport = page.getViewport({ scale: scale });
|
| 42 |
+
|
| 43 |
+
// Set canvas dimensions
|
| 44 |
+
canvas.height = scaledViewport.height;
|
| 45 |
+
canvas.width = scaledViewport.width;
|
| 46 |
+
|
| 47 |
+
// Render page
|
| 48 |
+
const renderContext = {
|
| 49 |
+
canvasContext: ctx,
|
| 50 |
+
viewport: scaledViewport
|
| 51 |
+
};
|
| 52 |
+
|
| 53 |
+
await page.render(renderContext).promise;
|
| 54 |
+
} catch (error) {
|
| 55 |
+
console.error('Error rendering PDF:', error);
|
| 56 |
+
ctx.fillStyle = '#ef4444';
|
| 57 |
+
ctx.font = '16px Arial';
|
| 58 |
+
ctx.fillText('Error loading PDF', 10, 30);
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
// Initialize on page load
|
| 63 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 64 |
+
initTheme();
|
| 65 |
+
|
| 66 |
+
// Check for PDF to render
|
| 67 |
+
const pdfUrl = document.getElementById('pdf-url');
|
| 68 |
+
const canvasId = document.getElementById('canvas-id');
|
| 69 |
+
|
| 70 |
+
if (pdfUrl && canvasId) {
|
| 71 |
+
renderPDF(pdfUrl.value, canvasId.value);
|
| 72 |
+
}
|
| 73 |
+
});
|