@bsky.app/react-native-uitextview 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -54,10 +54,6 @@ pod install
54
54
  React Native UITextView can - for the most part - be used as a drop-in replacement
55
55
  for existing blocks of `Text`. However, there are a few limitations:
56
56
 
57
- - Children of `UITextView` may only be other UITextView children (base `Text` children
58
- will be converted to `UITextView` children, so you only need to adjust the wrapper).
59
- This means that things like in-line images are not supported as they are in the base
60
- React Native `Text` component.
61
57
  - A few styles have not yet been implemented, but all should be possible.
62
58
 
63
59
  ## Usage
@@ -117,6 +113,23 @@ add to a link.
117
113
  </Text>
118
114
  ```
119
115
 
116
+ Inline views are also supported and participate in text measurement and wrapping:
117
+
118
+ ```tsx
119
+ <Text selectable uiTextView>
120
+ Selectable text{' '}
121
+ <View
122
+ style={{
123
+ paddingHorizontal: 5,
124
+ paddingVertical: 2,
125
+ borderRadius: 999,
126
+ backgroundColor: '#e5e7eb',
127
+ }}>
128
+ <Text style={{fontSize: 12}}>1/3</Text>
129
+ </View>
130
+ </Text>
131
+ ```
132
+
120
133
  Pressable `UITextView` text uses the same rounded gray press highlight as
121
134
  React Native `Text`. The highlight appears immediately, remains visible for at
122
135
  least 130 ms on a quick tap, and covers non-pressable nested spans belonging to
@@ -1,6 +1,8 @@
1
1
  #include "RNUITextViewShadowNode.h"
2
2
  #include "RNUITextViewChildShadowNode.h"
3
+ #include <react/debug/react_native_assert.h>
3
4
  #include <react/renderer/components/view/ViewShadowNode.h>
5
+ #include <react/renderer/graphics/rounding.h>
4
6
  #include <react/renderer/mounting/ShadowView.h>
5
7
 
6
8
  #include <cmath>
@@ -16,6 +18,22 @@ static ShadowView shadowViewFromShadowNode(const ShadowNode& shadowNode) {
16
18
  return shadowView;
17
19
  }
18
20
 
21
+ static ParagraphAttributes paragraphAttributesFromProps(
22
+ const RNUITextViewProps& props) {
23
+ auto paragraphAttributes = ParagraphAttributes{};
24
+ paragraphAttributes.maximumNumberOfLines = props.numberOfLines;
25
+ if (props.ellipsizeMode == RNUITextViewEllipsizeMode::Head) {
26
+ paragraphAttributes.ellipsizeMode = EllipsizeMode::Head;
27
+ } else if (props.ellipsizeMode == RNUITextViewEllipsizeMode::Middle) {
28
+ paragraphAttributes.ellipsizeMode = EllipsizeMode::Middle;
29
+ } else if (props.ellipsizeMode == RNUITextViewEllipsizeMode::Tail) {
30
+ paragraphAttributes.ellipsizeMode = EllipsizeMode::Tail;
31
+ } else if (props.ellipsizeMode == RNUITextViewEllipsizeMode::Clip) {
32
+ paragraphAttributes.ellipsizeMode = EllipsizeMode::Clip;
33
+ }
34
+ return paragraphAttributes;
35
+ }
36
+
19
37
  RNUITextViewShadowNode::RNUITextViewShadowNode(
20
38
  const ShadowNode& sourceShadowNode,
21
39
  const ShadowNodeFragment& fragment
@@ -26,19 +44,9 @@ Size RNUITextViewShadowNode::measureContent(
26
44
  const LayoutContext& layoutContext,
27
45
  const LayoutConstraints& layoutConstraints) const {
28
46
  const auto &baseProps = getConcreteProps();
29
- auto paragraphAttributes = ParagraphAttributes{};
30
- paragraphAttributes.maximumNumberOfLines = baseProps.numberOfLines;
31
- if (baseProps.ellipsizeMode == RNUITextViewEllipsizeMode::Head) {
32
- paragraphAttributes.ellipsizeMode = EllipsizeMode::Head;
33
- } else if (baseProps.ellipsizeMode == RNUITextViewEllipsizeMode::Middle) {
34
- paragraphAttributes.ellipsizeMode = EllipsizeMode::Middle;
35
- } else if (baseProps.ellipsizeMode == RNUITextViewEllipsizeMode::Tail) {
36
- paragraphAttributes.ellipsizeMode = EllipsizeMode::Tail;
37
- } else if (baseProps.ellipsizeMode == RNUITextViewEllipsizeMode::Clip) {
38
- paragraphAttributes.ellipsizeMode = EllipsizeMode::Clip;
39
- }
40
-
41
- const auto attributedString = getAttributedString(layoutContext);
47
+ const auto paragraphAttributes = paragraphAttributesFromProps(baseProps);
48
+ const auto content =
49
+ getContentWithMeasuredAttachments(layoutContext, layoutConstraints);
42
50
 
43
51
  TextLayoutContext textLayoutContext{
44
52
  .pointScaleFactor = layoutContext.pointScaleFactor,
@@ -47,16 +55,62 @@ Size RNUITextViewShadowNode::measureContent(
47
55
  const auto textLayoutManager = std::make_shared<const TextLayoutManager>(getContextContainer());
48
56
 
49
57
  return textLayoutManager->measure(
50
- AttributedStringBox{attributedString},
58
+ AttributedStringBox{content.attributedString},
51
59
  paragraphAttributes,
52
60
  textLayoutContext,
53
61
  layoutConstraints
54
62
  ).size;
55
63
  }
56
64
 
65
+ RNUITextViewContent RNUITextViewShadowNode::getContentWithMeasuredAttachments(
66
+ const LayoutContext& layoutContext,
67
+ const LayoutConstraints& layoutConstraints,
68
+ std::vector<RNUITextViewHighlightMetadata> *highlightMetadata) const {
69
+ auto attachments = std::vector<RNUITextViewAttachment>{};
70
+ auto attributedString =
71
+ getAttributedString(layoutContext, highlightMetadata, &attachments);
72
+
73
+ if (attachments.empty()) {
74
+ return RNUITextViewContent{
75
+ std::move(attributedString),
76
+ std::move(attachments),
77
+ };
78
+ }
79
+
80
+ auto localLayoutConstraints = layoutConstraints;
81
+ localLayoutConstraints.minimumSize = Size{0, 0};
82
+ auto &fragments = attributedString.getFragments();
83
+
84
+ for (const auto &attachment : attachments) {
85
+ const auto layoutableShadowNode =
86
+ dynamic_cast<const LayoutableShadowNode *>(attachment.shadowNode);
87
+ if (layoutableShadowNode == nullptr) {
88
+ continue;
89
+ }
90
+
91
+ auto size =
92
+ layoutableShadowNode->measure(layoutContext, localLayoutConstraints);
93
+ size.width += 0.01f;
94
+ size.height += 0.01f;
95
+ size = roundToPixel<&ceil>(size, layoutContext.pointScaleFactor);
96
+
97
+ auto fragmentLayoutMetrics = LayoutMetrics{};
98
+ fragmentLayoutMetrics.pointScaleFactor = layoutContext.pointScaleFactor;
99
+ fragmentLayoutMetrics.frame.size = size;
100
+ fragments[attachment.fragmentIndex].parentShadowView.layoutMetrics =
101
+ fragmentLayoutMetrics;
102
+ }
103
+
104
+ return RNUITextViewContent{
105
+ std::move(attributedString),
106
+ std::move(attachments),
107
+ };
108
+ }
109
+
57
110
  AttributedString RNUITextViewShadowNode::getAttributedString(
58
111
  const LayoutContext& layoutContext,
59
- std::vector<RNUITextViewHighlightMetadata> *highlightMetadata) const {
112
+ std::vector<RNUITextViewHighlightMetadata> *highlightMetadata,
113
+ std::vector<RNUITextViewAttachment> *attachments) const {
60
114
  const auto &baseProps = getConcreteProps();
61
115
  auto baseTextAttributes = TextAttributes::defaultTextAttributes();
62
116
  baseTextAttributes.backgroundColor = baseProps.backgroundColor;
@@ -156,6 +210,16 @@ AttributedString RNUITextViewShadowNode::getAttributedString(
156
210
  }
157
211
 
158
212
  baseAttributedString.appendFragment(std::move(fragment));
213
+ } else if (attachments != nullptr) {
214
+ auto fragment = AttributedString::Fragment{};
215
+ fragment.string = AttributedString::Fragment::AttachmentCharacter();
216
+ fragment.textAttributes = baseTextAttributes;
217
+ fragment.parentShadowView = shadowViewFromShadowNode(*child);
218
+ baseAttributedString.appendFragment(std::move(fragment));
219
+ attachments->push_back(RNUITextViewAttachment{
220
+ child,
221
+ baseAttributedString.getFragments().size() - 1,
222
+ });
159
223
  }
160
224
  }
161
225
 
@@ -164,10 +228,94 @@ AttributedString RNUITextViewShadowNode::getAttributedString(
164
228
 
165
229
  void RNUITextViewShadowNode::layout(LayoutContext layoutContext) {
166
230
  ensureUnsealed();
231
+ const auto layoutMetrics = getLayoutMetrics();
232
+ const auto size = layoutMetrics.getContentFrame().size;
233
+ const auto layoutConstraints = LayoutConstraints{
234
+ size,
235
+ size,
236
+ layoutMetrics.layoutDirection,
237
+ };
167
238
  auto highlightMetadata = std::vector<RNUITextViewHighlightMetadata>{};
239
+ auto content = getContentWithMeasuredAttachments(
240
+ layoutContext, layoutConstraints, &highlightMetadata);
241
+
168
242
  setStateData(RNUITextViewStateReal{
169
- getAttributedString(layoutContext, &highlightMetadata),
243
+ content.attributedString,
170
244
  std::move(highlightMetadata),
171
245
  });
246
+
247
+ if (content.attachments.empty()) {
248
+ return;
249
+ }
250
+
251
+ const auto paragraphAttributes =
252
+ paragraphAttributesFromProps(getConcreteProps());
253
+ const auto textLayoutContext = TextLayoutContext{
254
+ .pointScaleFactor = layoutContext.pointScaleFactor,
255
+ .surfaceId = getSurfaceId(),
256
+ };
257
+ const auto textLayoutManager =
258
+ std::make_shared<const TextLayoutManager>(getContextContainer());
259
+ const auto measurement = textLayoutManager->measure(
260
+ AttributedStringBox{content.attributedString},
261
+ paragraphAttributes,
262
+ textLayoutContext,
263
+ layoutConstraints);
264
+
265
+ react_native_assert(
266
+ content.attachments.size() == measurement.attachments.size());
267
+
268
+ auto textViewShadowNode = this;
269
+ auto textViewOwningShadowNode = std::shared_ptr<ShadowNode>{};
270
+
271
+ for (size_t i = 0; i < content.attachments.size(); i++) {
272
+ const auto &attachment = content.attachments[i];
273
+ if (dynamic_cast<const LayoutableShadowNode *>(attachment.shadowNode) ==
274
+ nullptr) {
275
+ continue;
276
+ }
277
+
278
+ auto clonedShadowNode = std::shared_ptr<ShadowNode>{};
279
+ textViewOwningShadowNode = textViewShadowNode->cloneTree(
280
+ attachment.shadowNode->getFamily(),
281
+ [&](const ShadowNode& oldShadowNode) {
282
+ clonedShadowNode = oldShadowNode.clone({});
283
+ return clonedShadowNode;
284
+ });
285
+ textViewShadowNode =
286
+ static_cast<RNUITextViewShadowNode *>(textViewOwningShadowNode.get());
287
+
288
+ auto &layoutableShadowNode =
289
+ dynamic_cast<LayoutableShadowNode &>(*clonedShadowNode);
290
+ const auto &attachmentMeasurement = measurement.attachments[i];
291
+ if (attachmentMeasurement.isClipped) {
292
+ layoutableShadowNode.setLayoutMetrics(
293
+ LayoutMetrics{.frame = {}, .displayType = DisplayType::None});
294
+ continue;
295
+ }
296
+
297
+ auto attachmentFrame = attachmentMeasurement.frame;
298
+ attachmentFrame.origin.x += layoutMetrics.contentInsets.left;
299
+ attachmentFrame.origin.y += layoutMetrics.contentInsets.top;
300
+ const auto attachmentSize = roundToPixel<&ceil>(
301
+ attachmentFrame.size, layoutMetrics.pointScaleFactor);
302
+ const auto attachmentOrigin = roundToPixel<&round>(
303
+ attachmentFrame.origin, layoutMetrics.pointScaleFactor);
304
+ const auto attachmentLayoutConstraints = LayoutConstraints{
305
+ attachmentSize,
306
+ attachmentSize,
307
+ layoutConstraints.layoutDirection,
308
+ };
309
+
310
+ layoutableShadowNode.layoutTree(
311
+ layoutContext, attachmentLayoutConstraints);
312
+ auto attachmentLayoutMetrics = layoutableShadowNode.getLayoutMetrics();
313
+ attachmentLayoutMetrics.frame.origin = attachmentOrigin;
314
+ layoutableShadowNode.setLayoutMetrics(attachmentLayoutMetrics);
315
+ }
316
+
317
+ if (textViewShadowNode != this) {
318
+ children_ = textViewShadowNode->children_;
319
+ }
172
320
  }
173
321
  }
@@ -24,6 +24,16 @@ struct RNUITextViewHighlightMetadata {
24
24
  Float pressRetentionOffsetLeft;
25
25
  };
26
26
 
27
+ struct RNUITextViewAttachment {
28
+ const ShadowNode *shadowNode;
29
+ size_t fragmentIndex;
30
+ };
31
+
32
+ struct RNUITextViewContent {
33
+ AttributedString attributedString;
34
+ std::vector<RNUITextViewAttachment> attachments;
35
+ };
36
+
27
37
  class RNUITextViewStateReal final {
28
38
  public:
29
39
  AttributedString attributedString;
@@ -57,8 +67,14 @@ public:
57
67
  const LayoutConstraints& layoutConstraints) const override;
58
68
 
59
69
  private:
60
- AttributedString getAttributedString(
70
+ RNUITextViewContent getContentWithMeasuredAttachments(
61
71
  const LayoutContext& layoutContext,
72
+ const LayoutConstraints& layoutConstraints,
62
73
  std::vector<RNUITextViewHighlightMetadata> *highlightMetadata = nullptr) const;
74
+
75
+ AttributedString getAttributedString(
76
+ const LayoutContext& layoutContext,
77
+ std::vector<RNUITextViewHighlightMetadata> *highlightMetadata = nullptr,
78
+ std::vector<RNUITextViewAttachment> *attachments = nullptr) const;
63
79
  };
64
80
  } // namespace facebook::React
@@ -1 +1 @@
1
- {"version":3,"file":"App.d.ts","sourceRoot":"","sources":["../../../../../example/src/App.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AA8E9B,MAAM,CAAC,OAAO,UAAU,GAAG,sBA4wB1B"}
1
+ {"version":3,"file":"App.d.ts","sourceRoot":"","sources":["../../../../../example/src/App.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AA8E9B,MAAM,CAAC,OAAO,UAAU,GAAG,sBAuxB1B"}
@@ -1 +1 @@
1
- {"version":3,"file":"App.d.ts","sourceRoot":"","sources":["../../../../../example/src/App.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AA8E9B,MAAM,CAAC,OAAO,UAAU,GAAG,sBA4wB1B"}
1
+ {"version":3,"file":"App.d.ts","sourceRoot":"","sources":["../../../../../example/src/App.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AA8E9B,MAAM,CAAC,OAAO,UAAU,GAAG,sBAuxB1B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bsky.app/react-native-uitextview",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "description": "UITextView for React Native",
5
5
  "source": "./src/index.tsx",
6
6
  "main": "./lib/commonjs/index.js",