-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathwindow.ts
124 lines (106 loc) · 3.37 KB
/
window.ts
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
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Channel } from './channel'
import { WindowAPI } from './types/window.types'
export default function createWindow(
currentGlobal: typeof globalThis,
channel: Channel,
): WindowAPI {
const { document, MutationObserver, ResizeObserver } = currentGlobal
let oldHeight: number
let isAutoResizing = false
let checkAbsoluteElements = false
const absolutePositionedElems: Set<Element> = new Set()
const mutationObserver = new MutationObserver((mutations: Array<MutationRecord>) => {
checkAbsolutePositionedElems(mutations)
if (isAutoResizing) {
self.updateHeight()
}
})
const resizeObserver = new ResizeObserver(() => {
self.updateHeight()
})
mutationObserver.observe(document.body, {
attributes: true,
childList: true,
subtree: true,
characterData: true,
})
const self = { startAutoResizer, stopAutoResizer, updateHeight }
return self
function checkAbsoluteElementStyle(type: MutationRecordType, element: Element) {
const computedStyle = getComputedStyle(element)
if (computedStyle.position !== 'absolute') {
return false
}
switch (type) {
case 'attributes':
return computedStyle.display !== 'none'
default:
return true
}
}
function checkAbsolutePositionedElems(mutations: Array<MutationRecord>) {
mutations.forEach((mutation) => {
switch (mutation.type) {
case 'attributes':
if (mutation.target.nodeType === Node.ELEMENT_NODE) {
const element = mutation.target as Element
if (checkAbsoluteElementStyle(mutation.type, element)) {
absolutePositionedElems.add(element)
} else {
absolutePositionedElems.delete(element)
}
}
break
case 'childList':
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node as Element
if (checkAbsoluteElementStyle(mutation.type, element)) {
absolutePositionedElems.add(element)
}
}
})
mutation.removedNodes.forEach((node) => {
const element = node as Element
absolutePositionedElems.delete(element)
})
break
}
})
}
function startAutoResizer({ absoluteElements = false } = {}) {
checkAbsoluteElements = Boolean(absoluteElements)
self.updateHeight()
if (isAutoResizing) {
return
}
isAutoResizing = true
resizeObserver.observe(document.body)
}
function stopAutoResizer() {
if (!isAutoResizing) {
return
}
isAutoResizing = false
resizeObserver.disconnect()
}
function updateHeight(height: number | null = null) {
if (height === null) {
const documentHeight = Math.ceil(document.documentElement.getBoundingClientRect().height)
// Only check for absolute elements if option is provided to startAutoResizer
if (checkAbsoluteElements && absolutePositionedElems.size) {
let maxHeight = documentHeight
absolutePositionedElems.forEach((element) => {
maxHeight = Math.max(element.getBoundingClientRect().bottom, maxHeight)
})
height = maxHeight
} else {
height = documentHeight
}
}
if (height !== oldHeight) {
channel.send('setHeight', height)
oldHeight = height
}
}
}