feat(md-print): add timestamp footer option

This commit is contained in:
2026-10-23 13:26:34 +08:00
parent 1ce76dca64
commit 8957e3d2f1
13 changed files with 126 additions and 23 deletions
+30 -12
View File
@@ -28,6 +28,11 @@ import {
} from "./paginate";
import { exportPrintPDF } from "./print-export";
function formatTimestamp(date: Date): string {
const pad = (n: number) => String(n).padStart(2, "0");
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
}
export interface PrintPreviewProps {
src: string;
section?: string;
@@ -69,6 +74,23 @@ export function PrintPreview(props: PrintPreviewProps) {
articlePath: props.articlePath,
});
const applyFooter = (pageList: HTMLElement[], timestamp: string) => {
pageList.forEach((page, index) => {
const footer = page.querySelector(".md-print-footer");
if (!footer) return;
const pageLabel = props.config.pageNumbers
? `${index + 1} / ${pageList.length}`
: "";
footer.textContent = [pageLabel, timestamp].filter(Boolean).join(" · ");
// Left A5 gets the left corner, right A5 gets the right corner.
footer.classList.add(
index % 2 === 0
? "md-print-footer--left"
: "md-print-footer--right",
);
});
};
const runPagination = async () => {
const md = content();
const source = sourceEl();
@@ -88,18 +110,10 @@ export function PrintPreview(props: PrintPreviewProps) {
await waitForStableSize(source, 300, 4000);
const result = paginate(source, options(), mount);
result.pages.forEach((page, index) => {
const footer = page.querySelector(".md-print-footer");
if (footer) {
footer.textContent = `${index + 1} / ${result.pages.length}`;
// Left A5 gets the left corner, right A5 gets the right corner.
footer.classList.add(
index % 2 === 0
? "md-print-footer--left"
: "md-print-footer--right",
);
}
});
applyFooter(
result.pages,
props.config.timestamp ? formatTimestamp(new Date()) : "",
);
setPages(result.pages);
setWarnings(result.warnings);
} catch (e) {
@@ -164,6 +178,10 @@ export function PrintPreview(props: PrintPreviewProps) {
const handleExport = async () => {
try {
// Refresh the timestamp so the exported file records the export time.
if (props.config.timestamp) {
applyFooter(pages(), formatTimestamp(new Date()));
}
await exportPrintPDF(pages(), props.config, title(), props.articlePath);
} catch (e) {
setError(e instanceof Error ? e.message : String(e));