@bsky.app/react-native-uitextview 2.3.0 → 2.4.1
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 +3 -4
- package/ios/RNUITextView.mm +66 -35
- package/ios/RNUITextViewChild.mm +0 -1
- package/ios/RNUITextViewManager.mm +0 -1
- package/ios/RNUITextViewShadowNode.cpp +32 -13
- package/ios/RNUITextViewShadowNode.h +1 -1
- package/lib/typescript/commonjs/example/src/App.d.ts.map +1 -1
- package/lib/typescript/module/example/src/App.d.ts.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -39,10 +39,9 @@ block of text.
|
|
|
39
39
|
> work the same as `2.x` and higher.
|
|
40
40
|
|
|
41
41
|
> [!NOTE]
|
|
42
|
-
> Version 2.
|
|
43
|
-
>
|
|
44
|
-
>
|
|
45
|
-
> `RNUITextViewShadowNode.cpp`.
|
|
42
|
+
> Version 2.4.0 of `@bsky.app/react-native-uitextview` is tested against React Native 0.86 and supports React Native 0.81.5 and newer.
|
|
43
|
+
> React Native's new-architecture text layout APIs can change between releases; compatibility issues are most likely to arise
|
|
44
|
+
> in `RNUITextViewShadowNode.cpp`.
|
|
46
45
|
|
|
47
46
|
```sh
|
|
48
47
|
yarn add @bsky.app/react-native-uitextview
|
package/ios/RNUITextView.mm
CHANGED
|
@@ -12,6 +12,20 @@
|
|
|
12
12
|
|
|
13
13
|
using namespace facebook::react;
|
|
14
14
|
|
|
15
|
+
static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsizeMode ellipsizeMode)
|
|
16
|
+
{
|
|
17
|
+
switch (ellipsizeMode) {
|
|
18
|
+
case RNUITextViewEllipsizeMode::Clip:
|
|
19
|
+
return NSLineBreakByClipping;
|
|
20
|
+
case RNUITextViewEllipsizeMode::Head:
|
|
21
|
+
return NSLineBreakByTruncatingHead;
|
|
22
|
+
case RNUITextViewEllipsizeMode::Tail:
|
|
23
|
+
return NSLineBreakByTruncatingTail;
|
|
24
|
+
case RNUITextViewEllipsizeMode::Middle:
|
|
25
|
+
return NSLineBreakByTruncatingMiddle;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
15
29
|
@interface RNUITextView () <RCTRNUITextViewViewProtocol, UIGestureRecognizerDelegate, UITextViewDelegate>
|
|
16
30
|
|
|
17
31
|
@end
|
|
@@ -42,8 +56,13 @@ using namespace facebook::react;
|
|
|
42
56
|
_textView = [[UITextView alloc] init];
|
|
43
57
|
_textView.scrollEnabled = false;
|
|
44
58
|
_textView.editable = false;
|
|
59
|
+
_textView.selectable = false;
|
|
45
60
|
_textView.textContainerInset = UIEdgeInsetsZero;
|
|
46
61
|
_textView.textContainer.lineFragmentPadding = 0;
|
|
62
|
+
// Keep UIKit's initial state in sync with the Codegen prop default. Since
|
|
63
|
+
// both oldProps and newProps default to `tail`, updateProps will not apply
|
|
64
|
+
// this value on the first render.
|
|
65
|
+
_textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
|
|
47
66
|
_textView.delegate = self;
|
|
48
67
|
// Must match RCTTextLayoutManager, which measures with usesFontLeading = NO.
|
|
49
68
|
_textView.layoutManager.usesFontLeading = NO;
|
|
@@ -147,7 +166,7 @@ using namespace facebook::react;
|
|
|
147
166
|
|
|
148
167
|
__block std::vector<std::string> lines;
|
|
149
168
|
const int maxLines = props.numberOfLines;
|
|
150
|
-
[_textView.layoutManager enumerateLineFragmentsForGlyphRange:NSMakeRange(0,
|
|
169
|
+
[_textView.layoutManager enumerateLineFragmentsForGlyphRange:NSMakeRange(0, _textView.layoutManager.numberOfGlyphs) usingBlock:^(CGRect rect,
|
|
151
170
|
CGRect usedRect,
|
|
152
171
|
NSTextContainer * _Nonnull textContainer,
|
|
153
172
|
NSRange glyphRange,
|
|
@@ -188,15 +207,8 @@ using namespace facebook::react;
|
|
|
188
207
|
}
|
|
189
208
|
|
|
190
209
|
if (oldViewProps.ellipsizeMode != newViewProps.ellipsizeMode) {
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
} else if (newViewProps.ellipsizeMode == RNUITextViewEllipsizeMode::Middle) {
|
|
194
|
-
_textView.textContainer.lineBreakMode = NSLineBreakMode::NSLineBreakByTruncatingMiddle;
|
|
195
|
-
} else if (newViewProps.ellipsizeMode == RNUITextViewEllipsizeMode::Tail) {
|
|
196
|
-
_textView.textContainer.lineBreakMode = NSLineBreakMode::NSLineBreakByTruncatingTail;
|
|
197
|
-
} else if (newViewProps.ellipsizeMode == RNUITextViewEllipsizeMode::Clip) {
|
|
198
|
-
_textView.textContainer.lineBreakMode = NSLineBreakMode::NSLineBreakByClipping;
|
|
199
|
-
}
|
|
210
|
+
_textView.textContainer.lineBreakMode =
|
|
211
|
+
RCTNSLineBreakModeFromEllipsizeMode(newViewProps.ellipsizeMode);
|
|
200
212
|
}
|
|
201
213
|
|
|
202
214
|
|
|
@@ -258,49 +270,68 @@ using namespace facebook::react;
|
|
|
258
270
|
return [sender locationInView:_textView];
|
|
259
271
|
}
|
|
260
272
|
|
|
261
|
-
- (
|
|
273
|
+
- (std::shared_ptr<const RNUITextViewChildEventEmitter>)getTouchEventEmitter:(CGPoint)location
|
|
262
274
|
{
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
RNUITextViewChild* textChild = (RNUITextViewChild*)child;
|
|
275
|
+
CGFloat fraction;
|
|
276
|
+
const auto characterIndex = [_textView.layoutManager characterIndexForPoint:location
|
|
277
|
+
inTextContainer:_textView.textContainer
|
|
278
|
+
fractionOfDistanceBetweenInsertionPoints:&fraction];
|
|
279
|
+
|
|
280
|
+
const auto textLength = _textView.attributedText.length;
|
|
281
|
+
if (textLength == 0 || characterIndex == NSNotFound || characterIndex >= textLength) {
|
|
282
|
+
return nullptr;
|
|
283
|
+
}
|
|
275
284
|
|
|
276
|
-
|
|
277
|
-
|
|
285
|
+
const auto lastCharacterRange =
|
|
286
|
+
[_textView.attributedText.string rangeOfComposedCharacterSequenceAtIndex:textLength - 1];
|
|
287
|
+
if ((characterIndex == 0 && fraction <= 0) ||
|
|
288
|
+
(characterIndex >= lastCharacterRange.location && fraction >= 1)) {
|
|
289
|
+
return nullptr;
|
|
290
|
+
}
|
|
278
291
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
292
|
+
const auto glyphIndex = [_textView.layoutManager glyphIndexForCharacterAtIndex:characterIndex];
|
|
293
|
+
const auto lineUsedRect = [_textView.layoutManager lineFragmentUsedRectForGlyphAtIndex:glyphIndex
|
|
294
|
+
effectiveRange:nullptr];
|
|
295
|
+
const auto textContainerLocation = CGPointMake(
|
|
296
|
+
location.x - _textView.textContainerInset.left,
|
|
297
|
+
location.y - _textView.textContainerInset.top);
|
|
298
|
+
if (!CGRectContainsPoint(lineUsedRect, textContainerLocation)) {
|
|
299
|
+
return nullptr;
|
|
282
300
|
}
|
|
283
301
|
|
|
284
|
-
|
|
302
|
+
NSData *eventEmitterWrapper =
|
|
303
|
+
(NSData *)[_textView.attributedText attribute:RCTAttributedStringEventEmitterKey
|
|
304
|
+
atIndex:characterIndex
|
|
305
|
+
effectiveRange:nullptr];
|
|
306
|
+
const auto eventEmitter = RCTUnwrapEventEmitter(eventEmitterWrapper);
|
|
307
|
+
return std::dynamic_pointer_cast<const RNUITextViewChildEventEmitter>(eventEmitter);
|
|
285
308
|
}
|
|
286
309
|
|
|
287
310
|
- (void)handlePressIfNecessary:(UITapGestureRecognizer*)sender
|
|
288
311
|
{
|
|
289
312
|
const auto location = [self getLocationOfPress:sender];
|
|
290
|
-
const auto
|
|
313
|
+
const auto eventEmitter = [self getTouchEventEmitter:location];
|
|
291
314
|
|
|
292
|
-
if (
|
|
293
|
-
|
|
315
|
+
if (eventEmitter) {
|
|
316
|
+
const auto eventTarget = eventEmitter->getEventTarget();
|
|
317
|
+
const auto target = eventTarget ? eventTarget->getTag() : 0;
|
|
318
|
+
eventEmitter->onPress(RNUITextViewChildEventEmitter::OnPress{target});
|
|
294
319
|
}
|
|
295
320
|
}
|
|
296
321
|
|
|
297
322
|
- (void)handleLongPressIfNecessary:(UILongPressGestureRecognizer*)sender
|
|
298
323
|
{
|
|
324
|
+
if (sender.state != UIGestureRecognizerStateBegan) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
|
|
299
328
|
const auto location = [self getLocationOfPress:sender];
|
|
300
|
-
const auto
|
|
329
|
+
const auto eventEmitter = [self getTouchEventEmitter:location];
|
|
301
330
|
|
|
302
|
-
if (
|
|
303
|
-
|
|
331
|
+
if (eventEmitter) {
|
|
332
|
+
const auto eventTarget = eventEmitter->getEventTarget();
|
|
333
|
+
const auto target = eventTarget ? eventTarget->getTag() : 0;
|
|
334
|
+
eventEmitter->onLongPress(RNUITextViewChildEventEmitter::OnLongPress{target});
|
|
304
335
|
}
|
|
305
336
|
}
|
|
306
337
|
|
package/ios/RNUITextViewChild.mm
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
#include "RNUITextViewShadowNode.h"
|
|
2
2
|
#include "RNUITextViewChildShadowNode.h"
|
|
3
3
|
#include <react/renderer/components/view/ViewShadowNode.h>
|
|
4
|
+
#include <react/renderer/mounting/ShadowView.h>
|
|
4
5
|
|
|
5
6
|
#include <cmath>
|
|
6
7
|
|
|
7
8
|
namespace facebook::react {
|
|
8
9
|
|
|
10
|
+
static ShadowView shadowViewFromShadowNode(const ShadowNode& shadowNode) {
|
|
11
|
+
auto shadowView = ShadowView{shadowNode};
|
|
12
|
+
// The attributed string only needs the component identity and event emitter.
|
|
13
|
+
// Dropping props and state avoids retaining the shadow tree through state.
|
|
14
|
+
shadowView.props = nullptr;
|
|
15
|
+
shadowView.state = nullptr;
|
|
16
|
+
return shadowView;
|
|
17
|
+
}
|
|
18
|
+
|
|
9
19
|
RNUITextViewShadowNode::RNUITextViewShadowNode(
|
|
10
20
|
const ShadowNode& sourceShadowNode,
|
|
11
21
|
const ShadowNodeFragment& fragment
|
|
@@ -28,6 +38,25 @@ Size RNUITextViewShadowNode::measureContent(
|
|
|
28
38
|
paragraphAttributes.ellipsizeMode = EllipsizeMode::Clip;
|
|
29
39
|
}
|
|
30
40
|
|
|
41
|
+
const auto attributedString = getAttributedString(layoutContext);
|
|
42
|
+
|
|
43
|
+
TextLayoutContext textLayoutContext{
|
|
44
|
+
.pointScaleFactor = layoutContext.pointScaleFactor,
|
|
45
|
+
.surfaceId = getSurfaceId(),
|
|
46
|
+
};
|
|
47
|
+
const auto textLayoutManager = std::make_shared<const TextLayoutManager>(getContextContainer());
|
|
48
|
+
|
|
49
|
+
return textLayoutManager->measure(
|
|
50
|
+
AttributedStringBox{attributedString},
|
|
51
|
+
paragraphAttributes,
|
|
52
|
+
textLayoutContext,
|
|
53
|
+
layoutConstraints
|
|
54
|
+
).size;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
AttributedString RNUITextViewShadowNode::getAttributedString(
|
|
58
|
+
const LayoutContext& layoutContext) const {
|
|
59
|
+
const auto &baseProps = getConcreteProps();
|
|
31
60
|
auto baseTextAttributes = TextAttributes::defaultTextAttributes();
|
|
32
61
|
baseTextAttributes.backgroundColor = baseProps.backgroundColor;
|
|
33
62
|
baseTextAttributes.allowFontScaling = baseProps.allowFontScaling;
|
|
@@ -111,29 +140,19 @@ Size RNUITextViewShadowNode::measureContent(
|
|
|
111
140
|
|
|
112
141
|
fragment.string = props.text;
|
|
113
142
|
fragment.textAttributes = textAttributes;
|
|
143
|
+
fragment.parentShadowView = shadowViewFromShadowNode(*textViewChild);
|
|
114
144
|
|
|
115
145
|
baseAttributedString.appendFragment(std::move(fragment));
|
|
116
146
|
}
|
|
117
147
|
}
|
|
118
148
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
TextLayoutContext textLayoutContext{};
|
|
122
|
-
textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
|
|
123
|
-
const auto textLayoutManager = std::make_shared<const TextLayoutManager>(getContextContainer());
|
|
124
|
-
|
|
125
|
-
return textLayoutManager->measure(
|
|
126
|
-
AttributedStringBox{baseAttributedString},
|
|
127
|
-
paragraphAttributes,
|
|
128
|
-
textLayoutContext,
|
|
129
|
-
layoutConstraints
|
|
130
|
-
).size;
|
|
149
|
+
return baseAttributedString;
|
|
131
150
|
}
|
|
132
151
|
|
|
133
152
|
void RNUITextViewShadowNode::layout(LayoutContext layoutContext) {
|
|
134
153
|
ensureUnsealed();
|
|
135
154
|
setStateData(RNUITextViewStateReal{
|
|
136
|
-
|
|
155
|
+
getAttributedString(layoutContext)
|
|
137
156
|
});
|
|
138
157
|
}
|
|
139
158
|
}
|
|
@@ -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,
|
|
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,sBAkwB1B"}
|
|
@@ -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,
|
|
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,sBAkwB1B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bsky.app/react-native-uitextview",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
4
4
|
"description": "UITextView for React Native",
|
|
5
5
|
"source": "./src/index.tsx",
|
|
6
6
|
"main": "./lib/commonjs/index.js",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
},
|
|
83
83
|
"peerDependencies": {
|
|
84
84
|
"react": "*",
|
|
85
|
-
"react-native": ">=0.
|
|
85
|
+
"react-native": ">=0.81.5"
|
|
86
86
|
},
|
|
87
87
|
"workspaces": [
|
|
88
88
|
"example"
|