mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 11:57:46 +08:00
Fabric/Text: text module, text part
Summary: <Text> component is used to describe text attributes in React. Reviewed By: mdvacca Differential Revision: D7751851 fbshipit-source-id: f90a4367cad64283ee64828b0d5e24470ee3d9f7
This commit is contained in:
committed by
Facebook Github Bot
parent
02c3cd4c4e
commit
dc7a87e737
29
ReactCommon/fabric/text/propsConversions.h
Normal file
29
ReactCommon/fabric/text/propsConversions.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* 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 {
|
||||
|
||||
APPLY_RAW_PROP_TEMPLATE(EllipsizeMode, ellipsizeModeFromDynamic)
|
||||
APPLY_RAW_PROP_TEMPLATE(FontWeight, fontWeightFromDynamic)
|
||||
APPLY_RAW_PROP_TEMPLATE(FontStyle, fontStyleFromDynamic)
|
||||
APPLY_RAW_PROP_TEMPLATE(FontVariant, fontVariantFromDynamic)
|
||||
APPLY_RAW_PROP_TEMPLATE(WritingDirection, writingDirectionFromDynamic)
|
||||
APPLY_RAW_PROP_TEMPLATE(TextAlignment, textAlignmentFromDynamic)
|
||||
APPLY_RAW_PROP_TEMPLATE(TextDecorationLineType, textDecorationLineTypeFromDynamic)
|
||||
APPLY_RAW_PROP_TEMPLATE(TextDecorationLineStyle, textDecorationLineStyleFromDynamic)
|
||||
APPLY_RAW_PROP_TEMPLATE(TextDecorationLinePattern, textDecorationLinePatternFromDynamic)
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
@@ -9,6 +9,6 @@
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(TextLayoutManagerTest, testSomething) {
|
||||
TEST(TextTest, testSomething) {
|
||||
// TODO:
|
||||
}
|
||||
|
||||
25
ReactCommon/fabric/text/text/TextComponentDescriptor.h
Normal file
25
ReactCommon/fabric/text/text/TextComponentDescriptor.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* 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/core/ConcreteComponentDescriptor.h>
|
||||
#include <fabric/text/TextProps.h>
|
||||
#include <fabric/text/TextShadowNode.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
class TextComponentDescriptor: public ConcreteComponentDescriptor<TextShadowNode> {
|
||||
public:
|
||||
ComponentName getComponentName() const override {
|
||||
return "Text";
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
75
ReactCommon/fabric/text/text/TextProps.cpp
Normal file
75
ReactCommon/fabric/text/text/TextProps.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#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 {
|
||||
|
||||
void TextProps::apply(const RawProps &rawProps) {
|
||||
Props::apply(rawProps);
|
||||
|
||||
// Color
|
||||
applyRawProp(rawProps, "color", textAttributes_.foregroundColor);
|
||||
applyRawProp(rawProps, "backgroundColor", textAttributes_.backgroundColor);
|
||||
applyRawProp(rawProps, "opacity", textAttributes_.opacity);
|
||||
|
||||
// Font
|
||||
applyRawProp(rawProps, "fontFamily", textAttributes_.fontFamily);
|
||||
applyRawProp(rawProps, "fontSize", textAttributes_.fontSize);
|
||||
applyRawProp(rawProps, "fontSizeMultiplier", textAttributes_.fontSizeMultiplier);
|
||||
applyRawProp(rawProps, "fontWeight", textAttributes_.fontWeight);
|
||||
applyRawProp(rawProps, "fontStyle", textAttributes_.fontStyle);
|
||||
applyRawProp(rawProps, "fontVariant", textAttributes_.fontVariant);
|
||||
applyRawProp(rawProps, "allowFontScaling", textAttributes_.allowFontScaling);
|
||||
applyRawProp(rawProps, "letterSpacing", textAttributes_.letterSpacing);
|
||||
|
||||
// Paragraph
|
||||
applyRawProp(rawProps, "lineHeight", textAttributes_.lineHeight);
|
||||
applyRawProp(rawProps, "alignment", textAttributes_.alignment);
|
||||
applyRawProp(rawProps, "baseWritingDirection", textAttributes_.baseWritingDirection);
|
||||
|
||||
// Decoration
|
||||
applyRawProp(rawProps, "textDecorationColor", textAttributes_.textDecorationColor);
|
||||
applyRawProp(rawProps, "textDecorationLineType", textAttributes_.textDecorationLineType);
|
||||
applyRawProp(rawProps, "textDecorationLineStyle", textAttributes_.textDecorationLineStyle);
|
||||
applyRawProp(rawProps, "textDecorationLinePattern", textAttributes_.textDecorationLinePattern);
|
||||
|
||||
// Shadow
|
||||
applyRawProp(rawProps, "textShadowOffset", textAttributes_.textShadowOffset);
|
||||
applyRawProp(rawProps, "textShadowRadius", textAttributes_.textShadowRadius);
|
||||
applyRawProp(rawProps, "textShadowColor", textAttributes_.textShadowColor);
|
||||
|
||||
// Special
|
||||
applyRawProp(rawProps, "isHighlighted", textAttributes_.isHighlighted);
|
||||
}
|
||||
|
||||
#pragma mark - Getters
|
||||
|
||||
TextAttributes TextProps::getTextAttributes() const {
|
||||
return textAttributes_;
|
||||
}
|
||||
|
||||
#pragma mark - DebugStringConvertible
|
||||
|
||||
SharedDebugStringConvertibleList TextProps::getDebugProps() const {
|
||||
SharedDebugStringConvertibleList list = {};
|
||||
|
||||
auto textAttributesPropsList = textAttributes_.getDebugProps();
|
||||
std::move(textAttributesPropsList.begin(), textAttributesPropsList.end(), std::back_inserter(list));
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
46
ReactCommon/fabric/text/text/TextProps.h
Normal file
46
ReactCommon/fabric/text/text/TextProps.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* 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/TextAttributes.h>
|
||||
#include <fabric/core/Props.h>
|
||||
#include <fabric/graphics/Color.h>
|
||||
#include <fabric/graphics/Geometry.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
class TextProps;
|
||||
|
||||
using SharedTextProps = std::shared_ptr<const TextProps>;
|
||||
|
||||
class TextProps:
|
||||
public Props {
|
||||
|
||||
public:
|
||||
void apply(const RawProps &rawProps) override;
|
||||
|
||||
#pragma mark - Getters
|
||||
|
||||
TextAttributes getTextAttributes() const;
|
||||
|
||||
private:
|
||||
|
||||
/*
|
||||
* Not all `TextAttributes` fields make sense and is used as TextProps values.
|
||||
*/
|
||||
TextAttributes textAttributes_;
|
||||
|
||||
#pragma mark - DebugStringConvertible
|
||||
|
||||
SharedDebugStringConvertibleList getDebugProps() const override;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
|
||||
59
ReactCommon/fabric/text/text/TextShadowNode.cpp
Normal file
59
ReactCommon/fabric/text/text/TextShadowNode.cpp
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "TextShadowNode.h"
|
||||
|
||||
#include <fabric/debug/DebugStringConvertibleItem.h>
|
||||
|
||||
#include "RawTextShadowNode.h"
|
||||
#include "RawTextProps.h"
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
ComponentName TextShadowNode::getComponentName() const {
|
||||
return ComponentName("Text");
|
||||
}
|
||||
|
||||
AttributedString TextShadowNode::getAttributedString(const TextAttributes &baseTextAttributes) const {
|
||||
// TODO: Implement caching.
|
||||
|
||||
TextAttributes textAttributes = baseTextAttributes;
|
||||
textAttributes.apply(getProps()->getTextAttributes());
|
||||
|
||||
AttributedString attributedString;
|
||||
|
||||
for (auto &&childNode : *getChildren()) {
|
||||
// RawShadowNode
|
||||
SharedRawTextShadowNode rawTextShadowNode = std::dynamic_pointer_cast<const RawTextShadowNode>(childNode);
|
||||
if (rawTextShadowNode) {
|
||||
AttributedString::Fragment fragment;
|
||||
fragment.string = rawTextShadowNode->getProps()->getText();
|
||||
fragment.textAttributes = textAttributes;
|
||||
attributedString.appendFragment(fragment);
|
||||
continue;
|
||||
}
|
||||
|
||||
// TextShadowNode
|
||||
SharedTextShadowNode textShadowNode = std::dynamic_pointer_cast<const TextShadowNode>(childNode);
|
||||
if (textShadowNode) {
|
||||
attributedString.appendAttributedString(textShadowNode->getAttributedString(textAttributes));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Any other kind of ShadowNode
|
||||
AttributedString::Fragment fragment;
|
||||
fragment.shadowNode = childNode;
|
||||
fragment.textAttributes = textAttributes;
|
||||
attributedString.appendFragment(fragment);
|
||||
}
|
||||
|
||||
return attributedString;
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
40
ReactCommon/fabric/text/text/TextShadowNode.h
Normal file
40
ReactCommon/fabric/text/text/TextShadowNode.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* 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/AttributedString.h>
|
||||
#include <fabric/attributedstring/TextAttributes.h>
|
||||
#include <fabric/core/ConcreteShadowNode.h>
|
||||
#include <fabric/core/ShadowNode.h>
|
||||
#include <fabric/text/TextProps.h>
|
||||
#include <fabric/text/TextShadowNode.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
class TextShadowNode;
|
||||
|
||||
using SharedTextShadowNode = std::shared_ptr<const TextShadowNode>;
|
||||
|
||||
class TextShadowNode:
|
||||
public ConcreteShadowNode<TextProps> {
|
||||
|
||||
public:
|
||||
|
||||
using ConcreteShadowNode::ConcreteShadowNode;
|
||||
|
||||
ComponentName getComponentName() const override;
|
||||
|
||||
/*
|
||||
* Returns a `AttributedString` which represents text content of the node.
|
||||
*/
|
||||
AttributedString getAttributedString(const TextAttributes &baseTextAttributes) const;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
Reference in New Issue
Block a user