Fabric: New props treatment in `attributedstring` module

Summary:
Adopting template-generated `convertRawProp` and `debugStringConvertibleItem` functions in `attributedstring` module.
Note, to do so we have to change signatures of some conversions functions to make them more overloading-friendly.

Reviewed By: fkgozali

Differential Revision: D7958245

fbshipit-source-id: 275a58bd3955a6ceb4881bffff86bf1d4501b3d2
This commit is contained in:
Valentin Shergin 2018-05-14 15:43:45 -07:00 committed by Facebook Github Bot
parent 7048c9134a
commit 9d21e6661a
14 changed files with 199 additions and 271 deletions

View File

@ -7,10 +7,8 @@
#include "ParagraphAttributes.h"
#include <fabric/attributedstring/textValuesConversions.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include "debugStringConvertibleUtils.h"
#include <fabric/attributedstring/conversions.h>
#include <fabric/debug/debugStringConvertibleUtils.h>
namespace facebook {
namespace react {

View File

@ -9,7 +9,7 @@
#include <limits>
#include <fabric/attributedstring/TextPrimitives.h>
#include <fabric/attributedstring/primitives.h>
#include <fabric/debug/DebugStringConvertible.h>
#include <fabric/graphics/Geometry.h>

View File

@ -7,10 +7,10 @@
#include "TextAttributes.h"
#include <fabric/attributedstring/conversions.h>
#include <fabric/core/debugStringConvertibleUtils.h>
#include <fabric/graphics/debugStringConvertibleUtils.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include "debugStringConvertibleUtils.h"
namespace facebook {
namespace react {

View File

@ -9,7 +9,7 @@
#include <limits>
#include <fabric/attributedstring/TextPrimitives.h>
#include <fabric/attributedstring/primitives.h>
#include <fabric/core/LayoutPrimitives.h>
#include <fabric/core/ReactPrimitives.h>
#include <fabric/debug/DebugStringConvertible.h>

View File

@ -0,0 +1,192 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <fabric/attributedstring/primitives.h>
#include <fabric/graphics/Geometry.h>
#include <folly/dynamic.h>
namespace facebook {
namespace react {
inline std::string toString(const EllipsizeMode &ellipsisMode) {
switch (ellipsisMode) {
case EllipsizeMode::Clip: return "clip";
case EllipsizeMode::Head: return "head";
case EllipsizeMode::Tail: return "tail";
case EllipsizeMode::Middle: return "middle";
}
}
inline void fromDynamic(const folly::dynamic &value, EllipsizeMode &result) {
auto string = value.getString();
if (string == "clip") { result = EllipsizeMode::Clip; return; }
if (string == "head") { result = EllipsizeMode::Head; return; }
if (string == "tail") { result = EllipsizeMode::Tail; return; }
if (string == "middle") { result = EllipsizeMode::Middle; return; }
abort();
}
inline void fromDynamic(const folly::dynamic &value, FontWeight &result) {
auto string = value.asString();
if (string == "normal") { result = FontWeight::Regular; return; }
if (string == "regular") { result = FontWeight::Regular; return; }
if (string == "bold") { result = FontWeight::Bold; return; }
if (string == "100") { result = FontWeight::Weight100; return; }
if (string == "200") { result = FontWeight::Weight200; return; }
if (string == "300") { result = FontWeight::Weight300; return; }
if (string == "400") { result = FontWeight::Weight400; return; }
if (string == "500") { result = FontWeight::Weight500; return; }
if (string == "600") { result = FontWeight::Weight600; return; }
if (string == "700") { result = FontWeight::Weight700; return; }
if (string == "800") { result = FontWeight::Weight800; return; }
if (string == "900") { result = FontWeight::Weight900; return; }
abort();
}
inline std::string toString(const FontWeight &fontWeight) {
return std::to_string((int)fontWeight);
}
inline void fromDynamic(const folly::dynamic &value, FontStyle &result) {
auto string = value.asString();
if (string == "normal") { result = FontStyle::Normal; return; }
if (string == "italic") { result = FontStyle::Italic; return; }
if (string == "oblique") { result = FontStyle::Oblique; return; }
abort();
}
inline std::string toString(const FontStyle &fontStyle) {
switch (fontStyle) {
case FontStyle::Normal: return "normal";
case FontStyle::Italic: return "italic";
case FontStyle::Oblique: return "oblique";
}
}
inline void fromDynamic(const folly::dynamic &value, FontVariant &result) {
assert(value.isArray());
result = FontVariant::Default;
for (auto &&item : value) {
auto string = item.asString();
if (string == "small-caps") { result = (FontVariant)((int)result | (int)FontVariant::SmallCaps); continue; }
if (string == "oldstyle-nums") { result = (FontVariant)((int)result | (int)FontVariant::OldstyleNums); continue; }
if (string == "lining-nums") { result = (FontVariant)((int)result | (int)FontVariant::LiningNums); continue; }
if (string == "tabular-nums") { result = (FontVariant)((int)result | (int)FontVariant::TabularNums); continue; }
if (string == "proportional-nums") { result = (FontVariant)((int)result | (int)FontVariant::ProportionalNums); continue; }
}
}
inline std::string toString(const FontVariant &fontVariant) {
std::string result;
std::string separator = ", ";
if ((int)fontVariant & (int)FontVariant::SmallCaps) { result += "small-caps" + separator; }
if ((int)fontVariant & (int)FontVariant::OldstyleNums) { result += "oldstyle-nums" + separator; }
if ((int)fontVariant & (int)FontVariant::LiningNums) { result += "lining-nums" + separator; }
if ((int)fontVariant & (int)FontVariant::TabularNums) { result += "tabular-nums" + separator; }
if ((int)fontVariant & (int)FontVariant::ProportionalNums) { result += "proportional-nums" + separator; }
if (!result.empty()) {
result.erase(result.length() - separator.length());
}
return result;
}
inline void fromDynamic(const folly::dynamic &value, TextAlignment &result) {
auto string = value.asString();
if (string == "natural") { result = TextAlignment::Natural; return; }
if (string == "left") { result = TextAlignment::Left; return; }
if (string == "center") { result = TextAlignment::Center; return; }
if (string == "right") { result = TextAlignment::Right; return; }
if (string == "justified") { result = TextAlignment::Justified; return; }
abort();
}
inline std::string toString(const TextAlignment &textAlignment) {
switch (textAlignment) {
case TextAlignment::Natural: return "natural";
case TextAlignment::Left: return "left";
case TextAlignment::Center: return "center";
case TextAlignment::Right: return "right";
case TextAlignment::Justified: return "justified";
}
}
inline void fromDynamic(const folly::dynamic &value, WritingDirection &result) {
auto string = value.asString();
if (string == "natural") { result = WritingDirection::Natural; return; }
if (string == "ltr") { result = WritingDirection::LeftToRight; return; }
if (string == "rtl") { result = WritingDirection::RightToLeft; return; }
abort();
}
inline std::string toString(const WritingDirection &writingDirection) {
switch (writingDirection) {
case WritingDirection::Natural: return "natural";
case WritingDirection::LeftToRight: return "ltr";
case WritingDirection::RightToLeft: return "rtl";
}
}
inline void fromDynamic(const folly::dynamic &value, TextDecorationLineType &result) {
auto string = value.asString();
if (string == "none") { result = TextDecorationLineType::None; return; }
if (string == "underline") { result = TextDecorationLineType::Underline; return; }
if (string == "strikethrough") { result = TextDecorationLineType::Strikethrough; return; }
if (string == "underline-strikethrough") { result = TextDecorationLineType::UnderlineStrikethrough; return; }
abort();
}
inline std::string toString(const TextDecorationLineType &textDecorationLineType) {
switch (textDecorationLineType) {
case TextDecorationLineType::None: return "none";
case TextDecorationLineType::Underline: return "underline";
case TextDecorationLineType::Strikethrough: return "strikethrough";
case TextDecorationLineType::UnderlineStrikethrough: return "underline-strikethrough";
}
}
inline void fromDynamic(const folly::dynamic &value, TextDecorationLineStyle &result) {
auto string = value.asString();
if (string == "single") { result = TextDecorationLineStyle::Single; return; }
if (string == "thick") { result = TextDecorationLineStyle::Thick; return; }
if (string == "double") { result = TextDecorationLineStyle::Double; return; }
abort();
}
inline std::string toString(const TextDecorationLineStyle &textDecorationLineStyle) {
switch (textDecorationLineStyle) {
case TextDecorationLineStyle::Single: return "single";
case TextDecorationLineStyle::Thick: return "thick";
case TextDecorationLineStyle::Double: return "double";
}
}
inline void fromDynamic(const folly::dynamic &value, TextDecorationLinePattern &result) {
auto string = value.asString();
if (string == "solid") { result = TextDecorationLinePattern::Solid; return; }
if (string == "dot") { result = TextDecorationLinePattern::Dot; return; }
if (string == "dash") { result = TextDecorationLinePattern::Dash; return; }
if (string == "dash-dot") { result = TextDecorationLinePattern::DashDot; return; }
if (string == "dash-dot-dot") { result = TextDecorationLinePattern::DashDotDot; return; }
abort();
}
inline std::string toString(const TextDecorationLinePattern &textDecorationLinePattern) {
switch (textDecorationLinePattern) {
case TextDecorationLinePattern::Solid: return "solid";
case TextDecorationLinePattern::Dot: return "dot";
case TextDecorationLinePattern::Dash: return "dash";
case TextDecorationLinePattern::DashDot: return "dash-dot";
case TextDecorationLinePattern::DashDotDot: return "dash-dot-dot";
}
}
} // namespace react
} // namespace facebook

View File

@ -1,27 +0,0 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <fabric/debug/debugStringConvertibleUtils.h>
#include <fabric/attributedstring/textValuesConversions.h>
namespace facebook {
namespace react {
DEBUG_STRING_CONVERTIBLE_TEMPLATE(EllipsizeMode, stringFromEllipsizeMode)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(FontWeight, stringFromFontWeight)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(FontStyle, stringFromFontStyle)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(FontVariant, stringFromFontVariant)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(TextAlignment, stringFromTextAlignment)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(WritingDirection, stringFromWritingDirection)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(TextDecorationLineType, stringFromTextDecorationLineType)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(TextDecorationLineStyle, stringFromTextDecorationLineStyle)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(TextDecorationLinePattern, stringFromTextDecorationLinePattern)
} // namespace react
} // namespace facebook

View File

@ -9,10 +9,6 @@
#include <gtest/gtest.h>
#include <fabric/attributedstring/TextPrimitives.h>
using namespace facebook::react;
TEST(AttributedStringTest, testSomething) {
// TODO
}

View File

@ -1,193 +0,0 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <fabric/attributedstring/TextPrimitives.h>
#include <fabric/graphics/Geometry.h>
#include <folly/dynamic.h>
namespace facebook {
namespace react {
inline std::string stringFromEllipsizeMode(const EllipsizeMode &ellipsisMode) {
switch (ellipsisMode) {
case EllipsizeMode::Clip: return "clip";
case EllipsizeMode::Head: return "head";
case EllipsizeMode::Tail: return "tail";
case EllipsizeMode::Middle: return "middle";
}
}
inline EllipsizeMode ellipsizeModeFromDynamic(const folly::dynamic &value) {
auto string = value.getString();
if (string == "clip") { return EllipsizeMode::Clip; }
if (string == "head") { return EllipsizeMode::Head; }
if (string == "tail") { return EllipsizeMode::Tail; }
if (string == "middle") { return EllipsizeMode::Middle; }
abort();
}
inline FontWeight fontWeightFromDynamic(const folly::dynamic &value) {
auto string = value.asString();
if (string == "normal") { return FontWeight::Regular; }
if (string == "regular") { return FontWeight::Regular; }
if (string == "bold") { return FontWeight::Bold; }
if (string == "100") { return FontWeight::Weight100; }
if (string == "200") { return FontWeight::Weight200; }
if (string == "300") { return FontWeight::Weight300; }
if (string == "400") { return FontWeight::Weight400; }
if (string == "500") { return FontWeight::Weight500; }
if (string == "600") { return FontWeight::Weight600; }
if (string == "700") { return FontWeight::Weight700; }
if (string == "800") { return FontWeight::Weight800; }
if (string == "900") { return FontWeight::Weight900; }
abort();
}
inline std::string stringFromFontWeight(const FontWeight &fontWeight) {
return std::to_string((int)fontWeight);
}
inline FontStyle fontStyleFromDynamic(const folly::dynamic &value) {
auto string = value.asString();
if (string == "normal") { return FontStyle::Normal; }
if (string == "italic") { return FontStyle::Italic; }
if (string == "oblique") { return FontStyle::Oblique; }
abort();
}
inline std::string stringFromFontStyle(const FontStyle &fontStyle) {
switch (fontStyle) {
case FontStyle::Normal: return "normal";
case FontStyle::Italic: return "italic";
case FontStyle::Oblique: return "oblique";
}
}
inline FontVariant fontVariantFromDynamic(const folly::dynamic &value) {
assert(value.isArray());
FontVariant fontVariant = FontVariant::Default;
for (auto &&item : value) {
auto string = item.asString();
if (string == "small-caps") { fontVariant = (FontVariant)((int)fontVariant | (int)FontVariant::SmallCaps); continue; }
if (string == "oldstyle-nums") { fontVariant = (FontVariant)((int)fontVariant | (int)FontVariant::OldstyleNums); continue; }
if (string == "lining-nums") { fontVariant = (FontVariant)((int)fontVariant | (int)FontVariant::LiningNums); continue; }
if (string == "tabular-nums") { fontVariant = (FontVariant)((int)fontVariant | (int)FontVariant::TabularNums); continue; }
if (string == "proportional-nums") { fontVariant = (FontVariant)((int)fontVariant | (int)FontVariant::ProportionalNums); continue; }
}
return fontVariant;
}
inline std::string stringFromFontVariant(const FontVariant &fontVariant) {
std::string result;
std::string separator = ", ";
if ((int)fontVariant & (int)FontVariant::SmallCaps) { result += "small-caps" + separator; }
if ((int)fontVariant & (int)FontVariant::OldstyleNums) { result += "oldstyle-nums" + separator; }
if ((int)fontVariant & (int)FontVariant::LiningNums) { result += "lining-nums" + separator; }
if ((int)fontVariant & (int)FontVariant::TabularNums) { result += "tabular-nums" + separator; }
if ((int)fontVariant & (int)FontVariant::ProportionalNums) { result += "proportional-nums" + separator; }
if (!result.empty()) {
result.erase(result.length() - separator.length());
}
return result;
}
inline TextAlignment textAlignmentFromDynamic(const folly::dynamic &value) {
auto string = value.asString();
if (string == "natural") { return TextAlignment::Natural; }
if (string == "left") { return TextAlignment::Left; }
if (string == "center") { return TextAlignment::Center; }
if (string == "right") { return TextAlignment::Right; }
if (string == "justified") { return TextAlignment::Justified; }
abort();
}
inline std::string stringFromTextAlignment(const TextAlignment &textAlignment) {
switch (textAlignment) {
case TextAlignment::Natural: return "natural";
case TextAlignment::Left: return "left";
case TextAlignment::Center: return "center";
case TextAlignment::Right: return "right";
case TextAlignment::Justified: return "justified";
}
}
inline WritingDirection writingDirectionFromDynamic(const folly::dynamic &value) {
auto string = value.asString();
if (string == "natural") { return WritingDirection::Natural; }
if (string == "ltr") { return WritingDirection::LeftToRight; }
if (string == "rtl") { return WritingDirection::RightToLeft; }
abort();
}
inline std::string stringFromWritingDirection(const WritingDirection &writingDirection) {
switch (writingDirection) {
case WritingDirection::Natural: return "natural";
case WritingDirection::LeftToRight: return "ltr";
case WritingDirection::RightToLeft: return "rtl";
}
}
inline TextDecorationLineType textDecorationLineTypeFromDynamic(const folly::dynamic &value) {
auto string = value.asString();
if (string == "none") { return TextDecorationLineType::None; }
if (string == "underline") { return TextDecorationLineType::Underline; }
if (string == "strikethrough") { return TextDecorationLineType::Strikethrough; }
if (string == "underline-strikethrough") { return TextDecorationLineType::UnderlineStrikethrough; }
abort();
}
inline std::string stringFromTextDecorationLineType(const TextDecorationLineType &textDecorationLineType) {
switch (textDecorationLineType) {
case TextDecorationLineType::None: return "none";
case TextDecorationLineType::Underline: return "underline";
case TextDecorationLineType::Strikethrough: return "strikethrough";
case TextDecorationLineType::UnderlineStrikethrough: return "underline-strikethrough";
}
}
inline TextDecorationLineStyle textDecorationLineStyleFromDynamic(const folly::dynamic &value) {
auto string = value.asString();
if (string == "single") { return TextDecorationLineStyle::Single; }
if (string == "thick") { return TextDecorationLineStyle::Thick; }
if (string == "double") { return TextDecorationLineStyle::Double; }
abort();
}
inline std::string stringFromTextDecorationLineStyle(const TextDecorationLineStyle &textDecorationLineStyle) {
switch (textDecorationLineStyle) {
case TextDecorationLineStyle::Single: return "single";
case TextDecorationLineStyle::Thick: return "thick";
case TextDecorationLineStyle::Double: return "double";
}
}
inline TextDecorationLinePattern textDecorationLinePatternFromDynamic(const folly::dynamic &value) {
auto string = value.asString();
if (string == "solid") { return TextDecorationLinePattern::Solid; }
if (string == "dot") { return TextDecorationLinePattern::Dot; }
if (string == "dash") { return TextDecorationLinePattern::Dash; }
if (string == "dash-dot") { return TextDecorationLinePattern::DashDot; }
if (string == "dash-dot-dot") { return TextDecorationLinePattern::DashDotDot; }
abort();
}
inline std::string stringFromTextDecorationLinePattern(const TextDecorationLinePattern &textDecorationLinePattern) {
switch (textDecorationLinePattern) {
case TextDecorationLinePattern::Solid: return "solid";
case TextDecorationLinePattern::Dot: return "dot";
case TextDecorationLinePattern::Dash: return "dash";
case TextDecorationLinePattern::DashDot: return "dash-dot";
case TextDecorationLinePattern::DashDotDot: return "dash-dot-dot";
}
}
} // namespace react
} // namespace facebook

View File

@ -7,11 +7,10 @@
#include "BaseTextProps.h"
#include <fabric/attributedstring/textValuesConversions.h>
#include <fabric/attributedstring/conversions.h>
#include <fabric/core/propsConversions.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include <fabric/text/propsConversions.h>
namespace facebook {
namespace react {

View File

@ -8,7 +8,6 @@
#include "ParagraphLocalData.h"
#include <fabric/debug/debugStringConvertibleUtils.h>
#include <fabric/attributedstring/debugStringConvertibleUtils.h>
namespace facebook {
namespace react {

View File

@ -7,10 +7,9 @@
#include "ParagraphProps.h"
#include <fabric/attributedstring/textValuesConversions.h>
#include <fabric/attributedstring/conversions.h>
#include <fabric/core/propsConversions.h>
#include <fabric/debug/debugStringConvertibleUtils.h>
#include <fabric/text/propsConversions.h>
namespace facebook {
namespace react {

View File

@ -1,29 +0,0 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <fabric/attributedstring/TextPrimitives.h>
#include <fabric/attributedstring/textValuesConversions.h>
#include <fabric/core/propsConversions.h>
#include <folly/dynamic.h>
namespace facebook {
namespace react {
CONVERT_RAW_PROP_TEMPLATE(EllipsizeMode, ellipsizeModeFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(FontWeight, fontWeightFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(FontStyle, fontStyleFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(FontVariant, fontVariantFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(WritingDirection, writingDirectionFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(TextAlignment, textAlignmentFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(TextDecorationLineType, textDecorationLineTypeFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(TextDecorationLineStyle, textDecorationLineStyleFromDynamic)
CONVERT_RAW_PROP_TEMPLATE(TextDecorationLinePattern, textDecorationLinePatternFromDynamic)
} // namespace react
} // namespace facebook

View File

@ -7,12 +7,6 @@
#include "TextProps.h"
#include <fabric/attributedstring/textValuesConversions.h>
#include <fabric/core/propsConversions.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include <fabric/text/propsConversions.h>
namespace facebook {
namespace react {