@bsky.app/react-native-uitextview 2.4.1 → 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.
Files changed (34) hide show
  1. package/README.md +23 -4
  2. package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/Props.cpp +31 -1
  3. package/android/generated/android/app/build/generated/source/codegen/jni/react/renderer/components/RNUITextViewSpec/Props.h +6 -0
  4. package/ios/RNUITextView.mm +503 -41
  5. package/ios/RNUITextViewShadowNode.cpp +179 -16
  6. package/ios/RNUITextViewShadowNode.h +33 -1
  7. package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/Props.cpp +31 -1
  8. package/ios/generated/build/generated/ios/ReactCodegen/react/renderer/components/RNUITextViewSpec/Props.h +6 -0
  9. package/lib/commonjs/RNUITextViewChildNativeComponent.ts +9 -0
  10. package/lib/commonjs/Text.js +81 -23
  11. package/lib/commonjs/Text.js.map +1 -1
  12. package/lib/commonjs/press.js +23 -0
  13. package/lib/commonjs/press.js.map +1 -0
  14. package/lib/module/RNUITextViewChildNativeComponent.ts +9 -0
  15. package/lib/module/Text.js +82 -24
  16. package/lib/module/Text.js.map +1 -1
  17. package/lib/module/press.js +19 -0
  18. package/lib/module/press.js.map +1 -0
  19. package/lib/typescript/commonjs/example/src/App.d.ts.map +1 -1
  20. package/lib/typescript/commonjs/src/RNUITextViewChildNativeComponent.d.ts +9 -0
  21. package/lib/typescript/commonjs/src/RNUITextViewChildNativeComponent.d.ts.map +1 -1
  22. package/lib/typescript/commonjs/src/Text.d.ts.map +1 -1
  23. package/lib/typescript/commonjs/src/press.d.ts +10 -0
  24. package/lib/typescript/commonjs/src/press.d.ts.map +1 -0
  25. package/lib/typescript/module/example/src/App.d.ts.map +1 -1
  26. package/lib/typescript/module/src/RNUITextViewChildNativeComponent.d.ts +9 -0
  27. package/lib/typescript/module/src/RNUITextViewChildNativeComponent.d.ts.map +1 -1
  28. package/lib/typescript/module/src/Text.d.ts.map +1 -1
  29. package/lib/typescript/module/src/press.d.ts +10 -0
  30. package/lib/typescript/module/src/press.d.ts.map +1 -0
  31. package/package.json +1 -1
  32. package/src/RNUITextViewChildNativeComponent.ts +9 -0
  33. package/src/Text.tsx +80 -11
  34. package/src/press.ts +36 -0
@@ -10,8 +10,16 @@
10
10
  #import <react/renderer/components/RNUITextViewSpec/RCTComponentViewHelpers.h>
11
11
  #import "RCTFabricComponentsPlugins.h"
12
12
 
13
+ #include <cmath>
14
+
13
15
  using namespace facebook::react;
14
16
 
17
+ // Matches Pressability's DEFAULT_MIN_PRESS_DURATION. Without this, a quick tap
18
+ // can add and remove the highlight layer before Core Animation presents it.
19
+ static const CFTimeInterval RNUITextViewMinimumPressHighlightDuration = 0.13;
20
+ static const CFTimeInterval RNUITextViewLongPressDelay = 0.5;
21
+ static const CGFloat RNUITextViewPressMovementThreshold = 10;
22
+
15
23
  static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsizeMode ellipsizeMode)
16
24
  {
17
25
  switch (ellipsizeMode) {
@@ -26,6 +34,79 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
26
34
  }
27
35
  }
28
36
 
37
+ @interface RNUITextViewPressGestureRecognizer : UIGestureRecognizer
38
+
39
+ @property (nonatomic, assign) BOOL hasLongPressHandler;
40
+ @property (nonatomic, assign) BOOL exceededLongPressMovement;
41
+
42
+ @end
43
+
44
+
45
+ @implementation RNUITextViewPressGestureRecognizer {
46
+ CGPoint _initialLocation;
47
+ }
48
+
49
+ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
50
+ {
51
+ if (self.state != UIGestureRecognizerStatePossible || touches.count != 1) {
52
+ self.state = UIGestureRecognizerStateCancelled;
53
+ return;
54
+ }
55
+
56
+ _initialLocation = [touches.anyObject locationInView:self.view];
57
+ self.exceededLongPressMovement = NO;
58
+ // Unlike UILongPressGestureRecognizer, transition synchronously from the raw
59
+ // touch instead of waiting for a timer-backed recognition pass.
60
+ self.state = UIGestureRecognizerStateBegan;
61
+ }
62
+
63
+ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
64
+ {
65
+ const auto location = [touches.anyObject locationInView:self.view];
66
+ if (std::hypot(location.x - _initialLocation.x, location.y - _initialLocation.y) >
67
+ RNUITextViewPressMovementThreshold) {
68
+ // Pressability cancels long-press recognition after 10 points of travel,
69
+ // but keeps the ordinary press alive while it remains inside the expanded
70
+ // press-retention rectangle.
71
+ self.exceededLongPressMovement = YES;
72
+ }
73
+ self.state = UIGestureRecognizerStateChanged;
74
+ }
75
+
76
+ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
77
+ {
78
+ self.state = UIGestureRecognizerStateEnded;
79
+ }
80
+
81
+ - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
82
+ {
83
+ self.state = UIGestureRecognizerStateCancelled;
84
+ }
85
+
86
+ - (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)preventedGestureRecognizer
87
+ {
88
+ // When this recognizer is active, the touch began on pressable text. In that
89
+ // case link interaction wins over UITextView's selection recognizers, while
90
+ // ancestor gestures such as a parent scroll view remain simultaneous.
91
+ const auto belongsToTextView =
92
+ preventedGestureRecognizer.view != nil &&
93
+ [preventedGestureRecognizer.view isDescendantOfView:self.view];
94
+ return self.hasLongPressHandler && belongsToTextView;
95
+ }
96
+
97
+ - (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer
98
+ {
99
+ const auto belongsToTextView =
100
+ preventingGestureRecognizer.view != nil &&
101
+ [preventingGestureRecognizer.view isDescendantOfView:self.view];
102
+ if (belongsToTextView) {
103
+ return !self.hasLongPressHandler;
104
+ }
105
+ return YES;
106
+ }
107
+
108
+ @end
109
+
29
110
  @interface RNUITextView () <RCTRNUITextViewViewProtocol, UIGestureRecognizerDelegate, UITextViewDelegate>
30
111
 
31
112
  @end
@@ -35,6 +116,24 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
35
116
  UITextView * _textView;
36
117
  RNUITextViewShadowNode::ConcreteState::Shared _state;
37
118
  UITapGestureRecognizer * _outsideTapRecognizer;
119
+ RNUITextViewPressGestureRecognizer * _pressGestureRecognizer;
120
+ CAShapeLayer * _highlightLayer;
121
+ NSString * _activeHighlightGroup;
122
+ NSString * _activePressGroup;
123
+ std::shared_ptr<const RNUITextViewChildEventEmitter> _activePressEventEmitter;
124
+ BOOL _pressIsActive;
125
+ BOOL _pressIsWithinRetention;
126
+ BOOL _longPressIsEligible;
127
+ BOOL _activePressSuppressesHighlighting;
128
+ BOOL _longPressDispatched;
129
+ BOOL _activePressHasPressHandler;
130
+ BOOL _activePressHasLongPressHandler;
131
+ NSUInteger _pressGeneration;
132
+ CFTimeInterval _pressActivationTime;
133
+ CGRect _activePressRect;
134
+ UIEdgeInsets _activePressRetentionOffset;
135
+ CFTimeInterval _highlightActivationTime;
136
+ NSUInteger _highlightGeneration;
38
137
  BOOL _suppressSelectionChange;
39
138
  }
40
139
 
@@ -53,7 +152,14 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
53
152
  self.contentView = _view;
54
153
  self.clipsToBounds = true;
55
154
 
56
- _textView = [[UITextView alloc] init];
155
+ if (@available(iOS 16.0, *)) {
156
+ // This component relies on NSLayoutManager for measurement, hit-testing,
157
+ // and press-highlight geometry. Opt into TextKit 1 up front instead of
158
+ // making UITextView create TextKit 2 and fall back on first access.
159
+ _textView = [UITextView textViewUsingTextLayoutManager:NO];
160
+ } else {
161
+ _textView = [[UITextView alloc] init];
162
+ }
57
163
  _textView.scrollEnabled = false;
58
164
  _textView.editable = false;
59
165
  _textView.selectable = false;
@@ -68,17 +174,11 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
68
174
  _textView.layoutManager.usesFontLeading = NO;
69
175
  [self addSubview:_textView];
70
176
 
71
- const auto longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self
72
- action:@selector(handleLongPressIfNecessary:)];
73
- longPressGestureRecognizer.delegate = self;
74
-
75
- const auto pressGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
76
- action:@selector(handlePressIfNecessary:)];
77
- pressGestureRecognizer.delegate = self;
78
- [pressGestureRecognizer requireGestureRecognizerToFail:longPressGestureRecognizer];
79
-
80
- [_textView addGestureRecognizer:pressGestureRecognizer];
81
- [_textView addGestureRecognizer:longPressGestureRecognizer];
177
+ _pressGestureRecognizer = [[RNUITextViewPressGestureRecognizer alloc] initWithTarget:self
178
+ action:@selector(handlePressGesture:)];
179
+ _pressGestureRecognizer.cancelsTouchesInView = NO;
180
+ _pressGestureRecognizer.delegate = self;
181
+ [_textView addGestureRecognizer:_pressGestureRecognizer];
82
182
 
83
183
  _outsideTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
84
184
  action:@selector(handleOutsideTap:)];
@@ -95,6 +195,8 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
95
195
  if (self.window) {
96
196
  [self.window addGestureRecognizer:_outsideTapRecognizer];
97
197
  } else {
198
+ [self invalidateActivePress];
199
+ [self clearPressHighlight];
98
200
  [_outsideTapRecognizer.view removeGestureRecognizer:_outsideTapRecognizer];
99
201
  }
100
202
  }
@@ -108,6 +210,8 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
108
210
  - (void)prepareForRecycle
109
211
  {
110
212
  [super prepareForRecycle];
213
+ [self invalidateActivePress];
214
+ [self clearPressHighlight];
111
215
  _state.reset();
112
216
 
113
217
  // Reset the frame to zero so that when it properly lays out on the next use
@@ -164,6 +268,10 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
164
268
  _textView.frame = _view.frame;
165
269
  }
166
270
 
271
+ if (_activeHighlightGroup != nil) {
272
+ [self updatePressHighlight];
273
+ }
274
+
167
275
  __block std::vector<std::string> lines;
168
276
  const int maxLines = props.numberOfLines;
169
277
  [_textView.layoutManager enumerateLineFragmentsForGlyphRange:NSMakeRange(0, _textView.layoutManager.numberOfGlyphs) usingBlock:^(CGRect rect,
@@ -234,6 +342,17 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
234
342
 
235
343
  - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
236
344
  {
345
+ if (gestureRecognizer == _pressGestureRecognizer || otherGestureRecognizer == _pressGestureRecognizer) {
346
+ const auto companion = gestureRecognizer == _pressGestureRecognizer
347
+ ? otherGestureRecognizer
348
+ : gestureRecognizer;
349
+ const auto belongsToTextView =
350
+ companion.view != nil && [companion.view isDescendantOfView:_textView];
351
+ // Press-only spans coexist with UITextView's recognizers; an actual
352
+ // selection invalidates the press in textViewDidChangeSelection:. Links
353
+ // with onLongPress own the hold, while ancestor scrolling can cancel both.
354
+ return belongsToTextView && !_pressGestureRecognizer.hasLongPressHandler;
355
+ }
237
356
  return YES;
238
357
  }
239
358
 
@@ -247,6 +366,24 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
247
366
  UIView *hitView = [window hitTest:[touch locationInView:nil] withEvent:nil];
248
367
  return ![hitView isDescendantOfView:self];
249
368
  }
369
+ if (gestureRecognizer == _pressGestureRecognizer) {
370
+ const auto location = [touch locationInView:_textView];
371
+ const auto eventEmitter = [self getTouchEventEmitter:location];
372
+ BOOL hasLongPressHandler = NO;
373
+ const auto group = [self highlightGroupForEventEmitter:eventEmitter
374
+ suppressHighlighting:nullptr
375
+ hasPressHandler:nullptr
376
+ hasLongPressHandler:&hasLongPressHandler
377
+ pressRetentionOffset:nullptr];
378
+ _pressGestureRecognizer.hasLongPressHandler = hasLongPressHandler;
379
+ // The recognizer enters `Began` synchronously. For a span that owns a
380
+ // long press, cancel delivery of that same touch to UITextView so its
381
+ // selection interaction cannot start in parallel. Keep this disabled for
382
+ // press-only spans: a hold on those should still be available to native
383
+ // text selection.
384
+ _pressGestureRecognizer.cancelsTouchesInView = hasLongPressHandler;
385
+ return group != nil;
386
+ }
250
387
  return YES;
251
388
  }
252
389
 
@@ -265,6 +402,343 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
265
402
 
266
403
  // MARK: - Touch handling
267
404
 
405
+ - (NSString *)highlightGroupForEventEmitter:(std::shared_ptr<const RNUITextViewChildEventEmitter>)eventEmitter
406
+ suppressHighlighting:(BOOL *)suppressHighlighting
407
+ hasPressHandler:(BOOL *)hasPressHandler
408
+ hasLongPressHandler:(BOOL *)hasLongPressHandler
409
+ pressRetentionOffset:(UIEdgeInsets *)pressRetentionOffset
410
+ {
411
+ if (!_state || !eventEmitter) {
412
+ return nil;
413
+ }
414
+
415
+ const auto eventTarget = eventEmitter->getEventTarget();
416
+ if (!eventTarget) {
417
+ return nil;
418
+ }
419
+
420
+ const auto tag = eventTarget->getTag();
421
+ for (const auto &metadata : _state->getData().highlightMetadata) {
422
+ if (metadata.tag == tag) {
423
+ const auto separator = metadata.group.rfind('|');
424
+ const auto handlers = separator == std::string::npos
425
+ ? std::string{}
426
+ : metadata.group.substr(separator + 1);
427
+ if (suppressHighlighting != nullptr) {
428
+ *suppressHighlighting = metadata.suppressHighlighting;
429
+ }
430
+ if (hasPressHandler != nullptr) {
431
+ *hasPressHandler = handlers.find('p') != std::string::npos;
432
+ }
433
+ if (hasLongPressHandler != nullptr) {
434
+ *hasLongPressHandler = handlers.find('l') != std::string::npos;
435
+ }
436
+ if (pressRetentionOffset != nullptr) {
437
+ *pressRetentionOffset = UIEdgeInsetsMake(
438
+ metadata.pressRetentionOffsetTop,
439
+ metadata.pressRetentionOffsetLeft,
440
+ metadata.pressRetentionOffsetBottom,
441
+ metadata.pressRetentionOffsetRight);
442
+ }
443
+ return [NSString stringWithUTF8String:metadata.group.c_str()];
444
+ }
445
+ }
446
+
447
+ return nil;
448
+ }
449
+
450
+ - (CGRect)textRectForHighlightGroup:(NSString *)highlightGroup
451
+ {
452
+ __block CGRect textRect = CGRectNull;
453
+ const auto fullRange = NSMakeRange(0, _textView.attributedText.length);
454
+ [_textView.attributedText enumerateAttribute:RCTAttributedStringEventEmitterKey
455
+ inRange:fullRange
456
+ options:0
457
+ usingBlock:^(NSData *eventEmitterWrapper, NSRange characterRange, BOOL *stop) {
458
+ if (eventEmitterWrapper == nil) {
459
+ return;
460
+ }
461
+
462
+ const auto eventEmitter = std::dynamic_pointer_cast<const RNUITextViewChildEventEmitter>(
463
+ RCTUnwrapEventEmitter(eventEmitterWrapper));
464
+ const auto group = [self highlightGroupForEventEmitter:eventEmitter
465
+ suppressHighlighting:nullptr
466
+ hasPressHandler:nullptr
467
+ hasLongPressHandler:nullptr
468
+ pressRetentionOffset:nullptr];
469
+ if (![group isEqualToString:highlightGroup]) {
470
+ return;
471
+ }
472
+
473
+ const auto glyphRange = [self->_textView.layoutManager glyphRangeForCharacterRange:characterRange
474
+ actualCharacterRange:nullptr];
475
+ [self->_textView.layoutManager enumerateEnclosingRectsForGlyphRange:glyphRange
476
+ withinSelectedGlyphRange:glyphRange
477
+ inTextContainer:self->_textView.textContainer
478
+ usingBlock:^(CGRect enclosingRect, BOOL *anotherStop) {
479
+ const auto inset = self->_textView.textContainerInset;
480
+ const auto rect = CGRectOffset(enclosingRect, inset.left, inset.top);
481
+ textRect = CGRectIsNull(textRect) ? rect : CGRectUnion(textRect, rect);
482
+ }];
483
+ }];
484
+ return textRect;
485
+ }
486
+
487
+ - (BOOL)isLocationWithinActivePressRect:(CGPoint)location
488
+ {
489
+ if (CGRectIsNull(_activePressRect)) {
490
+ return NO;
491
+ }
492
+
493
+ const auto offset = _activePressRetentionOffset;
494
+ const auto retainedRect = CGRectMake(
495
+ CGRectGetMinX(_activePressRect) - offset.left,
496
+ CGRectGetMinY(_activePressRect) - offset.top,
497
+ CGRectGetWidth(_activePressRect) + offset.left + offset.right,
498
+ CGRectGetHeight(_activePressRect) + offset.top + offset.bottom);
499
+ return CGRectContainsPoint(retainedRect, location);
500
+ }
501
+
502
+ - (void)clearPressHighlight
503
+ {
504
+ _highlightGeneration++;
505
+ _activeHighlightGroup = nil;
506
+ [CATransaction begin];
507
+ [CATransaction setDisableActions:YES];
508
+ [_highlightLayer removeFromSuperlayer];
509
+ [CATransaction commit];
510
+ _highlightLayer = nil;
511
+ }
512
+
513
+ - (void)activatePressHighlight
514
+ {
515
+ _highlightGeneration++;
516
+ _activeHighlightGroup = _activePressGroup;
517
+ _highlightActivationTime = CACurrentMediaTime();
518
+ [self updatePressHighlight];
519
+ }
520
+
521
+ - (void)invalidateActivePress
522
+ {
523
+ _pressGeneration++;
524
+ _pressIsActive = NO;
525
+ _pressIsWithinRetention = NO;
526
+ _longPressIsEligible = NO;
527
+ _activePressSuppressesHighlighting = NO;
528
+ _longPressDispatched = NO;
529
+ _activePressHasPressHandler = NO;
530
+ _activePressHasLongPressHandler = NO;
531
+ _activePressRect = CGRectNull;
532
+ _activePressGroup = nil;
533
+ _activePressEventEmitter.reset();
534
+ }
535
+
536
+ - (void)emitPressEvent:(BOOL)isLongPress
537
+ eventEmitter:(std::shared_ptr<const RNUITextViewChildEventEmitter>)eventEmitter
538
+ {
539
+ if (!eventEmitter) {
540
+ return;
541
+ }
542
+
543
+ const auto eventTarget = eventEmitter->getEventTarget();
544
+ const auto target = eventTarget ? eventTarget->getTag() : 0;
545
+ if (isLongPress) {
546
+ eventEmitter->onLongPress(RNUITextViewChildEventEmitter::OnLongPress{target});
547
+ } else {
548
+ eventEmitter->onPress(RNUITextViewChildEventEmitter::OnPress{target});
549
+ }
550
+ }
551
+
552
+ - (void)emitLongPressIfNecessary
553
+ {
554
+ if (!_pressIsActive || !_pressIsWithinRetention || !_longPressIsEligible ||
555
+ _longPressDispatched || !_activePressHasLongPressHandler) {
556
+ return;
557
+ }
558
+
559
+ // Set this before emitting so a synchronous update cannot make release fall
560
+ // through to onPress while the long-press event is being dispatched.
561
+ _longPressDispatched = YES;
562
+ [self emitPressEvent:YES eventEmitter:_activePressEventEmitter];
563
+ }
564
+
565
+ - (void)clearPressHighlightAfterMinimumDuration
566
+ {
567
+ if (_activeHighlightGroup == nil) {
568
+ return;
569
+ }
570
+
571
+ const auto elapsed = CACurrentMediaTime() - _highlightActivationTime;
572
+ const auto remaining = RNUITextViewMinimumPressHighlightDuration - elapsed;
573
+ if (remaining <= 0) {
574
+ [self clearPressHighlight];
575
+ return;
576
+ }
577
+
578
+ const auto generation = ++_highlightGeneration;
579
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(remaining * NSEC_PER_SEC)),
580
+ dispatch_get_main_queue(), ^{
581
+ if (self->_highlightGeneration == generation) {
582
+ [self clearPressHighlight];
583
+ }
584
+ });
585
+ }
586
+
587
+ - (void)updatePressHighlight
588
+ {
589
+ if (_activeHighlightGroup == nil || _textView.attributedText.length == 0) {
590
+ [self clearPressHighlight];
591
+ return;
592
+ }
593
+
594
+ __block UIBezierPath *highlightPath = nil;
595
+ const auto fullRange = NSMakeRange(0, _textView.attributedText.length);
596
+ [_textView.attributedText enumerateAttribute:RCTAttributedStringEventEmitterKey
597
+ inRange:fullRange
598
+ options:0
599
+ usingBlock:^(NSData *eventEmitterWrapper, NSRange characterRange, BOOL *stop) {
600
+ if (eventEmitterWrapper == nil) {
601
+ return;
602
+ }
603
+
604
+ const auto eventEmitter = std::dynamic_pointer_cast<const RNUITextViewChildEventEmitter>(
605
+ RCTUnwrapEventEmitter(eventEmitterWrapper));
606
+ const auto group = [self highlightGroupForEventEmitter:eventEmitter
607
+ suppressHighlighting:nullptr
608
+ hasPressHandler:nullptr
609
+ hasLongPressHandler:nullptr
610
+ pressRetentionOffset:nullptr];
611
+ if (![group isEqualToString:self->_activeHighlightGroup]) {
612
+ return;
613
+ }
614
+
615
+ const auto glyphRange = [self->_textView.layoutManager glyphRangeForCharacterRange:characterRange
616
+ actualCharacterRange:nullptr];
617
+ [self->_textView.layoutManager enumerateEnclosingRectsForGlyphRange:glyphRange
618
+ withinSelectedGlyphRange:glyphRange
619
+ inTextContainer:self->_textView.textContainer
620
+ usingBlock:^(CGRect enclosingRect, BOOL *anotherStop) {
621
+ const auto inset = self->_textView.textContainerInset;
622
+ const auto textViewRect = CGRectOffset(enclosingRect, inset.left, inset.top);
623
+ const auto path = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(textViewRect, -2, -2)
624
+ cornerRadius:2];
625
+ if (highlightPath != nil) {
626
+ [highlightPath appendPath:path];
627
+ } else {
628
+ highlightPath = path;
629
+ }
630
+ }];
631
+ }];
632
+
633
+ if (highlightPath == nil) {
634
+ [self clearPressHighlight];
635
+ return;
636
+ }
637
+
638
+ [CATransaction begin];
639
+ [CATransaction setDisableActions:YES];
640
+ if (_highlightLayer == nil) {
641
+ _highlightLayer = [CAShapeLayer layer];
642
+ _highlightLayer.fillColor = [UIColor colorWithWhite:0 alpha:0.25].CGColor;
643
+ [_textView.layer addSublayer:_highlightLayer];
644
+ }
645
+ _highlightLayer.frame = _textView.bounds;
646
+ _highlightLayer.path = highlightPath.CGPath;
647
+ [CATransaction commit];
648
+ }
649
+
650
+ - (void)handlePressGesture:(RNUITextViewPressGestureRecognizer *)sender
651
+ {
652
+ if (sender.state == UIGestureRecognizerStateBegan) {
653
+ [self invalidateActivePress];
654
+ [self clearPressHighlight];
655
+
656
+ const auto location = [self getLocationOfPress:sender];
657
+ const auto eventEmitter = [self getTouchEventEmitter:location];
658
+ BOOL suppressHighlighting = NO;
659
+ BOOL hasPressHandler = NO;
660
+ UIEdgeInsets pressRetentionOffset = UIEdgeInsetsZero;
661
+ const auto group = [self highlightGroupForEventEmitter:eventEmitter
662
+ suppressHighlighting:&suppressHighlighting
663
+ hasPressHandler:&hasPressHandler
664
+ hasLongPressHandler:nullptr
665
+ pressRetentionOffset:&pressRetentionOffset];
666
+ if (group == nil) {
667
+ return;
668
+ }
669
+
670
+ _pressIsActive = YES;
671
+ _pressIsWithinRetention = YES;
672
+ _activePressGroup = group;
673
+ _activePressEventEmitter = eventEmitter;
674
+ _activePressHasPressHandler = hasPressHandler;
675
+ _activePressHasLongPressHandler = sender.hasLongPressHandler;
676
+ _longPressIsEligible = _activePressHasLongPressHandler;
677
+ _activePressSuppressesHighlighting = suppressHighlighting;
678
+ _activePressRect = [self textRectForHighlightGroup:group];
679
+ _activePressRetentionOffset = pressRetentionOffset;
680
+ _pressActivationTime = CACurrentMediaTime();
681
+
682
+ if (!_activePressSuppressesHighlighting) {
683
+ [self activatePressHighlight];
684
+ }
685
+
686
+ if (_activePressHasLongPressHandler) {
687
+ const auto generation = _pressGeneration;
688
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(RNUITextViewLongPressDelay * NSEC_PER_SEC)),
689
+ dispatch_get_main_queue(), ^{
690
+ if (self->_pressGeneration == generation) {
691
+ [self emitLongPressIfNecessary];
692
+ }
693
+ });
694
+ }
695
+ return;
696
+ }
697
+
698
+ if (sender.state == UIGestureRecognizerStateChanged && _pressIsActive) {
699
+ const auto location = [self getLocationOfPress:sender];
700
+ const auto isWithinRetention = [self isLocationWithinActivePressRect:location];
701
+ if (sender.exceededLongPressMovement || !isWithinRetention) {
702
+ _longPressIsEligible = NO;
703
+ }
704
+ if (_pressIsWithinRetention && !isWithinRetention) {
705
+ _pressIsWithinRetention = NO;
706
+ [self clearPressHighlightAfterMinimumDuration];
707
+ } else if (!_pressIsWithinRetention && isWithinRetention) {
708
+ _pressIsWithinRetention = YES;
709
+ if (!_activePressSuppressesHighlighting) {
710
+ [self activatePressHighlight];
711
+ }
712
+ }
713
+ return;
714
+ }
715
+
716
+ if (sender.state == UIGestureRecognizerStateEnded) {
717
+ _pressIsWithinRetention =
718
+ _pressIsActive && [self isLocationWithinActivePressRect:[self getLocationOfPress:sender]];
719
+ if (_pressIsActive && _pressIsWithinRetention && !_longPressDispatched &&
720
+ _longPressIsEligible && _activePressHasLongPressHandler &&
721
+ CACurrentMediaTime() - _pressActivationTime >= RNUITextViewLongPressDelay) {
722
+ // The finger can lift after the threshold but before the main-queue timer
723
+ // gets serviced. Treat elapsed time as authoritative in that race.
724
+ [self emitLongPressIfNecessary];
725
+ }
726
+ if (_pressIsActive && _pressIsWithinRetention && !_longPressDispatched &&
727
+ _activePressHasPressHandler) {
728
+ [self emitPressEvent:NO eventEmitter:_activePressEventEmitter];
729
+ }
730
+ [self invalidateActivePress];
731
+ [self clearPressHighlightAfterMinimumDuration];
732
+ return;
733
+ }
734
+
735
+ if (sender.state == UIGestureRecognizerStateCancelled ||
736
+ sender.state == UIGestureRecognizerStateFailed) {
737
+ [self invalidateActivePress];
738
+ [self clearPressHighlightAfterMinimumDuration];
739
+ }
740
+ }
741
+
268
742
  - (CGPoint)getLocationOfPress:(UIGestureRecognizer*)sender
269
743
  {
270
744
  return [sender locationInView:_textView];
@@ -307,34 +781,6 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
307
781
  return std::dynamic_pointer_cast<const RNUITextViewChildEventEmitter>(eventEmitter);
308
782
  }
309
783
 
310
- - (void)handlePressIfNecessary:(UITapGestureRecognizer*)sender
311
- {
312
- const auto location = [self getLocationOfPress:sender];
313
- const auto eventEmitter = [self getTouchEventEmitter:location];
314
-
315
- if (eventEmitter) {
316
- const auto eventTarget = eventEmitter->getEventTarget();
317
- const auto target = eventTarget ? eventTarget->getTag() : 0;
318
- eventEmitter->onPress(RNUITextViewChildEventEmitter::OnPress{target});
319
- }
320
- }
321
-
322
- - (void)handleLongPressIfNecessary:(UILongPressGestureRecognizer*)sender
323
- {
324
- if (sender.state != UIGestureRecognizerStateBegan) {
325
- return;
326
- }
327
-
328
- const auto location = [self getLocationOfPress:sender];
329
- const auto eventEmitter = [self getTouchEventEmitter:location];
330
-
331
- if (eventEmitter) {
332
- const auto eventTarget = eventEmitter->getEventTarget();
333
- const auto target = eventTarget ? eventTarget->getTag() : 0;
334
- eventEmitter->onLongPress(RNUITextViewChildEventEmitter::OnLongPress{target});
335
- }
336
- }
337
-
338
784
  // MARK: - UITextViewDelegate
339
785
 
340
786
  - (void)textViewDidChangeSelection:(UITextView *)textView
@@ -342,11 +788,27 @@ static NSLineBreakMode RCTNSLineBreakModeFromEllipsizeMode(RNUITextViewEllipsize
342
788
  if (_suppressSelectionChange) {
343
789
  return;
344
790
  }
791
+
792
+ const NSRange selectedRange = textView.selectedRange;
793
+ if (_pressIsActive && selectedRange.location != NSNotFound && selectedRange.length > 0) {
794
+ if (_activePressHasLongPressHandler) {
795
+ // A link with onLongPress owns the hold. Undo any selection started by
796
+ // UITextView without cancelling the link's pending long-press timer.
797
+ _suppressSelectionChange = YES;
798
+ textView.selectedTextRange = nil;
799
+ _suppressSelectionChange = NO;
800
+ return;
801
+ }
802
+
803
+ // Without onLongPress, native selection owns the hold and the pending tap
804
+ // must not fire when the finger eventually lifts.
805
+ [self invalidateActivePress];
806
+ [self clearPressHighlightAfterMinimumDuration];
807
+ }
345
808
  if (_eventEmitter == nullptr) {
346
809
  return;
347
810
  }
348
811
 
349
- const NSRange selectedRange = textView.selectedRange;
350
812
  if (selectedRange.location == NSNotFound) {
351
813
  return;
352
814
  }