File size: 3,652 Bytes
07bbbbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * Iframe Height Fix Module
 * 
 * This module provides functions to initialize iframe content for proper height calculation
 * and navigation handling. Functions accept document and window as parameters to work
 * with the iframe's browsing context.
 */

/**
 * Calculate document height using multiple measurements
 */
function getDocHeight(doc: Document): number {
  let referenceHeight = 0;
  const bottomElement = doc.getElementById("__mdx_iframe_bottom_element");
  
  if (bottomElement) {
    referenceHeight = Math.round(bottomElement.getBoundingClientRect().bottom);
  }
  
  // Use multiple height measurements for accuracy
  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;
}


/**
 * Fix body height and notify parent
 */
function fixHeight(iframe: HTMLIFrameElement): void {
  iframe.style.height = getDocHeight(iframe.contentDocument as Document) + 16 + "px";
}

/**
 * Setup DOM change observation using MutationObserver or fallback
 */
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 {
      // Fallback for older browsers
      iframe.contentDocument?.addEventListener('DOMSubtreeModified', function() {
        console.debug("DOM change detected by DOMSubtreeModified event");
        fixHeight(iframe);
      } as EventListener);
    }
  }
}

/**
 * Setup ResizeObserver for body size changes
 * @deprecated Reserved for future use
 */
// @ts-ignore - Reserved for future use
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function _setupObserver(iframe: HTMLIFrameElement): void {
    // ResizeObserver for body size changes
    console.debug("Setting up ResizeObserver");
    const observer = new ResizeObserver(() => {
      console.debug("ResizeObserver triggered");
      fixHeight(iframe);
    });
    
    observer.observe(iframe.contentDocument?.body as Element);
    // Also observe document element for comprehensive coverage
    observer.observe(iframe.contentDocument?.documentElement as Element);
  }

/**
 * Initialize iframe content for height tracking and navigation
 * 
    * @param iframe - The iframe element
 */
export function fixHeightSetup(
  iframe: HTMLIFrameElement,
): void {
  console.debug("Initializing iframe fixHeight");
  
  // Immediate height calculation
  fixHeight(iframe);
  
  // Setup observers for dynamic content changes
  // Use shorter delay for better responsiveness
  setTimeout(function() {
    setupObserveDOMChange(iframe);
    //setupObserver(iframe);
    console.debug("All observers setup completed");
  }, 500);  
}