sudodoki / copy-to-clipboard

Copy stuff into clipboard from your browser using JS
http://sudodoki.github.io/copy-to-clipboard/example/
MIT License
1.28k stars 132 forks source link

Seemed wrong preventDefault? #131

Open xiaomingTang opened 1 year ago

xiaomingTang commented 1 year ago

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);
  }
})