Introduce toDynamic conversion for AttributedString class

Summary: This diff introduces a way to convert AttributedString object to dynamic objects

Reviewed By: shergin

Differential Revision: D9801438

fbshipit-source-id: b762f54917ae90bf53c7f9d07f63b876d1265ece
This commit is contained in:
David Vacca 2018-09-16 23:46:09 -07:00 committed by Facebook Github Bot
parent 37e0f824f2
commit c5276ef816
2 changed files with 49 additions and 2 deletions

View File

@ -8,9 +8,10 @@
#pragma once #pragma once
#include <fabric/attributedstring/conversions.h> #include <fabric/attributedstring/conversions.h>
#include <fabric/attributedstring/primitives.h>
#include <fabric/attributedstring/AttributedString.h>
#include <fabric/attributedstring/ParagraphAttributes.h> #include <fabric/attributedstring/ParagraphAttributes.h>
#include <fabric/attributedstring/TextAttributes.h> #include <fabric/attributedstring/TextAttributes.h>
#include <fabric/attributedstring/primitives.h>
#include <fabric/core/conversions.h> #include <fabric/core/conversions.h>
#include <fabric/graphics/conversions.h> #include <fabric/graphics/conversions.h>
#include <fabric/graphics/Geometry.h> #include <fabric/graphics/Geometry.h>
@ -276,5 +277,19 @@ inline folly::dynamic toDynamic(const TextAttributes &textAttributes) {
return _textAttributes; return _textAttributes;
} }
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;
dynamicFragment["textAttributes"] = toDynamic(fragment.textAttributes);
fragments.push_back(dynamicFragment);
}
value("fragments", fragments);
value("string", attributedString.getString());
return value;
}
} // namespace react } // namespace react
} // namespace facebook } // namespace facebook

View File

@ -7,8 +7,40 @@
#include <memory> #include <memory>
#include <fabric/attributedstring/TextAttributes.h>
#include <fabric/attributedstring/conversions.h>
#include <fabric/attributedstring/primitives.h>
#include <fabric/graphics/conversions.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <assert.h>
#include <string>
#include <iostream>
namespace facebook {
namespace react {
TEST(AttributedStringTest, testSomething) { TEST(AttributedStringTest, testSomething) {
// TODO auto attString = new AttributedString();
auto fragment = new AttributedString::Fragment();
fragment->string = "test";
auto text = new TextAttributes();
text->foregroundColor = {colorFromComponents({100/255.0, 153/255.0, 200/255.0, 1.0})};
text->opacity = 0.5;
text->fontStyle = FontStyle::Italic;
text->fontWeight = FontWeight::Thin;
text->fontVariant = FontVariant::TabularNums;
fragment->textAttributes = *text;
attString->prependFragment(*fragment);
auto result = toDynamic(*attString);
assert(result["string"] == fragment->string);
auto textAttribute = result["fragments"][0]["textAttributes"];
assert(textAttribute["foregroundColor"] == toDynamic(text->foregroundColor));
assert(textAttribute["opacity"] == text->opacity);
assert(textAttribute["fontStyle"] == toString(*text->fontStyle));
assert(textAttribute["fontWeight"] == toString(*text->fontWeight));
}
}
} }