0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-10 15:42:39 +00:00

refctor logic

This commit is contained in:
Víctor Losada Hernández
2024-09-07 19:12:59 +02:00
parent 9e1a532105
commit 3360b4e829

View File

@@ -79,17 +79,15 @@ const BrewRenderer = (props)=>{
const locationHash = window.location.hash; const locationHash = window.location.hash;
const iframe = document.getElementById('BrewRenderer'); const iframe = document.getElementById('BrewRenderer');
// Regular expression to match page IDs like '#p1'
const pageIdRegex = /^#p\d+$/; const pageIdRegex = /^#p\d+$/;
iframe.addEventListener('load', () => {
const handleIframeLoad = () => {
setTimeout(() => { setTimeout(() => {
if (pageIdRegex.test(locationHash)) { if (pageIdRegex.test(locationHash)) {
// Extract page number from the ID
const pageNumber = parseInt(locationHash.slice(2)); const pageNumber = parseInt(locationHash.slice(2));
scrollToPage(pageNumber - 1); scrollToPage(pageNumber - 1);
} else { } else {
// Treat it as an element ID const elementId = locationHash.slice(1);
const elementId = locationHash.slice(1); // Remove the leading '#'
if (elementId) { if (elementId) {
getPageContainingElement(elementId) getPageContainingElement(elementId)
.then((pageNumber) => { .then((pageNumber) => {
@@ -103,10 +101,16 @@ const BrewRenderer = (props)=>{
} }
} }
}, 100); }, 100);
}); };
if (locationHash) {
iframe.addEventListener('load', handleIframeLoad);
}
// Cleanup function for removing the resize event listener
return () => { return () => {
if (locationHash) {
iframe.removeEventListener('load', handleIframeLoad);
}
window.removeEventListener('resize', updateSize); window.removeEventListener('resize', updateSize);
}; };
}, []); }, []);