Skip to content

Commit

Permalink
Extract fragment conversions to separate functions (#42597)
Browse files Browse the repository at this point in the history
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 react-native-community/discussions-and-proposals#695.

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

[INTERNAL] [CHANGE] - Extract fragment conversions to separate functions

Pull Request resolved: #42597

Reviewed By: NickGerleman

Differential Revision: D52960655

Pulled By: robhogan

fbshipit-source-id: 0df62b9980c06a1c2fc113d645ba8b6b668fa394
  • Loading branch information
cubuspl42 authored and facebook-github-bot committed Jan 23, 2024
1 parent e27452a commit 2722f95
Showing 1 changed file with 40 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2722f95

Please sign in to comment.