mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-07 12:02:44 +00:00
refctor logic
This commit is contained in:
@@ -76,40 +76,44 @@ const BrewRenderer = (props)=>{
|
|||||||
}
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const locationHash = window.location.hash;
|
const locationHash = window.location.hash;
|
||||||
const iframe = document.getElementById('BrewRenderer');
|
const iframe = document.getElementById('BrewRenderer');
|
||||||
|
|
||||||
|
const pageIdRegex = /^#p\d+$/;
|
||||||
|
|
||||||
|
const handleIframeLoad = () => {
|
||||||
|
setTimeout(() => {
|
||||||
|
if (pageIdRegex.test(locationHash)) {
|
||||||
|
const pageNumber = parseInt(locationHash.slice(2));
|
||||||
|
scrollToPage(pageNumber - 1);
|
||||||
|
} else {
|
||||||
|
const elementId = locationHash.slice(1);
|
||||||
|
if (elementId) {
|
||||||
|
getPageContainingElement(elementId)
|
||||||
|
.then((pageNumber) => {
|
||||||
|
if (pageNumber !== -1) {
|
||||||
|
scrollToPage(pageNumber);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error('Error:', error);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 100);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (locationHash) {
|
||||||
|
iframe.addEventListener('load', handleIframeLoad);
|
||||||
|
}
|
||||||
|
|
||||||
// Regular expression to match page IDs like '#p1'
|
return () => {
|
||||||
const pageIdRegex = /^#p\d+$/;
|
if (locationHash) {
|
||||||
iframe.addEventListener('load', () => {
|
iframe.removeEventListener('load', handleIframeLoad);
|
||||||
setTimeout(() => {
|
}
|
||||||
if (pageIdRegex.test(locationHash)) {
|
window.removeEventListener('resize', updateSize);
|
||||||
// Extract page number from the ID
|
};
|
||||||
const pageNumber = parseInt(locationHash.slice(2));
|
}, []);
|
||||||
scrollToPage(pageNumber-1);
|
|
||||||
} else {
|
|
||||||
// Treat it as an element ID
|
|
||||||
const elementId = locationHash.slice(1); // Remove the leading '#'
|
|
||||||
if (elementId) {
|
|
||||||
getPageContainingElement(elementId)
|
|
||||||
.then((pageNumber) => {
|
|
||||||
if (pageNumber !== -1) {
|
|
||||||
scrollToPage(pageNumber);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error('Error:', error);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 100);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Cleanup function for removing the resize event listener
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener('resize', updateSize);
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
|
|
||||||
const scrollToPage = (pageNumber) => {
|
const scrollToPage = (pageNumber) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user