|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getDocHeight(doc: Document): number { |
|
|
let referenceHeight = 0; |
|
|
const bottomElement = doc.getElementById("__mdx_iframe_bottom_element"); |
|
|
|
|
|
if (bottomElement) { |
|
|
referenceHeight = Math.round(bottomElement.getBoundingClientRect().bottom); |
|
|
} |
|
|
|
|
|
|
|
|
const scrollHeight = doc.body.scrollHeight; |
|
|
const offsetHeight = doc.body.offsetHeight; |
|
|
const clientHeight = doc.body.clientHeight; |
|
|
const documentHeight = doc.documentElement.scrollHeight; |
|
|
|
|
|
console.debug( |
|
|
`scrollHeight:${scrollHeight} offsetHeight:${offsetHeight} clientHeight:${clientHeight} documentHeight:${documentHeight} referenceHeight:${referenceHeight}` |
|
|
); |
|
|
let finalHeight = referenceHeight; |
|
|
if (referenceHeight < 16) { |
|
|
finalHeight = Math.max(referenceHeight, documentHeight, scrollHeight); |
|
|
} |
|
|
return finalHeight; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function fixHeight(iframe: HTMLIFrameElement): void { |
|
|
iframe.style.height = getDocHeight(iframe.contentDocument as Document) + 16 + "px"; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setupObserveDOMChange(iframe: HTMLIFrameElement): void { |
|
|
console.debug("Setup observer"); |
|
|
if (iframe && iframe.contentDocument && iframe.contentDocument.body) { |
|
|
if (typeof MutationObserver !== 'undefined') { |
|
|
let mutationsObserver = new MutationObserver(function(_mutations: MutationRecord[], _observer: MutationObserver) { |
|
|
console.debug("DOM change detected by MutationObserver"); |
|
|
fixHeight(iframe); |
|
|
}); |
|
|
let mutationObserverOption = { |
|
|
attributes: true, |
|
|
childList: true, |
|
|
characterData: true, |
|
|
subtree: true |
|
|
}; |
|
|
mutationsObserver.observe(iframe.contentDocument.body, mutationObserverOption); |
|
|
} else { |
|
|
|
|
|
iframe.contentDocument?.addEventListener('DOMSubtreeModified', function() { |
|
|
console.debug("DOM change detected by DOMSubtreeModified event"); |
|
|
fixHeight(iframe); |
|
|
} as EventListener); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _setupObserver(iframe: HTMLIFrameElement): void { |
|
|
|
|
|
console.debug("Setting up ResizeObserver"); |
|
|
const observer = new ResizeObserver(() => { |
|
|
console.debug("ResizeObserver triggered"); |
|
|
fixHeight(iframe); |
|
|
}); |
|
|
|
|
|
observer.observe(iframe.contentDocument?.body as Element); |
|
|
|
|
|
observer.observe(iframe.contentDocument?.documentElement as Element); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function fixHeightSetup( |
|
|
iframe: HTMLIFrameElement, |
|
|
): void { |
|
|
console.debug("Initializing iframe fixHeight"); |
|
|
|
|
|
|
|
|
fixHeight(iframe); |
|
|
|
|
|
|
|
|
|
|
|
setTimeout(function() { |
|
|
setupObserveDOMChange(iframe); |
|
|
|
|
|
console.debug("All observers setup completed"); |
|
|
}, 500); |
|
|
} |