@@ -63,7 +63,6 @@ - (void)setDefaults {
6363 _recentlyActiveLinkRange = NSMakeRange (0 , 0 );
6464 _recentlyChangedRange = NSMakeRange (0 , 0 );
6565 _recentlyEmittedString = @" " ;
66- currentSelection = NSMakeRange (0 , 0 );
6766
6867 stylesDict = @{
6968 @([BoldStyle getStyleType ]) : [[BoldStyle alloc ] initWithEditor: self ],
@@ -221,7 +220,7 @@ - (void)tryUpdatingActiveStyles {
221220 for (NSNumber * type in stylesDict) {
222221 id <BaseStyleProtocol> style = stylesDict[type];
223222 BOOL wasActive = [_activeStyles containsObject: type];
224- BOOL isActive = [style detectStyle: currentSelection ];
223+ BOOL isActive = [style detectStyle: textView.selectedRange ];
225224 if (wasActive != isActive) {
226225 updateNeeded = YES ;
227226 if (isActive) {
@@ -238,8 +237,8 @@ - (void)tryUpdatingActiveStyles {
238237 NSRange candidateLinkRange = NSMakeRange (0 , 0 );
239238 LinkStyle *linkStyleClass = (LinkStyle *)stylesDict[@([LinkStyle getStyleType ])];
240239 if (linkStyleClass != nullptr ) {
241- candidateLinkData = [linkStyleClass getCurrentLinkDataIn: currentSelection ];
242- candidateLinkRange = [linkStyleClass getFullLinkRangeAt: currentSelection .location];
240+ candidateLinkData = [linkStyleClass getCurrentLinkDataIn: textView.selectedRange ];
241+ candidateLinkRange = [linkStyleClass getFullLinkRangeAt: textView.selectedRange .location];
243242 }
244243
245244 if (wasActive == NO ) {
@@ -348,8 +347,8 @@ - (void)emitOnLinkDetectedEvent:(NSString *)text url:(NSString *)url {
348347- (void )toggleRegularStyle : (StyleType)type {
349348 id <BaseStyleProtocol> styleClass = stylesDict[@(type)];
350349
351- if ([self handleStyleBlocksAndConflicts: type range: currentSelection ]) {
352- [styleClass applyStyle: currentSelection ];
350+ if ([self handleStyleBlocksAndConflicts: type range: textView.selectedRange ]) {
351+ [styleClass applyStyle: textView.selectedRange ];
353352 [self tryUpdatingActiveStyles ];
354353 }
355354}
@@ -361,7 +360,7 @@ - (void)addLinkAt:(NSInteger)start end:(NSInteger)end text:(NSString *)text url:
361360 // translate the output start-end notation to range
362361 NSRange linkRange = NSMakeRange (start, end - start);
363362 if ([self handleStyleBlocksAndConflicts: [LinkStyle getStyleType ] range: linkRange]) {
364- [linkStyleClass addLink: text url: url range: currentSelection manual: YES ];
363+ [linkStyleClass addLink: text url: url range: linkRange manual: YES ];
365364 [self tryUpdatingActiveStyles ];
366365 }
367366}
@@ -401,12 +400,12 @@ - (BOOL)handleStyleBlocksAndConflicts:(StyleType)type range:(NSRange)range {
401400 for (NSNumber *type in types) {
402401 id <BaseStyleProtocol> styleClass = stylesDict[type];
403402
404- if (currentSelection .length >= 1 ) {
405- if ([styleClass anyOccurence: currentSelection ]) {
403+ if (textView. selectedRange .length >= 1 ) {
404+ if ([styleClass anyOccurence: textView.selectedRange ]) {
406405 [resultArray addObject: type];
407406 }
408407 } else {
409- if ([styleClass detectStyle: currentSelection ]) {
408+ if ([styleClass detectStyle: textView.selectedRange ]) {
410409 [resultArray addObject: type];
411410 }
412411 }
@@ -417,18 +416,15 @@ - (BOOL)handleStyleBlocksAndConflicts:(StyleType)type range:(NSRange)range {
417416// MARK: - UITextView delegate methods
418417
419418- (void )textViewDidBeginEditing : (UITextView *)textView {
420- // update current selection as well
421- currentSelection = textView.selectedRange ;
422-
423419 auto emitter = [self getEventEmitter ];
424420 if (emitter != nullptr ) {
425421 // send onFocus event
426422 emitter->onInputFocus ({});
427423
428- NSString *textAtSelection = [[[NSMutableString alloc ] initWithString: textView.textStorage.string] substringWithRange: currentSelection ];
424+ NSString *textAtSelection = [[[NSMutableString alloc ] initWithString: textView.textStorage.string] substringWithRange: textView.selectedRange ];
429425 emitter->onChangeSelection ({
430- .start = static_cast <int >(currentSelection .location ),
431- .end = static_cast <int >(currentSelection. location + currentSelection .length ),
426+ .start = static_cast <int >(textView. selectedRange .location ),
427+ .end = static_cast <int >(textView. selectedRange . location + textView. selectedRange .length ),
432428 .text = [textAtSelection toCppString ]
433429 });
434430 }
@@ -448,21 +444,18 @@ - (bool)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range r
448444}
449445
450446- (void )textViewDidChangeSelection : (UITextView *)textView {
451- // update current selection and emit event
452- if (!NSEqualRanges (textView.selectedRange , currentSelection)) {
453- currentSelection = textView.selectedRange ;
454- NSString *textAtSelection = [[[NSMutableString alloc ] initWithString: textView.textStorage.string] substringWithRange: currentSelection];
447+ // emit the event
448+ NSString *textAtSelection = [[[NSMutableString alloc ] initWithString: textView.textStorage.string] substringWithRange: textView.selectedRange];
455449
456- auto emitter = [self getEventEmitter ];
457- if (emitter != nullptr ) {
458- // iOS range works differently because it specifies location and length
459- // here, start is the location, but end is the first index BEHIND the end. So a 0 length range will have equal start and end
460- emitter->onChangeSelection ({
461- .start = static_cast <int >(currentSelection.location ),
462- .end = static_cast <int >(currentSelection.location + currentSelection.length ),
463- .text = [textAtSelection toCppString ]
464- });
465- }
450+ auto emitter = [self getEventEmitter ];
451+ if (emitter != nullptr ) {
452+ // iOS range works differently because it specifies location and length
453+ // here, start is the location, but end is the first index BEHIND the end. So a 0 length range will have equal start and end
454+ emitter->onChangeSelection ({
455+ .start = static_cast <int >(textView.selectedRange .location ),
456+ .end = static_cast <int >(textView.selectedRange .location + textView.selectedRange .length ),
457+ .text = [textAtSelection toCppString ]
458+ });
466459 }
467460
468461 // link typing attributes fix
@@ -497,12 +490,9 @@ - (void)textViewDidChange:(UITextView *)textView {
497490 }
498491 }
499492
500- BOOL needsActiveStylesUpdate = NO ;
501-
502493 // revert typing attributes to the defaults if field is empty
503494 if (textView.textStorage .length == 0 ) {
504495 textView.typingAttributes = _defaultTypingAttributes;
505- needsActiveStylesUpdate = YES ;
506496 }
507497
508498 LinkStyle* linkStyle = [stylesDict objectForKey: @([LinkStyle getStyleType ])];
@@ -516,18 +506,14 @@ - (void)textViewDidChange:(UITextView *)textView {
516506 continue ;
517507 }
518508
519- BOOL automaticLinkUpdated = [linkStyle handleAutomaticLinks: wordText inRange: [wordRange rangeValue ]];
520-
521- needsActiveStylesUpdate = needsActiveStylesUpdate || automaticLinkUpdated;
509+ [linkStyle handleAutomaticLinks: wordText inRange: [wordRange rangeValue ]];
522510 }
523511 }
524512
525513 // update height on each character change
526514 [self tryUpdatingHeight ];
527- // update active styles if needed
528- if (needsActiveStylesUpdate) {
529- [self tryUpdatingActiveStyles ];
530- }
515+ // for safety: update active styles as well
516+ [self tryUpdatingActiveStyles ];
531517}
532518
533519- (BOOL )textView : (UITextView *)textView shouldInteractWithURL : (NSURL *)URL inRange : (NSRange )characterRange interaction : (UITextItemInteraction)interaction {
0 commit comments