/* eslint-disable max-lines */ require('./toolBar.less'); const React = require('react'); const { useState, useEffect } = React; const _ = require('lodash'); import { Anchored, AnchoredBox, AnchoredTrigger } from '../../../components/Anchored.jsx'; const MAX_ZOOM = 300; const MIN_ZOOM = 10; const TOOLBAR_VISIBILITY = 'HB_renderer_toolbarVisibility'; const ToolBar = ({ displayOptions, onDisplayOptionsChange, visiblePages, totalPages, headerState, setHeaderState })=>{ const [pageNum, setPageNum] = useState(1); const [toolsVisible, setToolsVisible] = useState(true); useEffect(()=>{ // format multiple visible pages as a range (e.g. "150-153") const pageRange = visiblePages.length === 1 ? `${visiblePages[0]}` : `${visiblePages[0]} - ${visiblePages.at(-1)}`; setPageNum(pageRange); }, [visiblePages]); useEffect(()=>{ const Visibility = localStorage.getItem(TOOLBAR_VISIBILITY); if(Visibility) setToolsVisible(Visibility === 'true'); }, []); const handleZoomButton = (zoom)=>{ handleOptionChange('zoomLevel', _.round(_.clamp(zoom, MIN_ZOOM, MAX_ZOOM))); }; const handleOptionChange = (optionKey, newValue)=>{ onDisplayOptionsChange({ ...displayOptions, [optionKey]: newValue }); }; const handlePageInput = (pageInput)=>{ if(/[0-9]/.test(pageInput)) setPageNum(parseInt(pageInput)); // input type is 'text', so `page` comes in as a string, not number. }; // scroll to a page, used in the Prev/Next Page buttons. const scrollToPage = (pageNumber)=>{ if(typeof pageNumber !== 'number') return; 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' }); }; const calculateChange = (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. const widestPage = _.maxBy([...pages], 'offsetWidth').offsetWidth; if(displayOptions.spread === 'facing') desiredZoom = (iframeWidth / ((widestPage * 2) + parseInt(displayOptions.columnGap))) * 100; else desiredZoom = (iframeWidth / (widestPage + 20)) * 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 minDimRatio; if(displayOptions.spread === 'single') minDimRatio = [...pages].reduce( (minRatio, page)=>Math.min(minRatio, iframeWidth / page.offsetWidth, iframeHeight / page.offsetHeight ), Infinity ); else minDimRatio = [...pages].reduce( (minRatio, page)=>Math.min(minRatio, iframeWidth / ((page.offsetWidth * 2) + parseInt(displayOptions.columnGap)), iframeHeight / page.offsetHeight ), Infinity ); desiredZoom = minDimRatio * 100; } const margin = 5; // extra space so page isn't edge to edge (not truly "to fill") const deltaZoom = (desiredZoom - displayOptions.zoomLevel) - margin; return deltaZoom; }; return (