1
1
#import " RNDocumentPicker.h"
2
- # import < React/RCTUtils.h >
2
+
3
3
#import < Cocoa/Cocoa.h>
4
- #import < UniformTypeIdentifiers/UniformTypeIdentifiers.h>
5
4
#import < CoreServices/CoreServices.h>
6
-
7
- #ifdef RCT_NEW_ARCH_ENABLED
8
- #import < ReactCommon/TurboModuleUtils.h>
9
- #import " RNDocumentPickerSpec.h"
10
- #endif
5
+ #import < UniformTypeIdentifiers/UniformTypeIdentifiers.h>
11
6
12
7
@implementation RNDocumentPicker
13
8
14
9
RCT_EXPORT_MODULE ();
15
10
16
- #pragma mark - React exports
11
+ #ifdef RCT_NEW_ARCH_ENABLED
12
+
13
+ - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule : (const facebook::react::ObjCTurboModule::InitParams &)params
14
+ {
15
+ return std::make_shared<facebook::react::NativeRNDocumentPickerSpecJSI>(params);
16
+ }
17
17
18
- RCT_EXPORT_METHOD (
19
- pick:(NSDictionary *)options
20
- resolver:(RCTPromiseResolveBlock)resolve
21
- rejecter:(RCTPromiseRejectBlock)reject)
18
+ - (void )pick : (JS::NativeRNDocumentPicker::SpecPickOptions &)options
19
+ resolve : (RCTPromiseResolveBlock)resolve
20
+ reject : (RCTPromiseRejectBlock)reject
21
+ {
22
+ BOOL allowDirectorySelection = options.allowDirectorySelection ().value_or (NO );
23
+ BOOL allowFileSelection = options.allowFileSelection ().value_or (NO );
24
+ BOOL multiple = options.multiple ().value_or (NO );
25
+ NSArray <NSString *> *allowedUTIs = nil ;
26
+
27
+ if (auto utisOpt = options.allowedUTIs (); utisOpt) {
28
+ const auto &vec = *utisOpt;
29
+ NSMutableArray <NSString *> *tmp = [NSMutableArray arrayWithCapacity: vec.size ()];
30
+ for (NSString *uti : vec) [tmp addObject: uti];
31
+ allowedUTIs = tmp;
32
+ }
33
+
34
+ [self pickImpl: allowDirectorySelection
35
+ allowFileSelection: allowFileSelection
36
+ multiple: multiple
37
+ allowedUTIs: allowedUTIs
38
+ resolve: resolve
39
+ reject: reject];
40
+ }
41
+
42
+ #else
43
+
44
+ RCT_EXPORT_METHOD (pick:(NSDictionary <NSString *, id > * _Nonnull)options
45
+ resolve:(RCTPromiseResolveBlock)resolve
46
+ reject:(RCTPromiseRejectBlock)reject)
47
+ {
48
+ BOOL allowDirectorySelection = [options[@" allowDirectorySelection" ] boolValue ];
49
+ BOOL allowFileSelection = [options[@" allowFileSelection" ] boolValue ];
50
+ BOOL multiple = [options[@" multiple" ] boolValue ];
51
+ NSArray <NSString *> *allowedUTIs = nil ;
52
+
53
+ if ([options[@" allowedUTIs" ] isKindOfClass: [NSArray class ]]) {
54
+ allowedUTIs = (NSArray <NSString *> *)options[@" allowedUTIs" ];
55
+ }
56
+
57
+ [self pickImpl: allowDirectorySelection
58
+ allowFileSelection: allowFileSelection
59
+ multiple: multiple
60
+ allowedUTIs: allowedUTIs
61
+ resolve: resolve
62
+ reject: reject];
63
+ }
64
+
65
+ #endif
66
+
67
+ - (void )pickImpl : (BOOL )allowDirectorySelection
68
+ allowFileSelection : (BOOL )allowFileSelection
69
+ multiple : (BOOL )multiple
70
+ allowedUTIs : (NSArray <NSString *> *)allowedUTIs
71
+ resolve : (RCTPromiseResolveBlock)resolve
72
+ reject : (RCTPromiseRejectBlock)reject
22
73
{
23
74
dispatch_async (dispatch_get_main_queue (), ^{
24
75
NSOpenPanel *panel = [NSOpenPanel openPanel ];
25
76
26
- BOOL allowDirectories = [options objectForKey: @" allowDirectorySelection" ] ? [options[@" allowDirectorySelection" ] boolValue ] : NO ;
27
- BOOL allowFiles = [options objectForKey: @" allowFileSelection" ] ? [options[@" allowFileSelection" ] boolValue ] : YES ;
28
- BOOL multiple = [options objectForKey: @" multiple" ] ? [options[@" multiple" ] boolValue ] : NO ;
29
- NSArray *utis = [options objectForKey: @" allowedUTIs" ];
30
-
31
- panel.canChooseDirectories = allowDirectories;
32
- panel.canChooseFiles = allowFiles;
77
+ panel.canChooseDirectories = allowDirectorySelection;
78
+ panel.canChooseFiles = allowFileSelection;
33
79
panel.allowsMultipleSelection = multiple;
34
- panel.resolvesAliases = YES ;
35
-
36
- if (utis != nil && [utis isKindOfClass: [NSArray class ]] && utis.count > 0 ) {
37
- if (@available (macOS 11.0 , *)) {
38
- NSMutableArray <UTType *> *contentTypes = [NSMutableArray new ];
39
- for (NSString *uti in utis) {
40
- UTType *type = [UTType typeWithIdentifier: uti];
41
- if (type) {
42
- [contentTypes addObject: type];
43
- }
80
+ panel.resolvesAliases = YES ;
81
+
82
+ if (allowedUTIs != nil && [allowedUTIs isKindOfClass: [NSArray class ]] && allowedUTIs.count > 0 ) {
83
+ NSMutableArray <UTType *> *contentTypes = [NSMutableArray new ];
84
+ for (NSString *uti in allowedUTIs) {
85
+ UTType *type = [UTType typeWithIdentifier: uti];
86
+ if (type) {
87
+ [contentTypes addObject: type];
44
88
}
45
- panel.allowedContentTypes = contentTypes;
46
- } else {
47
- panel.allowedFileTypes = utis;
48
89
}
90
+ panel.allowedContentTypes = contentTypes;
49
91
}
50
92
51
93
NSInteger result = [panel runModal ];
@@ -58,20 +100,9 @@ @implementation RNDocumentPicker
58
100
NSDictionary *attributes = [fileManager attributesOfItemAtPath: path error: nil ];
59
101
NSNumber *size = attributes[NSFileSize ];
60
102
NSString *mimeType = nil ;
61
- if (@available (macOS 11.0 , *)) {
62
- UTType *uti = [url resourceValuesForKeys: @[NSURLContentTypeKey] error: nil ][NSURLContentTypeKey];
63
- if (uti) {
64
- mimeType = uti.preferredMIMEType ;
65
- }
66
- } else {
67
- NSString *uti = nil ;
68
- [url getResourceValue: &uti forKey: NSURLTypeIdentifierKey error: nil ];
69
- if (uti) {
70
- CFStringRef mimeTypeCF = UTTypeCopyPreferredTagWithClass ((__bridge CFStringRef)uti, kUTTagClassMIMEType );
71
- if (mimeTypeCF) {
72
- mimeType = (__bridge_transfer NSString *)mimeTypeCF;
73
- }
74
- }
103
+ UTType *uti = [url resourceValuesForKeys: @[NSURLContentTypeKey] error: nil ][NSURLContentTypeKey];
104
+ if (uti) {
105
+ mimeType = uti.preferredMIMEType ;
75
106
}
76
107
NSString *name = url.lastPathComponent ;
77
108
@@ -100,11 +131,4 @@ @implementation RNDocumentPicker
100
131
});
101
132
}
102
133
103
- #ifdef RCT_NEW_ARCH_ENABLED
104
- - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule : (const facebook::react::ObjCTurboModule::InitParams &)params
105
- {
106
- return std::make_shared<facebook::react::NativeRNDocumentPickerSpec>(params);
107
- }
108
- #endif
109
-
110
134
@end
0 commit comments