-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.ts
45 lines (39 loc) · 1.01 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict'
var doc = window.document
var copyElem: HTMLTextAreaElement
function handleText (content: string | number) {
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 (content: string | number): boolean {
var result = false
try {
handleText(content)
copyElem.select()
copyElem.setSelectionRange(0, copyElem.value.length)
result = doc.execCommand('copy')
copyElem.blur()
} catch (e) {}
return result
}
/**
* 复制内容到剪贴板
*
* @param {String} content Text 内容
*/
function XEClipboard (content: string | number): boolean {
return copyText(content)
}
XEClipboard.copy = copyText
export default XEClipboard