Skip to content

Commit 8b01e10

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 1351028 commit 8b01e10

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
@@ -602,7 +602,24 @@ - (BOOL)textInputShouldHandleKeyEvent:(nonnull NSEvent *)event {
602602
}
603603

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

608625
#endif // macOS]

0 commit comments

Comments
 (0)