-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iOS: handle RCTParagraphComponentView components in new arch
- Loading branch information
1 parent
97f26f3
commit b0a43a3
Showing
8 changed files
with
296 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
packages/react-native-session-replay/ios/Sources/RCTFabricWrapper.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
#import <Foundation/Foundation.h> | ||
#import "RCTTextPropertiesWrapper.h" | ||
|
||
@interface RCTFabricWrapper : NSObject | ||
|
||
- (nullable RCTTextPropertiesWrapper*)tryToExtractTextPropertiesFromView:(UIView* _Nonnull)view; | ||
|
||
@end |
109 changes: 109 additions & 0 deletions
109
packages/react-native-session-replay/ios/Sources/RCTFabricWrapper.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
#import "RCTFabricWrapper.h" | ||
|
||
#if RCT_NEW_ARCH_ENABLED | ||
#import <React-RCTFabric/React/RCTParagraphComponentView.h> | ||
#import <React-Fabric/react/renderer/components/text/ParagraphProps.h> | ||
namespace rct = facebook::react; | ||
#endif | ||
|
||
@implementation RCTFabricWrapper | ||
/** | ||
* Extracts the text properties from the given UIView when the view is of type RCTParagraphComponentView, returns nil otherwise. | ||
*/ | ||
- (nullable RCTTextPropertiesWrapper*)tryToExtractTextPropertiesFromView:(UIView *)view { | ||
#if RCT_NEW_ARCH_ENABLED | ||
if (![view isKindOfClass:[RCTParagraphComponentView class]]) { | ||
return nil; | ||
} | ||
|
||
// Cast view to RCTParagraphComponentView | ||
RCTParagraphComponentView* paragraphComponentView = (RCTParagraphComponentView *)view; | ||
if (paragraphComponentView == nil) { | ||
return nil; | ||
} | ||
|
||
// Retrieve ParagraphProps from shared pointer | ||
const rct::ParagraphProps* props = (rct::ParagraphProps*)paragraphComponentView.props.get(); | ||
if (props == nil) { | ||
return nil; | ||
} | ||
|
||
// Extract Attributes | ||
RCTTextPropertiesWrapper* textPropertiesWrapper = [[RCTTextPropertiesWrapper alloc] init]; | ||
textPropertiesWrapper.text = [RCTFabricWrapper getTextFromView:paragraphComponentView]; | ||
textPropertiesWrapper.contentRect = paragraphComponentView.bounds; | ||
|
||
rct::TextAttributes textAttributes = props->textAttributes; | ||
textPropertiesWrapper.alignment = [RCTFabricWrapper getAlignmentFromAttributes:textAttributes]; | ||
textPropertiesWrapper.foregroundColor = [RCTFabricWrapper getForegroundColorFromAttributes:textAttributes]; | ||
textPropertiesWrapper.fontSize = [RCTFabricWrapper getFontSizeFromAttributes:textAttributes]; | ||
|
||
return textPropertiesWrapper; | ||
#else | ||
return nil; | ||
#endif | ||
} | ||
|
||
#if RCT_NEW_ARCH_ENABLED | ||
+ (NSString* _Nonnull)getTextFromView:(RCTParagraphComponentView*)view { | ||
if (view == nil || view.attributedText == nil) { | ||
return RCTTextPropertiesDefaultText; | ||
} | ||
|
||
return view.attributedText.string; | ||
} | ||
|
||
+ (NSTextAlignment)getAlignmentFromAttributes:(rct::TextAttributes)textAttributes { | ||
const rct::TextAlignment alignment = textAttributes.alignment.has_value() ? | ||
textAttributes.alignment.value() : | ||
rct::TextAlignment::Natural; | ||
|
||
switch (alignment) { | ||
case rct::TextAlignment::Natural: | ||
return NSTextAlignmentNatural; | ||
|
||
case rct::TextAlignment::Left: | ||
return NSTextAlignmentLeft; | ||
|
||
case rct::TextAlignment::Center: | ||
return NSTextAlignmentCenter; | ||
|
||
case rct::TextAlignment::Right: | ||
return NSTextAlignmentRight; | ||
|
||
case rct::TextAlignment::Justified: | ||
return NSTextAlignmentJustified; | ||
|
||
default: | ||
return RCTTextPropertiesDefaultAlignment; | ||
} | ||
} | ||
|
||
+ (UIColor* _Nonnull)getForegroundColorFromAttributes:(rct::TextAttributes)textAttributes { | ||
@try { | ||
rct::Color color = *textAttributes.foregroundColor; | ||
UIColor* uiColor = (__bridge UIColor*)color.getUIColor().get(); | ||
if (uiColor != nil) { | ||
return uiColor; | ||
} | ||
} @catch (NSException *exception) { | ||
// TODO: Add telemetry? | ||
NSLog(@"Caught Objective-C exception while unwrapping text color property: %@", exception); | ||
} | ||
|
||
return RCTTextPropertiesDefaultForegroundColor; | ||
} | ||
|
||
+ (CGFloat)getFontSizeFromAttributes:(rct::TextAttributes)textAttributes { | ||
// Float is just an alias for CGFloat, but this could change in the future. | ||
_Static_assert(sizeof(rct::Float) == sizeof(CGFloat), "Float and CGFloat are expected to have the same size."); | ||
return isnan(textAttributes.fontSize) ? RCTTextPropertiesDefaultFontSize : (CGFloat)textAttributes.fontSize; | ||
} | ||
#endif | ||
@end |
23 changes: 23 additions & 0 deletions
23
packages/react-native-session-replay/ios/Sources/RCTTextPropertiesWrapper.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
@interface RCTTextPropertiesWrapper : NSObject | ||
|
||
extern NSString* const RCTTextPropertiesDefaultText; | ||
extern NSTextAlignment const RCTTextPropertiesDefaultAlignment; | ||
extern UIColor* const RCTTextPropertiesDefaultForegroundColor; | ||
extern CGFloat const RCTTextPropertiesDefaultFontSize; | ||
extern CGRect const RCTTextPropertiesDefaultContentRect; | ||
|
||
@property (nonatomic, strong, nonnull) NSString* text; | ||
@property (nonatomic, assign) NSTextAlignment alignment; | ||
@property (nonatomic, strong, nonnull) UIColor* foregroundColor; | ||
@property (nonatomic, assign) CGFloat fontSize; | ||
@property (nonatomic, assign) CGRect contentRect; | ||
|
||
- (instancetype _Nonnull) init; | ||
|
||
@end |
28 changes: 28 additions & 0 deletions
28
packages/react-native-session-replay/ios/Sources/RCTTextPropertiesWrapper.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
#import "RCTTextPropertiesWrapper.h" | ||
|
||
@implementation RCTTextPropertiesWrapper | ||
|
||
NSString* const RCTTextPropertiesDefaultText = @""; | ||
NSTextAlignment const RCTTextPropertiesDefaultAlignment = NSTextAlignmentNatural; | ||
UIColor* const RCTTextPropertiesDefaultForegroundColor = [UIColor blackColor]; | ||
CGFloat const RCTTextPropertiesDefaultFontSize = 14.0; | ||
CGRect const RCTTextPropertiesDefaultContentRect = CGRectZero; | ||
|
||
- (instancetype)init { | ||
self = [super init]; | ||
if (self) { | ||
_text = RCTTextPropertiesDefaultText; | ||
_alignment = RCTTextPropertiesDefaultAlignment; | ||
_foregroundColor = RCTTextPropertiesDefaultForegroundColor; | ||
_fontSize = RCTTextPropertiesDefaultFontSize; | ||
_contentRect = RCTTextPropertiesDefaultContentRect; | ||
} | ||
return self; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.