File size: 1,578 Bytes
aa1ee73 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import { useMemo } from "react";
const MM_TO_PX = 3.78;
const A4_HEIGHT_PX = 297 * MM_TO_PX;
// 最多只允许缩小到 90%,保证文字可读性和美观
const MIN_SCALE = 0.9;
interface UseAutoOnePageOptions {
contentHeight: number;
pagePadding: number;
enabled: boolean;
}
interface UseAutoOnePageResult {
scaleFactor: number;
isScaled: boolean;
/** 内容过多,即使缩放到下限也无法完美一页 */
cannotFit: boolean;
}
export function useAutoOnePage({
contentHeight,
pagePadding,
enabled,
}: UseAutoOnePageOptions): UseAutoOnePageResult {
return useMemo(() => {
if (!enabled || contentHeight <= 0) {
return { scaleFactor: 1, isScaled: false, cannotFit: false };
}
// A4 可用内容高度 = A4 总高度 - 上下页边距
const availableHeight = A4_HEIGHT_PX - 2 * pagePadding;
// 实际内容高度(去掉 #resume-preview 的上下 padding)
const actualContentHeight = contentHeight - 2 * pagePadding;
if (actualContentHeight <= availableHeight) {
// 内容未超出一页,不需要缩放
return { scaleFactor: 1, isScaled: false, cannotFit: false };
}
const idealScale = availableHeight / actualContentHeight;
if (idealScale >= MIN_SCALE) {
// 在合理范围内,直接缩放
return { scaleFactor: idealScale, isScaled: true, cannotFit: false };
}
// 超出合理缩放范围,仍按下限缩放,但标记 cannotFit
return { scaleFactor: MIN_SCALE, isScaled: true, cannotFit: true };
}, [contentHeight, pagePadding, enabled]);
}
|