sudodoki/copy-to-clipboard

Seemed wrong preventDefault?

xiaomingTang opened this issue · 0 comments

https://github.com/sudodoki/copy-to-clipboard/blob/main/index.js#L69

If set options.onCopy, and NOT set options.format, copy will failed.

import copy from 'copy-to-clipboard'

copy('text', {
  // do NOT set format
  onCopy: () => {
    // onCopy callback will be triggered
    // but WONT copy correctly
  },
})

because:

mark.addEventListener('copy', function (e) {
  if (options.format) {
    // the preventDefault() is ok
    e.preventDefault()
    // do something...
    // re-set the clipboard data here
    clipboard.setData(options.format, text)
  }

  if (options.onCopy) {
    // the preventDefault() prevent the default copy behavior
    // it cause a bug
    // the preventDefault() should be removed.
    e.preventDefault()
    options.onCopy(e.clipboardData);
  }
})