diff --git a/client/components/Anchored.jsx b/client/components/Anchored.jsx index 4c7a225e4..87af5a6e1 100644 --- a/client/components/Anchored.jsx +++ b/client/components/Anchored.jsx @@ -7,6 +7,11 @@ import './Anchored.less'; // **The Anchor Positioning API is not available in Firefox yet** // So in Firefox the positioning isn't perfect but is likely sufficient, and FF team seems to be working on the API quickly. +// When Anchor Positioning is added to Firefox, this can also be rewritten using the Popover API-- add the `popover` attribute +// to the container div, which will render the container in the *top level* and give it better interactions like +// click outside to dismiss. **Do not** add without Anchor, though, because positioning is very limited with the `popover` +// attribute. + const Anchored = ({ children })=>{ const [visible, setVisible] = useState(false); diff --git a/client/homebrew/brewRenderer/brewRenderer.jsx b/client/homebrew/brewRenderer/brewRenderer.jsx index 5eba86a6a..5563c5e04 100644 --- a/client/homebrew/brewRenderer/brewRenderer.jsx +++ b/client/homebrew/brewRenderer/brewRenderer.jsx @@ -1,7 +1,7 @@ /*eslint max-lines: ["warn", {"max": 300, "skipBlankLines": true, "skipComments": true}]*/ require('./brewRenderer.less'); const React = require('react'); -const { useState, useRef, useCallback, useMemo } = React; +const { useState, useRef, useMemo, useEffect } = React; const _ = require('lodash'); const MarkdownLegacy = require('naturalcrit/markdownLegacy.js'); @@ -36,8 +36,46 @@ const BrewPage = (props)=>{ index : 0, ...props }; + const pageRef = useRef(null); const cleanText = safeHTML(props.contents); - return
+ + useEffect(()=>{ + if(!pageRef.current) return; + + // Observer for tracking pages within the `.pages` div + const visibleObserver = new IntersectionObserver( + (entries)=>{ + entries.forEach((entry)=>{ + if(entry.isIntersecting) + props.onVisibilityChange(props.index + 1, true, false); // add page to array of visible pages. + else + props.onVisibilityChange(props.index + 1, false, false); + } + )}, + { threshold: .3, rootMargin: '0px 0px 0px 0px' } // detect when >30% of page is within bounds. + ); + + // Observer for tracking the page at the center of the iframe. + const centerObserver = new IntersectionObserver( + (entries)=>{ + entries.forEach((entry)=>{ + if(entry.isIntersecting) + props.onVisibilityChange(props.index + 1, true, true); // Set this page as the center page + } + )}, + { threshold: 0, rootMargin: '-50% 0px -50% 0px' } // Detect when the page is at the center + ); + + // attach observers to each `.page` + visibleObserver.observe(pageRef.current); + centerObserver.observe(pageRef.current); + return ()=>{ + visibleObserver.disconnect(); + centerObserver.disconnect(); + }; + }, []); + + return
; }; @@ -64,8 +102,10 @@ const BrewRenderer = (props)=>{ }; const [state, setState] = useState({ - isMounted : false, - visibility : 'hidden' + isMounted : false, + visibility : 'hidden', + visiblePages : [], + centerPage : 1 }); const [displayOptions, setDisplayOptions] = useState({ @@ -83,34 +123,23 @@ const BrewRenderer = (props)=>{ rawPages = props.text.split(/^\\page$/gm); } - const scrollToHash = (hash)=>{ - if(!hash) return; + const handlePageVisibilityChange = (pageNum, isVisible, isCenter)=>{ + setState((prevState)=>{ + const updatedVisiblePages = new Set(prevState.visiblePages); + if(!isCenter) + isVisible ? updatedVisiblePages.add(pageNum) : updatedVisiblePages.delete(pageNum); - const iframeDoc = document.getElementById('BrewRenderer').contentDocument; - let anchor = iframeDoc.querySelector(hash); + return { + ...prevState, + visiblePages : [...updatedVisiblePages].sort((a, b)=>a - b), + centerPage : isCenter ? pageNum : prevState.centerPage + }; + }); - if(anchor) { - anchor.scrollIntoView({ behavior: 'smooth' }); - } else { - // Use MutationObserver to wait for the element if it's not immediately available - new MutationObserver((mutations, obs)=>{ - anchor = iframeDoc.querySelector(hash); - if(anchor) { - anchor.scrollIntoView({ behavior: 'smooth' }); - obs.disconnect(); - } - }).observe(iframeDoc, { childList: true, subtree: true }); - } + if(isCenter) + props.onPageChange(pageNum); }; - const updateCurrentPage = useCallback(_.throttle((e)=>{ - const { scrollTop, clientHeight, scrollHeight } = e.target; - const totalScrollableHeight = scrollHeight - clientHeight; - const currentPageNumber = Math.max(Math.ceil((scrollTop / totalScrollableHeight) * rawPages.length), 1); - - props.onPageChange(currentPageNumber); - }, 200), []); - const isInView = (index)=>{ if(!state.isMounted) return false; @@ -137,19 +166,21 @@ const BrewRenderer = (props)=>{ }; const renderPage = (pageText, index)=>{ + + const styles = { + ...(!displayOptions.pageShadows ? { boxShadow: 'none' } : {}) + // Add more conditions as needed + }; + if(props.renderer == 'legacy') { const html = MarkdownLegacy.render(pageText); - return ; + + return ; } else { pageText += `\n\n \n\\column\n `; //Artificial column break at page end to emulate column-fill:auto (until `wide` is used, when column-fill:balance will reappear) const html = Markdown.render(pageText, index); - const styles = { - ...(!displayOptions.pageShadows ? { boxShadow: 'none' } : {}) - // Add more conditions as needed - }; - - return ; + return ; } }; @@ -182,6 +213,26 @@ const BrewRenderer = (props)=>{ } }; + const scrollToHash = (hash)=>{ + if(!hash) return; + + const iframeDoc = document.getElementById('BrewRenderer').contentDocument; + let anchor = iframeDoc.querySelector(hash); + + if(anchor) { + anchor.scrollIntoView({ behavior: 'smooth' }); + } else { + // Use MutationObserver to wait for the element if it's not immediately available + new MutationObserver((mutations, obs)=>{ + anchor = iframeDoc.querySelector(hash); + if(anchor) { + anchor.scrollIntoView({ behavior: 'smooth' }); + obs.disconnect(); + } + }).observe(iframeDoc, { childList: true, subtree: true }); + } + }; + const frameDidMount = ()=>{ //This triggers when iFrame finishes internal "componentDidMount" scrollToHash(window.location.hash); @@ -217,13 +268,13 @@ const BrewRenderer = (props)=>{ } const renderedStyle = useMemo(()=>renderStyle(), [props.style, props.themeBundle]); - renderedPages = useMemo(()=>renderPages(), [displayOptions.pageShadows, props.text]); + renderedPages = useMemo(()=>renderPages(), [props.text, displayOptions]); return ( <> {/*render dummy page while iFrame is mounting.*/} {!state.isMounted - ?
+ ?
{renderDummyPage(1)}
@@ -236,7 +287,7 @@ const BrewRenderer = (props)=>{
- + 0 ? state.visiblePages : [state.centerPage]} totalPages={rawPages.length}/> {/*render in iFrame so broken code doesn't crash the site.*/} { onClick={()=>{emitClick();}} >
+ style={ styleObject } + > {/* Apply CSS from Style tab and render pages from Markdown tab */} {state.isMounted && <> {renderedStyle} -
+
{renderedPages}
diff --git a/client/homebrew/brewRenderer/brewRenderer.less b/client/homebrew/brewRenderer/brewRenderer.less index 5f08e9455..b40e9062e 100644 --- a/client/homebrew/brewRenderer/brewRenderer.less +++ b/client/homebrew/brewRenderer/brewRenderer.less @@ -17,7 +17,7 @@ grid-template-columns: repeat(2, auto); grid-template-rows: repeat(3, auto); gap: 10px 10px; - justify-content: center; + justify-content: safe center; &.recto .page:first-child { // sets first page on 'right' ('recto') of the preview, as if for a Cover page. // todo: add a checkbox to toggle this setting @@ -33,7 +33,7 @@ display: flex; flex-wrap: wrap; gap: 10px; - justify-content: flex-start; + justify-content: safe center; & :where(.page) { flex: 0 0 auto; margin-left: unset !important; diff --git a/client/homebrew/brewRenderer/toolBar/toolBar.jsx b/client/homebrew/brewRenderer/toolBar/toolBar.jsx index b0300e0e4..ce60971aa 100644 --- a/client/homebrew/brewRenderer/toolBar/toolBar.jsx +++ b/client/homebrew/brewRenderer/toolBar/toolBar.jsx @@ -1,29 +1,30 @@ +/* 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'; -// import * as ZoomIcons from '../../../icons/icon-components/zoomIcons.jsx'; const MAX_ZOOM = 300; const MIN_ZOOM = 10; -const ToolBar = ({ displayOptions, currentPage, totalPages, onDisplayOptionsChange })=>{ +const ToolBar = ({ displayOptions, onDisplayOptionsChange, visiblePages, totalPages })=>{ - const [pageNum, setPageNum] = useState(currentPage); + const [pageNum, setPageNum] = useState(1); const [toolsVisible, setToolsVisible] = useState(true); useEffect(()=>{ - setPageNum(currentPage); - }, [currentPage]); + // 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]); const handleZoomButton = (zoom)=>{ handleOptionChange('zoomLevel', _.round(_.clamp(zoom, MIN_ZOOM, MAX_ZOOM))); }; const handleOptionChange = (optionKey, newValue)=>{ - //setDisplayOptions(prevOptions => ({ ...prevOptions, [optionKey]: newValue })); onDisplayOptionsChange({ ...displayOptions, [optionKey]: newValue }); }; @@ -32,16 +33,16 @@ const ToolBar = ({ displayOptions, currentPage, totalPages, onDisplayOptionsChan 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' }); - setPageNum(pageNumber); }; - const calculateChange = (mode)=>{ const iframe = document.getElementById('BrewRenderer'); const iframeWidth = iframe.getBoundingClientRect().width; @@ -57,8 +58,12 @@ const ToolBar = ({ displayOptions, currentPage, totalPages, onDisplayOptionsChan desiredZoom = (iframeWidth / widestPage) * 100; } else if(mode == 'fit'){ + let minDimRatio; // find the page with the largest single dim (height or width) so that zoom can be adapted to fit it. - const minDimRatio = [...pages].reduce((minRatio, page)=>Math.min(minRatio, iframeWidth / page.offsetWidth, iframeHeight / page.offsetHeight), Infinity); + if(displayOptions.spread === 'facing') + minDimRatio = [...pages].reduce((minRatio, page)=>Math.min(minRatio, iframeWidth / page.offsetWidth / 2), Infinity); // if 'facing' spread, fit two pages in view + else + minDimRatio = [...pages].reduce((minRatio, page)=>Math.min(minRatio, iframeWidth / page.offsetWidth, iframeHeight / page.offsetHeight), Infinity); desiredZoom = minDimRatio * 100; } @@ -185,8 +190,8 @@ const ToolBar = ({ displayOptions, currentPage, totalPages, onDisplayOptionsChan className='previousPage tool' type='button' title='Previous Page(s)' - onClick={()=>scrollToPage(pageNum - 1)} - disabled={pageNum <= 1} + onClick={()=>scrollToPage(_.min(visiblePages) - visiblePages.length)} + disabled={visiblePages.includes(1)} > @@ -205,6 +210,7 @@ const ToolBar = ({ displayOptions, currentPage, totalPages, onDisplayOptionsChan onChange={(e)=>handlePageInput(e.target.value)} onBlur={()=>scrollToPage(pageNum)} onKeyDown={(e)=>e.key == 'Enter' && scrollToPage(pageNum)} + style={{ width: `${pageNum.length}ch` }} /> / {totalPages}
@@ -214,8 +220,8 @@ const ToolBar = ({ displayOptions, currentPage, totalPages, onDisplayOptionsChan className='tool' type='button' title='Next Page(s)' - onClick={()=>scrollToPage(pageNum + 1)} - disabled={pageNum >= totalPages} + onClick={()=>scrollToPage(_.max(visiblePages) + 1)} + disabled={visiblePages.includes(totalPages)} > diff --git a/client/homebrew/brewRenderer/toolBar/toolBar.less b/client/homebrew/brewRenderer/toolBar/toolBar.less index c787a6f6b..b8e92c1a6 100644 --- a/client/homebrew/brewRenderer/toolBar/toolBar.less +++ b/client/homebrew/brewRenderer/toolBar/toolBar.less @@ -104,9 +104,9 @@ height : 1.5em; padding : 2px 5px; font-family : 'Open Sans', sans-serif; - color : #000000; - background : #EEEEEE; - border : 1px solid gray; + color : inherit; + background : #3B3B3B; + border : none; &:focus { outline : 1px solid #D3D3D3; } // `.range-input` if generic to all range inputs, or `#zoom-slider` if only for zoom slider @@ -141,7 +141,7 @@ // `.text-input` if generic to all range inputs, or `#page-input` if only for current page input &#page-input { - width : 4ch; + min-width : 5ch; margin-right : 1ch; text-align : center; }