Skip to content

Commit

Permalink
Fabric: Use state in Paragraph component
Browse files Browse the repository at this point in the history
Summary:
All props to Eric Lewis! cc ericlewis

This revert of revert of land of changes originally published in #24873 (with some slight fixes). The change removes usage of LocalData from the `<Text>` component.

After this change we only have ---one (maybe two)--- three components left using LocalData.

Reviewed By: mdvacca

Differential Revision: D15962376

fbshipit-source-id: 19f41109ce9d71ce30d618a45eb2b547a11f29a2
  • Loading branch information
shergin authored and facebook-github-bot committed Jul 8, 2019
1 parent 71f0079 commit 06ce568
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#import "RCTParagraphComponentView.h"

#import <react/components/text/ParagraphComponentDescriptor.h>
#import <react/components/text/ParagraphLocalData.h>
#import <react/components/text/ParagraphProps.h>
#import <react/components/text/ParagraphState.h>
#import <react/components/text/RawTextComponentDescriptor.h>
#import <react/components/text/TextComponentDescriptor.h>
#import <react/core/LocalData.h>
Expand All @@ -21,7 +21,7 @@
using namespace facebook::react;

@implementation RCTParagraphComponentView {
SharedParagraphLocalData _paragraphLocalData;
ParagraphShadowNode::ConcreteState::Shared _state;
ParagraphAttributes _paragraphAttributes;
}

Expand Down Expand Up @@ -63,31 +63,31 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
[super updateProps:props oldProps:oldProps];
}

- (void)updateLocalData:(SharedLocalData)localData oldLocalData:(SharedLocalData)oldLocalData
- (void)updateState:(State::Shared const &)state oldState:(State::Shared const &)oldState
{
_paragraphLocalData = std::static_pointer_cast<const ParagraphLocalData>(localData);
_state = std::static_pointer_cast<ParagraphShadowNode::ConcreteState const>(state);
[self setNeedsDisplay];
}

- (void)prepareForRecycle
{
[super prepareForRecycle];
_paragraphLocalData.reset();
_state.reset();
}

- (void)drawRect:(CGRect)rect
{
if (!_paragraphLocalData) {
if (!_state) {
return;
}

SharedTextLayoutManager textLayoutManager = _paragraphLocalData->getTextLayoutManager();
SharedTextLayoutManager textLayoutManager = _state->getData().layoutManager;
RCTTextLayoutManager *nativeTextLayoutManager =
(__bridge RCTTextLayoutManager *)textLayoutManager->getNativeTextLayoutManager();

CGRect frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame());

[nativeTextLayoutManager drawAttributedString:_paragraphLocalData->getAttributedString()
[nativeTextLayoutManager drawAttributedString:_state->getData().attributedString
paragraphAttributes:_paragraphAttributes
frame:frame];
}
Expand All @@ -101,26 +101,26 @@ - (NSString *)accessibilityLabel
return superAccessibilityLabel;
}

if (!_paragraphLocalData) {
if (!_state) {
return nil;
}

return RCTNSStringFromString(_paragraphLocalData->getAttributedString().getString());
return RCTNSStringFromString(_state->getData().attributedString.getString());
}

- (SharedTouchEventEmitter)touchEventEmitterAtPoint:(CGPoint)point
{
if (!_paragraphLocalData) {
if (!_state) {
return _eventEmitter;
}

SharedTextLayoutManager textLayoutManager = _paragraphLocalData->getTextLayoutManager();
SharedTextLayoutManager textLayoutManager = _state->getData().layoutManager;
RCTTextLayoutManager *nativeTextLayoutManager =
(__bridge RCTTextLayoutManager *)textLayoutManager->getNativeTextLayoutManager();
CGRect frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame());

SharedEventEmitter eventEmitter =
[nativeTextLayoutManager getEventEmitterWithAttributeString:_paragraphLocalData->getAttributedString()
[nativeTextLayoutManager getEventEmitterWithAttributeString:_state->getData().attributedString
paragraphAttributes:_paragraphAttributes
frame:frame
atPoint:point];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.IViewManagerWithChildren;
import com.facebook.react.uimanager.ReactStylesDiffMap;
import com.facebook.react.uimanager.StateWrapper;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.yoga.YogaMeasureMode;
import java.util.Map;
Expand Down Expand Up @@ -72,9 +73,9 @@ public boolean needsCustomLayoutForChildren() {
}

@Override
public Object updateLocalData(
ReactTextView view, ReactStylesDiffMap props, ReactStylesDiffMap localData) {
ReadableMap attributedString = localData.getMap("attributedString");
public Object updateState(
ReactTextView view, ReactStylesDiffMap props, StateWrapper stateWrapper) {
ReadableMap attributedString = stateWrapper.getState().getMap("attributedString");

Spannable spanned =
TextLayoutManager.getOrCreateSpannableForText(view.getContext(), attributedString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ParagraphComponentDescriptor final

private:
SharedTextLayoutManager textLayoutManager_;
std::unique_ptr<const ParagraphMeasurementCache> measureCache_;
std::unique_ptr<ParagraphMeasurementCache const> measureCache_;
};

} // namespace react
Expand Down

This file was deleted.

58 changes: 0 additions & 58 deletions ReactCommon/fabric/components/text/paragraph/ParagraphLocalData.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace facebook {
namespace react {

static ParagraphAttributes convertRawProp(
const RawProps &rawProps,
const ParagraphAttributes &defaultParagraphAttributes) {
RawProps const &rawProps,
ParagraphAttributes const &defaultParagraphAttributes) {
auto paragraphAttributes = ParagraphAttributes{};

paragraphAttributes.maximumNumberOfLines = convertRawProp(
Expand Down Expand Up @@ -44,8 +44,8 @@ static ParagraphAttributes convertRawProp(
}

ParagraphProps::ParagraphProps(
const ParagraphProps &sourceProps,
const RawProps &rawProps)
ParagraphProps const &sourceProps,
RawProps const &rawProps)
: ViewProps(sourceProps, rawProps),
BaseTextProps(sourceProps, rawProps),
paragraphAttributes(
Expand Down
6 changes: 3 additions & 3 deletions ReactCommon/fabric/components/text/paragraph/ParagraphProps.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ namespace react {
class ParagraphProps : public ViewProps, public BaseTextProps {
public:
ParagraphProps() = default;
ParagraphProps(const ParagraphProps &sourceProps, const RawProps &rawProps);
ParagraphProps(ParagraphProps const &sourceProps, RawProps const &rawProps);

#pragma mark - Props

/*
* Contains all prop values that affect visual representation of the
* paragraph.
*/
const ParagraphAttributes paragraphAttributes{};
ParagraphAttributes const paragraphAttributes{};

/*
* Defines can the text be selected (and copied) or not.
*/
const bool isSelectable{};
bool const isSelectable{};

#pragma mark - DebugStringConvertible

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
*/

#include "ParagraphShadowNode.h"
#include "ParagraphLocalData.h"
#include "ParagraphMeasurementCache.h"
#include "ParagraphState.h"

namespace facebook {
namespace react {

const char ParagraphComponentName[] = "Paragraph";
char const ParagraphComponentName[] = "Paragraph";

AttributedString ParagraphShadowNode::getAttributedString() const {
if (!cachedAttributedString_.has_value()) {
Expand All @@ -33,26 +33,21 @@ void ParagraphShadowNode::setTextLayoutManager(
}

void ParagraphShadowNode::setMeasureCache(
const ParagraphMeasurementCache *cache) {
ParagraphMeasurementCache const *cache) {
ensureUnsealed();
measureCache_ = cache;
}

void ParagraphShadowNode::updateLocalDataIfNeeded() {
void ParagraphShadowNode::updateStateIfNeeded() {
ensureUnsealed();

auto attributedString = getAttributedString();
auto currentLocalData =
std::static_pointer_cast<const ParagraphLocalData>(getLocalData());
if (currentLocalData &&
currentLocalData->getAttributedString() == attributedString) {
auto const &state = getStateData();
if (state.attributedString == attributedString) {
return;
}

auto localData = std::make_shared<ParagraphLocalData>();
localData->setAttributedString(std::move(attributedString));
localData->setTextLayoutManager(textLayoutManager_);
setLocalData(localData);
setStateData(ParagraphState{attributedString, textLayoutManager_});
}

#pragma mark - LayoutableShadowNode
Expand All @@ -64,15 +59,16 @@ Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
return {0, 0};
}

const ParagraphAttributes paragraphAttributes =
ParagraphAttributes const paragraphAttributes =
getProps()->paragraphAttributes;

// Cache results of this function so we don't need to call measure()
// repeatedly.
if (measureCache_) {
return measureCache_->get(
ParagraphMeasurementCacheKey{attributedString, paragraphAttributes, layoutConstraints},
[&](const ParagraphMeasurementCacheKey &key) {
ParagraphMeasurementCacheKey{
attributedString, paragraphAttributes, layoutConstraints},
[&](ParagraphMeasurementCacheKey const &key) {
return textLayoutManager_->measure(
attributedString, paragraphAttributes, layoutConstraints);
});
Expand All @@ -83,7 +79,7 @@ Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
}

void ParagraphShadowNode::layout(LayoutContext layoutContext) {
updateLocalDataIfNeeded();
updateStateIfNeeded();
ConcreteViewShadowNode::layout(layoutContext);
}

Expand Down
Loading

0 comments on commit 06ce568

Please sign in to comment.