0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-05-07 16:38:38 +00:00

Check if Navigation API exists, use setInterval if not

This commit is contained in:
G.Ambatte
2026-04-12 18:24:41 +12:00
parent 077b88139a
commit 86f8898ee4
+19 -6
View File
@@ -135,6 +135,7 @@ const BrewRenderer = (props)=>{
const mainRef = useRef(null);
const pagesRef = useRef(null);
const urlRef = useRef('');
if(props.renderer == 'legacy') {
rawPages = props.text.split(PAGEBREAK_REGEX_LEGACY);
@@ -272,12 +273,24 @@ const BrewRenderer = (props)=>{
const frameDidMount = ()=>{ //This triggers when iFrame finishes internal "componentDidMount"
scrollToHash(window.location.hash);
navigation.addEventListener('navigate', (e)=>{
if(e.hashChange && e.destination.sameDocument){
const dest = e.destination.url.slice(e.destination.url.indexOf('#'));
scrollToHash(dest);
}
});
if(navigation) {
navigation.addEventListener('navigate', (e)=>{
if(e.hashChange && e.destination.sameDocument){
const dest = e.destination.url.slice(e.destination.url.indexOf('#'));
scrollToHash(dest);
}
});
} else {
urlRef.current = window.location.href;
setInterval(()=>{
if(window.location.href != urlRef.current){
urlRef.current = window.location.href;
const tmpURL = new URL(window.location.href);
const target = tmpURL.hash;
scrollToHash(target);
}
}, 1000);
};
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