| import { useMemo } from "react"; |
|
|
| const MM_TO_PX = 3.78; |
| const A4_HEIGHT_PX = 297 * MM_TO_PX; |
| |
| 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 }; |
| } |
|
|
| |
| const availableHeight = A4_HEIGHT_PX - 2 * pagePadding; |
|
|
| |
| 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 }; |
| } |
|
|
| |
| return { scaleFactor: MIN_SCALE, isScaled: true, cannotFit: true }; |
| }, [contentHeight, pagePadding, enabled]); |
| } |
|
|