forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRCTBackedTextInputDelegateAdapter.mm
More file actions
588 lines (489 loc) · 19 KB
/
RCTBackedTextInputDelegateAdapter.mm
File metadata and controls
588 lines (489 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <React/RCTBackedTextInputDelegateAdapter.h>
#import "RCTBackedTextInputViewProtocol.h" // [macOS
#import "RCTBackedTextInputDelegate.h"
#import <React/RCTTextUIKit.h> // macOS]
#pragma mark - RCTBackedTextFieldDelegateAdapter (for UITextField)
static void *TextFieldSelectionObservingContext = &TextFieldSelectionObservingContext;
#if !TARGET_OS_OSX // [macOS]
@interface RCTBackedTextFieldDelegateAdapter () <UITextFieldDelegate, UITextDropDelegate>
#else // [macOS
@interface RCTBackedTextFieldDelegateAdapter () <RCTUITextFieldDelegate>
#endif // macOS]
@end
@implementation RCTBackedTextFieldDelegateAdapter {
__weak UITextField<RCTBackedTextInputViewProtocol> *_backedTextInputView;
BOOL _textDidChangeIsComing;
#if !TARGET_OS_OSX // [macOS]
UITextRange *_previousSelectedTextRange;
#else // [macOS
NSRange _previousSelectedTextRange;
#endif // macOS]
}
- (instancetype)initWithTextField:(UITextField<RCTBackedTextInputViewProtocol> *)backedTextInputView
{
if (self = [super init]) {
_backedTextInputView = backedTextInputView;
backedTextInputView.delegate = self;
#if !TARGET_OS_OSX // [macOS]
backedTextInputView.textDropDelegate = self;
#endif // [macOS]
#if !TARGET_OS_OSX // [macOS]
[_backedTextInputView addTarget:self
action:@selector(textFieldDidChange)
forControlEvents:UIControlEventEditingChanged];
[_backedTextInputView addTarget:self
action:@selector(textFieldDidEndEditingOnExit)
forControlEvents:UIControlEventEditingDidEndOnExit];
#endif // [macOS]
}
return self;
}
- (void)dealloc
{
#if !TARGET_OS_OSX // [macOS]
[_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingChanged];
[_backedTextInputView removeTarget:self action:nil forControlEvents:UIControlEventEditingDidEndOnExit];
#endif // [macOS]
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(__unused UITextField *)textField
{
return [_backedTextInputView.textInputDelegate textInputShouldBeginEditing];
}
- (void)textFieldDidBeginEditing:(__unused UITextField *)textField
{
[_backedTextInputView.textInputDelegate textInputDidBeginEditing];
}
- (BOOL)textFieldShouldEndEditing:(__unused UITextField *)textField
{
return [_backedTextInputView.textInputDelegate textInputShouldEndEditing];
}
- (void)textFieldDidEndEditing:(__unused UITextField *)textField
{
if (_textDidChangeIsComing) {
// iOS does't call `textViewDidChange:` delegate method if the change was happened because of autocorrection
// which was triggered by losing focus. So, we call it manually.
_textDidChangeIsComing = NO;
[_backedTextInputView.textInputDelegate textInputDidChange];
}
[_backedTextInputView.textInputDelegate textInputDidEndEditing];
}
- (BOOL)textField:(__unused UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSString *newText = [_backedTextInputView.textInputDelegate textInputShouldChangeText:string inRange:range];
if (newText == nil) {
return NO;
}
if ([newText isEqualToString:string]) {
_textDidChangeIsComing = YES;
return YES;
}
NSMutableAttributedString *attributedString = [_backedTextInputView.attributedText mutableCopy];
[attributedString replaceCharactersInRange:range withString:newText];
[_backedTextInputView setAttributedText:[attributedString copy]];
#if !TARGET_OS_OSX // [macOS]
// Setting selection to the end of the replaced text.
UITextPosition *position = [_backedTextInputView positionFromPosition:_backedTextInputView.beginningOfDocument
offset:(range.location + newText.length)];
[_backedTextInputView setSelectedTextRange:[_backedTextInputView textRangeFromPosition:position toPosition:position]
notifyDelegate:YES];
#endif // [macOS]
[self textFieldDidChange];
return NO;
}
- (BOOL)textFieldShouldReturn:(__unused UITextField *)textField
{
// Ignore the value of whether we submitted; just make sure the submit event is called if necessary.
[_backedTextInputView.textInputDelegate textInputShouldSubmitOnReturn];
return [_backedTextInputView.textInputDelegate textInputShouldReturn];
}
#pragma mark - UIControlEventEditing* Family Events
- (void)textFieldDidChange
{
_textDidChangeIsComing = NO;
[_backedTextInputView.textInputDelegate textInputDidChange];
// `selectedTextRangeWasSet` isn't triggered during typing.
[self textFieldProbablyDidChangeSelection];
}
- (void)textFieldDidEndEditingOnExit
{
[_backedTextInputView.textInputDelegate textInputDidReturn];
}
#pragma mark - UIKeyboardInput (private UIKit protocol)
// This method allows us to detect a [Backspace] `keyPress`
// even when there is no more text in the `UITextField`.
- (BOOL)keyboardInputShouldDelete:(__unused UITextField *)textField
{
[_backedTextInputView.textInputDelegate textInputShouldChangeText:@"" inRange:NSMakeRange(0, 0)];
return YES;
}
#pragma mark - Public Interface
#if !TARGET_OS_OSX // [macOS]
- (void)skipNextTextInputDidChangeSelectionEventWithTextRange:(UITextRange *)textRange
#else // [macOS
- (void)skipNextTextInputDidChangeSelectionEventWithTextRange:(NSRange)textRange
#endif // macOS]
{
_previousSelectedTextRange = textRange;
}
- (void)selectedTextRangeWasSet
{
[self textFieldProbablyDidChangeSelection];
}
#pragma mark - Generalization
- (void)textFieldProbablyDidChangeSelection
{
if (RCTTextSelectionEqual([_backedTextInputView selectedTextRange], _previousSelectedTextRange)) { // [macOS]
return;
}
_previousSelectedTextRange = [_backedTextInputView selectedTextRange]; // [macOS] setter not defined for mac
[_backedTextInputView.textInputDelegate textInputDidChangeSelection];
}
#if !TARGET_OS_OSX // [macOS]
#pragma mark - UITextDropDelegate
- (UITextDropEditability)textDroppableView:(UIView<UITextDroppable> *)textDroppableView
willBecomeEditableForDrop:(id<UITextDropRequest>)drop
{
if (!_backedTextInputView.enabled) {
return UITextDropEditabilityNo;
}
if ([self _shouldAcceptDrop:drop]) {
return UITextDropEditabilityYes;
} else {
return UITextDropEditabilityNo;
}
}
- (UITextDropProposal *)textDroppableView:(UIView<UITextDroppable> *)textDroppableView
proposalForDrop:(id<UITextDropRequest>)drop
{
if ([self _shouldAcceptDrop:drop]) {
return drop.suggestedProposal;
} else {
return [[UITextDropProposal alloc] initWithDropOperation:UIDropOperationCancel];
}
}
- (bool)_shouldAcceptDrop:(id<UITextDropRequest>)drop
{
if (_backedTextInputView.acceptDragAndDropTypes) {
// If we have accepted types, we should only accept drops that match one of them
return [drop.dropSession hasItemsConformingToTypeIdentifiers:_backedTextInputView.acceptDragAndDropTypes];
} else {
// If we don't have any accepted types, we should accept any drop
return true;
}
}
#endif // macOS]
#if TARGET_OS_OSX // [macOS
#pragma mark - NSTextFieldDelegate
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
return [self textFieldShouldEndEditing:_backedTextInputView];
}
- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{
id<RCTBackedTextInputDelegate> textInputDelegate = [_backedTextInputView textInputDelegate];
BOOL commandHandled = NO;
// enter/return
if (commandSelector == @selector(insertNewline:) || commandSelector == @selector(insertNewlineIgnoringFieldEditor:)) {
[self textFieldDidEndEditingOnExit];
if ([textInputDelegate textInputShouldSubmitOnReturn]) {
[[_backedTextInputView window] makeFirstResponder:nil];
}
commandHandled = YES;
//backspace
} else if (commandSelector == @selector(deleteBackward:)) {
if (textInputDelegate != nil && ![textInputDelegate textInputShouldHandleDeleteBackward:_backedTextInputView]) {
commandHandled = YES;
} else {
[self keyboardInputShouldDelete:_backedTextInputView];
}
//deleteForward
} else if (commandSelector == @selector(deleteForward:)) {
id<RCTBackedTextInputDelegate> textInputDelegate = [_backedTextInputView textInputDelegate];
if (textInputDelegate != nil && ![textInputDelegate textInputShouldHandleDeleteForward:_backedTextInputView]) {
commandHandled = YES;
} else {
[self keyboardInputShouldDelete:_backedTextInputView];
}
//paste
} else if (commandSelector == @selector(paste:)) {
id<RCTBackedTextInputDelegate> textInputDelegate = [_backedTextInputView textInputDelegate];
if (textInputDelegate != nil && ![textInputDelegate textInputShouldHandlePaste:_backedTextInputView]) {
commandHandled = YES;
} else {
_backedTextInputView.textWasPasted = YES;
}
//escape
} else if (commandSelector == @selector(cancelOperation:)) {
[textInputDelegate textInputDidCancel];
if (![textInputDelegate hasKeyDownEventOrKeyUpEvent:@"Escape"]) {
[[_backedTextInputView window] makeFirstResponder:nil];
}
commandHandled = YES;
}
return commandHandled;
}
- (void)textFieldBeginEditing:(NSTextField *)textField
{
[self textFieldDidBeginEditing:_backedTextInputView];
}
- (void)textFieldDidChange:(NSTextField *)textField
{
[self textFieldDidChange];
}
- (void)textFieldEndEditing:(NSTextField *)textField
{
[self textFieldDidEndEditing:_backedTextInputView];
}
- (void)textFieldDidChangeSelection:(NSTextField *)textField
{
[self selectedTextRangeWasSet];
}
#endif // macOS]
@end
#pragma mark - RCTBackedTextViewDelegateAdapter (for UITextView)
#if !TARGET_OS_OSX // [macOS]
@interface RCTBackedTextViewDelegateAdapter () <UITextViewDelegate, UITextDropDelegate>
#else // [macOS
@interface RCTBackedTextViewDelegateAdapter () <UITextViewDelegate>
#endif // macOS]
@end
@implementation RCTBackedTextViewDelegateAdapter {
#if !TARGET_OS_OSX // [macOS]
__weak UITextView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
#else // [macOS
__unsafe_unretained UITextView<RCTBackedTextInputViewProtocol> *_backedTextInputView;
#endif // macOS]
NSAttributedString *_lastStringStateWasUpdatedWith;
BOOL _ignoreNextTextInputCall;
BOOL _textDidChangeIsComing;
#if !TARGET_OS_OSX // [macOS]
UITextRange *_previousSelectedTextRange;
#else // [macOS
NSRange _previousSelectedTextRange;
NSUndoManager *_undoManager;
#endif // macOS]
}
- (instancetype)initWithTextView:(UITextView<RCTBackedTextInputViewProtocol> *)backedTextInputView
{
if (self = [super init]) {
_backedTextInputView = backedTextInputView;
backedTextInputView.delegate = self;
#if !TARGET_OS_OSX // [macOS]
backedTextInputView.textDropDelegate = self;
#endif // [macOS]
}
return self;
}
#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(__unused UITextView *)textView
{
return [_backedTextInputView.textInputDelegate textInputShouldBeginEditing];
}
- (void)textViewDidBeginEditing:(__unused UITextView *)textView
{
[_backedTextInputView.textInputDelegate textInputDidBeginEditing];
}
- (BOOL)textViewShouldEndEditing:(__unused UITextView *)textView
{
return [_backedTextInputView.textInputDelegate textInputShouldEndEditing];
}
- (void)textViewDidEndEditing:(__unused UITextView *)textView
{
if (_textDidChangeIsComing) {
// iOS does't call `textViewDidChange:` delegate method if the change was happened because of autocorrection
// which was triggered by losing focus. So, we call it manually.
_textDidChangeIsComing = NO;
[_backedTextInputView.textInputDelegate textInputDidChange];
}
[_backedTextInputView.textInputDelegate textInputDidEndEditing];
}
- (BOOL)textView:(__unused UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
#if !TARGET_OS_OSX // [macOS]
// Custom implementation of `textInputShouldReturn` and `textInputDidReturn` pair for `UITextView`.
if (!_backedTextInputView.textWasPasted && [text isEqualToString:@"\n"]) {
const BOOL shouldSubmit = [_backedTextInputView.textInputDelegate textInputShouldSubmitOnReturn];
const BOOL shouldReturn = [_backedTextInputView.textInputDelegate textInputShouldReturn];
if (shouldReturn) {
[_backedTextInputView.textInputDelegate textInputDidReturn];
[_backedTextInputView endEditing:NO];
return NO;
} else if (shouldSubmit) {
return NO;
}
}
#endif // [macOS]
NSString *newText = [_backedTextInputView.textInputDelegate textInputShouldChangeText:text inRange:range];
if (newText == nil) {
return NO;
}
#if !TARGET_OS_OSX // [macOS]
if (range.location + range.length > _backedTextInputView.text.length) {
range = NSMakeRange(range.location, _backedTextInputView.text.length - range.location);
#else // [macOS
if (range.location + range.length > _backedTextInputView.string.length) {
range = NSMakeRange(range.location, _backedTextInputView.string.length - range.location);
#endif // macOS]
} else if ([newText isEqualToString:text]) {
_textDidChangeIsComing = YES;
return YES;
}
NSMutableAttributedString *attributedString = [_backedTextInputView.attributedText mutableCopy];
[attributedString replaceCharactersInRange:range withString:newText];
[_backedTextInputView setAttributedText:[attributedString copy]];
#if !TARGET_OS_OSX // [macOS]
// Setting selection to the end of the replaced text.
UITextPosition *position = [_backedTextInputView positionFromPosition:_backedTextInputView.beginningOfDocument
offset:(range.location + newText.length)];
[_backedTextInputView setSelectedTextRange:[_backedTextInputView textRangeFromPosition:position toPosition:position]
notifyDelegate:YES];
#endif // [macOS]
[self textViewDidChange:_backedTextInputView];
return NO;
}
- (void)textViewDidChange:(__unused UITextView *)textView
{
if (_ignoreNextTextInputCall && [_lastStringStateWasUpdatedWith isEqual:_backedTextInputView.attributedText]) {
_ignoreNextTextInputCall = NO;
return;
}
_textDidChangeIsComing = NO;
[_backedTextInputView.textInputDelegate textInputDidChange];
}
#if !TARGET_OS_OSX // [macOS]
- (void)textViewDidChangeSelection:(__unused UITextView *)textView
{
if (_lastStringStateWasUpdatedWith && ![_lastStringStateWasUpdatedWith isEqual:_backedTextInputView.attributedText]) {
[self textViewDidChange:_backedTextInputView];
if (![_backedTextInputView isGhostTextChanging]) { // [macOS]
_ignoreNextTextInputCall = YES;
} // [macOS]
}
_lastStringStateWasUpdatedWith = _backedTextInputView.attributedText;
[self textViewProbablyDidChangeSelection];
}
#endif // [macOS]
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(RCTUIScrollView *)scrollView // [macOS]
{
if ([_backedTextInputView.textInputDelegate respondsToSelector:@selector(scrollViewDidScroll:)]) {
[_backedTextInputView.textInputDelegate scrollViewDidScroll:scrollView];
}
}
#if TARGET_OS_OSX // [macOS
#pragma mark - NSTextViewDelegate
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(nullable NSString *)replacementString
{
return [self textView:textView shouldChangeTextInRange:affectedCharRange replacementText:replacementString];
}
- (void)textViewDidChangeSelection:(NSNotification *)notification
{
[self textViewProbablyDidChangeSelection];
}
- (void)textDidBeginEditing:(NSNotification *)notification
{
[self textViewDidBeginEditing:_backedTextInputView];
}
- (void)textDidChange:(NSNotification *)notification
{
[self textViewDidChange:_backedTextInputView];
}
- (void)textDidEndEditing:(NSNotification *)notification
{
[self textViewDidEndEditing:_backedTextInputView];
}
- (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
BOOL commandHandled = NO;
id<RCTBackedTextInputDelegate> textInputDelegate = [_backedTextInputView textInputDelegate];
// enter/return
if ((commandSelector == @selector(insertNewline:) || commandSelector == @selector(insertNewlineIgnoringFieldEditor:))) {
if ([textInputDelegate textInputShouldSubmitOnReturn]) {
[_backedTextInputView.window makeFirstResponder:nil];
commandHandled = YES;
}
//backspace
} else if (commandSelector == @selector(deleteBackward:)) {
commandHandled = textInputDelegate != nil && ![textInputDelegate textInputShouldHandleDeleteBackward:_backedTextInputView];
//deleteForward
} else if (commandSelector == @selector(deleteForward:)) {
commandHandled = textInputDelegate != nil && ![textInputDelegate textInputShouldHandleDeleteForward:_backedTextInputView];
//escape
} else if (commandSelector == @selector(cancelOperation:)) {
[textInputDelegate textInputDidCancel];
if (![textInputDelegate hasKeyDownEventOrKeyUpEvent:@"Escape"]) {
[[_backedTextInputView window] makeFirstResponder:nil];
}
commandHandled = YES;
}
return commandHandled;
}
- (NSUndoManager *)undoManagerForTextView:(NSTextView *)textView {
if (!_undoManager) {
_undoManager = [NSUndoManager new];
}
return _undoManager;
}
#endif // macOS]
#pragma mark - Public Interface
#if !TARGET_OS_OSX // [macOS]
- (void)skipNextTextInputDidChangeSelectionEventWithTextRange:(UITextRange *)textRange
#else // [macOS
- (void)skipNextTextInputDidChangeSelectionEventWithTextRange:(NSRange)textRange
#endif // macOS]
{
_previousSelectedTextRange = textRange;
}
#pragma mark - Generalization
- (void)textViewProbablyDidChangeSelection
{
if (RCTTextSelectionEqual([_backedTextInputView selectedTextRange], _previousSelectedTextRange)) { // [macOS]
return;
}
_previousSelectedTextRange = [_backedTextInputView selectedTextRange]; // [macOS]
[_backedTextInputView.textInputDelegate textInputDidChangeSelection];
}
#if !TARGET_OS_OSX // [macOS]
#pragma mark - UITextDropDelegate
- (UITextDropEditability)textDroppableView:(UIView<UITextDroppable> *)textDroppableView
willBecomeEditableForDrop:(id<UITextDropRequest>)drop
{
if (!_backedTextInputView.isEditable) {
return UITextDropEditabilityNo;
}
if ([self _shouldAcceptDrop:drop]) {
return UITextDropEditabilityYes;
} else {
return UITextDropEditabilityNo;
}
}
- (UITextDropProposal *)textDroppableView:(UIView<UITextDroppable> *)textDroppableView
proposalForDrop:(id<UITextDropRequest>)drop
{
if ([self _shouldAcceptDrop:drop]) {
return drop.suggestedProposal;
} else {
return [[UITextDropProposal alloc] initWithDropOperation:UIDropOperationCancel];
}
}
- (bool)_shouldAcceptDrop:(id<UITextDropRequest>)drop
{
if (_backedTextInputView.acceptDragAndDropTypes) {
// If we have accepted types, we should only accept drops that match one of them
return [drop.dropSession hasItemsConformingToTypeIdentifiers:(_backedTextInputView.acceptDragAndDropTypes)];
} else {
// If we don't have any accepted types, we should accept any drop
return true;
}
}
#endif // macOS]
@end