Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract fragment conversions to separate functions #42597

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading