type LocationLike = Pick < Location , 'protocol' | 'host' | 'pathname' | 'hash' | 'search' > ; type QuerySetter = ( ... args : Parameters < History [ 'pushState' ] > ) => void ; const body = document . body ; const replacer = ( ... args ) => history . replaceState ( ... args ) ; export const ViewModeNormal = '' ; export const ViewModePresenter = 'presenter' ; export const ViewModeNext = 'next' ; export const viewModes = [ ViewModeNormal , ViewModePresenter , ViewModeNext , ] as const ; export const classPrefix = 'bespoke-marp-' ; export const dataAttrPrefix = `data-${ classPrefix }` as const ; export const generateURLfromParams = ( params , { protocol , host , pathname , hash } = location ) => { const q = params . toString ( ) ; return `${ protocol }//${ host }${ pathname }${ q ? '?' : '' }${ q }${ hash }` ; } ; export const getViewMode = ( ) => body . dataset . bespokeView as typeof viewModes [ number ] ; export const readQuery = ( name ) => new URLSearchParams ( location . search ) . get ( name ) ; export const popQuery = ( name ) => { const value = readQuery ( name ) ; setQuery ( { [ name ] : undefined } ) ; return value ; } ; export const setQuery = ( queries , opts = { } ) => { const options = { location , setter : replacer , ... opts } ; const params = new URLSearchParams ( options . location . search ) ; for ( const k of Object . keys ( queries ) ) { const value = queries [ k ] ; if ( typeof value === 'string' ) { params . set ( k , value ) ; } else { params . delete ( k ) ; } } try { options . setter ( { ... ( window . history . state ?? { } ) } , '' , generateURLfromParams ( params , options . location ) ) ; } catch ( e ) { console . error ( e ) ; } } ; export const setHistoryState = ( state ) => setQuery ( { } , { setter : ( s , ... r ) => replacer ( { ... s , ... state } , ... r ) } ) ; export const setViewMode = ( ) => { const view = readQuery ( 'view' ) ; body . dataset . bespokeView = view === ViewModeNext || view === ViewModePresenter ? view : ViewModeNormal ; } ; export const storage = ( ( ) => { const available = ( ( ) => { const key = 'bespoke-marp' as const ; try { localStorage . setItem ( key , key ) ; localStorage . removeItem ( key ) ; return true ; } catch ( e ) { return false ; } } ) ( ) ; return { available , get : ( key ) : string | null => { try { return localStorage . getItem ( key ) ; } catch ( e ) { return null ; } } , set : ( key , value ) : boolean => { try { localStorage . setItem ( key , value ) ; return true ; } catch ( e ) { return false ; } } , remove : ( key ) : boolean => { try { localStorage . removeItem ( key ) ; return true ; } catch ( e ) { return false ; } } , } ; } ) ( ) ; export const toggleAriaHidden = ( element , value ) => { const key = 'aria-hidden' as const ; if ( value ) { element . setAttribute ( key , 'true' ) ; } else { element . removeAttribute ( key ) ; } } ;