All files / utils copyTextToClipboard.ts

100% Statements 21/21
100% Branches 4/4
100% Functions 1/1
100% Lines 21/21

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                                      2x   2x     2x     2x 2x 2x 2x   2x 1x     2x     2x       2x 2x     2x 2x   2x 2x 2x     2x     2x         2x    
export interface CopyTextToClipboardOptions {
  /**
   * 复制容器的类名。
   */
  containerClass?: string
}
 
/**
 * 复制文本到剪切板。
 *
 * @param text 要复制的文本
 * @param options 选项
 * @returns 返回是否复制成功
 */
export function copyTextToClipboard(
  text: string,
  options?: CopyTextToClipboardOptions,
): boolean {
  // https://github.com/sindresorhus/copy-text-to-clipboard/blob/master/index.js
  const element = document.createElement('textarea')
 
  element.value = text
 
  // Prevent keyboard from showing on mobile
  element.setAttribute('readonly', '')
 
  // @ts-ignore
  element.style.contain = 'strict'
  element.style.position = 'absolute'
  element.style.left = '-9999px'
  element.style.fontSize = '12pt' // Prevent zooming on iOS
 
  if (options?.containerClass) {
    element.className = options.containerClass
  }
 
  const selection = document.getSelection()!
  let originalRange: Range | undefined
  /* istanbul ignore if */
  if (selection.rangeCount > 0) {
    originalRange = selection.getRangeAt(0)
  }
 
  document.body.appendChild(element)
  element.select()
 
  // Explicit selection workaround for iOS
  element.selectionStart = 0
  element.selectionEnd = text.length
 
  let isSuccess = false
  try {
    isSuccess = document.execCommand('copy')
  } catch (_) {}
 
  document.body.removeChild(element)
 
  /* istanbul ignore if */
  if (originalRange) {
    selection.removeAllRanges()
    selection.addRange(originalRange)
  }
 
  return isSuccess
}