0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-16 16:52:39 +00:00

Call scrollToHash from our existing "frameDidMount`

`frameDidMount` is equivalent to using iframe.addEventListener('load');

Let's not add a new listener and just use the existing event we already have. Functionality still works.
This commit is contained in:
Trevor Buckner
2024-10-22 13:03:12 -04:00
parent 8ef88a4799
commit 5b136f651c

View File

@@ -77,11 +77,8 @@ const BrewRenderer = (props)=>{
rawPages = props.text.split(/^\\page$/gm); rawPages = props.text.split(/^\\page$/gm);
} }
useEffect(() => { const scrollToHash = (hash) => {
const iframe = document.getElementById('BrewRenderer'); const iframe = document.getElementById('BrewRenderer');
const hash = window.location.hash;
const scrollToHash = () => {
const iframeDoc = iframe?.contentDocument || iframe?.contentWindow?.document; const iframeDoc = iframe?.contentDocument || iframe?.contentWindow?.document;
let anchor = iframeDoc.querySelector(hash); let anchor = iframeDoc.querySelector(hash);
@@ -106,20 +103,6 @@ const BrewRenderer = (props)=>{
} }
}; };
if (hash) {
iframe.addEventListener('load', scrollToHash);
if (iframe.contentDocument?.readyState === 'complete') {
scrollToHash();
}
}
return ()=>{
iframe.removeEventListener('load', scrollToHash);
window.removeEventListener('resize', updateSize);
};
}, []);
const updateCurrentPage = useCallback(_.throttle((e)=>{ const updateCurrentPage = useCallback(_.throttle((e)=>{
const { scrollTop, clientHeight, scrollHeight } = e.target; const { scrollTop, clientHeight, scrollHeight } = e.target;
const totalScrollableHeight = scrollHeight - clientHeight; const totalScrollableHeight = scrollHeight - clientHeight;
@@ -193,6 +176,8 @@ const BrewRenderer = (props)=>{
}; };
const frameDidMount = ()=>{ //This triggers when iFrame finishes internal "componentDidMount" const frameDidMount = ()=>{ //This triggers when iFrame finishes internal "componentDidMount"
scrollToHash(window.location.hash);
setTimeout(()=>{ //We still see a flicker where the style isn't applied yet, so wait 100ms before showing iFrame setTimeout(()=>{ //We still see a flicker where the style isn't applied yet, so wait 100ms before showing iFrame
renderPages(); //Make sure page is renderable before showing renderPages(); //Make sure page is renderable before showing
setState((prevState)=>({ setState((prevState)=>({