Files
react-native/ReactCommon/fabric/components/text/paragraph/ParagraphShadowNode.cpp
T
Valentin Shergin c4876d0313 Fabric: Introducing AttributedStringBox
Summary:
The diff implements a new class called `AttributedStringBox` that represents an object storing a shared `AttributedString` *or* a shared pointer to some opaque platform-specific object that can be used as an attributed string. The class serves two main purposes:
- Represent type-erased attributed string entity (which can be platform-specific or platform-independent);
- Represent a container that can be copied with constant complexity.

Why? Several reasons:
- Sometimes it makes sense to keep an attributed string as a shared resource. This way we don't need to pay for expensive copying and we also implement a copy-on-write semantics on top of that if needed.
- We need to extend a TextLayoutMeasure API to support measuring some platform-specific attributed string implementation to remove the necessity of converting a string back and forth between representations. That's especially important for TextInput because we will need to measure that very efficiently (and the source of measuring, in this case, is a platform attributed string).

In other words, we need something to store inside TextInputState to measure and update very efficiently. The source of this data might be a native TextInput control or a data from React, to represent that kinda object we need this data structure (and interfaces that deal with it).

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D18670793

fbshipit-source-id: bc0164f801f28642f7c6da340af12acf33b85d24
2019-12-04 18:36:48 -08:00

78 lines
2.0 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "ParagraphShadowNode.h"
#include <react/attributedstring/AttributedStringBox.h>
#include "ParagraphState.h"
namespace facebook {
namespace react {
char const ParagraphComponentName[] = "Paragraph";
AttributedString ParagraphShadowNode::getAttributedString() const {
if (!cachedAttributedString_.has_value()) {
auto textAttributes = TextAttributes::defaultTextAttributes();
textAttributes.apply(getProps()->textAttributes);
cachedAttributedString_ =
BaseTextShadowNode::getAttributedString(textAttributes, *this);
}
return cachedAttributedString_.value();
}
void ParagraphShadowNode::setTextLayoutManager(
SharedTextLayoutManager textLayoutManager) {
ensureUnsealed();
textLayoutManager_ = textLayoutManager;
}
void ParagraphShadowNode::updateStateIfNeeded() {
ensureUnsealed();
auto attributedString = getAttributedString();
auto const &state = getStateData();
assert(textLayoutManager_);
assert(
(!state.layoutManager || state.layoutManager == textLayoutManager_) &&
"`StateData` refers to a different `TextLayoutManager`");
if (state.attributedString == attributedString &&
state.layoutManager == textLayoutManager_) {
return;
}
setStateData(ParagraphState{
attributedString, getProps()->paragraphAttributes, textLayoutManager_});
}
#pragma mark - LayoutableShadowNode
Size ParagraphShadowNode::measure(LayoutConstraints layoutConstraints) const {
AttributedString attributedString = getAttributedString();
if (attributedString.isEmpty()) {
return {0, 0};
}
return textLayoutManager_->measure(
AttributedStringBox{attributedString},
getProps()->paragraphAttributes,
layoutConstraints);
}
void ParagraphShadowNode::layout(LayoutContext layoutContext) {
updateStateIfNeeded();
ConcreteViewShadowNode::layout(layoutContext);
}
} // namespace react
} // namespace facebook