@react-native-clipboard/clipboard 1.11.2 → 1.13.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.
@@ -0,0 +1,201 @@
1
+ import {
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
+ TurboModule,
6
+ EmitterSubscription,
7
+ NativeEventEmitter,
8
+ } from 'react-native';
9
+ // @ts-ignore - remove this comment when RN in the repo & example app is upgraded
10
+ import type {Int32} from 'react-native/Libraries/Types/CodegenTypes';
11
+
12
+ export interface Spec extends TurboModule {
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
+ * (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
+ }
152
+
153
+ const ClipboardTurboModule =
154
+ TurboModuleRegistry.getEnforcing<Spec>('RNCClipboard');
155
+
156
+ export default ClipboardTurboModule;
157
+
158
+ const EVENT_NAME = 'RNCClipboard_TEXT_CHANGED';
159
+ const eventEmitter = new NativeEventEmitter(ClipboardTurboModule);
160
+
161
+ let listenerCount = eventEmitter.listenerCount;
162
+
163
+ // listenerCount is only available from RN 0.64
164
+ // Older versions only have `listeners`
165
+ if (!listenerCount) {
166
+ listenerCount = (eventType: string) => {
167
+ // @ts-ignore
168
+ return eventEmitter.listeners(eventType).length;
169
+ };
170
+ } else {
171
+ listenerCount = eventEmitter.listenerCount.bind(eventEmitter);
172
+ }
173
+
174
+ const addListener = (callback: () => void): EmitterSubscription => {
175
+ if (listenerCount(EVENT_NAME) === 0) {
176
+ ClipboardTurboModule.setListener();
177
+ }
178
+
179
+ let res = eventEmitter.addListener(EVENT_NAME, callback);
180
+
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
+
193
+ return res;
194
+ };
195
+
196
+ const removeAllListeners = () => {
197
+ eventEmitter.removeAllListeners(EVENT_NAME);
198
+ ClipboardTurboModule.removeListener();
199
+ };
200
+
201
+ export {addListener, removeAllListeners};
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import {Clipboard} from './Clipboard';
2
+ export {useClipboard} from './useClipboard';
3
+ export default Clipboard;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * useClipboard.ts
3
+ * This code is inspired from the @react-native-community/hooks package
4
+ * All credit goes to author of the useClipboard custom hooks.
5
+ * https://github.com/react-native-community/hooks
6
+ */
7
+
8
+ import {useEffect, useState} from 'react';
9
+ import {Clipboard} from './Clipboard';
10
+
11
+ type Listener = (content: string) => void;
12
+ const listeners = new Set<Listener>();
13
+
14
+ function setString(content: string) {
15
+ Clipboard.setString(content);
16
+ listeners.forEach((listener) => listener(content));
17
+ }
18
+
19
+ export const useClipboard = (): [string, (content: string) => void] => {
20
+ const [data, updateClipboardData] = useState('');
21
+
22
+ useEffect(() => {
23
+ Clipboard.getString().then(updateClipboardData);
24
+ }, []);
25
+
26
+ useEffect(() => {
27
+ listeners.add(updateClipboardData);
28
+
29
+ return () => {
30
+ listeners.delete(updateClipboardData);
31
+ };
32
+ }, []);
33
+
34
+ return [data, setString];
35
+ };
@@ -8,7 +8,7 @@ namespace NativeClipboard {
8
8
  if (dataPackageView.Contains(datatransfer::StandardDataFormats::Text())) {
9
9
  dataPackageView.GetTextAsync().Completed([promise, dataPackageView](IAsyncOperation<winrt::hstring> info, AsyncStatus status) {
10
10
  if (status == AsyncStatus::Completed) {
11
- auto text = Microsoft::Common::Unicode::Utf16ToUtf8(info.GetResults());
11
+ auto text = winrt::to_string(info.GetResults());
12
12
  promise.Resolve(text);
13
13
  }
14
14
  else {
@@ -22,8 +22,23 @@ namespace NativeClipboard {
22
22
 
23
23
  void ClipboardModule::SetString(std::string const& str) noexcept
24
24
  {
25
- datatransfer::DataPackage dataPackage{};
26
- dataPackage.SetText(Microsoft::Common::Unicode::Utf8ToUtf16(str));
27
- datatransfer::Clipboard::SetContent(dataPackage);
25
+ _context.UIDispatcher().Post([str](){
26
+ datatransfer::DataPackage dataPackage{};
27
+ dataPackage.SetText(winrt::to_hstring(str));
28
+ datatransfer::Clipboard::SetContent(dataPackage);
29
+ });
30
+ }
31
+
32
+ void ClipboardModule::AddListener(std::string const& event) noexcept
33
+ {
34
+ _listenerCount++;
35
+ }
36
+
37
+ void ClipboardModule::RemoveListeners(int count) noexcept
38
+ {
39
+ _listenerCount--;
40
+ if (_listenerCount == 0) {
41
+ // Disconnect any native eventing here
42
+ }
28
43
  }
29
44
  }
@@ -16,11 +16,26 @@ namespace NativeClipboard {
16
16
  REACT_MODULE(ClipboardModule, L"RNCClipboard");
17
17
  struct ClipboardModule
18
18
  {
19
+ REACT_INIT(Initialize);
20
+ void Initialize(const winrt::Microsoft::ReactNative::ReactContext& reactContext) noexcept
21
+ {
22
+ _context = reactContext;
23
+ }
19
24
 
20
25
  REACT_METHOD(GetString, L"getString");
21
26
  void GetString(React::ReactPromise<std::string>&& result) noexcept;
22
27
 
23
28
  REACT_METHOD(SetString, L"setString");
24
29
  void SetString(std::string const& str) noexcept;
30
+
31
+ REACT_METHOD(AddListener, L"addListener");
32
+ void AddListener(std::string const& event) noexcept;
33
+
34
+ REACT_METHOD(RemoveListeners, L"removeListeners");
35
+ void RemoveListeners(int count) noexcept;
36
+
37
+ private:
38
+ int _listenerCount = 0;
39
+ ReactContext _context;
25
40
  };
26
41
  }
@@ -14,8 +14,14 @@
14
14
  <AppContainerApplication>true</AppContainerApplication>
15
15
  <ApplicationType>Windows Store</ApplicationType>
16
16
  <ApplicationTypeRevision>10.0</ApplicationTypeRevision>
17
- <WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.18362.0</WindowsTargetPlatformVersion>
18
- <WindowsTargetPlatformMinVersion>10.0.17134.0</WindowsTargetPlatformMinVersion>
17
+ </PropertyGroup>
18
+ <PropertyGroup Label="ReactNativeWindowsProps">
19
+ <ReactNativeWindowsDir Condition="'$(ReactNativeWindowsDir)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), 'node_modules\react-native-windows\package.json'))\node_modules\react-native-windows\</ReactNativeWindowsDir>
20
+ </PropertyGroup>
21
+ <Import Project="$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.WindowsSdk.Default.props" Condition="Exists('$(ReactNativeWindowsDir)\PropertySheets\External\Microsoft.ReactNative.WindowsSdk.Default.props')" />
22
+ <PropertyGroup Label="Fallback Windows SDK Versions">
23
+ <WindowsTargetPlatformVersion Condition=" '$(WindowsTargetPlatformVersion)' == '' ">10.0.18362.0</WindowsTargetPlatformVersion>
24
+ <WindowsTargetPlatformMinVersion Condition=" '$(WindowsTargetPlatformMinVersion)' == '' ">10.0.17763.0</WindowsTargetPlatformMinVersion>
19
25
  </PropertyGroup>
20
26
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
21
27
  <ItemGroup Label="ProjectConfigurations">
@@ -69,9 +75,6 @@
69
75
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
70
76
  <ImportGroup Label="ExtensionSettings">
71
77
  </ImportGroup>
72
- <PropertyGroup>
73
- <ReactNativeWindowsDir Condition="'$(ReactNativeWindowsDir)' == ''">$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), 'node_modules\react-native-windows\package.json'))\node_modules\react-native-windows\</ReactNativeWindowsDir>
74
- </PropertyGroup>
75
78
  <ImportGroup Label="Shared">
76
79
  <Import Project="$(ReactNativeWindowsDir)\Microsoft.ReactNative.Cxx\Microsoft.ReactNative.Cxx.vcxitems" Label="Shared" />
77
80
  </ImportGroup>