Skip to content

Commit a8a63d8

Browse files
committed
feat: 0 length range detection revamp
1 parent 649b500 commit a8a63d8

12 files changed

Lines changed: 86 additions & 70 deletions

example/ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2690,7 +2690,7 @@ SPEC CHECKSUMS:
26902690
ReactAppDependencyProvider: 77a7129540c0e06ab6be9a2bcb887d3d2e594431
26912691
ReactCodegen: b62625187ce853918021a7a9178cc406e9820d56
26922692
ReactCommon: d07170f92e0e853091a70b2741b7c43f5dfdea73
2693-
ReactNativeEnriched: b11d66700889cd36c9938a825736de1929349b24
2693+
ReactNativeEnriched: ce7a893fdefce993826a3177da6811b513e26526
26942694
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
26952695
Yoga: 6af5d1e0290903c82b3e0cb6836a5f898c1c4634
26962696

ios/styles/BlockQuoteStyle.mm

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ - (void)removeTypingAttributes {
121121
- (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
122122
if(
123123
[self detectStyle:_input->textView.selectedRange] &&
124-
NSEqualRanges(_input->textView.selectedRange, NSMakeRange(0, 0)) &&
125-
[text isEqualToString:@""]
124+
NSEqualRanges(_input->textView.selectedRange, NSMakeRange(0, 0)) &&
125+
[text isEqualToString:@""]
126126
) {
127127
// removing first quote line by backspacing doesn't remove typing attributes because it doesn't run textViewDidChange
128128
// so we try guessing that a line should be deleted here
@@ -162,22 +162,11 @@ - (BOOL)detectStyle:(NSRange)range {
162162
}
163163
];
164164
} else {
165-
NSInteger searchLocation = range.location;
166-
if(searchLocation == _input->textView.textStorage.length) {
167-
NSParagraphStyle *pStyle = _input->textView.typingAttributes[NSParagraphStyleAttributeName];
168-
return [self styleCondition:pStyle :NSMakeRange(0, 0)];
169-
}
170-
171-
NSRange paragraphRange = NSMakeRange(0, 0);
172-
NSRange inputRange = NSMakeRange(0, _input->textView.textStorage.length);
173-
NSParagraphStyle *paragraph = [_input->textView.textStorage
174-
attribute:NSParagraphStyleAttributeName
175-
atIndex:searchLocation
176-
longestEffectiveRange: &paragraphRange
177-
inRange:inputRange
165+
return [OccurenceUtils detect:NSParagraphStyleAttributeName withInput:_input atIndex:range.location checkPrevious:YES
166+
withCondition:^BOOL(id _Nullable value, NSRange range) {
167+
return [self styleCondition:value :range];
168+
}
178169
];
179-
180-
return [self styleCondition:paragraph :NSMakeRange(0, 0)];
181170
}
182171
}
183172

ios/styles/BoldStyle.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ - (BOOL)detectStyle:(NSRange)range {
9898
}
9999
];
100100
} else {
101-
UIFont *currentFontAttr = (UIFont *)_input->textView.typingAttributes[NSFontAttributeName];
102-
return [self styleCondition:currentFontAttr :range];
101+
return [OccurenceUtils detect:NSFontAttributeName withInput:_input atIndex:range.location checkPrevious:NO
102+
withCondition:^BOOL(id _Nullable value, NSRange range) {
103+
return [self styleCondition:value :range];
104+
}
105+
];
103106
}
104107
}
105108

ios/styles/HeadingStyleBase.mm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,11 @@ - (BOOL)detectStyle:(NSRange)range {
117117
}
118118
];
119119
} else {
120-
UIFont *currentFontAttr = (UIFont *)[self typedInput]->textView.typingAttributes[NSFontAttributeName];
121-
if(currentFontAttr == nullptr) {
122-
return false;
123-
}
124-
return currentFontAttr.pointSize == [self getHeadingFontSize];
120+
return [OccurenceUtils detect:NSFontAttributeName withInput:[self typedInput] atIndex:range.location checkPrevious:YES
121+
withCondition:^BOOL(id _Nullable value, NSRange range) {
122+
return [self styleCondition:value :range];
123+
}
124+
];
125125
}
126126
}
127127

ios/styles/InlineCodeStyle.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ - (BOOL)detectStyle:(NSRange)range {
140140

141141
return detected;
142142
} else {
143-
UIColor *currentBgColorAttr = (UIColor *)_input->textView.typingAttributes[NSBackgroundColorAttributeName];
144-
return [self styleCondition:currentBgColorAttr :range];
143+
return [OccurenceUtils detect:NSBackgroundColorAttributeName withInput:_input atIndex:range.location checkPrevious:NO
144+
withCondition:^BOOL(id _Nullable value, NSRange range) {
145+
return [self styleCondition:value :range];
146+
}
147+
];
145148
}
146149
}
147150

ios/styles/ItalicStyle.mm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ - (BOOL)detectStyle:(NSRange)range {
8383
}
8484
];
8585
} else {
86-
UIFont *currentFontAttr = (UIFont *)_input->textView.typingAttributes[NSFontAttributeName];
87-
if(currentFontAttr == nullptr) {
88-
return false;
89-
}
90-
return [currentFontAttr isItalic];
86+
return [OccurenceUtils detect:NSFontAttributeName withInput:_input atIndex:range.location checkPrevious:NO
87+
withCondition:^BOOL(id _Nullable value, NSRange range) {
88+
return [self styleCondition:value :range];
89+
}
90+
];
9191
}
9292
}
9393

ios/styles/OrderedListStyle.mm

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ - (void)removeTypingAttributes {
133133
- (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
134134
if(
135135
[self detectStyle:_input->textView.selectedRange] &&
136-
NSEqualRanges(_input->textView.selectedRange, NSMakeRange(0, 0)) &&
137-
[text isEqualToString:@""]
136+
NSEqualRanges(_input->textView.selectedRange, NSMakeRange(0, 0)) &&
137+
[text isEqualToString:@""]
138138
) {
139139
// removing first list point by backspacing doesn't remove typing attributes because it doesn't run textViewDidChange
140140
// so we try guessing that a point should be deleted here
@@ -204,22 +204,11 @@ - (BOOL)detectStyle:(NSRange)range {
204204
}
205205
];
206206
} else {
207-
NSInteger searchLocation = range.location;
208-
if(searchLocation == _input->textView.textStorage.length) {
209-
NSParagraphStyle *pStyle = _input->textView.typingAttributes[NSParagraphStyleAttributeName];
210-
return [self styleCondition:pStyle :NSMakeRange(0, 0)];
211-
}
212-
213-
NSRange paragraphRange = NSMakeRange(0, 0);
214-
NSRange inputRange = NSMakeRange(0, _input->textView.textStorage.length);
215-
NSParagraphStyle *paragraph = [_input->textView.textStorage
216-
attribute:NSParagraphStyleAttributeName
217-
atIndex:searchLocation
218-
longestEffectiveRange: &paragraphRange
219-
inRange:inputRange
207+
return [OccurenceUtils detect:NSParagraphStyleAttributeName withInput:_input atIndex:range.location checkPrevious:YES
208+
withCondition:^BOOL(id _Nullable value, NSRange range) {
209+
return [self styleCondition:value :range];
210+
}
220211
];
221-
222-
return [self styleCondition:paragraph :NSMakeRange(0, 0)];
223212
}
224213
}
225214

ios/styles/StrikethroughStyle.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ - (BOOL)detectStyle:(NSRange)range {
5656
}
5757
];
5858
} else {
59-
NSNumber *currenStrikethroughAttr = (NSNumber *)_input->textView.typingAttributes[NSStrikethroughStyleAttributeName];
60-
return currenStrikethroughAttr != nullptr;
59+
return [OccurenceUtils detect:NSStrikethroughStyleAttributeName withInput:_input atIndex:range.location checkPrevious:NO
60+
withCondition:^BOOL(id _Nullable value, NSRange range) {
61+
return [self styleCondition:value :range];
62+
}
63+
];
6164
}
6265
}
6366

ios/styles/UnderlineStyle.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,11 @@ - (BOOL)detectStyle:(NSRange)range {
8888
}
8989
];
9090
} else {
91-
NSNumber *currentUnderlineAttr = (NSNumber *)_input->textView.typingAttributes[NSUnderlineStyleAttributeName];
92-
return [self styleCondition:currentUnderlineAttr :range];
91+
return [OccurenceUtils detect:NSUnderlineStyleAttributeName withInput:_input atIndex:range.location checkPrevious:NO
92+
withCondition:^BOOL(id _Nullable value, NSRange range) {
93+
return [self styleCondition:value :range];
94+
}
95+
];
9396
}
9497
}
9598

ios/styles/UnorderedListStyle.mm

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ - (void)removeTypingAttributes {
133133
- (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
134134
if(
135135
[self detectStyle:_input->textView.selectedRange] &&
136-
NSEqualRanges(_input->textView.selectedRange, NSMakeRange(0, 0)) &&
137-
[text isEqualToString:@""]
136+
NSEqualRanges(_input->textView.selectedRange, NSMakeRange(0, 0)) &&
137+
[text isEqualToString:@""]
138138
) {
139139
// removing first list point by backspacing doesn't remove typing attributes because it doesn't run textViewDidChange
140140
// so we try guessing that a point should be deleted here
@@ -204,22 +204,11 @@ - (BOOL)detectStyle:(NSRange)range {
204204
}
205205
];
206206
} else {
207-
NSInteger searchLocation = range.location;
208-
if(searchLocation == _input->textView.textStorage.length) {
209-
NSParagraphStyle *pStyle = _input->textView.typingAttributes[NSParagraphStyleAttributeName];
210-
return [self styleCondition:pStyle :NSMakeRange(0, 0)];
211-
}
212-
213-
NSRange paragraphRange = NSMakeRange(0, 0);
214-
NSRange inputRange = NSMakeRange(0, _input->textView.textStorage.length);
215-
NSParagraphStyle *paragraph = [_input->textView.textStorage
216-
attribute:NSParagraphStyleAttributeName
217-
atIndex:searchLocation
218-
longestEffectiveRange: &paragraphRange
219-
inRange:inputRange
207+
return [OccurenceUtils detect:NSParagraphStyleAttributeName withInput:_input atIndex:range.location checkPrevious:YES
208+
withCondition:^BOOL(id _Nullable value, NSRange range) {
209+
return [self styleCondition:value :range];
210+
}
220211
];
221-
222-
return [self styleCondition:paragraph :NSMakeRange(0, 0)];
223212
}
224213
}
225214

0 commit comments

Comments
 (0)