-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathclipboard.ts
44 lines (38 loc) · 1.31 KB
/
clipboard.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/*-----------------------------------------------------------------------------
| Copyright (c) 2014-2019, PhosphorJS Contributors
|
| Distributed under the terms of the BSD 3-Clause License.
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
/**
* The namespace for clipboard related functionality.
*/
export namespace ClipboardExt {
/**
* Copy text to the system clipboard.
*
* @param text - The text to copy to the clipboard.
*/
export function copyText(text: string): void {
if (typeof document === 'undefined') return;
// Fetch the document body.
const body = document.body;
// Set up the clipboard event listener.
const handler = (event: ClipboardEvent) => {
// Stop the event propagation.
event.preventDefault();
event.stopPropagation();
// Set the clipboard data.
event.clipboardData!.setData('text', text);
// Remove the event listener.
body.removeEventListener('copy', handler, true);
};
// Add the event listener.
body.addEventListener('copy', handler, true);
// Trigger the event.
document.execCommand('copy');
}
}