loopable / web /src /sheet /pageViews.ts
fsanyoto's picture
Deploy AIOS web (React glide grid + FastAPI slice)
dcd43e2 verified
Raw History Blame Contribute Delete
1.57 kB
import type { SheetPrintSettings, Workbook } from "./types";
import { planSheetPdf } from "./sheetExport";
/** Page geometry in document pixels, before the grid applies zoom and scrolling. */
export interface SheetPageGrid {
/** Distinct ascending page starts, always beginning at zero. */
xs: number[];
ys: number[];
/** One printable page, in sheet pixels. */
pageWidth: number;
pageHeight: number;
/** Margins in sheet pixels for the Page layout insets. */
marginX: number;
marginY: number;
}
/** Use the PDF's boundaries; invalid print settings must never take down a view. */
export const sheetPageGrid = (
book: Workbook, sheetId: string, settings: SheetPrintSettings | undefined,
): SheetPageGrid | null => {
try {
const plan = planSheetPdf(book, {
...(settings ?? { sheetIds: [], paper: "letter", orientation: "landscape", fit: "width" }),
sheetIds: [sheetId], from: undefined, to: undefined,
});
const pages = plan.pages.filter(page => page.sheetId === sheetId);
if (!pages.length) return null;
const scale = pages[0].scale;
if (!Number.isFinite(scale) || scale <= 0) return null;
return {
xs: [...new Set(pages.map(page => page.x))].sort((a, b) => a - b),
ys: [...new Set(pages.map(page => page.y))].sort((a, b) => a - b),
pageWidth: plan.contentWidth / scale,
pageHeight: plan.contentHeight / scale,
marginX: (plan.width - plan.contentWidth) / 2 / scale,
marginY: (plan.height - plan.contentHeight) / 2 / scale,
};
} catch {
return null;
}
};