UNPKG

@kelvininc/ui-components

Version:
39 lines (34 loc) 1.23 kB
'use strict'; var isNil = require('./isNil-B-fGcnNC.js'); function fallbackCopyTextToClipboard(text) { const textArea = document.createElement('textarea'); let isCopySuccessful = true; textArea.value = text; textArea.style.position = 'fixed'; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { isCopySuccessful = document.execCommand('copy'); } catch (err) { isCopySuccessful = false; } document.body.removeChild(textArea); return isCopySuccessful; } async function copyTextToClipboard(text) { const sanitizedText = !isNil.isNil(text) ? text.replace(/^\s+|\s+$/gm, '') : ''; if (isNil.isNil(navigator.clipboard)) { return fallbackCopyTextToClipboard(sanitizedText); } return await navigator.clipboard.writeText(sanitizedText).then(() => true, _ => false); } var clipboard_helper = /*#__PURE__*/Object.freeze({ __proto__: null, copyTextToClipboard: copyTextToClipboard, fallbackCopyTextToClipboard: fallbackCopyTextToClipboard }); exports.clipboard_helper = clipboard_helper; exports.copyTextToClipboard = copyTextToClipboard; exports.fallbackCopyTextToClipboard = fallbackCopyTextToClipboard;