Skip to content

Commit b033fcc

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Refactor ParagraphComponentDescriptor to support subclassing (facebook#55634)
Summary: Extract ParagraphComponentDescriptor logic into a template base class BaseParagraphComponentDescriptor<ShadowNodeT> so that other paragraph-like component descriptors can reuse the same TextLayoutManager wiring. Remove `final` from ParagraphShadowNode to allow subclassing. Move TextLayoutManagerKey from extern linkage in .cpp to constexpr in the new header. This is a pure refactor with no behavioral change. Changelog: [Internal] Reviewed By: cortinico Differential Revision: D93829402
1 parent 4f7fa58 commit b033fcc

File tree

4 files changed

+53
-43
lines changed

4 files changed

+53
-43
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/core/ConcreteComponentDescriptor.h>
11+
#include <react/renderer/textlayoutmanager/TextLayoutManager.h>
12+
#include <react/utils/ContextContainer.h>
13+
14+
namespace facebook::react {
15+
16+
constexpr const char *const TextLayoutManagerKey = "TextLayoutManager";
17+
18+
template <typename ShadowNodeT>
19+
class BaseParagraphComponentDescriptor : public ConcreteComponentDescriptor<ShadowNodeT> {
20+
public:
21+
explicit BaseParagraphComponentDescriptor(const ComponentDescriptorParameters &parameters)
22+
: ConcreteComponentDescriptor<ShadowNodeT>(parameters),
23+
textLayoutManager_(getManagerByName<TextLayoutManager>(this->contextContainer_, TextLayoutManagerKey))
24+
{
25+
}
26+
27+
ComponentName getComponentName() const override
28+
{
29+
return ShadowNodeT::Name();
30+
}
31+
32+
protected:
33+
void adopt(ShadowNode &shadowNode) const override
34+
{
35+
ConcreteComponentDescriptor<ShadowNodeT>::adopt(shadowNode);
36+
37+
auto &paragraphShadowNode = static_cast<ShadowNodeT &>(shadowNode);
38+
39+
// `ParagraphShadowNode` uses `TextLayoutManager` to measure text content
40+
// and communicate text rendering metrics to mounting layer.
41+
paragraphShadowNode.setTextLayoutManager(textLayoutManager_);
42+
}
43+
44+
private:
45+
// Every `ParagraphShadowNode` has a reference to a shared `TextLayoutManager`
46+
const std::shared_ptr<const TextLayoutManager> textLayoutManager_;
47+
};
48+
49+
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/components/text/ParagraphComponentDescriptor.cpp

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/react-native/ReactCommon/react/renderer/components/text/ParagraphComponentDescriptor.h

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,16 @@
77

88
#pragma once
99

10+
#include <react/renderer/components/text/BaseParagraphComponentDescriptor.h>
1011
#include <react/renderer/components/text/ParagraphShadowNode.h>
11-
#include <react/renderer/core/ConcreteComponentDescriptor.h>
12-
#include <react/renderer/textlayoutmanager/TextLayoutManager.h>
13-
#include <react/utils/ContextContainer.h>
1412

1513
namespace facebook::react {
16-
17-
extern const char TextLayoutManagerKey[];
18-
1914
/*
2015
* Descriptor for <Paragraph> component.
2116
*/
22-
class ParagraphComponentDescriptor final : public ConcreteComponentDescriptor<ParagraphShadowNode> {
17+
class ParagraphComponentDescriptor final : public BaseParagraphComponentDescriptor<ParagraphShadowNode> {
2318
public:
24-
explicit ParagraphComponentDescriptor(const ComponentDescriptorParameters &parameters)
25-
: ConcreteComponentDescriptor<ParagraphShadowNode>(parameters),
26-
textLayoutManager_(getManagerByName<TextLayoutManager>(contextContainer_, TextLayoutManagerKey))
27-
{
28-
}
29-
30-
protected:
31-
void adopt(ShadowNode &shadowNode) const override
32-
{
33-
ConcreteComponentDescriptor::adopt(shadowNode);
34-
35-
auto &paragraphShadowNode = static_cast<ParagraphShadowNode &>(shadowNode);
36-
37-
// `ParagraphShadowNode` uses `TextLayoutManager` to measure text content
38-
// and communicate text rendering metrics to mounting layer.
39-
paragraphShadowNode.setTextLayoutManager(textLayoutManager_);
40-
}
41-
42-
private:
43-
// Every `ParagraphShadowNode` has a reference to a shared `TextLayoutManager`
44-
const std::shared_ptr<const TextLayoutManager> textLayoutManager_;
19+
using BaseParagraphComponentDescriptor::BaseParagraphComponentDescriptor;
4520
};
4621

4722
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extern const char ParagraphComponentName[];
2626
* containing and displaying text. Text content is represented as nested <Text>
2727
* and <RawText> components.
2828
*/
29-
class ParagraphShadowNode final
29+
class ParagraphShadowNode
3030
: public ConcreteViewShadowNode<ParagraphComponentName, ParagraphProps, ParagraphEventEmitter, ParagraphState>,
3131
public BaseTextShadowNode {
3232
public:

0 commit comments

Comments
 (0)