The two call number lines now take 0.56 in at the top of the card instead of about 0.9 in, with a full-width line under them that marks where the top edge of the book's pages sits. PDFs ask the viewer to print unscaled, and the calibration page has a 6 inch line to check the print scale. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
130 lines
5.7 KiB
TypeScript
130 lines
5.7 KiB
TypeScript
import { useEffect, useRef, useState } from 'preact/hooks';
|
|
import { api } from '../api.ts';
|
|
import { printStatus } from '../bookstate.ts';
|
|
import { useDialog } from '../dialog.ts';
|
|
import { useQueue } from '../live.ts';
|
|
import { removeWithUndo } from '../removal.ts';
|
|
import { sheetCount } from '../sheets.ts';
|
|
import { showError, showToast } from '../toast-store.ts';
|
|
import { QueueRow } from '../components/BookRow.tsx';
|
|
|
|
interface MadePdf { ids: number[]; url: string }
|
|
|
|
export function CardsToPrint({ onFix }: { onFix: (id: number) => void }) {
|
|
const queue = useQueue();
|
|
const status = printStatus(queue);
|
|
const [open, setOpen] = useState(false);
|
|
const [made, setMade] = useState<MadePdf | null>(null);
|
|
const pdfUrl = useRef<string | null>(null);
|
|
useEffect(() => () => { if (pdfUrl.current) URL.revokeObjectURL(pdfUrl.current); }, []);
|
|
|
|
// Nothing left to print while the panel is open (for example the last card was removed): close it.
|
|
useEffect(() => { if (open && status.ready === 0 && !made) setOpen(false); }, [open, status.ready, made]);
|
|
|
|
function forget() {
|
|
if (pdfUrl.current) URL.revokeObjectURL(pdfUrl.current);
|
|
pdfUrl.current = null; setMade(null);
|
|
}
|
|
|
|
return (
|
|
<section class="cards" aria-labelledby="cards-heading">
|
|
<div class="cards-head">
|
|
<h2 id="cards-heading">Cards to print <span class="count">{queue.length}</span></h2>
|
|
<div class="cards-print">
|
|
{status.reason ? <span class="cards-reason" role="status">{status.reason}</span> : null}
|
|
<div class="print-anchor">
|
|
<button type="button" class={'btn ' + (open ? '' : 'btn-primary')} disabled={status.ready === 0 && !made}
|
|
aria-haspopup="dialog" aria-expanded={open} onClick={() => setOpen(o => !o)}>{status.label}</button>
|
|
{open ? <PrintPanel ready={status.ready} made={made} setMade={setMade} pdfUrl={pdfUrl} forget={forget} onClose={() => setOpen(false)} /> : null}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{queue.length === 0 ? (
|
|
<p class="empty">No cards waiting. Scan a book to start.</p>
|
|
) : (
|
|
<ul class="rows">
|
|
{queue.map(b => <QueueRow key={b.id} book={b} onFix={() => onFix(b.id)} onRemove={() => removeWithUndo(b)} />)}
|
|
</ul>
|
|
)}
|
|
</section>
|
|
);
|
|
}
|
|
|
|
/** Where the first card goes, then the PDF, then "did it print?". Anchored under the print button. */
|
|
function PrintPanel({ ready, made, setMade, pdfUrl, forget, onClose }: {
|
|
ready: number; made: MadePdf | null; setMade: (m: MadePdf | null) => void;
|
|
pdfUrl: { current: string | null }; forget: () => void; onClose: () => void;
|
|
}) {
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
useDialog(ref, onClose, false);
|
|
const [start, setStart] = useState(1);
|
|
const [busy, setBusy] = useState(false);
|
|
const sheets = sheetCount(ready, start);
|
|
|
|
// Keep keyboard focus inside when the panel changes from the question to the confirmation.
|
|
const first = useRef(true);
|
|
useEffect(() => {
|
|
if (first.current) { first.current = false; return; }
|
|
ref.current?.querySelector<HTMLElement>('.btn-primary')?.focus();
|
|
}, [made === null]);
|
|
|
|
async function makePdf() {
|
|
setBusy(true);
|
|
// Open the tab inside the click so the popup blocker allows it, then point it at the PDF.
|
|
const tab = window.open('', '_blank');
|
|
try {
|
|
const { blob, ids } = await api.fetchPdf(start);
|
|
if (pdfUrl.current) URL.revokeObjectURL(pdfUrl.current); // the previous PDF is no longer needed
|
|
const url = URL.createObjectURL(new Blob([blob], { type: 'application/pdf' }));
|
|
pdfUrl.current = url;
|
|
if (tab) tab.location.href = url;
|
|
setMade({ ids, url });
|
|
} catch (e) {
|
|
tab?.close();
|
|
showError(`Could not make the PDF: ${(e as Error).message}`);
|
|
}
|
|
setBusy(false);
|
|
}
|
|
|
|
async function confirm() {
|
|
if (!made) return;
|
|
try {
|
|
await api.confirmPrint(made.ids);
|
|
showToast(`Cleared ${made.ids.length} ${made.ids.length === 1 ? 'card' : 'cards'} from the list`);
|
|
forget(); onClose();
|
|
} catch (e) { showError(`Could not clear the cards: ${(e as Error).message}`); }
|
|
}
|
|
|
|
return (
|
|
<div class="print-panel" role="dialog" aria-label="Print cards" ref={ref} tabIndex={-1}>
|
|
{made ? (
|
|
<>
|
|
<p class="print-question">Did the cards print correctly?</p>
|
|
<p class="help"><a href={made.url} target="_blank" rel="noopener">Open the PDF again</a> if the tab did not open.</p>
|
|
<div class="actions">
|
|
<button type="button" class="btn btn-primary" onClick={() => void confirm()}>Yes, clear them</button>
|
|
<button type="button" class="btn" onClick={() => { forget(); onClose(); }}>Not yet</button>
|
|
</div>
|
|
</>
|
|
) : (
|
|
<>
|
|
<div class="slots-wrap">
|
|
<span class="slots-label" id="slots-label">First card goes in</span>
|
|
<div class="sheet" role="radiogroup" aria-labelledby="slots-label">
|
|
{[1, 2, 3].map(n => (
|
|
<button type="button" role="radio" aria-checked={start === n} aria-label={`Slot ${n} of 3`}
|
|
class={'slot' + (start === n ? ' on' : '') + (n < start ? ' used' : '')} onClick={() => setStart(n)}>{n}</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
<p class="print-note">Print at actual size (100% scale, not Fit to page), single-sided, from the manual feed slot</p>
|
|
<p class="help">{ready} {ready === 1 ? 'card' : 'cards'} on {sheets} {sheets === 1 ? 'sheet' : 'sheets'}</p>
|
|
<div class="actions">
|
|
<button type="button" class="btn btn-primary" disabled={busy || ready === 0} onClick={() => void makePdf()}>{busy ? 'Making PDF…' : 'Make PDF'}</button>
|
|
</div>
|
|
</>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|