All files dom.ts

100% Statements 120/120
100% Branches 2/2
100% Functions 10/10
100% Lines 120/120

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 1211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * @description Parsing the parameters attached to the url as objects.
 * @param url
 */
export function getUrlSearchParams(url = location.href) {
  const urlP = new URL(url)
  return Object.fromEntries(new URLSearchParams(urlP.search))
}
 
/* c8 ignore start */
interface ObserveElementP {
  /** @default The whole viewport */
  containerE?: Element
  observeE: Element | Element[]
  // eslint-disable-next-line @typescript-eslint/ban-types,no-unused-vars
  doSomeWhenIn?: (entry?: IntersectionObserverEntry) => void
  // eslint-disable-next-line no-unused-vars
  doSomeWhenOut?: (entry?: IntersectionObserverEntry) => void
  /** @default "0px 0px 0px 0px" */
  rootMargin?: string
}
 
/**
 * @description  'observeE' is in 'containerE', and 'containerE' is generally scrollable. 'containerE' can intersection observe the 'observeE' , when observeE come in(or out) containerE, you can do something by using 'doSomeWhenIn'.
 * @param observeElementP
 * @link https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver
 */
export function intersectionObserverElement(observeElementP: ObserveElementP) {
  const { containerE, observeE, doSomeWhenIn, doSomeWhenOut, rootMargin } = observeElementP
  if (!observeE) {
    console.log('ObserveElement must be Element type.')
    return
  }
  const callBack: IntersectionObserverCallback = (entries) => {
    entries.forEach((item) => {
      if (item.isIntersecting) {
        doSomeWhenIn && doSomeWhenIn(item)
      } else {
        doSomeWhenOut && doSomeWhenOut(item)
      }
    })
  }
  const Observer = new IntersectionObserver(callBack, { root: containerE, rootMargin })
  if (Array.isArray(observeE)) {
    observeE.forEach((e) => {
      Observer.observe(e)
    })
  } else {
    Observer.observe(observeE)
  }
}
 
interface DownloadFileP {
  file?: Blob | File
  fileUrl?: string
  fileName: string
}
 
/**
 * @description Single file download. notice:You must select one between 'file' and 'fileUrl'. Notice: when you use fileUrl to download, you should has 'a common origin', otherwise the filename setting will not take effect.
 * @param data
 */
export function downloadFile(data: DownloadFileP) {
  const { file, fileUrl, fileName } = data
  const a = document.createElement('a')
  a.style.display = 'none'
  a.download = fileName
  if (file && fileUrl) {
    throw "You must select one between 'file' and 'fileUrl' as incoming parameters"
  } else if (fileUrl) {
    a.href = fileUrl
  } else if (file) {
    a.href = URL.createObjectURL(file)
  } else {
    throw 'Can not find the file data.'
  }
  document.body.appendChild(a)
  a.click()
  URL.revokeObjectURL(a.href)
  document.body.removeChild(a)
}
 
/**
 * @description More responsive audio playback. And just use js code.
 * @param audioUrl
 */
export function playAudio(audioUrl: string): Promise<AudioBufferSourceNode> {
  return new Promise((resolve, reject) => {
    const audioCtx = new AudioContext()
    fetch(audioUrl)
      .then((res) => res.arrayBuffer())
      .then((arraybuffer) => audioCtx.decodeAudioData(arraybuffer))
      .then((audioBuffer) => {
        const audioBufferSourceNode = audioCtx.createBufferSource()
        audioBufferSourceNode.buffer = audioBuffer
        audioBufferSourceNode.connect(audioCtx.destination)
        resolve(audioBufferSourceNode)
      })
      .catch((err) => {
        reject(err)
      })
  })
}
 
/**
 * @description One-click copy.
 * @param text
 * @param preserveFormatting
 */
export function fastCopy(text: string, preserveFormatting = false) {
  const element = preserveFormatting ? 'textarea' : 'input'
  const input = document.createElement(element)
  input.value = text
  input.setAttribute('readOnly', 'true')
  document.body.appendChild(input)
  input.select()
  document.execCommand('copy')
  document.body.removeChild(input)
}
/* c8 ignore stop */