Skip to content

Commit

Permalink
Fabric: New props treatment in attributedstring module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shergin authored and facebook-github-bot committed May 14, 2018
1 parent 7048c91 commit 9d21e66
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 271 deletions.
6 changes: 2 additions & 4 deletions ReactCommon/fabric/attributedstring/ParagraphAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/fabric/attributedstring/ParagraphAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/fabric/attributedstring/TextAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/fabric/attributedstring/TextAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
192 changes: 192 additions & 0 deletions ReactCommon/fabric/attributedstring/conversions.h
Original file line number Diff line number Diff line change
@@ -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
27 changes: 0 additions & 27 deletions ReactCommon/fabric/attributedstring/debugStringConvertibleUtils.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@

#include <gtest/gtest.h>

#include <fabric/attributedstring/TextPrimitives.h>

using namespace facebook::react;

TEST(AttributedStringTest, testSomething) {
// TODO
}
Loading

0 comments on commit 9d21e66

Please sign in to comment.