internal import "array-findindex-polyfill" ; import { inspect } from "./util/inspect" ; /**
 * Viewport breakpoint
 */ export interface ViewportBreakpoint { name : string ; size : { width : number ; height : number ; } ; } /**
 * Viewport configuration
 */ export interface ViewportConfiguration { context : string ; breakpoints : ViewportBreakpoint [ ] ; } /**
 * Viewport callback
 */ export type ViewportCallback < T > = ( breakpoint : string ) => T ; internal declare global { interface Window { HTMLIFrameElement : typeof HTMLIFrameElement ; } } param breakpoints param first param last return export function range ( breakpoints , first , last = first ) { const [ from , to ] = [ first , last ] . map ( name => { const index = breakpoints . findIndex ( breakpoint => breakpoint . name === name ) ; if ( index === - 1 ) throw new ReferenceError ( `Invalid breakpoint: ${ inspect ( name ) }` ) ; return index ; } ) ; return breakpoints . slice ( from , to + 1 ) ; } param value return function isPromise ( value ) { return Promise . resolve ( value ) === value ; } export class Viewport { /**
     * Viewport configuration
     */ public config : Readonly < ViewportConfiguration > ; /**
     * Viewport context
     */ public context : HTMLIFrameElement ; constructor param config param parent public constructor ( config , parent ) { let current = parent ; let context = parent . document . querySelector ( config . context ) ; while ( ! context && current !== current . parent ) { current = current . parent ; context = current . document . querySelector ( config . context ) ; } if ( ! ( context instanceof current . HTMLIFrameElement ) ) throw new ReferenceError ( `No match for selector: ${ inspect ( config . context ) }` ) ; this . config = config ; this . context = context ; } param url return public load ( url , cb ? ) { return new Promise < void > ( resolve => { const load = ( ) => { this . context . removeEventListener ( "load" , load ) ; if ( cb ) cb ( ) ; resolve ( ) ; } ; this . context . addEventListener ( "load" , load ) ; this . context . src = url ; } ) ; } param x param y public offset ( x , y = 0 ) { this . context . contentWindow ! . scrollTo ( x , y ) ; } param widthOrBreakpoint param height public set ( width , height ? ) ; public set ( breakpoint ) ; public set ( widthOrBreakpoint , height ? ) { if ( typeof widthOrBreakpoint === "string" ) { const [ breakpoint ] = range ( this . config . breakpoints , widthOrBreakpoint ) ; return this . set ( breakpoint . size . width , breakpoint . size . height ) ; } else { const width = widthOrBreakpoint ; if ( typeof width !== "number" || width <= 0 ) throw new TypeError ( `Invalid breakpoint width: ${ width }` ) ; if ( height && ( typeof height !== "number" || height <= 0 ) ) throw new TypeError ( `Invalid breakpoint height: ${ height }` ) ; this . context . style . width = `${ width }px` ; if ( height ) this . context . style . height = `${ height }px` ; } this . context . contentDocument ! . body . getBoundingClientRect ( ) ; } /**
     * Reset viewport
     */ public reset ( ) { this . context . contentWindow ! . scrollTo ( 0 , 0 ) ; this . context . style . width = "" ; this . context . style . height = "" ; this . context . contentDocument ! . body . getBoundingClientRect ( ) ; } example param first param last param cb return public between < T extends Promise < any > > ( first , last , cb ) ; public between < T > ( first , last , cb ) ; public between < T > ( first , last , cb ) { const [ initial , ... rest ] = range ( this . config . breakpoints , first , last ) ; const invoke = ( breakpoint ) => { this . set ( breakpoint . size . width , breakpoint . size . height ) ; return cb ( breakpoint . name ) ; } ; const result = invoke ( initial ) ; if ( isPromise ( result ) ) return rest . reduce ( ( promise , breakpoint ) => { return promise . then ( ( ) => breakpoint ) . then ( invoke ) ; } , result ) . then ( ( ) => this . reset ( ) ) ; rest . forEach ( invoke ) ; this . reset ( ) ; } example param cb return public each < T extends Promise < any > > ( cb ) ; public each < T > ( cb ) ; public each < T > ( cb ) { return this . between ( this . config . breakpoints [ 0 ] . name , this . config . breakpoints [ this . config . breakpoints . length - 1 ] . name , cb ) ; } example param first param cb return public from < T extends Promise < any > > ( first , cb ) ; public from < T > ( first , cb ) ; public from < T > ( first , cb ) { return this . between ( first , this . config . breakpoints [ this . config . breakpoints . length - 1 ] . name , cb ) ; } example param last param cb return public to < T extends Promise < any > > ( last , cb ) ; public to < T > ( last , cb ) ; public to < T > ( last , cb ) { return this . between ( this . config . breakpoints [ 0 ] . name , last , cb ) ; } }