Debian Codegraph @

packages/domutils/src/clipboard.ts

raw
1 // Copyright (c) Jupyter Development Team.
2 // Distributed under the terms of the Modified BSD License.
3 /*-----------------------------------------------------------------------------
4 | Copyright (c) 2014-2019, PhosphorJS Contributors
5 |
6 | Distributed under the terms of the BSD 3-Clause License.
7 |
8 | The full license is in the file LICENSE, distributed with this software.
9 |----------------------------------------------------------------------------*/
10
11 /**
12 * The namespace for clipboard related functionality.
13 */
14 export namespace ClipboardExt {
15 /**
16 * Copy text to the system clipboard.
17 *
18 * @param text - The text to copy to the clipboard.
19 */
20 export function copyText(text: string): void {
21 // Fetch the document body.
22 const body = document.body;
23
24 // Set up the clipboard event listener.
25 const handler = (event: ClipboardEvent) => {
26 // Stop the event propagation.
27 event.preventDefault();
28 event.stopPropagation();
29
30 // Set the clipboard data.
31 event.clipboardData!.setData('text', text);
32
33 // Remove the event listener.
34 body.removeEventListener('copy', handler, true);
35 };
36
37 // Add the event listener.
38 body.addEventListener('copy', handler, true);
39
40 // Trigger the event.
41 document.execCommand('copy');
42 }
43 }