pdf.js
This commit is contained in:
59
web/app.js
59
web/app.js
@@ -1,12 +1,12 @@
|
||||
let state;
|
||||
let groups = [];
|
||||
let selectedIndex = -1;
|
||||
let currentPdfUrl = "";
|
||||
let pdfViewer = null;
|
||||
|
||||
const people = document.querySelector("#people");
|
||||
const fields = document.querySelector("#fields");
|
||||
const pdf = document.querySelector("#pdf");
|
||||
const viewer = document.querySelector(".viewer");
|
||||
const pdfContainer = document.querySelector("#pdfViewer");
|
||||
const pdfMessage = document.querySelector("#pdfMessage");
|
||||
const status = document.querySelector("#status");
|
||||
const search = document.querySelector("#search");
|
||||
@@ -23,6 +23,7 @@ const fieldDefs = [
|
||||
null,
|
||||
["Address", "address"],
|
||||
["State", "state"],
|
||||
null,
|
||||
["Businesses from Notes", "notes_business_raw"],
|
||||
["Types of Businesses", "types_of_business_raw"],
|
||||
["Background Experience", "background_experience"],
|
||||
@@ -111,30 +112,34 @@ function renderList() {
|
||||
}
|
||||
|
||||
async function loadPdf(index) {
|
||||
if (currentPdfUrl) {
|
||||
URL.revokeObjectURL(currentPdfUrl);
|
||||
currentPdfUrl = "";
|
||||
if (pdfViewer) {
|
||||
pdfViewer.destroy();
|
||||
}
|
||||
pdf.removeAttribute("src");
|
||||
pdfViewer = new PdfViewer(pdfContainer);
|
||||
viewer.classList.remove("loaded");
|
||||
pdfMessage.hidden = false;
|
||||
pdfMessage.textContent = "Loading PDF\u2026";
|
||||
|
||||
const startTime = performance.now();
|
||||
try {
|
||||
const response = await fetch(`/api/pdf?index=${index}`, {
|
||||
cache: "no-store",
|
||||
const response = await fetch("/api/pdf/prepare", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ index }),
|
||||
});
|
||||
if (!response.ok) {
|
||||
const body = await response.json().catch(() => ({}));
|
||||
throw new Error(
|
||||
body.error || `PDF request failed with HTTP ${response.status}.`,
|
||||
);
|
||||
throw new Error(body.error || `Prepare failed: HTTP ${response.status}`);
|
||||
}
|
||||
const blob = await response.blob();
|
||||
currentPdfUrl = URL.createObjectURL(blob);
|
||||
pdf.src = currentPdfUrl;
|
||||
const data = await response.json();
|
||||
const prepareMs = Math.round(performance.now() - startTime);
|
||||
console.log(
|
||||
`[BizMatch QC] PDF prepare: ${prepareMs}ms, disk cache ${data.cacheStatus}`,
|
||||
);
|
||||
|
||||
viewer.classList.add("loaded");
|
||||
pdfMessage.hidden = true;
|
||||
await pdfViewer.load(data.url, data.size);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
pdfMessage.textContent = `Cannot open PDF: ${message}`;
|
||||
@@ -222,7 +227,6 @@ saveSettings.onclick = async (event) => {
|
||||
event.preventDefault();
|
||||
saveSettings.disabled = true;
|
||||
settingsError.hidden = true;
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/config", {
|
||||
method: "POST",
|
||||
@@ -247,31 +251,6 @@ saveSettings.onclick = async (event) => {
|
||||
}
|
||||
};
|
||||
|
||||
let resizeTimer;
|
||||
globalThis.addEventListener("resize", () => {
|
||||
clearTimeout(resizeTimer);
|
||||
resizeTimer = setTimeout(() => {
|
||||
fetch("/api/window-size", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
width: globalThis.innerWidth,
|
||||
height: globalThis.innerHeight,
|
||||
}),
|
||||
});
|
||||
}, 300);
|
||||
});
|
||||
|
||||
globalThis.addEventListener("beforeunload", () => {
|
||||
navigator.sendBeacon(
|
||||
"/api/window-size",
|
||||
JSON.stringify({
|
||||
width: globalThis.innerWidth,
|
||||
height: globalThis.innerHeight,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
async function load() {
|
||||
try {
|
||||
const response = await fetch("/api/state", { cache: "no-store" });
|
||||
|
||||
Reference in New Issue
Block a user