@react-native-clipboard/clipboard 1.15.0 → 1.16.1

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.
@@ -1,161 +1,161 @@
1
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';
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 'react-native/Libraries/Types/CodegenTypes';
10
+ import type { Int32 } from "react-native/Libraries/Types/CodegenTypes";
11
11
 
12
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
- * (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;
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
- TurboModuleRegistry.getEnforcing<Spec>('RNCClipboard');
154
+ TurboModuleRegistry.getEnforcing<Spec>("RNCClipboard");
155
155
 
156
156
  export default ClipboardTurboModule;
157
157
 
158
- const EVENT_NAME = 'RNCClipboard_TEXT_CHANGED';
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
- listenerCount = (eventType: string) => {
167
- // @ts-ignore
168
- return eventEmitter.listeners(eventType).length;
169
- };
166
+ listenerCount = (eventType: string) => {
167
+ // @ts-ignore
168
+ return eventEmitter.listeners(eventType).length;
169
+ };
170
170
  } else {
171
- listenerCount = eventEmitter.listenerCount.bind(eventEmitter);
171
+ listenerCount = eventEmitter.listenerCount.bind(eventEmitter);
172
172
  }
173
173
 
174
174
  const addListener = (callback: () => void): EmitterSubscription => {
175
- if (listenerCount(EVENT_NAME) === 0) {
176
- ClipboardTurboModule.setListener();
177
- }
175
+ if (listenerCount(EVENT_NAME) === 0) {
176
+ ClipboardTurboModule.setListener();
177
+ }
178
178
 
179
- let res = eventEmitter.addListener(EVENT_NAME, callback);
179
+ const res = eventEmitter.addListener(EVENT_NAME, callback);
180
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
- };
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
- return res;
193
+ return res;
194
194
  };
195
195
 
196
196
  const removeAllListeners = () => {
197
- eventEmitter.removeAllListeners(EVENT_NAME);
198
- ClipboardTurboModule.removeListener();
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 './Clipboard';
2
- export {useClipboard} from './useClipboard';
1
+ import { Clipboard } from "./Clipboard";
2
+ export { useClipboard } from "./useClipboard";
3
3
  export default Clipboard;
@@ -5,31 +5,33 @@
5
5
  * https://github.com/react-native-community/hooks
6
6
  */
7
7
 
8
- import {useEffect, useState} from 'react';
9
- import {Clipboard} from './Clipboard';
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
- Clipboard.setString(content);
16
- listeners.forEach((listener) => listener(content));
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
- const [data, updateClipboardData] = useState('');
22
+ const [data, updateClipboardData] = useState("");
21
23
 
22
- useEffect(() => {
23
- Clipboard.getString().then(updateClipboardData);
24
- }, []);
24
+ useEffect(() => {
25
+ Clipboard.getString().then(updateClipboardData);
26
+ }, []);
25
27
 
26
- useEffect(() => {
27
- listeners.add(updateClipboardData);
28
+ useEffect(() => {
29
+ listeners.add(updateClipboardData);
28
30
 
29
- return () => {
30
- listeners.delete(updateClipboardData);
31
- };
32
- }, []);
31
+ return () => {
32
+ listeners.delete(updateClipboardData);
33
+ };
34
+ }, []);
33
35
 
34
- return [data, setString];
36
+ return [data, setString];
35
37
  };
@@ -1,9 +1,14 @@
1
1
  #include "pch.h"
2
+
2
3
  #include "Clipboard.h"
3
- #include <Unicode.h>
4
+ #include "Unicode.h"
5
+
6
+ namespace winrt::Clipboard
7
+ {
4
8
 
5
- namespace NativeClipboard {
6
- void ClipboardModule::GetString(React::ReactPromise<std::string>&& promise) noexcept {
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
- _context.UIDispatcher().Post([str](){
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 <NativeModules.h>
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 NativeClipboard {
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 _context;
47
+ winrt::Microsoft::ReactNative::ReactContext m_context;
40
48
  };
41
- }
49
+
50
+ } // namespace winrt::Clipboard
Binary file