From 2722f9572018cfcae41457fa8a40386e2272551e Mon Sep 17 00:00:00 2001 From: Jakub Trzebiatowski Date: Tue, 23 Jan 2024 00:48:38 -0800 Subject: [PATCH] Extract fragment conversions to separate functions (#42597) Summary: Extract fragment conversions to separate functions to make refactoring easier and simplify reasoning about the code. This code is being modified later. This is a minor improvement in the context of my multi-PR work on https://github.com/react-native-community/discussions-and-proposals/issues/695. ## Changelog: [INTERNAL] [CHANGE] - Extract fragment conversions to separate functions Pull Request resolved: https://github.com/facebook/react-native/pull/42597 Reviewed By: NickGerleman Differential Revision: D52960655 Pulled By: robhogan fbshipit-source-id: 0df62b9980c06a1c2fc113d645ba8b6b668fa394 --- .../renderer/attributedstring/conversions.h | 74 ++++++++++--------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h b/packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h index fd3dedfc585f18..2edfdf69c44646 100644 --- a/packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h +++ b/packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h @@ -896,24 +896,28 @@ inline folly::dynamic toDynamic(const TextAttributes& textAttributes) { return _textAttributes; } +inline folly::dynamic toDynamic(const AttributedString::Fragment& fragment) { + folly::dynamic value = folly::dynamic::object(); + + value["string"] = fragment.string; + if (fragment.parentShadowView.componentHandle) { + value["reactTag"] = fragment.parentShadowView.tag; + } + if (fragment.isAttachment()) { + value["isAttachment"] = true; + value["width"] = fragment.parentShadowView.layoutMetrics.frame.size.width; + value["height"] = fragment.parentShadowView.layoutMetrics.frame.size.height; + } + value["textAttributes"] = toDynamic(fragment.textAttributes); + + return value; +} + inline folly::dynamic toDynamic(const AttributedString& attributedString) { auto value = folly::dynamic::object(); auto fragments = folly::dynamic::array(); for (auto fragment : attributedString.getFragments()) { - folly::dynamic dynamicFragment = folly::dynamic::object(); - dynamicFragment["string"] = fragment.string; - if (fragment.parentShadowView.componentHandle) { - dynamicFragment["reactTag"] = fragment.parentShadowView.tag; - } - if (fragment.isAttachment()) { - dynamicFragment["isAttachment"] = true; - dynamicFragment["width"] = - fragment.parentShadowView.layoutMetrics.frame.size.width; - dynamicFragment["height"] = - fragment.parentShadowView.layoutMetrics.frame.size.height; - } - dynamicFragment["textAttributes"] = toDynamic(fragment.textAttributes); - fragments.push_back(dynamicFragment); + fragments.push_back(toDynamic(fragment)); } value("fragments", fragments); value( @@ -1134,31 +1138,33 @@ inline MapBuffer toMapBuffer(const TextAttributes& textAttributes) { return builder.build(); } +inline MapBuffer toMapBuffer(const AttributedString::Fragment& fragment) { + auto builder = MapBufferBuilder(); + + builder.putString(FR_KEY_STRING, fragment.string); + if (fragment.parentShadowView.componentHandle) { + builder.putInt(FR_KEY_REACT_TAG, fragment.parentShadowView.tag); + } + if (fragment.isAttachment()) { + builder.putBool(FR_KEY_IS_ATTACHMENT, true); + builder.putDouble( + FR_KEY_WIDTH, fragment.parentShadowView.layoutMetrics.frame.size.width); + builder.putDouble( + FR_KEY_HEIGHT, + fragment.parentShadowView.layoutMetrics.frame.size.height); + } + auto textAttributesMap = toMapBuffer(fragment.textAttributes); + builder.putMapBuffer(FR_KEY_TEXT_ATTRIBUTES, textAttributesMap); + + return builder.build(); +} + inline MapBuffer toMapBuffer(const AttributedString& attributedString) { auto fragmentsBuilder = MapBufferBuilder(); int index = 0; for (auto fragment : attributedString.getFragments()) { - auto dynamicFragmentBuilder = MapBufferBuilder(); - dynamicFragmentBuilder.putString(FR_KEY_STRING, fragment.string); - if (fragment.parentShadowView.componentHandle) { - dynamicFragmentBuilder.putInt( - FR_KEY_REACT_TAG, fragment.parentShadowView.tag); - } - if (fragment.isAttachment()) { - dynamicFragmentBuilder.putBool(FR_KEY_IS_ATTACHMENT, true); - dynamicFragmentBuilder.putDouble( - FR_KEY_WIDTH, - fragment.parentShadowView.layoutMetrics.frame.size.width); - dynamicFragmentBuilder.putDouble( - FR_KEY_HEIGHT, - fragment.parentShadowView.layoutMetrics.frame.size.height); - } - auto textAttributesMap = toMapBuffer(fragment.textAttributes); - dynamicFragmentBuilder.putMapBuffer( - FR_KEY_TEXT_ATTRIBUTES, textAttributesMap); - auto dynamicFragmentMap = dynamicFragmentBuilder.build(); - fragmentsBuilder.putMapBuffer(index++, dynamicFragmentMap); + fragmentsBuilder.putMapBuffer(index++, toMapBuffer(fragment)); } auto builder = MapBufferBuilder();