Skip to content

Commit b10b342

Browse files
committed
Use our own implementation of copyToClipboard.
1 parent 56aa99b commit b10b342

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"@types/moment": "^2.13.0",
1717
"@types/moment-timezone": "^0.5.6",
1818
"browserify": "^16.2.2",
19-
"clipboard-copy": "^3.0.0",
2019
"markdown-it": "^8.4.2",
2120
"moment": "^2.22.2",
2221
"moment-timezone": "^0.5.20",

ts/CurrentEvent.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as dto from './dto';
22
import * as html from './html';
33
import * as moment from 'moment';
4-
import copy = require('clipboard-copy')
54
import ComponentsArray from './ComponentsArray';
65
import Countdown from './Countdown';
76
import UiComponent from './UiComponent';

ts/Timestamp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import * as dto from './dto';
22
import * as html from './html';
33
import * as moment from 'moment';
4-
import copy = require('clipboard-copy')
54
import ComponentsArray from './ComponentsArray';
65
import Countdown from './Countdown';
76
import UiComponent from './UiComponent';
87
import TwitchPlayer from './TwitchPlayer';
8+
import copyToClipboard from './util/copyToClipboard';
99

1010
export default class CurrentEvent implements UiComponent {
1111
constructor(private _timestamp: string) {
1212
}
1313

1414
onCopyClick = () => {
15-
copy(this._timestamp)
15+
copyToClipboard(this._timestamp)
1616
}
1717

1818
appendTo(entry: HTMLElement | null): void {

ts/util/copyToClipboard.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
export default function copyToClipboard(text: string) {
2+
// Use the Async Clipboard API when available. Requires a secure browing context (i.e. HTTPS)
3+
if ((navigator as any).clipboard) {
4+
return (navigator as any).clipboard.writeText(text)
5+
}
6+
7+
// ...Otherwise, use document.execCommand() fallback
8+
9+
// Put the text to copy into a <span>
10+
const span = document.createElement('span')
11+
span.textContent = text
12+
13+
// Preserve consecutive spaces and newlines
14+
span.style.whiteSpace = 'pre'
15+
16+
// Add the <span> to the page
17+
document.body.appendChild(span)
18+
19+
// Make a selection object representing the range of text selected by the user
20+
const selection = window.getSelection()
21+
const range = window.document.createRange()
22+
23+
if (!selection) {
24+
return Promise.reject()
25+
}
26+
27+
selection.removeAllRanges()
28+
range.selectNode(span)
29+
selection.addRange(range)
30+
31+
// Copy text to the clipboard
32+
let success = false
33+
try {
34+
success = window.document.execCommand('copy')
35+
} catch (err) {
36+
console.log('Failed to copy text. Error: ', err)
37+
}
38+
39+
// Cleanup
40+
selection.removeAllRanges()
41+
window.document.body.removeChild(span)
42+
43+
// The Async Clipboard API returns a promise that may reject with `undefined`
44+
// so we match that here for consistency.
45+
return success
46+
? Promise.resolve()
47+
: Promise.reject()
48+
}

0 commit comments

Comments
 (0)