1 |
|
2 |
|
3 |
|
4 | import { MimeData } from '@lumino/coreutils';
|
5 |
|
6 |
|
7 | export type ClipboardData = string | MimeData;
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export namespace Clipboard {
|
13 | |
14 |
|
15 |
|
16 | export function getInstance(): MimeData {
|
17 | return Private.instance;
|
18 | }
|
19 |
|
20 | |
21 |
|
22 |
|
23 | export function setInstance(value: MimeData): void {
|
24 | Private.instance = value;
|
25 | }
|
26 |
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export function copyToSystem(clipboardData: ClipboardData): void {
|
34 | const node = document.body;
|
35 | const handler = (event: ClipboardEvent) => {
|
36 | const data = event.clipboardData || (window as any).clipboardData;
|
37 | if (typeof clipboardData === 'string') {
|
38 | data.setData('text', clipboardData);
|
39 | } else {
|
40 | (clipboardData as MimeData).types().map((mimeType: string) => {
|
41 | data.setData(mimeType, clipboardData.getData(mimeType));
|
42 | });
|
43 | }
|
44 | event.preventDefault();
|
45 | node.removeEventListener('copy', handler);
|
46 | };
|
47 | node.addEventListener('copy', handler);
|
48 | generateEvent(node);
|
49 | }
|
50 |
|
51 | |
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | export function generateEvent(
|
63 | node: HTMLElement,
|
64 | type: 'copy' | 'cut' = 'copy'
|
65 | ): void {
|
66 |
|
67 |
|
68 |
|
69 | let sel = window.getSelection();
|
70 |
|
71 |
|
72 | const savedRanges: any[] = [];
|
73 | for (let i = 0, len = sel?.rangeCount || 0; i < len; ++i) {
|
74 | savedRanges[i] = sel!.getRangeAt(i).cloneRange();
|
75 | }
|
76 |
|
77 |
|
78 | const range = document.createRange();
|
79 | range.selectNodeContents(node);
|
80 | if (sel) {
|
81 | sel.removeAllRanges();
|
82 | sel.addRange(range);
|
83 | }
|
84 |
|
85 |
|
86 | document.execCommand(type);
|
87 |
|
88 |
|
89 | sel = window.getSelection();
|
90 | if (sel) {
|
91 | sel.removeAllRanges();
|
92 | for (let i = 0, len = savedRanges.length; i < len; ++i) {
|
93 | sel.addRange(savedRanges[i]);
|
94 | }
|
95 | }
|
96 | }
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | namespace Private {
|
103 | |
104 |
|
105 |
|
106 | export let instance = new MimeData();
|
107 | }
|