"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Reference: https://gist.github.com/Chalarangelo/4ff1e8c0ec03d9294628efbae49216db#file-copytoclipboard-js
*
* @param {string} str text to copy
* @category dom
* @module copyToClipboard
*/
var copyToClipboard = function (str) {
var _a;
// Create a <textarea> element
var el = document.createElement('textarea');
// Set its value to the string that you want copied
el.value = str;
// Make it readonly to be tamper-proof
el.setAttribute('readonly', '');
el.style.position = 'absolute';
// Move outside the screen to make it invisible
el.style.left = '-9999px';
document.body.appendChild(el);
// Append the <textarea> element to the HTML document
var selection = document.getSelection();
// Check if there is content selected previously
var selected = ((selection === null || selection === void 0 ? void 0 : selection.rangeCount) || 0) > 0
// Store selection if found
? (_a = document.getSelection()) === null || _a === void 0 ? void 0 : _a.getRangeAt(0)
// Mark as false to know no selection existed before
: false;
// Select the <textarea> content
el.select();
// Copy - only works as a result of a user action (e.g. click events)
document.execCommand('copy');
// Remove the <textarea> element
document.body.removeChild(el);
// If a selection existed before copying
if (selected) {
// Unselect everything on the HTML document
selection === null || selection === void 0 ? void 0 : selection.removeAllRanges();
// Restore the original selection
selection === null || selection === void 0 ? void 0 : selection.addRange(selected);
}
};
exports.default = copyToClipboard;
Source