90 lines
2.2 KiB
TypeScript
90 lines
2.2 KiB
TypeScript
const setupMatchMedia = (): void => {
|
|
if (typeof window === 'undefined' || typeof window.matchMedia === 'function') {
|
|
return
|
|
}
|
|
|
|
window.matchMedia = ((query: string) => ({
|
|
matches: false,
|
|
media: query,
|
|
onchange: null,
|
|
addListener: () => {},
|
|
removeListener: () => {},
|
|
addEventListener: () => {},
|
|
removeEventListener: () => {},
|
|
dispatchEvent: () => false,
|
|
})) as typeof window.matchMedia
|
|
}
|
|
|
|
const setupAnimationFrame = (): void => {
|
|
if (typeof window === 'undefined') {
|
|
return
|
|
}
|
|
|
|
if (typeof window.requestAnimationFrame !== 'function') {
|
|
window.requestAnimationFrame = ((callback: FrameRequestCallback) => (
|
|
window.setTimeout(() => callback(Date.now()), 16)
|
|
)) as typeof window.requestAnimationFrame
|
|
}
|
|
|
|
if (typeof window.cancelAnimationFrame !== 'function') {
|
|
window.cancelAnimationFrame = ((handle: number) => {
|
|
window.clearTimeout(handle)
|
|
}) as typeof window.cancelAnimationFrame
|
|
}
|
|
}
|
|
|
|
const setupResizeObserver = (): void => {
|
|
if (typeof globalThis.ResizeObserver === 'function') {
|
|
return
|
|
}
|
|
|
|
class ResizeObserverStub {
|
|
observe(): void {}
|
|
unobserve(): void {}
|
|
disconnect(): void {}
|
|
}
|
|
|
|
globalThis.ResizeObserver = ResizeObserverStub as typeof ResizeObserver
|
|
}
|
|
|
|
const setupProgressEvent = (): void => {
|
|
if (typeof globalThis.ProgressEvent === 'function') {
|
|
return
|
|
}
|
|
|
|
class ProgressEventStub extends Event {
|
|
lengthComputable: boolean
|
|
loaded: number
|
|
total: number
|
|
|
|
constructor(type: string, init: ProgressEventInit = {}) {
|
|
super(type, init)
|
|
this.lengthComputable = init.lengthComputable ?? false
|
|
this.loaded = init.loaded ?? 0
|
|
this.total = init.total ?? 0
|
|
}
|
|
}
|
|
|
|
globalThis.ProgressEvent = ProgressEventStub as typeof ProgressEvent
|
|
}
|
|
|
|
const setupDomParser = (): void => {
|
|
if (typeof globalThis.DOMParser === 'function') {
|
|
return
|
|
}
|
|
|
|
if (typeof window !== 'undefined' && typeof window.DOMParser === 'function') {
|
|
globalThis.DOMParser = window.DOMParser
|
|
return
|
|
}
|
|
|
|
globalThis.DOMParser = new JSDOM('').window.DOMParser
|
|
}
|
|
|
|
setupMatchMedia()
|
|
setupAnimationFrame()
|
|
setupResizeObserver()
|
|
setupProgressEvent()
|
|
setupDomParser()
|
|
import { JSDOM } from 'jsdom'
|