11#import " RNDocumentPicker.h"
2- # import < React/RCTUtils.h >
2+
33#import < Cocoa/Cocoa.h>
4- #import < UniformTypeIdentifiers/UniformTypeIdentifiers.h>
54#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>
116
127@implementation RNDocumentPicker
138
149RCT_EXPORT_MODULE ();
1510
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+ }
1717
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
2273{
2374 dispatch_async (dispatch_get_main_queue (), ^{
2475 NSOpenPanel *panel = [NSOpenPanel openPanel ];
2576
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;
3379 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];
4488 }
45- panel.allowedContentTypes = contentTypes;
46- } else {
47- panel.allowedFileTypes = utis;
4889 }
90+ panel.allowedContentTypes = contentTypes;
4991 }
5092
5193 NSInteger result = [panel runModal ];
@@ -58,20 +100,9 @@ @implementation RNDocumentPicker
58100 NSDictionary *attributes = [fileManager attributesOfItemAtPath: path error: nil ];
59101 NSNumber *size = attributes[NSFileSize ];
60102 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 ;
75106 }
76107 NSString *name = url.lastPathComponent ;
77108
@@ -100,11 +131,4 @@ @implementation RNDocumentPicker
100131 });
101132}
102133
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-
110134@end
0 commit comments