|
| 1 | +import deselectCurrent from './toggle-selection'; |
| 2 | + |
| 3 | +interface Options { |
| 4 | + debug?: boolean; |
| 5 | + message?: string; |
| 6 | + format?: string; // MIME type |
| 7 | + onCopy?: (clipboardData: object) => void; |
| 8 | +} |
| 9 | + |
| 10 | +const clipboardToIE11Formatting = { |
| 11 | + 'text/plain': 'Text', |
| 12 | + 'text/html': 'Url', |
| 13 | + default: 'Text', |
| 14 | +}; |
| 15 | + |
| 16 | +const defaultMessage = 'Copy to clipboard: #{key}, Enter'; |
| 17 | + |
| 18 | +function format(message: string) { |
| 19 | + const copyKey = (/mac os x/i.test(navigator.userAgent) ? '⌘' : 'Ctrl') + '+C'; |
| 20 | + return message.replace(/#{\s*key\s*}/g, copyKey); |
| 21 | +} |
| 22 | + |
| 23 | +function copy(text: string, options?: Options): boolean { |
| 24 | + let message, |
| 25 | + reselectPrevious, |
| 26 | + range, |
| 27 | + selection, |
| 28 | + mark, |
| 29 | + success = false; |
| 30 | + if (!options) { |
| 31 | + options = {}; |
| 32 | + } |
| 33 | + const debug = options.debug || false; |
| 34 | + try { |
| 35 | + reselectPrevious = deselectCurrent(); |
| 36 | + |
| 37 | + range = document.createRange(); |
| 38 | + selection = document.getSelection(); |
| 39 | + |
| 40 | + mark = document.createElement('span'); |
| 41 | + mark.textContent = text; |
| 42 | + // reset user styles for span element |
| 43 | + mark.style.all = 'unset'; |
| 44 | + // prevents scrolling to the end of the page |
| 45 | + mark.style.position = 'fixed'; |
| 46 | + mark.style.top = 0; |
| 47 | + mark.style.clip = 'rect(0, 0, 0, 0)'; |
| 48 | + // used to preserve spaces and line breaks |
| 49 | + mark.style.whiteSpace = 'pre'; |
| 50 | + // do not inherit user-select (it may be `none`) |
| 51 | + mark.style.webkitUserSelect = 'text'; |
| 52 | + mark.style.MozUserSelect = 'text'; |
| 53 | + mark.style.msUserSelect = 'text'; |
| 54 | + mark.style.userSelect = 'text'; |
| 55 | + mark.addEventListener('copy', function(e) { |
| 56 | + e.stopPropagation(); |
| 57 | + if (options.format) { |
| 58 | + e.preventDefault(); |
| 59 | + if (typeof e.clipboardData === 'undefined') { |
| 60 | + // IE 11 |
| 61 | + debug && console.warn('unable to use e.clipboardData'); |
| 62 | + debug && console.warn('trying IE specific stuff'); |
| 63 | + (window as any).clipboardData.clearData(); |
| 64 | + const format = |
| 65 | + clipboardToIE11Formatting[options.format] || clipboardToIE11Formatting['default']; |
| 66 | + (window as any).clipboardData.setData(format, text); |
| 67 | + } else { |
| 68 | + // all other browsers |
| 69 | + e.clipboardData.clearData(); |
| 70 | + e.clipboardData.setData(options.format, text); |
| 71 | + } |
| 72 | + } |
| 73 | + if (options.onCopy) { |
| 74 | + e.preventDefault(); |
| 75 | + options.onCopy(e.clipboardData); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + document.body.appendChild(mark); |
| 80 | + |
| 81 | + range.selectNodeContents(mark); |
| 82 | + selection.addRange(range); |
| 83 | + |
| 84 | + const successful = document.execCommand('copy'); |
| 85 | + if (!successful) { |
| 86 | + throw new Error('copy command was unsuccessful'); |
| 87 | + } |
| 88 | + success = true; |
| 89 | + } catch (err) { |
| 90 | + debug && console.error('unable to copy using execCommand: ', err); |
| 91 | + debug && console.warn('trying IE specific stuff'); |
| 92 | + try { |
| 93 | + (window as any).clipboardData.setData(options.format || 'text', text); |
| 94 | + options.onCopy && options.onCopy((window as any).clipboardData); |
| 95 | + success = true; |
| 96 | + } catch (err) { |
| 97 | + debug && console.error('unable to copy using clipboardData: ', err); |
| 98 | + debug && console.error('falling back to prompt'); |
| 99 | + message = format('message' in options ? options.message : defaultMessage); |
| 100 | + window.prompt(message, text); |
| 101 | + } |
| 102 | + } finally { |
| 103 | + if (selection) { |
| 104 | + if (typeof selection.removeRange == 'function') { |
| 105 | + selection.removeRange(range); |
| 106 | + } else { |
| 107 | + selection.removeAllRanges(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (mark) { |
| 112 | + document.body.removeChild(mark); |
| 113 | + } |
| 114 | + reselectPrevious(); |
| 115 | + } |
| 116 | + |
| 117 | + return success; |
| 118 | +} |
| 119 | + |
| 120 | +export default copy; |
0 commit comments