@react-native-clipboard/clipboard 1.15.0 → 1.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Clipboard.d.ts +1 -1
- package/dist/Clipboard.js +26 -22
- package/dist/Clipboard.web.js +13 -15
- package/dist/NativeClipboard.d.ts +1 -1
- package/dist/NativeClipboard.js +9 -9
- package/dist/NativeClipboardModule.d.ts +2 -2
- package/dist/NativeClipboardModule.js +9 -9
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/useClipboard.js +11 -9
- package/jest/clipboard-mock.js +14 -14
- package/package.json +112 -97
- package/src/Clipboard.ts +199 -199
- package/src/Clipboard.web.ts +29 -32
- package/src/NativeClipboardModule.ts +172 -172
- package/src/index.ts +2 -2
- package/src/useClipboard.ts +17 -15
- package/windows/Clipboard/Clipboard.cpp +11 -5
- package/windows/Clipboard/Clipboard.h +17 -8
- package/windows/Clipboard/Clipboard.rc +0 -0
- package/windows/Clipboard/Clipboard.vcxproj +89 -93
- package/windows/Clipboard/Clipboard.vcxproj.filters +32 -27
- package/windows/Clipboard/ReactPackageProvider.cpp +17 -10
- package/windows/Clipboard/ReactPackageProvider.h +14 -10
- package/windows/Clipboard/ReactPackageProvider.idl +8 -9
- package/windows/Clipboard/codegen/.clang-format +2 -0
- package/windows/Clipboard/codegen/NativeClipboardModuleSpec.g.h +130 -0
- package/windows/Clipboard/pch.cpp +1 -1
- package/windows/Clipboard/pch.h +29 -3
- package/windows/Clipboard/resource.h +5 -0
- package/windows/Clipboard/targetver.h +8 -0
- package/windows/Clipboard.sln +43 -0
- package/windows/README.md +2 -2
- package/windows/windows.sln +0 -295
|
@@ -1,161 +1,161 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
} from
|
|
2
|
+
// @ts-ignore - remove this comment when RN in the repo & example app is upgraded
|
|
3
|
+
TurboModuleRegistry,
|
|
4
|
+
// @ts-ignore - remove this comment when RN in the repo & example app is upgraded
|
|
5
|
+
type TurboModule,
|
|
6
|
+
type EmitterSubscription,
|
|
7
|
+
NativeEventEmitter,
|
|
8
|
+
} from "react-native";
|
|
9
9
|
// @ts-ignore - remove this comment when RN in the repo & example app is upgraded
|
|
10
|
-
import type {Int32} from
|
|
10
|
+
import type { Int32 } from "react-native/Libraries/Types/CodegenTypes";
|
|
11
11
|
|
|
12
12
|
export interface Spec extends TurboModule {
|
|
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
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Get content of string type, this method returns a `Promise`, so you can use following code to get clipboard content
|
|
15
|
+
* ```javascript
|
|
16
|
+
* async _getContent() {
|
|
17
|
+
* var content = await Clipboard.getString();
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
getString(): Promise<string>;
|
|
22
|
+
/**
|
|
23
|
+
* (iOS Only)
|
|
24
|
+
* Get contents of string array type, this method returns a `Promise`, so you can use following code to get clipboard content
|
|
25
|
+
* ```javascript
|
|
26
|
+
* async _getContent() {
|
|
27
|
+
* var content = await Clipboard.getStrings();
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
getStrings(): Promise<string[]>;
|
|
32
|
+
/**
|
|
33
|
+
* Get clipboard image as PNG in base64, this method returns a `Promise`, so you can use following code to get clipboard content
|
|
34
|
+
* ```javascript
|
|
35
|
+
* async _getContent() {
|
|
36
|
+
* var content = await Clipboard.getImagePNG();
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
getImagePNG(): Promise<string>;
|
|
41
|
+
/**
|
|
42
|
+
* Get clipboard image as JPG in base64, this method returns a `Promise`, so you can use following code to get clipboard content
|
|
43
|
+
* ```javascript
|
|
44
|
+
* async _getContent() {
|
|
45
|
+
* var content = await Clipboard.getImageJPG();
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
getImageJPG(): Promise<string>;
|
|
50
|
+
/**
|
|
51
|
+
* (iOS Only)
|
|
52
|
+
* Set content of base64 image type. You can use following code to set clipboard content
|
|
53
|
+
* ```javascript
|
|
54
|
+
* _setContent() {
|
|
55
|
+
* Clipboard.setImage(...);
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
* @param the content to be stored in the clipboard.
|
|
59
|
+
*/
|
|
60
|
+
setImage(content: string): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* (iOS and Android Only)
|
|
63
|
+
* Get clipboard image in base64, this method returns a `Promise`, so you can use following code to get clipboard content
|
|
64
|
+
* ```javascript
|
|
65
|
+
* async _getContent() {
|
|
66
|
+
* var content = await Clipboard.getImage();
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
getImage(): Promise<string>;
|
|
71
|
+
/**
|
|
72
|
+
* Set content of string type. You can use following code to set clipboard content
|
|
73
|
+
* ```javascript
|
|
74
|
+
* _setContent() {
|
|
75
|
+
* Clipboard.setString('hello world');
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
* @param the content to be stored in the clipboard.
|
|
79
|
+
*/
|
|
80
|
+
setString(content: string): void;
|
|
81
|
+
/**
|
|
82
|
+
* Set content of string array type. You can use following code to set clipboard content
|
|
83
|
+
* ```javascript
|
|
84
|
+
* _setContent() {
|
|
85
|
+
* Clipboard.setStrings(['hello world', 'second string']);
|
|
86
|
+
* }
|
|
87
|
+
* ```
|
|
88
|
+
* @param the content to be stored in the clipboard.
|
|
89
|
+
*/
|
|
90
|
+
setStrings(content: string[]): void;
|
|
91
|
+
/**
|
|
92
|
+
* Returns whether the clipboard has content or is empty.
|
|
93
|
+
* This method returns a `Promise`, so you can use following code to get clipboard content
|
|
94
|
+
* ```javascript
|
|
95
|
+
* async _hasContent() {
|
|
96
|
+
* var hasContent = await Clipboard.hasString();
|
|
97
|
+
* }
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
hasString(): Promise<boolean>;
|
|
101
|
+
/**
|
|
102
|
+
* Returns whether the clipboard has an image or is empty.
|
|
103
|
+
* This method returns a `Promise`, so you can use following code to check clipboard content
|
|
104
|
+
* ```javascript
|
|
105
|
+
* async _hasContent() {
|
|
106
|
+
* var hasContent = await Clipboard.hasImage();
|
|
107
|
+
* }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
hasImage(): Promise<boolean>;
|
|
111
|
+
/**
|
|
112
|
+
* (iOS Only)
|
|
113
|
+
* Returns whether the clipboard has a URL content. Can check
|
|
114
|
+
* if there is a URL content in clipboard without triggering PasteBoard notification for iOS 14+
|
|
115
|
+
* This method returns a `Promise`, so you can use following code to check for url content in clipboard.
|
|
116
|
+
* ```javascript
|
|
117
|
+
* async _hasURL() {
|
|
118
|
+
* var hasURL = await Clipboard.hasURL();
|
|
119
|
+
* }
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
hasURL(): Promise<boolean>;
|
|
123
|
+
/**
|
|
124
|
+
* (iOS 14+ Only)
|
|
125
|
+
* Returns whether the clipboard has a Number(UIPasteboardDetectionPatternNumber) content. Can check
|
|
126
|
+
* if there is a Number content in clipboard without triggering PasteBoard notification for iOS 14+
|
|
127
|
+
* This method returns a `Promise`, so you can use following code to check for Number content in clipboard.
|
|
128
|
+
* ```javascript
|
|
129
|
+
* async _hasNumber() {
|
|
130
|
+
* var hasNumber = await Clipboard.hasNumber();
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
hasNumber(): Promise<boolean>;
|
|
135
|
+
/**
|
|
136
|
+
* (iOS 14+ Only)
|
|
137
|
+
* Returns whether the clipboard has a WebURL(UIPasteboardDetectionPatternProbableWebURL) content. Can check
|
|
138
|
+
* if there is a WebURL content in clipboard without triggering PasteBoard notification for iOS 14+
|
|
139
|
+
* This method returns a `Promise`, so you can use following code to check for WebURL content in clipboard.
|
|
140
|
+
* ```javascript
|
|
141
|
+
* async _hasWebURL() {
|
|
142
|
+
* var hasWebURL = await Clipboard.hasWebURL();
|
|
143
|
+
* }
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
hasWebURL(): Promise<boolean>;
|
|
147
|
+
setListener(): void;
|
|
148
|
+
removeListener(): void;
|
|
149
|
+
addListener(eventName: string): void;
|
|
150
|
+
removeListeners(count: Int32): void;
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
const ClipboardTurboModule =
|
|
154
|
-
|
|
154
|
+
TurboModuleRegistry.getEnforcing<Spec>("RNCClipboard");
|
|
155
155
|
|
|
156
156
|
export default ClipboardTurboModule;
|
|
157
157
|
|
|
158
|
-
const EVENT_NAME =
|
|
158
|
+
const EVENT_NAME = "RNCClipboard_TEXT_CHANGED";
|
|
159
159
|
const eventEmitter = new NativeEventEmitter(ClipboardTurboModule);
|
|
160
160
|
|
|
161
161
|
let listenerCount = eventEmitter.listenerCount;
|
|
@@ -163,39 +163,39 @@ let listenerCount = eventEmitter.listenerCount;
|
|
|
163
163
|
// listenerCount is only available from RN 0.64
|
|
164
164
|
// Older versions only have `listeners`
|
|
165
165
|
if (!listenerCount) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
166
|
+
listenerCount = (eventType: string) => {
|
|
167
|
+
// @ts-ignore
|
|
168
|
+
return eventEmitter.listeners(eventType).length;
|
|
169
|
+
};
|
|
170
170
|
} else {
|
|
171
|
-
|
|
171
|
+
listenerCount = eventEmitter.listenerCount.bind(eventEmitter);
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
const addListener = (callback: () => void): EmitterSubscription => {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
175
|
+
if (listenerCount(EVENT_NAME) === 0) {
|
|
176
|
+
ClipboardTurboModule.setListener();
|
|
177
|
+
}
|
|
178
178
|
|
|
179
|
-
|
|
179
|
+
const res = eventEmitter.addListener(EVENT_NAME, callback);
|
|
180
180
|
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
181
|
+
// Path the remove call to also remove the native listener
|
|
182
|
+
// if we no longer have listeners
|
|
183
|
+
// @ts-ignore
|
|
184
|
+
res._remove = res.remove;
|
|
185
|
+
res.remove = function () {
|
|
186
|
+
// @ts-ignore
|
|
187
|
+
this._remove();
|
|
188
|
+
if (listenerCount(EVENT_NAME) === 0) {
|
|
189
|
+
ClipboardTurboModule.removeListener();
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
192
|
|
|
193
|
-
|
|
193
|
+
return res;
|
|
194
194
|
};
|
|
195
195
|
|
|
196
196
|
const removeAllListeners = () => {
|
|
197
|
-
|
|
198
|
-
|
|
197
|
+
eventEmitter.removeAllListeners(EVENT_NAME);
|
|
198
|
+
ClipboardTurboModule.removeListener();
|
|
199
199
|
};
|
|
200
200
|
|
|
201
|
-
export {addListener, removeAllListeners};
|
|
201
|
+
export { addListener, removeAllListeners };
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {Clipboard} from
|
|
2
|
-
export {useClipboard} from
|
|
1
|
+
import { Clipboard } from "./Clipboard";
|
|
2
|
+
export { useClipboard } from "./useClipboard";
|
|
3
3
|
export default Clipboard;
|
package/src/useClipboard.ts
CHANGED
|
@@ -5,31 +5,33 @@
|
|
|
5
5
|
* https://github.com/react-native-community/hooks
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import {useEffect, useState} from
|
|
9
|
-
import {Clipboard} from
|
|
8
|
+
import { useEffect, useState } from "react";
|
|
9
|
+
import { Clipboard } from "./Clipboard";
|
|
10
10
|
|
|
11
11
|
type Listener = (content: string) => void;
|
|
12
12
|
const listeners = new Set<Listener>();
|
|
13
13
|
|
|
14
14
|
function setString(content: string) {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
Clipboard.setString(content);
|
|
16
|
+
for (const listener of listeners) {
|
|
17
|
+
listener(content);
|
|
18
|
+
}
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
export const useClipboard = (): [string, (content: string) => void] => {
|
|
20
|
-
|
|
22
|
+
const [data, updateClipboardData] = useState("");
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
Clipboard.getString().then(updateClipboardData);
|
|
26
|
+
}, []);
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
listeners.add(updateClipboardData);
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
return () => {
|
|
32
|
+
listeners.delete(updateClipboardData);
|
|
33
|
+
};
|
|
34
|
+
}, []);
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
return [data, setString];
|
|
35
37
|
};
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
#include "pch.h"
|
|
2
|
+
|
|
2
3
|
#include "Clipboard.h"
|
|
3
|
-
#include
|
|
4
|
+
#include "Unicode.h"
|
|
5
|
+
|
|
6
|
+
namespace winrt::Clipboard
|
|
7
|
+
{
|
|
4
8
|
|
|
5
|
-
|
|
6
|
-
|
|
9
|
+
// See https://microsoft.github.io/react-native-windows/docs/native-modules for details on writing native modules
|
|
10
|
+
|
|
11
|
+
void ClipboardModule::GetString(React::ReactPromise<std::string>&& promise) noexcept {
|
|
7
12
|
auto dataPackageView = datatransfer::Clipboard::GetContent();
|
|
8
13
|
if (dataPackageView.Contains(datatransfer::StandardDataFormats::Text())) {
|
|
9
14
|
dataPackageView.GetTextAsync().Completed([promise, dataPackageView](IAsyncOperation<winrt::hstring> info, AsyncStatus status) {
|
|
@@ -22,7 +27,7 @@ namespace NativeClipboard {
|
|
|
22
27
|
|
|
23
28
|
void ClipboardModule::SetString(std::string const& str) noexcept
|
|
24
29
|
{
|
|
25
|
-
|
|
30
|
+
m_context.UIDispatcher().Post([str](){
|
|
26
31
|
datatransfer::DataPackage dataPackage{};
|
|
27
32
|
dataPackage.SetText(winrt::to_hstring(str));
|
|
28
33
|
datatransfer::Clipboard::SetContent(dataPackage);
|
|
@@ -41,4 +46,5 @@ namespace NativeClipboard {
|
|
|
41
46
|
// Disconnect any native eventing here
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
|
-
|
|
49
|
+
|
|
50
|
+
} // namespace winrt::Clipboard
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
#pragma once
|
|
2
|
+
|
|
2
3
|
#include "pch.h"
|
|
4
|
+
#include "resource.h"
|
|
5
|
+
|
|
6
|
+
#if __has_include("codegen/NativeClipboardDataTypes.g.h")
|
|
7
|
+
#include "codegen/NativeClipboardDataTypes.g.h"
|
|
8
|
+
#endif
|
|
9
|
+
#include "codegen/NativeClipboardModuleSpec.g.h"
|
|
3
10
|
|
|
4
11
|
#include <functional>
|
|
5
12
|
#include <string>
|
|
6
|
-
#include
|
|
13
|
+
#include "NativeModules.h"
|
|
7
14
|
|
|
8
15
|
#include <winrt/Windows.Foundation.h>
|
|
9
16
|
#include <winrt/Windows.ApplicationModel.DataTransfer.h>
|
|
@@ -12,16 +19,17 @@ using namespace winrt::Microsoft::ReactNative;
|
|
|
12
19
|
using namespace winrt::Windows::Foundation;
|
|
13
20
|
namespace datatransfer = winrt::Windows::ApplicationModel::DataTransfer;
|
|
14
21
|
|
|
15
|
-
namespace
|
|
22
|
+
namespace winrt::Clipboard
|
|
23
|
+
{
|
|
24
|
+
|
|
16
25
|
REACT_MODULE(ClipboardModule, L"RNCClipboard");
|
|
17
26
|
struct ClipboardModule
|
|
18
27
|
{
|
|
19
28
|
REACT_INIT(Initialize);
|
|
20
|
-
void Initialize(const winrt::Microsoft::ReactNative::ReactContext& reactContext) noexcept
|
|
21
|
-
|
|
22
|
-
_context = reactContext;
|
|
29
|
+
void Initialize(const winrt::Microsoft::ReactNative::ReactContext& reactContext) noexcept {
|
|
30
|
+
m_context = reactContext;
|
|
23
31
|
}
|
|
24
|
-
|
|
32
|
+
|
|
25
33
|
REACT_METHOD(GetString, L"getString");
|
|
26
34
|
void GetString(React::ReactPromise<std::string>&& result) noexcept;
|
|
27
35
|
|
|
@@ -36,6 +44,7 @@ namespace NativeClipboard {
|
|
|
36
44
|
|
|
37
45
|
private:
|
|
38
46
|
int _listenerCount = 0;
|
|
39
|
-
ReactContext
|
|
47
|
+
winrt::Microsoft::ReactNative::ReactContext m_context;
|
|
40
48
|
};
|
|
41
|
-
|
|
49
|
+
|
|
50
|
+
} // namespace winrt::Clipboard
|
|
Binary file
|