@bsky.app/react-native-uitextview 2.3.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/LICENSE +20 -0
- package/README.md +188 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/CMakeLists.txt +28 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/RNUITextViewSpec-generated.cpp +22 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/RNUITextViewSpec.h +24 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/ComponentDescriptors.cpp +23 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/ComponentDescriptors.h +25 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/EventEmitters.cpp +61 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/EventEmitters.h +49 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/Props.cpp +151 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/Props.h +236 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/RNUITextViewSpecJSI.h +19 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/ShadowNodes.cpp +18 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/ShadowNodes.h +43 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/States.cpp +16 -0
- package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/States.h +22 -0
- package/ios/RNUITextView.h +13 -0
- package/ios/RNUITextView.mm +338 -0
- package/ios/RNUITextViewChild.h +24 -0
- package/ios/RNUITextViewChild.mm +72 -0
- package/ios/RNUITextViewChildComponentDescriptor.h +13 -0
- package/ios/RNUITextViewChildShadowNode.cpp +6 -0
- package/ios/RNUITextViewChildShadowNode.h +16 -0
- package/ios/RNUITextViewComponentDescriptor.h +13 -0
- package/ios/RNUITextViewManager.mm +36 -0
- package/ios/RNUITextViewShadowNode.cpp +139 -0
- package/ios/RNUITextViewShadowNode.h +48 -0
- package/ios/generated/build/generated/ios/ReactCodegen/RNUITextViewSpec/RNUITextViewSpec-generated.mm +16 -0
- package/ios/generated/build/generated/ios/ReactCodegen/RNUITextViewSpec/RNUITextViewSpec.h +38 -0
- package/ios/generated/build/generated/ios/ReactCodegen/RNUITextViewSpecJSI.h +19 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/ComponentDescriptors.cpp +23 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/ComponentDescriptors.h +25 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/EventEmitters.cpp +61 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/EventEmitters.h +49 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/Props.cpp +151 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/Props.h +236 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/RCTComponentViewHelpers.h +24 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/ShadowNodes.cpp +18 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/ShadowNodes.h +43 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/States.cpp +16 -0
- package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/States.h +22 -0
- package/lib/commonjs/RNUITextViewChildNativeComponent.ts +49 -0
- package/lib/commonjs/RNUITextViewNativeComponent.ts +53 -0
- package/lib/commonjs/Text.js +111 -0
- package/lib/commonjs/Text.js.map +1 -0
- package/lib/commonjs/Text.web.js +23 -0
- package/lib/commonjs/Text.web.js.map +1 -0
- package/lib/commonjs/index.js +17 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/util.js +66 -0
- package/lib/commonjs/util.js.map +1 -0
- package/lib/module/RNUITextViewChildNativeComponent.ts +49 -0
- package/lib/module/RNUITextViewNativeComponent.ts +53 -0
- package/lib/module/Text.js +106 -0
- package/lib/module/Text.js.map +1 -0
- package/lib/module/Text.web.js +19 -0
- package/lib/module/Text.web.js.map +1 -0
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/util.js +62 -0
- package/lib/module/util.js.map +1 -0
- package/lib/typescript/commonjs/example/src/App.d.ts +3 -0
- package/lib/typescript/commonjs/example/src/App.d.ts.map +1 -0
- package/lib/typescript/commonjs/package.json +1 -0
- package/lib/typescript/commonjs/src/RNUITextViewChildNativeComponent.d.ts +29 -0
- package/lib/typescript/commonjs/src/RNUITextViewChildNativeComponent.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/RNUITextViewNativeComponent.d.ts +43 -0
- package/lib/typescript/commonjs/src/RNUITextViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/Text.d.ts +25 -0
- package/lib/typescript/commonjs/src/Text.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/Text.web.d.ts +19 -0
- package/lib/typescript/commonjs/src/Text.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/index.d.ts +2 -0
- package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/src/util.d.ts +163 -0
- package/lib/typescript/commonjs/src/util.d.ts.map +1 -0
- package/lib/typescript/module/example/src/App.d.ts +3 -0
- package/lib/typescript/module/example/src/App.d.ts.map +1 -0
- package/lib/typescript/module/package.json +1 -0
- package/lib/typescript/module/src/RNUITextViewChildNativeComponent.d.ts +29 -0
- package/lib/typescript/module/src/RNUITextViewChildNativeComponent.d.ts.map +1 -0
- package/lib/typescript/module/src/RNUITextViewNativeComponent.d.ts +43 -0
- package/lib/typescript/module/src/RNUITextViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/module/src/Text.d.ts +25 -0
- package/lib/typescript/module/src/Text.d.ts.map +1 -0
- package/lib/typescript/module/src/Text.web.d.ts +19 -0
- package/lib/typescript/module/src/Text.web.d.ts.map +1 -0
- package/lib/typescript/module/src/index.d.ts +2 -0
- package/lib/typescript/module/src/index.d.ts.map +1 -0
- package/lib/typescript/module/src/util.d.ts +163 -0
- package/lib/typescript/module/src/util.d.ts.map +1 -0
- package/package.json +185 -0
- package/react-native-uitextview.podspec +34 -0
- package/react-native.config.js +28 -0
- package/scripts/ensure-rnc-cli-executable.js +19 -0
- package/src/RNUITextViewChildNativeComponent.ts +49 -0
- package/src/RNUITextViewNativeComponent.ts +53 -0
- package/src/Text.tsx +125 -0
- package/src/Text.web.tsx +22 -0
- package/src/index.tsx +1 -0
- package/src/util.ts +67 -0
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
#import "RNUITextView.h"
|
|
2
|
+
#import "RNUITextViewShadowNode.h"
|
|
3
|
+
#import "RNUITextViewComponentDescriptor.h"
|
|
4
|
+
#import "RNUITextViewChild.h"
|
|
5
|
+
#import <React/RCTConversions.h>
|
|
6
|
+
|
|
7
|
+
#import <react/renderer/textlayoutmanager/RCTAttributedTextUtils.h>
|
|
8
|
+
#import <react/renderer/components/RNUITextViewSpec/EventEmitters.h>
|
|
9
|
+
#import <react/renderer/components/RNUITextViewSpec/Props.h>
|
|
10
|
+
#import <react/renderer/components/RNUITextViewSpec/RCTComponentViewHelpers.h>
|
|
11
|
+
#import "RCTFabricComponentsPlugins.h"
|
|
12
|
+
|
|
13
|
+
using namespace facebook::react;
|
|
14
|
+
|
|
15
|
+
@interface RNUITextView () <RCTRNUITextViewViewProtocol, UIGestureRecognizerDelegate, UITextViewDelegate>
|
|
16
|
+
|
|
17
|
+
@end
|
|
18
|
+
|
|
19
|
+
@implementation RNUITextView{
|
|
20
|
+
UIView * _view;
|
|
21
|
+
UITextView * _textView;
|
|
22
|
+
RNUITextViewShadowNode::ConcreteState::Shared _state;
|
|
23
|
+
UITapGestureRecognizer * _outsideTapRecognizer;
|
|
24
|
+
BOOL _suppressSelectionChange;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
+ (ComponentDescriptorProvider)componentDescriptorProvider
|
|
28
|
+
{
|
|
29
|
+
return concreteComponentDescriptorProvider<RNUITextViewComponentDescriptor>();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
- (instancetype)initWithFrame:(CGRect)frame
|
|
33
|
+
{
|
|
34
|
+
if (self = [super initWithFrame:frame]) {
|
|
35
|
+
static const auto defaultProps = std::make_shared<const RNUITextViewProps>();
|
|
36
|
+
_props = defaultProps;
|
|
37
|
+
|
|
38
|
+
_view = [[UIView alloc] init];
|
|
39
|
+
self.contentView = _view;
|
|
40
|
+
self.clipsToBounds = true;
|
|
41
|
+
|
|
42
|
+
_textView = [[UITextView alloc] init];
|
|
43
|
+
_textView.scrollEnabled = false;
|
|
44
|
+
_textView.editable = false;
|
|
45
|
+
_textView.textContainerInset = UIEdgeInsetsZero;
|
|
46
|
+
_textView.textContainer.lineFragmentPadding = 0;
|
|
47
|
+
_textView.delegate = self;
|
|
48
|
+
// Must match RCTTextLayoutManager, which measures with usesFontLeading = NO.
|
|
49
|
+
_textView.layoutManager.usesFontLeading = NO;
|
|
50
|
+
[self addSubview:_textView];
|
|
51
|
+
|
|
52
|
+
const auto longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
|
|
53
|
+
action:@selector(handleLongPressIfNecessary:)];
|
|
54
|
+
longPressGestureRecognizer.delegate = self;
|
|
55
|
+
|
|
56
|
+
const auto pressGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
|
|
57
|
+
action:@selector(handlePressIfNecessary:)];
|
|
58
|
+
pressGestureRecognizer.delegate = self;
|
|
59
|
+
[pressGestureRecognizer requireGestureRecognizerToFail:longPressGestureRecognizer];
|
|
60
|
+
|
|
61
|
+
[_textView addGestureRecognizer:pressGestureRecognizer];
|
|
62
|
+
[_textView addGestureRecognizer:longPressGestureRecognizer];
|
|
63
|
+
|
|
64
|
+
_outsideTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
|
|
65
|
+
action:@selector(handleOutsideTap:)];
|
|
66
|
+
_outsideTapRecognizer.cancelsTouchesInView = NO;
|
|
67
|
+
_outsideTapRecognizer.delegate = self;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return self;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
- (void)didMoveToWindow
|
|
74
|
+
{
|
|
75
|
+
[super didMoveToWindow];
|
|
76
|
+
if (self.window) {
|
|
77
|
+
[self.window addGestureRecognizer:_outsideTapRecognizer];
|
|
78
|
+
} else {
|
|
79
|
+
[_outsideTapRecognizer.view removeGestureRecognizer:_outsideTapRecognizer];
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
- (void)dealloc
|
|
84
|
+
{
|
|
85
|
+
[_outsideTapRecognizer.view removeGestureRecognizer:_outsideTapRecognizer];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// See RCTParagraphComponentView
|
|
89
|
+
- (void)prepareForRecycle
|
|
90
|
+
{
|
|
91
|
+
[super prepareForRecycle];
|
|
92
|
+
_state.reset();
|
|
93
|
+
|
|
94
|
+
// Reset the frame to zero so that when it properly lays out on the next use
|
|
95
|
+
_textView.frame = CGRectZero;
|
|
96
|
+
_textView.attributedText = nil;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
- (void)layoutSubviews
|
|
100
|
+
{
|
|
101
|
+
[super layoutSubviews];
|
|
102
|
+
// _textView's frame is assigned inside drawRect, which only fires when
|
|
103
|
+
// state changes. Trigger a redraw whenever the host frame moves out from
|
|
104
|
+
// under it (rotation, parent relayout) so the text view resizes and
|
|
105
|
+
// onTextLayout re-fires with the new line wrapping.
|
|
106
|
+
if (!CGRectEqualToRect(_textView.frame, _view.frame)) {
|
|
107
|
+
[self setNeedsDisplay];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
- (void)drawRect:(CGRect)rect
|
|
112
|
+
{
|
|
113
|
+
if (!_state) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const auto &props = *std::static_pointer_cast<RNUITextViewProps const>(_props);
|
|
118
|
+
|
|
119
|
+
const auto attrString = _state->getData().attributedString;
|
|
120
|
+
const auto convertedAttrString = RCTNSAttributedStringFromAttributedString(attrString);
|
|
121
|
+
|
|
122
|
+
// Setting attributedText clears any active text selection, and re-assigning
|
|
123
|
+
// the frame triggers a layout flush that has the same effect. Bail out
|
|
124
|
+
// entirely when nothing actually changed so a JS-side state update made in
|
|
125
|
+
// response to onSelectionChange doesn't deselect what the user is selecting.
|
|
126
|
+
const BOOL textChanged = ![_textView.attributedText isEqualToAttributedString:convertedAttrString];
|
|
127
|
+
const BOOL frameChanged = !CGRectEqualToRect(_textView.frame, _view.frame);
|
|
128
|
+
if (!textChanged && !frameChanged) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if (textChanged) {
|
|
132
|
+
// Reassigning attributedText clears any active selection. Save it and
|
|
133
|
+
// restore after, while suppressing the synthetic textViewDidChangeSelection
|
|
134
|
+
// events the clear-then-restore would otherwise produce — those would
|
|
135
|
+
// round-trip to JS and re-trigger this same path, causing a loop.
|
|
136
|
+
const NSRange savedRange = _textView.selectedRange;
|
|
137
|
+
_suppressSelectionChange = YES;
|
|
138
|
+
_textView.attributedText = convertedAttrString;
|
|
139
|
+
if (savedRange.length > 0 && NSMaxRange(savedRange) <= _textView.attributedText.length) {
|
|
140
|
+
_textView.selectedRange = savedRange;
|
|
141
|
+
}
|
|
142
|
+
_suppressSelectionChange = NO;
|
|
143
|
+
}
|
|
144
|
+
if (frameChanged) {
|
|
145
|
+
_textView.frame = _view.frame;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
__block std::vector<std::string> lines;
|
|
149
|
+
const int maxLines = props.numberOfLines;
|
|
150
|
+
[_textView.layoutManager enumerateLineFragmentsForGlyphRange:NSMakeRange(0, convertedAttrString.string.length) usingBlock:^(CGRect rect,
|
|
151
|
+
CGRect usedRect,
|
|
152
|
+
NSTextContainer * _Nonnull textContainer,
|
|
153
|
+
NSRange glyphRange,
|
|
154
|
+
BOOL * _Nonnull stop) {
|
|
155
|
+
const auto charRange = [self->_textView.layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:nil];
|
|
156
|
+
const auto line = [self->_textView.text substringWithRange:charRange];
|
|
157
|
+
lines.push_back(line.UTF8String);
|
|
158
|
+
// enumerateLineFragments overshoots maximumNumberOfLines by one on iOS
|
|
159
|
+
// 18, so cap explicitly.
|
|
160
|
+
if (maxLines > 0 && lines.size() >= (size_t)maxLines) {
|
|
161
|
+
*stop = YES;
|
|
162
|
+
}
|
|
163
|
+
}];
|
|
164
|
+
|
|
165
|
+
if (_eventEmitter != nullptr) {
|
|
166
|
+
std::dynamic_pointer_cast<const facebook::react::RNUITextViewEventEmitter>(_eventEmitter)
|
|
167
|
+
->onTextLayout(facebook::react::RNUITextViewEventEmitter::OnTextLayout{static_cast<int>(self.tag), lines});
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
|
|
172
|
+
{
|
|
173
|
+
const auto &oldViewProps = *std::static_pointer_cast<RNUITextViewProps const>(_props);
|
|
174
|
+
const auto &newViewProps = *std::static_pointer_cast<RNUITextViewProps const>(props);
|
|
175
|
+
|
|
176
|
+
if (oldViewProps.numberOfLines != newViewProps.numberOfLines) {
|
|
177
|
+
_textView.textContainer.maximumNumberOfLines = newViewProps.numberOfLines;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (oldViewProps.selectable != newViewProps.selectable) {
|
|
181
|
+
_textView.selectable = newViewProps.selectable;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (oldViewProps.allowFontScaling != newViewProps.allowFontScaling) {
|
|
185
|
+
if (@available(iOS 11.0, *)) {
|
|
186
|
+
_textView.adjustsFontForContentSizeCategory = newViewProps.allowFontScaling;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (oldViewProps.ellipsizeMode != newViewProps.ellipsizeMode) {
|
|
191
|
+
if (newViewProps.ellipsizeMode == RNUITextViewEllipsizeMode::Head) {
|
|
192
|
+
_textView.textContainer.lineBreakMode = NSLineBreakMode::NSLineBreakByTruncatingHead;
|
|
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
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
// I'm not sure if this is really the right way to handle this style. This means that the entire _view_ the text
|
|
204
|
+
// is in will have this background color applied. To apply it just to a particular part of a string, you'd need
|
|
205
|
+
// to do <Text><Text style={{backgroundColor: 'blue'}}>Hello</Text></Text>.
|
|
206
|
+
// This is how the base <Text> component works though, so we'll go with it for now. Can change later if we want.
|
|
207
|
+
if (oldViewProps.backgroundColor != newViewProps.backgroundColor) {
|
|
208
|
+
_textView.backgroundColor = RCTUIColorFromSharedColor(newViewProps.backgroundColor);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
[super updateProps:props oldProps:oldProps];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// See RCTParagraphComponentView
|
|
215
|
+
- (void)updateState:(const facebook::react::State::Shared &)state oldState:(const facebook::react::State::Shared &)oldState
|
|
216
|
+
{
|
|
217
|
+
_state = std::static_pointer_cast<const RNUITextViewShadowNode::ConcreteState>(state);
|
|
218
|
+
[self setNeedsDisplay];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// MARK: - UIGestureRecognizerDelegate
|
|
222
|
+
|
|
223
|
+
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
|
|
224
|
+
{
|
|
225
|
+
return YES;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
|
|
229
|
+
{
|
|
230
|
+
if (gestureRecognizer == _outsideTapRecognizer) {
|
|
231
|
+
UIWindow *window = touch.window;
|
|
232
|
+
if (!window) {
|
|
233
|
+
return NO;
|
|
234
|
+
}
|
|
235
|
+
UIView *hitView = [window hitTest:[touch locationInView:nil] withEvent:nil];
|
|
236
|
+
return ![hitView isDescendantOfView:self];
|
|
237
|
+
}
|
|
238
|
+
return YES;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
- (void)handleOutsideTap:(UITapGestureRecognizer *)sender
|
|
242
|
+
{
|
|
243
|
+
// Defer past the current event loop turn so any in-flight edit-menu action
|
|
244
|
+
// (Copy / Define / Look Up / …) reads the live selection before we clear it.
|
|
245
|
+
UITextView *textView = _textView;
|
|
246
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
247
|
+
UITextRange *range = textView.selectedTextRange;
|
|
248
|
+
if (range != nil && !range.isEmpty) {
|
|
249
|
+
textView.selectedTextRange = nil;
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// MARK: - Touch handling
|
|
255
|
+
|
|
256
|
+
- (CGPoint)getLocationOfPress:(UIGestureRecognizer*)sender
|
|
257
|
+
{
|
|
258
|
+
return [sender locationInView:_textView];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
- (RNUITextViewChild*)getTouchChild:(CGPoint)location
|
|
262
|
+
{
|
|
263
|
+
const auto charIndex = [_textView.layoutManager characterIndexForPoint:location
|
|
264
|
+
inTextContainer:_textView.textContainer
|
|
265
|
+
fractionOfDistanceBetweenInsertionPoints:nil
|
|
266
|
+
];
|
|
267
|
+
|
|
268
|
+
int currIndex = -1;
|
|
269
|
+
for (UIView* child in self.subviews) {
|
|
270
|
+
if (![child isKindOfClass:[RNUITextViewChild class]]) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
RNUITextViewChild* textChild = (RNUITextViewChild*)child;
|
|
275
|
+
|
|
276
|
+
// This is UTF16 code units!!
|
|
277
|
+
currIndex += textChild.text.length;
|
|
278
|
+
|
|
279
|
+
if (charIndex <= currIndex) {
|
|
280
|
+
return textChild;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return nil;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
- (void)handlePressIfNecessary:(UITapGestureRecognizer*)sender
|
|
288
|
+
{
|
|
289
|
+
const auto location = [self getLocationOfPress:sender];
|
|
290
|
+
const auto child = [self getTouchChild:location];
|
|
291
|
+
|
|
292
|
+
if (child) {
|
|
293
|
+
[child onPress];
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
- (void)handleLongPressIfNecessary:(UILongPressGestureRecognizer*)sender
|
|
298
|
+
{
|
|
299
|
+
const auto location = [self getLocationOfPress:sender];
|
|
300
|
+
const auto child = [self getTouchChild:location];
|
|
301
|
+
|
|
302
|
+
if (child) {
|
|
303
|
+
[child onLongPress];
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// MARK: - UITextViewDelegate
|
|
308
|
+
|
|
309
|
+
- (void)textViewDidChangeSelection:(UITextView *)textView
|
|
310
|
+
{
|
|
311
|
+
if (_suppressSelectionChange) {
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (_eventEmitter == nullptr) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
const NSRange selectedRange = textView.selectedRange;
|
|
319
|
+
if (selectedRange.location == NSNotFound) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Fires on programmatic selection changes too (e.g. the outside-tap clear
|
|
324
|
+
// in handleOutsideTap:), so JS will see a synthetic empty-range event then.
|
|
325
|
+
std::dynamic_pointer_cast<const facebook::react::RNUITextViewEventEmitter>(_eventEmitter)
|
|
326
|
+
->onSelectionChange(facebook::react::RNUITextViewEventEmitter::OnSelectionChange{
|
|
327
|
+
static_cast<int>(self.tag),
|
|
328
|
+
static_cast<int>(selectedRange.location),
|
|
329
|
+
static_cast<int>(selectedRange.location + selectedRange.length),
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
Class<RCTComponentViewProtocol> RNUITextViewCls(void)
|
|
334
|
+
{
|
|
335
|
+
return RNUITextView.class;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
@end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// This guard prevent this file to be compiled in the old architecture.
|
|
2
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
3
|
+
#import <React/RCTViewComponentView.h>
|
|
4
|
+
#import <React/RCTComponent.h>
|
|
5
|
+
#import <UIKit/UIKit.h>
|
|
6
|
+
|
|
7
|
+
#ifndef RNUITextViewChildNativeComponent_h
|
|
8
|
+
#define RNUITextViewChildNativeComponent_h
|
|
9
|
+
|
|
10
|
+
NS_ASSUME_NONNULL_BEGIN
|
|
11
|
+
|
|
12
|
+
@interface RNUITextViewChild : RCTViewComponentView
|
|
13
|
+
|
|
14
|
+
@property (nonatomic, copy, nullable) NSString *text;
|
|
15
|
+
|
|
16
|
+
- (void)onPress;
|
|
17
|
+
- (void)onLongPress;
|
|
18
|
+
|
|
19
|
+
@end
|
|
20
|
+
|
|
21
|
+
NS_ASSUME_NONNULL_END
|
|
22
|
+
|
|
23
|
+
#endif /* UitextviewViewNativeComponent_h */
|
|
24
|
+
#endif /* RCT_NEW_ARCH_ENABLED */
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#import "RNUITextViewChild.h"
|
|
2
|
+
#import "RNUITextView.h"
|
|
3
|
+
#import "RNUITextViewChildComponentDescriptor.h"
|
|
4
|
+
#import <react/renderer/components/RNUITextViewSpec/EventEmitters.h>
|
|
5
|
+
#import <react/renderer/components/RNUITextViewSpec/Props.h>
|
|
6
|
+
#import <react/renderer/components/RNUITextViewSpec/RCTComponentViewHelpers.h>
|
|
7
|
+
#import "RCTFabricComponentsPlugins.h"
|
|
8
|
+
#import "Utils.h"
|
|
9
|
+
|
|
10
|
+
using namespace facebook::react;
|
|
11
|
+
|
|
12
|
+
@interface RNUITextViewChild () <RCTRNUITextViewChildViewProtocol>
|
|
13
|
+
|
|
14
|
+
@end
|
|
15
|
+
|
|
16
|
+
@implementation RNUITextViewChild {
|
|
17
|
+
NSString * _text;
|
|
18
|
+
RCTBubblingEventBlock _onPress;
|
|
19
|
+
RCTBubblingEventBlock _onLongPress;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
+ (ComponentDescriptorProvider)componentDescriptorProvider
|
|
23
|
+
{
|
|
24
|
+
return concreteComponentDescriptorProvider<RNUITextViewChildComponentDescriptor>();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
- (instancetype)initWithFrame:(CGRect)frame
|
|
28
|
+
{
|
|
29
|
+
if (self = [super initWithFrame:frame]) {
|
|
30
|
+
static const auto defaultProps = std::make_shared<const RNUITextViewChildProps>();
|
|
31
|
+
_props = defaultProps;
|
|
32
|
+
}
|
|
33
|
+
return self;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
|
|
37
|
+
{
|
|
38
|
+
const auto &oldViewProps = *std::static_pointer_cast<RNUITextViewChildProps const>(_props);
|
|
39
|
+
const auto &newViewProps = *std::static_pointer_cast<RNUITextViewChildProps const>(props);
|
|
40
|
+
|
|
41
|
+
if (newViewProps.text != oldViewProps.text) {
|
|
42
|
+
NSString *text = [NSString stringWithUTF8String:newViewProps.text.c_str()];
|
|
43
|
+
_text = text;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
[super updateProps:props oldProps:oldProps];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
- (void)onPress {
|
|
50
|
+
if (_eventEmitter != nullptr) {
|
|
51
|
+
std::dynamic_pointer_cast<const facebook::react::RNUITextViewChildEventEmitter>(_eventEmitter)
|
|
52
|
+
->onPress(facebook::react::RNUITextViewChildEventEmitter::OnPress{});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
- (void)onLongPress {
|
|
57
|
+
if (_eventEmitter != nullptr) {
|
|
58
|
+
std::dynamic_pointer_cast<const facebook::react::RNUITextViewChildEventEmitter>(_eventEmitter)
|
|
59
|
+
->onLongPress(facebook::react::RNUITextViewChildEventEmitter::OnLongPress{});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
+ (BOOL)shouldBeRecycled {
|
|
64
|
+
return NO;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
Class<RCTComponentViewProtocol> RNUITextViewChildCls(void)
|
|
68
|
+
{
|
|
69
|
+
return RNUITextViewChild.class;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
@end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "RNUITextViewChildShadowNode.h"
|
|
4
|
+
|
|
5
|
+
#include <react/renderer/core/ConcreteComponentDescriptor.h>
|
|
6
|
+
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
|
|
7
|
+
|
|
8
|
+
namespace facebook::react {
|
|
9
|
+
using RNUITextViewChildComponentDescriptor = ConcreteComponentDescriptor<RNUITextViewChildShadowNode>;
|
|
10
|
+
|
|
11
|
+
void RNUITextViewChildSpec_registerComponentDescriptorsFromCodegen(
|
|
12
|
+
std::shared_ptr<const ComponentDescriptorProviderRegistry> registry);
|
|
13
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <react/renderer/components/RNUITextViewSpec/EventEmitters.h>
|
|
4
|
+
#include <react/renderer/components/RNUITextViewSpec/Props.h>
|
|
5
|
+
#include <react/renderer/components/RNUITextViewSpec/States.h>
|
|
6
|
+
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
|
|
7
|
+
|
|
8
|
+
namespace facebook::react {
|
|
9
|
+
extern const char RNUITextViewChildComponentName[];
|
|
10
|
+
|
|
11
|
+
using RNUITextViewChildShadowNode = ConcreteViewShadowNode<
|
|
12
|
+
RNUITextViewChildComponentName,
|
|
13
|
+
RNUITextViewChildProps,
|
|
14
|
+
RNUITextViewChildEventEmitter,
|
|
15
|
+
RNUITextViewChildState>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "RNUITextViewShadowNode.h"
|
|
4
|
+
|
|
5
|
+
#include <react/renderer/core/ConcreteComponentDescriptor.h>
|
|
6
|
+
#include <react/renderer/componentregistry/ComponentDescriptorProviderRegistry.h>
|
|
7
|
+
|
|
8
|
+
namespace facebook::react {
|
|
9
|
+
using RNUITextViewComponentDescriptor = ConcreteComponentDescriptor<RNUITextViewShadowNode>;
|
|
10
|
+
|
|
11
|
+
void RNUITextViewSpec_registerComponentDescriptorsFromCodegen(
|
|
12
|
+
std::shared_ptr<const ComponentDescriptorProviderRegistry> registry);
|
|
13
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
#import <React/RCTViewManager.h>
|
|
2
|
+
#import <React/RCTUIManager.h>
|
|
3
|
+
#import "RCTBridge.h"
|
|
4
|
+
#import "Utils.h"
|
|
5
|
+
|
|
6
|
+
@interface RNUITextViewManager : RCTViewManager
|
|
7
|
+
@end
|
|
8
|
+
|
|
9
|
+
@implementation RNUITextViewManager
|
|
10
|
+
|
|
11
|
+
RCT_EXPORT_MODULE(RNUITextView)
|
|
12
|
+
|
|
13
|
+
- (UIView *)view
|
|
14
|
+
{
|
|
15
|
+
return [[UIView alloc] init];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
RCT_CUSTOM_VIEW_PROPERTY(color, NSString, UIView)
|
|
19
|
+
{
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@end
|
|
23
|
+
|
|
24
|
+
@interface RNUITextViewChildManager : RCTViewManager
|
|
25
|
+
@end
|
|
26
|
+
|
|
27
|
+
@implementation RNUITextViewChildManager
|
|
28
|
+
|
|
29
|
+
RCT_EXPORT_MODULE(RNUITextViewChild)
|
|
30
|
+
|
|
31
|
+
- (UIView *)view
|
|
32
|
+
{
|
|
33
|
+
return nil;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
@end
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
#include "RNUITextViewShadowNode.h"
|
|
2
|
+
#include "RNUITextViewChildShadowNode.h"
|
|
3
|
+
#include <react/renderer/components/view/ViewShadowNode.h>
|
|
4
|
+
|
|
5
|
+
#include <cmath>
|
|
6
|
+
|
|
7
|
+
namespace facebook::react {
|
|
8
|
+
|
|
9
|
+
RNUITextViewShadowNode::RNUITextViewShadowNode(
|
|
10
|
+
const ShadowNode& sourceShadowNode,
|
|
11
|
+
const ShadowNodeFragment& fragment
|
|
12
|
+
) : ConcreteViewShadowNode(sourceShadowNode, fragment) {
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
Size RNUITextViewShadowNode::measureContent(
|
|
16
|
+
const LayoutContext& layoutContext,
|
|
17
|
+
const LayoutConstraints& layoutConstraints) const {
|
|
18
|
+
const auto &baseProps = getConcreteProps();
|
|
19
|
+
auto paragraphAttributes = ParagraphAttributes{};
|
|
20
|
+
paragraphAttributes.maximumNumberOfLines = baseProps.numberOfLines;
|
|
21
|
+
if (baseProps.ellipsizeMode == RNUITextViewEllipsizeMode::Head) {
|
|
22
|
+
paragraphAttributes.ellipsizeMode = EllipsizeMode::Head;
|
|
23
|
+
} else if (baseProps.ellipsizeMode == RNUITextViewEllipsizeMode::Middle) {
|
|
24
|
+
paragraphAttributes.ellipsizeMode = EllipsizeMode::Middle;
|
|
25
|
+
} else if (baseProps.ellipsizeMode == RNUITextViewEllipsizeMode::Tail) {
|
|
26
|
+
paragraphAttributes.ellipsizeMode = EllipsizeMode::Tail;
|
|
27
|
+
} else if (baseProps.ellipsizeMode == RNUITextViewEllipsizeMode::Clip) {
|
|
28
|
+
paragraphAttributes.ellipsizeMode = EllipsizeMode::Clip;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
auto baseTextAttributes = TextAttributes::defaultTextAttributes();
|
|
32
|
+
baseTextAttributes.backgroundColor = baseProps.backgroundColor;
|
|
33
|
+
baseTextAttributes.allowFontScaling = baseProps.allowFontScaling;
|
|
34
|
+
// Match RN Text measurement/rendering by carrying the multiplier through
|
|
35
|
+
// TextAttributes instead of pre-scaling fontSize/lineHeight ourselves.
|
|
36
|
+
baseTextAttributes.fontSizeMultiplier = layoutContext.fontSizeMultiplier;
|
|
37
|
+
|
|
38
|
+
auto baseAttributedString = AttributedString{};
|
|
39
|
+
baseAttributedString.setBaseTextAttributes(baseTextAttributes);
|
|
40
|
+
const auto &children = getChildren();
|
|
41
|
+
for (size_t i = 0; i < children.size(); i++) {
|
|
42
|
+
const auto child = children[i].get();
|
|
43
|
+
if (auto textViewChild = dynamic_cast<const RNUITextViewChildShadowNode *>(child)) {
|
|
44
|
+
auto &props = textViewChild->getConcreteProps();
|
|
45
|
+
auto fragment = AttributedString::Fragment{};
|
|
46
|
+
auto textAttributes = baseTextAttributes;
|
|
47
|
+
textAttributes.backgroundColor = props.backgroundColor;
|
|
48
|
+
textAttributes.fontSize = props.fontSize;
|
|
49
|
+
textAttributes.lineHeight = props.lineHeight;
|
|
50
|
+
textAttributes.foregroundColor = props.color;
|
|
51
|
+
textAttributes.textShadowColor = props.shadowColor;
|
|
52
|
+
textAttributes.textShadowOffset = props.shadowOffset;
|
|
53
|
+
textAttributes.textShadowRadius = props.shadowRadius;
|
|
54
|
+
textAttributes.letterSpacing = props.letterSpacing;
|
|
55
|
+
textAttributes.textDecorationColor = props.textDecorationColor;
|
|
56
|
+
textAttributes.fontFamily = props.fontFamily;
|
|
57
|
+
|
|
58
|
+
if (props.fontStyle == RNUITextViewChildFontStyle::Italic) {
|
|
59
|
+
textAttributes.fontStyle = FontStyle::Italic;
|
|
60
|
+
} else {
|
|
61
|
+
textAttributes.fontStyle = FontStyle::Normal;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (props.fontWeight == RNUITextViewChildFontWeight::Bold) {
|
|
65
|
+
textAttributes.fontWeight = FontWeight::Bold;
|
|
66
|
+
} else if (props.fontWeight == RNUITextViewChildFontWeight::UltraLight) {
|
|
67
|
+
textAttributes.fontWeight = FontWeight::UltraLight;
|
|
68
|
+
} else if (props.fontWeight == RNUITextViewChildFontWeight::Light) {
|
|
69
|
+
textAttributes.fontWeight = FontWeight::Light;
|
|
70
|
+
} else if (props.fontWeight == RNUITextViewChildFontWeight::Medium) {
|
|
71
|
+
textAttributes.fontWeight = FontWeight::Medium;
|
|
72
|
+
} else if (props.fontWeight == RNUITextViewChildFontWeight::Semibold) {
|
|
73
|
+
textAttributes.fontWeight = FontWeight::Semibold;
|
|
74
|
+
} else if (props.fontWeight == RNUITextViewChildFontWeight::Heavy) {
|
|
75
|
+
textAttributes.fontWeight = FontWeight::Heavy;
|
|
76
|
+
} else {
|
|
77
|
+
textAttributes.fontWeight = FontWeight::Regular;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (props.textDecorationLine == RNUITextViewChildTextDecorationLine::LineThrough) {
|
|
81
|
+
textAttributes.textDecorationLineType = TextDecorationLineType::Strikethrough;
|
|
82
|
+
} else if (props.textDecorationLine == RNUITextViewChildTextDecorationLine::Underline) {
|
|
83
|
+
textAttributes.textDecorationLineType = TextDecorationLineType::Underline;
|
|
84
|
+
} else {
|
|
85
|
+
textAttributes.textDecorationLineType = TextDecorationLineType::None;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (props.textDecorationStyle == RNUITextViewChildTextDecorationStyle::Solid) {
|
|
89
|
+
textAttributes.textDecorationStyle = TextDecorationStyle::Solid;
|
|
90
|
+
} else if (props.textDecorationStyle == RNUITextViewChildTextDecorationStyle::Dotted) {
|
|
91
|
+
textAttributes.textDecorationStyle = TextDecorationStyle::Dotted;
|
|
92
|
+
} else if (props.textDecorationStyle == RNUITextViewChildTextDecorationStyle::Dashed) {
|
|
93
|
+
textAttributes.textDecorationStyle = TextDecorationStyle::Dashed;
|
|
94
|
+
} else if (props.textDecorationStyle == RNUITextViewChildTextDecorationStyle::Double) {
|
|
95
|
+
textAttributes.textDecorationStyle = TextDecorationStyle::Double;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (props.textAlign == RNUITextViewChildTextAlign::Left) {
|
|
99
|
+
textAttributes.alignment = TextAlignment::Left;
|
|
100
|
+
} else if (props.textAlign == RNUITextViewChildTextAlign::Right) {
|
|
101
|
+
textAttributes.alignment = TextAlignment::Right;
|
|
102
|
+
} else if (props.textAlign == RNUITextViewChildTextAlign::Center) {
|
|
103
|
+
textAttributes.alignment = TextAlignment::Center;
|
|
104
|
+
} else if (props.textAlign == RNUITextViewChildTextAlign::Justify) {
|
|
105
|
+
textAttributes.alignment = TextAlignment::Justified;
|
|
106
|
+
} else if (props.textAlign == RNUITextViewChildTextAlign::Auto) {
|
|
107
|
+
textAttributes.alignment = TextAlignment::Natural;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
textAttributes.backgroundColor = props.backgroundColor;
|
|
111
|
+
|
|
112
|
+
fragment.string = props.text;
|
|
113
|
+
fragment.textAttributes = textAttributes;
|
|
114
|
+
|
|
115
|
+
baseAttributedString.appendFragment(std::move(fragment));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
_attributedString = baseAttributedString;
|
|
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;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
void RNUITextViewShadowNode::layout(LayoutContext layoutContext) {
|
|
134
|
+
ensureUnsealed();
|
|
135
|
+
setStateData(RNUITextViewStateReal{
|
|
136
|
+
_attributedString
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|