Add support for text copy to Paragraph component

Summary:
Changelog: [internal]

Add support for copying text. The implementation is copied over from old React Native renderer.

Reviewed By: JoshuaGross

Differential Revision: D27502474

fbshipit-source-id: 0d0df583f1adc584ca47e987f06bf78c32386fcc
This commit is contained in:
Samuel Susla
2021-04-01 10:43:52 -07:00
committed by Facebook GitHub Bot
parent 184b372720
commit e42ffdd859
2 changed files with 89 additions and 5 deletions
@@ -8,6 +8,7 @@
#import "RCTParagraphComponentView.h"
#import "RCTParagraphComponentAccessibilityProvider.h"
#import <MobileCoreServices/UTCoreTypes.h>
#import <react/renderer/components/text/ParagraphComponentDescriptor.h>
#import <react/renderer/components/text/ParagraphProps.h>
#import <react/renderer/components/text/ParagraphState.h>
@@ -28,6 +29,7 @@ using namespace facebook::react;
ParagraphShadowNode::ConcreteState::Shared _state;
ParagraphAttributes _paragraphAttributes;
RCTParagraphComponentAccessibilityProvider *_accessibilityProvider;
UILongPressGestureRecognizer *_longPressGestureRecognizer;
}
- (instancetype)initWithFrame:(CGRect)frame
@@ -80,10 +82,18 @@ using namespace facebook::react;
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
{
const auto &paragraphProps = std::static_pointer_cast<const ParagraphProps>(props);
auto const &oldParagraphProps = *std::static_pointer_cast<ParagraphProps const>(_props);
auto const &newParagraphProps = *std::static_pointer_cast<ParagraphProps const>(props);
assert(paragraphProps);
_paragraphAttributes = paragraphProps->paragraphAttributes;
_paragraphAttributes = newParagraphProps.paragraphAttributes;
if (newParagraphProps.isSelectable != oldParagraphProps.isSelectable) {
if (newParagraphProps.isSelectable) {
[self enableContextMenu];
} else {
[self disableContextMenu];
}
}
[super updateProps:props oldProps:oldProps];
}
@@ -204,6 +214,77 @@ using namespace facebook::react;
return std::static_pointer_cast<const TouchEventEmitter>(eventEmitter);
}
#pragma mark - Context Menu
- (void)enableContextMenu
{
_longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleLongPress:)];
[self addGestureRecognizer:_longPressGestureRecognizer];
}
- (void)disableContextMenu
{
[self removeGestureRecognizer:_longPressGestureRecognizer];
_longPressGestureRecognizer = nil;
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
// TODO: Adopt showMenuFromRect (necessary for UIKitForMac)
#if !TARGET_OS_UIKITFORMAC
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController.isMenuVisible) {
return;
}
if (!self.isFirstResponder) {
[self becomeFirstResponder];
}
[menuController setTargetRect:self.bounds inView:self];
[menuController setMenuVisible:YES animated:YES];
#endif
}
- (BOOL)canBecomeFirstResponder
{
auto const &paragraphProps = *std::static_pointer_cast<ParagraphProps const>(_props);
return paragraphProps.isSelectable;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
auto const &paragraphProps = *std::static_pointer_cast<ParagraphProps const>(_props);
if (paragraphProps.isSelectable && action == @selector(copy:)) {
return YES;
}
return [self.nextResponder canPerformAction:action withSender:sender];
}
- (void)copy:(id)sender
{
NSAttributedString *attributedText = self.attributedText;
NSMutableDictionary *item = [NSMutableDictionary new];
NSData *rtf = [attributedText dataFromRange:NSMakeRange(0, attributedText.length)
documentAttributes:@{NSDocumentTypeDocumentAttribute : NSRTFDTextDocumentType}
error:nil];
if (rtf) {
[item setObject:rtf forKey:(id)kUTTypeFlatRTFD];
}
[item setObject:attributedText.string forKey:(id)kUTTypeUTF8PlainText];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.items = @[ item ];
}
@end
Class<RCTComponentViewProtocol> RCTParagraphCls(void)
@@ -24,8 +24,11 @@ ParagraphProps::ParagraphProps(
BaseTextProps(sourceProps, rawProps),
paragraphAttributes(
convertRawProp(rawProps, sourceProps.paragraphAttributes, {})),
isSelectable(
convertRawProp(rawProps, "selectable", sourceProps.isSelectable, {})),
isSelectable(convertRawProp(
rawProps,
"selectable",
sourceProps.isSelectable,
false)),
onTextLayout(convertRawProp(
rawProps,
"onTextLayout",