Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e160deb

Browse files
authored
Merge pull request #13 from github/clipboard-copied-event
Dispatch clipboard-copy event
2 parents 74afacd + 684d29b commit e160deb

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,16 @@ import 'clipboard-copy-element'
5252

5353
## Events
5454

55-
After copying to the clipboard, a [copy][] event is dispatched that can be
56-
used to notify the user with confirmation, like a tooltip or button highlight.
55+
After copying to the clipboard, a `clipboard-copy` event is dispatched from
56+
the `<clipboard-copy>` element:
5757

5858
```js
59-
document.addEventListener('copy', function(event) {
60-
const button = document.activeElement
59+
document.addEventListener('clipboard-copy', function(event) {
60+
const button = event.target
6161
button.classList.add('highlight')
6262
})
6363
```
6464

65-
[copy]: https://developer.mozilla.org/en-US/docs/Web/Events/copy
66-
6765
## Browser support
6866

6967
Browsers without native [custom element support][support] require a [polyfill][].

examples/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
</style>
1212
<script>
13-
document.addEventListener('copy', function() {
13+
document.addEventListener('clipboard-copy', function() {
1414
const notice = document.getElementById('notice')
1515
notice.hidden = false
1616
setTimeout(function() {

src/clipboard-copy-element.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,30 @@ import {copyInput, copyNode, copyText} from './clipboard'
55
function copy(button: HTMLElement) {
66
const id = button.getAttribute('for')
77
const text = button.getAttribute('value')
8+
9+
function trigger() {
10+
button.dispatchEvent(new CustomEvent('clipboard-copy', {bubbles: true}))
11+
}
12+
813
if (text) {
9-
copyText(button, text)
14+
copyText(text).then(trigger)
1015
} else if (id) {
11-
copyTarget(button, id)
16+
const node = button.ownerDocument.getElementById(id)
17+
if (node) copyTarget(node).then(trigger)
1218
}
1319
}
1420

15-
function copyTarget(button: Element, id: string) {
16-
const content = button.ownerDocument.getElementById(id)
17-
if (!content) return
18-
21+
function copyTarget(content: Element) {
1922
if (content instanceof HTMLInputElement || content instanceof HTMLTextAreaElement) {
2023
if (content.type === 'hidden') {
21-
copyText(button, content.value)
24+
return copyText(content.value)
2225
} else {
23-
copyInput(button, content)
26+
return copyInput(content)
2427
}
2528
} else if (content instanceof HTMLAnchorElement && content.hasAttribute('href')) {
26-
copyText(button, content.href)
29+
return copyText(content.href)
2730
} else {
28-
copyNode(button, content)
31+
return copyNode(content)
2932
}
3033
}
3134

src/clipboard.js

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@ function createNode(text: string): Element {
1010
return node
1111
}
1212

13-
export function copyNode(button: Element, node: Element) {
14-
if (writeAsync(button, node.textContent)) return
13+
export function copyNode(node: Element): Promise<void> {
14+
if ('clipboard' in navigator) {
15+
// eslint-disable-next-line flowtype/no-flow-fix-me-comments
16+
// $FlowFixMe Clipboard is not defined in Flow yet.
17+
return navigator.clipboard.writeText(node.textContent)
18+
}
1519

1620
const selection = getSelection()
1721
if (selection == null) {
18-
return
22+
return Promise.reject(new Error())
1923
}
2024

2125
selection.removeAllRanges()
@@ -26,37 +30,40 @@ export function copyNode(button: Element, node: Element) {
2630

2731
document.execCommand('copy')
2832
selection.removeAllRanges()
33+
return Promise.resolve()
2934
}
3035

31-
export function copyText(button: Element, text: string) {
32-
if (writeAsync(button, text)) return
36+
export function copyText(text: string): Promise<void> {
37+
if ('clipboard' in navigator) {
38+
// eslint-disable-next-line flowtype/no-flow-fix-me-comments
39+
// $FlowFixMe Clipboard is not defined in Flow yet.
40+
return navigator.clipboard.writeText(text)
41+
}
3342

3443
const body = document.body
35-
if (!body) return
44+
if (!body) {
45+
return Promise.reject(new Error())
46+
}
3647

3748
const node = createNode(text)
3849
body.appendChild(node)
39-
copyNode(button, node)
50+
copyNode(node)
4051
body.removeChild(node)
52+
return Promise.resolve()
4153
}
4254

43-
export function copyInput(button: Element, node: HTMLInputElement | HTMLTextAreaElement) {
44-
if (writeAsync(button, node.value)) return
55+
export function copyInput(node: HTMLInputElement | HTMLTextAreaElement): Promise<void> {
56+
if ('clipboard' in navigator) {
57+
// eslint-disable-next-line flowtype/no-flow-fix-me-comments
58+
// $FlowFixMe Clipboard is not defined in Flow yet.
59+
return navigator.clipboard.writeText(node.value)
60+
}
4561

4662
node.select()
4763
document.execCommand('copy')
4864
const selection = getSelection()
4965
if (selection != null) {
5066
selection.removeAllRanges()
5167
}
52-
}
53-
54-
function writeAsync(button: Element, text: string): boolean {
55-
const clipboard = navigator.clipboard
56-
if (!clipboard) return false
57-
58-
clipboard.writeText(text).then(function() {
59-
button.dispatchEvent(new CustomEvent('copy', {bubbles: true}))
60-
})
61-
return true
68+
return Promise.resolve()
6269
}

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('clipboard-copy element', function() {
6565
})
6666

6767
whenCopied = new Promise(resolve => {
68-
document.addEventListener('copy', () => resolve(copiedText), {once: true})
68+
document.addEventListener('clipboard-copy', () => resolve(copiedText), {once: true})
6969
})
7070
})
7171

0 commit comments

Comments
 (0)