-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathuseClipboard.ts
More file actions
151 lines (132 loc) · 4.65 KB
/
useClipboard.ts
File metadata and controls
151 lines (132 loc) · 4.65 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {chain, useEffectEvent} from '@react-aria/utils';
import {DOMAttributes, DragItem, DropItem} from '@react-types/shared';
import {readFromDataTransfer, writeToDataTransfer} from './utils';
import {useEffect, useRef} from 'react';
import {useFocus} from '@react-aria/interactions';
export interface ClipboardProps {
/** A function that returns the items to copy. */
getItems?: (details: {action: 'cut' | 'copy'}) => DragItem[],
/** Handler that is called when the user triggers a copy interaction. */
onCopy?: () => void,
/** Handler that is called when the user triggers a cut interaction. */
onCut?: () => void,
/** Handler that is called when the user triggers a paste interaction. */
onPaste?: (items: DropItem[]) => void,
/** Whether the clipboard is disabled. */
isDisabled?: boolean
}
export interface ClipboardResult {
/** Props for the element that will handle clipboard events. */
clipboardProps: DOMAttributes
}
const globalEvents = new Map();
function addGlobalEventListener(event, fn) {
let eventData = globalEvents.get(event);
if (!eventData) {
let handlers = new Set<(e: Event) => void>();
let listener = (e) => {
for (let handler of handlers) {
handler(e);
}
};
eventData = {listener, handlers};
globalEvents.set(event, eventData);
document.addEventListener(event, listener);
}
eventData.handlers.add(fn);
return () => {
eventData.handlers.delete(fn);
if (eventData.handlers.size === 0) {
document.removeEventListener(event, eventData.listener);
globalEvents.delete(event);
}
};
}
/**
* Handles clipboard interactions for a focusable element. Supports items of multiple
* data types, and integrates with the operating system native clipboard.
*/
export function useClipboard(options: ClipboardProps): ClipboardResult {
let {isDisabled} = options;
let isFocusedRef = useRef(false);
let {focusProps} = useFocus({
onFocusChange: (isFocused) => {
isFocusedRef.current = isFocused;
}
});
let onBeforeCopy = useEffectEvent((e: ClipboardEvent) => {
// Enable the "Copy" menu item in Safari if this element is focused and copying is supported.
if (isFocusedRef.current && options.getItems) {
e.preventDefault();
}
});
let onCopy = useEffectEvent((e: ClipboardEvent) => {
if (!isFocusedRef.current || !options.getItems) {
return;
}
e.preventDefault();
if (e.clipboardData) {
writeToDataTransfer(e.clipboardData, options.getItems({action: 'copy'}));
options.onCopy?.();
}
});
let onBeforeCut = useEffectEvent((e: ClipboardEvent) => {
if (isFocusedRef.current && options.onCut && options.getItems) {
e.preventDefault();
}
});
let onCut = useEffectEvent((e: ClipboardEvent) => {
if (!isFocusedRef.current || !options.onCut || !options.getItems) {
return;
}
e.preventDefault();
if (e.clipboardData) {
writeToDataTransfer(e.clipboardData, options.getItems({action: 'cut'}));
options.onCut();
}
});
let onBeforePaste = useEffectEvent((e: ClipboardEvent) => {
// Unfortunately, e.clipboardData.types is not available in this event so we always
// have to enable the Paste menu item even if the type of data is unsupported.
if (isFocusedRef.current && options.onPaste) {
e.preventDefault();
}
});
let onPaste = useEffectEvent((e: ClipboardEvent) => {
if (!isFocusedRef.current || !options.onPaste) {
return;
}
e.preventDefault();
if (e.clipboardData) {
let items = readFromDataTransfer(e.clipboardData);
options.onPaste(items);
}
});
useEffect(() => {
if (isDisabled) {
return;
}
return chain(
addGlobalEventListener('beforecopy', onBeforeCopy),
addGlobalEventListener('copy', onCopy),
addGlobalEventListener('beforecut', onBeforeCut),
addGlobalEventListener('cut', onCut),
addGlobalEventListener('beforepaste', onBeforePaste),
addGlobalEventListener('paste', onPaste)
);
}, [isDisabled]);
return {
clipboardProps: focusProps
};
}