import moment from 'moment' ; import marked from 'marked' ; import Prism from 'prismjs' ; import { parse } from 'qs' ; import pathRegexp from 'path-to-regexp' ; import { Route } from '@/models/connect' ; import { ResponseResultType } from '@/models/I' ; export const getPageQuery = ( ) => parse ( window . location . href . split ( '?' ) [ 1 ] ) ; param router param pathname export const getAuthorityFromRouter = < T extends Route > ( router = [ ] , pathname ) : T | undefined => { const authority = router . find ( ( { routes , path = '/' , target = '_self' } ) => ( path && target !== '_blank' && pathRegexp ( path ) . exec ( pathname ) ) || ( routes && getAuthorityFromRouter ( routes , pathname ) ) ) ; if ( authority ) return authority ; return undefined ; } ; export function diffForHumans ( time ? ) { const mtime = moment ( time ) ; const now = moment ( ) ; if ( now . diff ( mtime , 'day' ) > 15 ) { return mtime . year ( ) === now . year ( ) ? mtime . format ( 'MM-DD HH:ss' ) : mtime . format ( 'YYYY-MM-DD HH:ss' ) ; } return mtime . fromNow ( ) ; } export function friendlyNumbers ( n ) { if ( n >= 1000 ) { return `${ Math . floor ( n / 1000 ) }k` ; } return String ( n ) ; } export function rendererLink ( href , title , text ) { let url = href ; let target = false ; if ( url . slice ( 0 , 1 ) !== '#' ) { const urlParams = new URL ( href , window . location . origin ) ; url = urlParams . href ; target = urlParams . host !== window . location . host ? '_blank' : false ; } if ( ! url ) { return text ; } let out = `<a href="${ url }"` ; if ( title ) { out += ` title="${ title }"` ; } if ( target !== false ) { out += ` target="${ target }"` ; } out += `>${ text }</a>` ; return out ; } export function rendererParagraph ( text ) { const regExp = /(^| +)@(?!_)(?!.*?_$)(?<username>[a-zA-Z0-9_\u4e00-\u9fa5]{1,10})( +|$)/g ; return `<p>${ text . replace ( regExp , '$1<a href="/$2">@$2</a>$3' ) }</p>` ; } export function getDefaultMarkedOptions ( ) { const renderer = new marked . Renderer ( ) ; renderer . link = rendererLink ; renderer . paragraph = rendererParagraph ; return { renderer , headerIds : false , gfm : true , breaks : true , highlight ( code , lang ) { if ( lang ) { const language = lang . toLowerCase ( ) ; const grammar = Prism . languages [ language ] ; if ( grammar ) { return Prism . highlight ( code , grammar , language ) ; } } return code ; } , } ; } export function resetMarkedOptions ( ) { marked . setOptions ( getDefaultMarkedOptions ( ) ) ; } export function getPositions ( dom ) { let left = dom . offsetLeft , top = dom . offsetTop + dom . scrollTop , current = dom . offsetParent ; while ( current !== null ) { left += current . offsetLeft ; top += current . offsetTop ; current = current . offsetParent ; } return { left : left , top : top } ; } export function isParentElement ( childElement , parentElement ) { const pEls = ! Array . isArray ( parentElement ) ? [ parentElement ] : parentElement ; while ( childElement . tagName . toUpperCase ( ) !== 'BODY' ) { if ( pEls . some ( ( el ) => childElement == el ) ) { return true ; } childElement = childElement . parentNode ; } return false ; } export function insertText ( texteara , str ) { if ( document . selection ) { const sel = document . selection . createRange ( ) ; sel . text = str ; } else if ( typeof texteara . selectionStart === 'number' && typeof texteara . selectionEnd === 'number' ) { const startPos = texteara . selectionStart ; const endPos = texteara . selectionEnd ; let cursorPos = startPos ; const tmpStr = texteara . value ; texteara . value = tmpStr . substring ( 0 , startPos ) + str + tmpStr . substring ( endPos , tmpStr . length ) ; cursorPos += str . length ; texteara . selectionStart = texteara . selectionEnd = cursorPos ; } else { texteara . value += str ; } return texteara . value ; } export function moveEnd ( texteara ) { texteara . focus ( ) ; const len = texteara . value . length ; if ( document . selection ) { const sel = texteara . createTextRange ( ) ; sel . moveStart ( 'character' , len ) ; sel . collapse ( ) ; sel . select ( ) ; } else if ( typeof texteara . selectionStart == 'number' && typeof texteara . selectionEnd == 'number' ) { texteara . selectionStart = texteara . selectionEnd = len ; } } export function scrollToAnchor ( id ) { const dom = document . getElementById ( id ) ; dom ?. scrollIntoView ( { behavior : 'smooth' , block : 'start' , inline : 'start' , } ) ; } export function umiformatPaginationResult ( res ) { return { list : res . data , total : res . meta ! . total ! , meta : res . meta ! , } ; }