File tree Expand file tree Collapse file tree 3 files changed +32
-2
lines changed Expand file tree Collapse file tree 3 files changed +32
-2
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ - (instancetype)initWithFrame:(CGRect)frame
51
51
self.insertionPointColor = [NSColor selectedControlColor ];
52
52
// Fix blurry text on non-retina displays.
53
53
self.canDrawSubviewsIntoLayer = YES ;
54
+ self.allowsUndo = YES ;
54
55
#endif // ]TODO(macOS GH#774)
55
56
56
57
_textInputDelegateAdapter = [[RCTBackedTextViewDelegateAdapter alloc ] initWithTextView: self ];
@@ -185,6 +186,18 @@ - (BOOL)becomeFirstResponder
185
186
186
187
return success;
187
188
}
189
+
190
+ - (BOOL )resignFirstResponder
191
+ {
192
+ BOOL success = [super resignFirstResponder ];
193
+
194
+ if (success) {
195
+ // Break undo coalescing when losing focus.
196
+ [self breakUndoCoalescing ];
197
+ }
198
+
199
+ return success;
200
+ }
188
201
#endif // ]TODO(macOS GH#774)
189
202
190
203
- (void )setDefaultTextAttributes : (NSDictionary <NSAttributedStringKey, id> *)defaultTextAttributes
@@ -247,6 +260,9 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
247
260
}
248
261
#else // [TODO(macOS GH#774)
249
262
if (![self .textStorage isEqualTo: attributedText.string]) {
263
+ // Break undo coalescing when the text is changed by JS (e.g. autocomplete).
264
+ [self breakUndoCoalescing ];
265
+
250
266
if (attributedText != nil ) {
251
267
[self .textStorage setAttributedString: attributedText];
252
268
} else {
Original file line number Diff line number Diff line change @@ -263,6 +263,7 @@ @implementation RCTBackedTextViewDelegateAdapter {
263
263
UITextRange *_previousSelectedTextRange;
264
264
#else // [TODO(macOS GH#774)
265
265
NSRange _previousSelectedTextRange;
266
+ NSUndoManager *_undoManager;
266
267
#endif // ]TODO(macOS GH#774)
267
268
}
268
269
@@ -428,6 +429,13 @@ - (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
428
429
return commandHandled;
429
430
}
430
431
432
+ - (NSUndoManager *)undoManagerForTextView : (NSTextView *)textView {
433
+ if (!_undoManager) {
434
+ _undoManager = [NSUndoManager new ];
435
+ }
436
+ return _undoManager;
437
+ }
438
+
431
439
#endif // ]TODO(macOS GH#774)
432
440
433
441
#pragma mark - Public Interface
Original file line number Diff line number Diff line change @@ -144,7 +144,7 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
144
144
BOOL textNeedsUpdate = NO ;
145
145
// Remove tag attribute to ensure correct attributed string comparison.
146
146
NSMutableAttributedString *const backedTextInputViewTextCopy = [self .backedTextInputView.attributedText mutableCopy ];
147
- NSMutableAttributedString *const attributedTextCopy = [attributedText mutableCopy ];
147
+ NSMutableAttributedString *const attributedTextCopy = [attributedText mutableCopy ] ?: [ NSMutableAttributedString new ] ;
148
148
149
149
[backedTextInputViewTextCopy removeAttribute: RCTTextAttributesTagAttributeName
150
150
range: NSMakeRange (0 , backedTextInputViewTextCopy.length)];
@@ -160,7 +160,13 @@ - (void)setAttributedText:(NSAttributedString *)attributedText
160
160
#else // [TODO(macOS GH#774)
161
161
NSRange selection = [self .backedTextInputView selectedTextRange ];
162
162
#endif // ]TODO(macOS GH#774)
163
- NSInteger oldTextLength = self.backedTextInputView .attributedText .string .length ;
163
+ NSAttributedString *oldAttributedText = [self .backedTextInputView.attributedText copy ];
164
+ NSInteger oldTextLength = oldAttributedText.string .length ;
165
+
166
+ [self .backedTextInputView.undoManager registerUndoWithTarget: self handler: ^(RCTBaseTextInputView *strongSelf) {
167
+ strongSelf.attributedText = oldAttributedText;
168
+ [strongSelf textInputDidChange ];
169
+ }];
164
170
165
171
self.backedTextInputView .attributedText = attributedText;
166
172
You can’t perform that action at this time.
0 commit comments