require('./toolBar.less'); const React = require('react'); const { useState, useEffect } = React; const _ = require('lodash'); const MAX_ZOOM = 300; const MIN_ZOOM = 10; const ToolBar = ({ onZoomChange, currentPage, onPageChange, totalPages })=>{ const [zoomLevel, setZoomLevel] = useState(100); const [pageNum, setPageNum] = useState(currentPage); useEffect(()=>{ onZoomChange(zoomLevel); }, [zoomLevel]); useEffect(()=>{ setPageNum(currentPage); }, [currentPage]); const handleZoomButton = (delta)=>{ setZoomLevel(_.round(_.clamp(zoomLevel + delta, MIN_ZOOM, MAX_ZOOM))); }; const handlePageInput = (pageInput)=>{ if(/[0-9]/.test(pageInput)) setPageNum(parseInt(pageInput)); // input type is 'text', so `page` comes in as a string, not number. }; const scrollToPage = (pageNumber)=>{ pageNumber = _.clamp(pageNumber, 1, totalPages); const iframe = document.getElementById('BrewRenderer'); const brewRenderer = iframe?.contentWindow?.document.querySelector('.brewRenderer'); const page = brewRenderer?.querySelector(`#p${pageNumber}`); page?.scrollIntoView({ block: 'start' }); setPageNum(pageNumber); }; const calculateZoom = (mode)=>{ const iframe = document.getElementById('BrewRenderer'); const iframeWidth = iframe.getBoundingClientRect().width; const iframeHeight = iframe.getBoundingClientRect().height; const pages = iframe.contentWindow.document.getElementsByClassName('page'); let desiredZoom = 0; if(mode == 'fill'){ // find widest page, in case pages are different widths, so that the zoom is adapted to not cut the widest page off screen. let widestPage = 0; [...pages].forEach((page)=>{ const width = page.offsetWidth; if(width > widestPage) widestPage = width; }); desiredZoom = (iframeWidth / widestPage) * 100; } else if(mode == 'fit'){ // find the page with the largest single dim (height or width) so that zoom can be adapted to fit it. let maxDimRatio = 0; [...pages].forEach((page)=>{ const widthRatio = iframeWidth / page.offsetWidth; const heightRatio = iframeHeight / page.offsetHeight; const dimensionRatio = Math.min(widthRatio, heightRatio); if(dimensionRatio < maxDimRatio || maxDimRatio == 0){ maxDimRatio = dimensionRatio; } }); desiredZoom = maxDimRatio * 100; } const margin = 5; // extra space so page isn't edge to edge (not truly "to fill") const deltaZoom = (desiredZoom - zoomLevel) - margin; return deltaZoom; }; return (
{/*v=====----------------------< Zoom Controls >---------------------=====v*/}
setZoomLevel(parseInt(e.target.value))} />
{/*v=====----------------------< Page Controls >---------------------=====v*/}
e.target.select()} onChange={(e)=>handlePageInput(e.target.value)} onBlur={()=>scrollToPage(pageNum)} onKeyDown={(e)=>e.key == 'Enter' && scrollToPage(pageNum)} /> / {totalPages}
); }; module.exports = ToolBar;