clipboard-text
Advanced tools
Comparing version 1.0.1 to 2.0.0
@@ -10,2 +10,4 @@ /** | ||
*/ | ||
export default function copy(text: string, parentElement?: Element): boolean; | ||
declare function copy(text: string, parentElement?: Element): boolean; | ||
export default copy; |
@@ -1,56 +0,26 @@ | ||
/*! | ||
* clipboard-text v1.0.1 | ||
* (c) Eduardo San Martin Morote <posva13@gmail.com> (https://esm.dev) | ||
* Released under the MIT License. | ||
*/ | ||
'use strict'; | ||
// Orignal `copy` function from | ||
// https://github.com/sindresorhus/copy-text-to-clipboard | ||
// by Sindre Sorhus | ||
// Fixed support for IE 11 since the package does not plan to support it: | ||
// https://github.com/sindresorhus/copy-text-to-clipboard/issues/28 and it's a | ||
// very simple set of changes | ||
/** | ||
* Copy arbitrary text to the clipboard | ||
* | ||
* @param text text to copy | ||
* @param parentElement element where the textarea should be inserted. Defaults | ||
* to body. You should provide a value if at the time the function is called, | ||
* the focus is trapped in a modal. The value should be the element where the | ||
* focus is trapped. Otherwise the browser may prevent the copy command | ||
*/ | ||
// index.ts | ||
function copy(text, parentElement) { | ||
var element = document.createElement('textarea'); | ||
element.value = text; // Prevent keyboard from showing on mobile | ||
element.setAttribute('readonly', ''); // @ts-ignore | ||
element.style.contain = 'strict'; | ||
element.style.position = 'absolute'; | ||
element.style.left = '-9999px'; | ||
element.style.fontSize = '12pt'; // Prevent zooming on iOS | ||
var selection = document.getSelection(); | ||
var originalRange = false; | ||
const element = document.createElement("textarea"); | ||
element.value = text; | ||
element.setAttribute("readonly", ""); | ||
element.style.contain = "strict"; | ||
element.style.position = "absolute"; | ||
element.style.left = "-9999px"; | ||
element.style.fontSize = "12pt"; | ||
const selection = document.getSelection(); | ||
let originalRange = false; | ||
if (selection && selection.rangeCount > 0) { | ||
originalRange = selection.getRangeAt(0); | ||
} | ||
parentElement = parentElement || document.body; | ||
parentElement.appendChild(element); | ||
element.select(); // Explicit selection workaround for iOS | ||
element.select(); | ||
element.selectionStart = 0; | ||
element.selectionEnd = text.length; | ||
var isSuccess = false; | ||
let isSuccess = false; | ||
try { | ||
isSuccess = document.execCommand('copy'); | ||
} catch (_) {} | ||
isSuccess = document.execCommand("copy"); | ||
} catch (_) { | ||
} | ||
parentElement.removeChild(element); | ||
if (originalRange && selection) { | ||
@@ -60,6 +30,6 @@ selection.removeAllRanges(); | ||
} | ||
return isSuccess; | ||
} | ||
module.exports = copy; | ||
export { | ||
copy as default | ||
}; |
{ | ||
"name": "clipboard-text", | ||
"description": "Simple and small copy-text-to-the-clipboard-utility with IE11 support", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"type": "module", | ||
"exports": "./dist/index.js", | ||
"engines": { | ||
"node": ">=12" | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.esm.js", | ||
"unpkg": "dist/index.umd.min.js", | ||
"jsdelivr": "dist/index.umd.min.js", | ||
"module": "dist/index.js", | ||
"unpkg": "dist/index.global.js", | ||
"jsdelivr": "dist/index.global.js", | ||
"author": { | ||
@@ -23,3 +28,5 @@ "name": "Eduardo San Martin Morote", | ||
"scripts": { | ||
"build": "bili index.ts --format es --format cjs --format umd-min --banner --concurrent --module-name clipboardText", | ||
"build:global": "tsup --global-name clipboardText --format iife --minify index.ts", | ||
"build:es": "tsup --dts --format esm index.ts", | ||
"build": "rm -rf dist && yarn run build:es && yarn run build:global", | ||
"prepublishOnly": "yarn run build", | ||
@@ -42,6 +49,5 @@ "test": "yarn run build" | ||
"devDependencies": { | ||
"bili": "^4.8.1", | ||
"rollup-plugin-typescript2": "^0.22.0", | ||
"typescript": "^3.5.3" | ||
"tsup": "^4.11.1", | ||
"typescript": "^4.0.2" | ||
} | ||
} |
2
Yes
5668
6
48