Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Jun 28, 2020
1 parent 1b5c698 commit e12de55
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ Get on [unpkg](https://unpkg.com/xe-clipboard/) and [cdnjs](https://cdn.jsdelivr
[Run Test](https://xuliangzhan.com/xe-clipboard/)

```html
<button class="btn">Copy</button>
<button id="btn1">Copy</button>
```

```javascript
document.querySelector('.btn').addEventListener('click', function (evnt) {
document.getElementById('btn1').addEventListener('click', function (evnt) {
if (XEClipboard.copy('Copy this content to the clipboard.')) {
alert('Copy success.')
} else {
Expand Down
49 changes: 25 additions & 24 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
'use strict'

var doc = window.document
var $elem = doc.createElement('textarea')
var copyElem: HTMLTextAreaElement

function handleText (content: string | number) {
var styles = $elem.style
$elem.id = '$XECopy'
styles.width = '48px'
styles.height = '24px'
styles.position = 'fixed'
styles.zIndex = '0'
styles.left = '-500px'
styles.top = '-500px'
$elem.value = content === null || content === undefined ? '' : ('' + content)
if (!$elem.parentNode) {
doc.body.appendChild($elem)
if (!copyElem) {
copyElem = doc.createElement('textarea')
copyElem.id = '$XECopy'
var styles = copyElem.style
styles.width = '48px'
styles.height = '24px'
styles.position = 'fixed'
styles.zIndex = '0'
styles.left = '-500px'
styles.top = '-500px'
doc.body.appendChild(copyElem)
}
copyElem.value = content === null || content === undefined ? '' : ('' + content)
}

function copyText (): boolean {
$elem.focus()
$elem.select()
$elem.setSelectionRange(0, $elem.value.length)
return doc.execCommand('copy')
function copyText (content: string | number): boolean {
var result = false
try {
handleText(content)
copyElem.focus()
copyElem.select()
copyElem.setSelectionRange(0, copyElem.value.length)
result = doc.execCommand('copy')
} catch (e) {}
return result
}

/**
Expand All @@ -31,14 +37,9 @@ function copyText (): boolean {
* @param {String} content Text 内容
*/
function XEClipboard (content: string | number): boolean {
var result = false
try {
handleText(content)
result = copyText()
} catch (e) {}
return result
return copyText(content)
}

XEClipboard.copy = XEClipboard
XEClipboard.copy = copyText

export default XEClipboard
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xe-clipboard",
"version": "1.8.0",
"version": "1.9.0",
"description": "纯 Javascript 实现复制文本到剪贴板,支持IE、Chrome、Firefox、Opera、Safari、IOS、Android",
"scripts": {
"lib": "gulp build",
Expand Down

0 comments on commit e12de55

Please sign in to comment.