/**
 * this helper helps manipulating DOM
 */ /**
 * when gitako is ready, make page's header narrower
 * or cancel it
 */ export function markGitakoReadyState ( ready ) { const readyClassName = 'gitako-ready' ; const classList = document . body . classList ; if ( ready ) classList . add ( readyClassName ) ; else classList . remove ( readyClassName ) ; } /**
 * if should show gitako, then move body right to make space for showing gitako
 * otherwise, hide the space
 */ export const bodySpacingClassName = 'with-gitako-spacing' ; export function setBodyIndent ( shouldShowGitako ) { if ( shouldShowGitako ) { document . body . classList . add ( bodySpacingClassName ) ; } else { document . body . classList . remove ( bodySpacingClassName ) ; } } export function $ ( selector ) ; export function $ < T1 > ( selector , existCallback ) ; export function $ < T1 , T2 > ( selector , existCallback , otherwise ) ; export function $ < T2 > ( selector , existCallback , otherwise ) ; export function $ ( selector , existCallback ? , otherwise ? ) { const element = document . querySelector ( selector ) ; if ( element ) { return existCallback ? existCallback ( element ) : element ; } return otherwise ? otherwise ( ) : null ; } /**
 * add the logo element into DOM
 */ export function insertLogoMountPoint ( ) { const logoID = 'gitako-logo-mount-point' ; const logoSelector = '#' + logoID ; return $ ( logoSelector , undefined , function createLogoMountPoint ( ) { const logoMountElement = document . createElement ( 'div' ) ; logoMountElement . setAttribute ( 'id' , logoID ) ; document . body . appendChild ( logoMountElement ) ; return logoMountElement ; } ) ; } /**
 * content above the file navigation bar is same for all pages of the repo
 * use this function to scroll down a bit to hide them
 */ export function scrollToRepoContent ( ) { const repositoryContentSelector = '.repository-content' ; $ ( repositoryContentSelector , repositoryContentElement => repositoryContentElement . scrollIntoView ( ) ) ; } /**
 * copy content of a DOM element to clipboard
 */ export function copyElementContent ( element , trimLeadingSpace ? ) { window . getSelection ( ) ?. removeAllRanges ( ) ; const range = document . createRange ( ) ; if ( trimLeadingSpace ) { let realWrapper = element ; while ( realWrapper ?. childElementCount === 1 ) realWrapper = realWrapper ?. firstElementChild ; if ( realWrapper ?. childElementCount && realWrapper . childElementCount > 1 ) { const first = realWrapper . firstElementChild ; const last = realWrapper . lastElementChild ; if ( first && last ) { range . selectNode ( first ) ; range . setEndAfter ( last ) ; } } } else { range . selectNode ( element ) ; } window . getSelection ( ) ?. addRange ( range ) ; const isCopySuccessful = document . execCommand ( 'copy' ) ; window . getSelection ( ) ?. removeAllRanges ( ) ; return isCopySuccessful ; } /**
 * focus to side bar, user will be able to manipulate it with keyboard
 */ export function focusFileExplorer ( ) { const sideBarContentSelector = '.gitako-side-bar .file-explorer' ; $ ( sideBarContentSelector , sideBarElement => { if ( sideBarElement instanceof HTMLElement ) sideBarElement . focus ( ) ; } ) ; } export function focusSearchInput ( ) { const searchInputSelector = '.search-input input' ; $ ( searchInputSelector , searchInputElement => { if ( document . activeElement !== searchInputElement && searchInputElement instanceof HTMLElement ) { searchInputElement . focus ( ) ; } } ) ; } export function findNodeElement ( node , rootElement ) { const nodeElement = rootElement . querySelector ( `a[href="${ node . url }"]` ) ; if ( nodeElement instanceof HTMLElement ) return nodeElement ; return null ; } export function setCSSVariable ( name , value , element ) { if ( value === undefined ) element . style . removeProperty ( name ) ; else element . style . setProperty ( name , value ) ; }