From 5755129c19d96de8e6cbc0a7b96a913650635170 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Thu, 12 Dec 2019 12:46:33 -0800 Subject: [PATCH] Fabric: Conversion function between `NSAttributedString` and `AttributedStringBox` Summary: It's nice to have those conversions between NSAttributedString and AttributedTextBox in some utils module because: An empty string must be stored as an empty C++ string, not like an empty NSAttributedString. That allows deferring this property from the object without accessing Objective-C runtime; It's nice to hide some tedious Objective-C object wrapping/unwrapping boilerplate. Changelog: [Internal] Fabric-specific internal change. Reviewed By: sammy-SC Differential Revision: D18950430 fbshipit-source-id: 842c202f243da17c47bc5cca9df6722cdcec07c5 --- ReactCommon/fabric/textlayoutmanager/BUCK | 1 + .../platform/ios/RCTAttributedTextUtils.h | 9 ++++++++- .../platform/ios/RCTAttributedTextUtils.mm | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ReactCommon/fabric/textlayoutmanager/BUCK b/ReactCommon/fabric/textlayoutmanager/BUCK index dccc6ad587a..0f2f7fa731c 100644 --- a/ReactCommon/fabric/textlayoutmanager/BUCK +++ b/ReactCommon/fabric/textlayoutmanager/BUCK @@ -125,6 +125,7 @@ rn_xplat_cxx_library( YOGA_CXX_TARGET, react_native_xplat_target("fabric/attributedstring:attributedstring"), react_native_xplat_target("fabric/core:core"), + react_native_xplat_target("utils:utils"), react_native_xplat_target("fabric/debug:debug"), react_native_xplat_target("fabric/graphics:graphics"), react_native_xplat_target("fabric/uimanager:uimanager"), diff --git a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.h b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.h index 406d8257d38..dd142aa3106 100644 --- a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.h +++ b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.h @@ -8,6 +8,7 @@ #import #include +#include #include NS_ASSUME_NONNULL_BEGIN @@ -25,7 +26,13 @@ NSDictionary *RCTNSTextAttributesFromTextAttributes( * Conversions amond `NSAttributedString`, `AttributedString` and `AttributedStringBox`. */ NSAttributedString *RCTNSAttributedStringFromAttributedString( - const facebook::react::AttributedString &attributedString); + facebook::react::AttributedString const &attributedString); + +NSAttributedString *RCTNSAttributedStringFromAttributedStringBox( + facebook::react::AttributedStringBox const &attributedStringBox); + +facebook::react::AttributedStringBox RCTAttributedStringBoxFromNSAttributedString( + NSAttributedString *nsAttributedString); @interface RCTWeakEventEmitterWrapper : NSObject @property (nonatomic, assign) facebook::react::SharedEventEmitter eventEmitter; diff --git a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm index d752c3806f1..17149b70ad2 100644 --- a/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm +++ b/ReactCommon/fabric/textlayoutmanager/platform/ios/RCTAttributedTextUtils.mm @@ -11,6 +11,7 @@ #include #include #include +#include using namespace facebook::react; @@ -268,3 +269,18 @@ NSAttributedString *RCTNSAttributedStringFromAttributedString(const AttributedSt return nsAttributedString; } + +NSAttributedString *RCTNSAttributedStringFromAttributedStringBox(AttributedStringBox const &attributedStringBox) +{ + switch (attributedStringBox.getMode()) { + case AttributedStringBox::Mode::Value: + return RCTNSAttributedStringFromAttributedString(attributedStringBox.getValue()); + case AttributedStringBox::Mode::OpaquePointer: + return (NSAttributedString *)unwrapManagedObject(attributedStringBox.getOpaquePointer()); + } +} + +AttributedStringBox RCTAttributedStringBoxFromNSAttributedString(NSAttributedString *nsAttributedString) +{ + return nsAttributedString.length ? AttributedStringBox{wrapManagedObject(nsAttributedString)} : AttributedStringBox{}; +}