0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-16 19:02:40 +00:00

testing mutation Observer, don't review this yet

This commit is contained in:
Víctor Losada Hernández
2024-10-09 21:33:26 +02:00
parent e396a51ad5
commit 6456c22c61

View File

@@ -16,8 +16,8 @@ const Frame = require('react-frame-component').default;
const dedent = require('dedent-tabs').default; const dedent = require('dedent-tabs').default;
const { printCurrentBrew } = require('../../../shared/helpers.js'); const { printCurrentBrew } = require('../../../shared/helpers.js');
const DOMPurify = require('dompurify'); //const DOMPurify = require('dompurify');
const purifyConfig = { FORCE_BODY: true, SANITIZE_DOM: false }; //const purifyConfig = { FORCE_BODY: true, SANITIZE_DOM: false };
const PAGE_HEIGHT = 1056; const PAGE_HEIGHT = 1056;
@@ -77,17 +77,42 @@ const BrewRenderer = (props)=>{
rawPages = props.text.split(/^\\page$/gm); rawPages = props.text.split(/^\\page$/gm);
} }
useEffect(() => { useEffect(()=>{
const iframeDoc = document.getElementById('brewRenderer');
const hash = window.location.hash;
console.log(hash);
if(hash && iframeDoc) {
const anchor = iframeDoc.querySelector(hash);
if(anchor) {
anchor.scrollIntoView();
} else {
// Use MutationObserver to wait for the element if it's not immediately available
const observer = new MutationObserver((mutations, obs)=>{
const anchorElement = iframeDoc.querySelector(hash);
if(anchorElement) {
anchorElement.scrollIntoView();
obs.disconnect(); // Stop observing once the element is found
}
});
observer.observe(iframeDoc, {
childList : true,
subtree : true,
});
}
}
/*
const elementId = window.location.hash.slice(1); const elementId = window.location.hash.slice(1);
if (elementId && state.isMounted) { if(elementId && state.isMounted) {
const element = document.getElementById(elementId); const element = document.getElementById(elementId);
element.scrollIntoView({ block: 'start' }); element.scrollIntoView({ block: 'start' });
} }
*/
return () => { return ()=>{
window.removeEventListener('resize', updateSize); window.removeEventListener('resize', updateSize);
}; };
}, [state.isMounted]); }, []);
const updateSize = ()=>{ const updateSize = ()=>{
setState((prevState)=>({ setState((prevState)=>({