Skip to content

Commit 63d6e76

Browse files
Nick LefeverSaadnajmi
authored andcommitted
[fabric] Add file paste support to TextInput component
Summary: Implement the file/image pasteboard pasting event for the `TextInput` component in Fabric. The payload for the event is the same as for the drag and drop events. Test Plan: * Run Zeratul with Fabric enabled * Copy an image/file on the pasteboard * Focus the message composer * Paste the image/file and send the message https://pxl.cl/4lk92 Reviewers: shawndempsey, #rn-desktop Reviewed By: shawndempsey Differential Revision: https://phabricator.intern.facebook.com/D53694922
1 parent 3d8f5f8 commit 63d6e76

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,24 @@ - (BOOL)textInputShouldHandleKeyEvent:(nonnull NSEvent *)event {
606606
}
607607

608608
- (BOOL)textInputShouldHandlePaste:(nonnull id<RCTBackedTextInputViewProtocol>)sender {
609-
return YES;
609+
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
610+
NSPasteboardType fileType = [pasteboard availableTypeFromArray:@[NSFilenamesPboardType, NSPasteboardTypePNG, NSPasteboardTypeTIFF]];
611+
NSArray<NSPasteboardType>* pastedTypes = ((RCTUITextView*) _backedTextInputView).readablePasteboardTypes;
612+
613+
// If there's a fileType that is of interest, notify JS. Also blocks notifying JS if it's a text paste
614+
if (_eventEmitter && fileType != nil && [pastedTypes containsObject:fileType]) {
615+
auto const &textInputEventEmitter = *std::static_pointer_cast<TextInputEventEmitter const>(_eventEmitter);
616+
std::vector<DataTransferItem> dataTransferItems{};
617+
[self buildDataTransferItems:dataTransferItems forPasteboard:pasteboard];
618+
619+
TextInputEventEmitter::PasteEvent pasteEvent = {
620+
.dataTransferItems = dataTransferItems,
621+
};
622+
textInputEventEmitter.onPaste(pasteEvent);
623+
}
624+
625+
// Only allow pasting text.
626+
return fileType == nil;
610627
}
611628

612629
#endif // macOS]

0 commit comments

Comments
 (0)