Skip to content

Commit e4b32a6

Browse files
fix: code review fixes
1 parent 74c71e0 commit e4b32a6

File tree

8 files changed

+34
-27
lines changed

8 files changed

+34
-27
lines changed

ios/EnrichedTextInputView.mm

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,10 @@ - (bool)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range r
12231223
[h3Style handleNewlinesInRange:range replacementText:text] ||
12241224
[ZeroWidthSpaceUtils handleBackspaceInRange:range replacementText:text input:self] ||
12251225
[ParagraphAttributesUtils handleBackspaceInRange:range replacementText:text input:self] ||
1226-
// this callback HAS TO be always evaluated last
1226+
// CRITICAL: This callback HAS TO be always evaluated last.
1227+
//
1228+
// This function is the "Generic Fallback": if no specific style claims the backspace action
1229+
// to change its state, only then do we proceed to physically delete the newline and merge paragraphs.
12271230
[ParagraphAttributesUtils handleNewlineBackspaceInRange:range replacementText:text input:self]
12281231
) {
12291232
[self anyTextMayHaveBeenModified];

ios/styles/CodeBlockStyle.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ - (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text {
137137

138138
- (BOOL)styleCondition:(id _Nullable)value :(NSRange)range {
139139
NSParagraphStyle *paragraph = (NSParagraphStyle *)value;
140-
return paragraph != nullptr && paragraph.textLists.count == 1 && [paragraph.textLists.firstObject.markerFormat isEqual:@"codeblock"];
140+
return paragraph != nullptr && paragraph.textLists.count == 1 && [paragraph.textLists.firstObject.markerFormat isEqualToString:@"codeblock"];
141141
}
142142

143143
- (BOOL)detectStyle:(NSRange)range {

ios/styles/H1Style.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
@implementation H1Style
55
+ (StyleType)getStyleType { return H1; }
6-
+ (BOOL)isParagraphStyle { return NO; }
6+
+ (BOOL)isParagraphStyle { return YES; }
77
- (CGFloat)getHeadingFontSize { return [((EnrichedTextInputView *)input)->config h1FontSize]; }
88
- (BOOL)isHeadingBold {
99
return [((EnrichedTextInputView *)input)->config h1Bold];

ios/styles/H2Style.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
@implementation H2Style
55
+ (StyleType)getStyleType { return H2; }
6-
+ (BOOL)isParagraphStyle { return NO; }
6+
+ (BOOL)isParagraphStyle { return YES; }
77
- (CGFloat)getHeadingFontSize { return [((EnrichedTextInputView *)input)->config h2FontSize]; }
88
- (BOOL)isHeadingBold {
99
return [((EnrichedTextInputView *)input)->config h2Bold];

ios/styles/H3Style.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
@implementation H3Style
55
+ (StyleType)getStyleType { return H3; }
6-
+ (BOOL)isParagraphStyle { return NO; }
6+
+ (BOOL)isParagraphStyle { return YES; }
77
- (CGFloat)getHeadingFontSize { return [((EnrichedTextInputView *)input)->config h3FontSize]; }
88
- (BOOL)isHeadingBold {
99
return [((EnrichedTextInputView *)input)->config h3Bold];

ios/styles/LinkStyle.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ - (void)handleAutomaticLinks:(NSString *)word inRange:(NSRange)wordRange {
297297
return;
298298
}
299299

300-
// we don't recognize links among codeblock
300+
// we don't recognize links in codeblocks
301301
if ([codeBlockStyle anyOccurence:wordRange]) {
302302
return;
303303
}

ios/utils/ParagraphAttributesUtils.mm

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,14 @@ + (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text i
3737

3838
// if the backspace removes the whole content of a paragraph (possibly more but has to start where the paragraph starts), we remove the typing attributes
3939
if(range.location == nonNewlineRange.location && range.length >= nonNewlineRange.length) {
40-
// for lists and quotes we want to remove the characters but keep attribtues so that a zero width space appears here
40+
// for lists, quotes and codeblocks we want to remove the characters but keep the attributes so that a zero width space appears here
4141
// so we do the removing manually and reapply attributes
42-
if([ulStyle detectStyle:nonNewlineRange]) {
43-
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:typedInput withSelection:YES];
44-
[ulStyle addAttributes:NSMakeRange(range.location, 0)];
45-
return YES;
46-
}
47-
if([olStyle detectStyle:nonNewlineRange]) {
48-
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:typedInput withSelection:YES];
49-
[olStyle addAttributes:NSMakeRange(range.location, 0)];
50-
return YES;
51-
}
52-
if([bqStyle detectStyle:nonNewlineRange]) {
53-
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:typedInput withSelection:YES];
54-
[bqStyle addAttributes:NSMakeRange(range.location, 0)];
55-
return YES;
56-
}
57-
if([cbStyle detectStyle:nonNewlineRange]) {
58-
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:typedInput withSelection:YES];
59-
[cbStyle addAttributes:NSMakeRange(range.location, 0)];
60-
return YES;
42+
NSArray *handledStyles = @[ulStyle, olStyle, bqStyle, cbStyle];
43+
for(id<BaseStyleProtocol> style in handledStyles) {
44+
if([style detectStyle:nonNewlineRange]) {
45+
[TextInsertionUtils replaceText:text at:range additionalAttributes:nullptr input:typedInput withSelection:YES];
46+
return YES;
47+
}
6148
}
6249

6350
// do the replacement manually
@@ -70,6 +57,23 @@ + (BOOL)handleBackspaceInRange:(NSRange)range replacementText:(NSString *)text i
7057
return NO;
7158
}
7259

60+
/**
61+
* Handles the specific case of backspacing a newline character, which results in merging two paragraphs.
62+
*
63+
* THE PROBLEM:
64+
* When merging a bottom paragraph (Source) into a top paragraph (Destination), the bottom paragraph
65+
* normally brings all its attributes with it. If the top paragraph is a restrictive style (like a CodeBlock),
66+
* and the bottom paragraph contains a conflicting style (like an H1 Header), a standard merge would
67+
* create an invalid state (e.g., a CodeBlock that is also a Header).
68+
*
69+
* THE SOLUTION:
70+
* 1. Identifies the dominant style of the paragraph ABOVE the deleted newline (`leftParagraphStyle`).
71+
* 2. Checks the paragraph BELOW the newline (`rightRange`) for any styles that conflict with or are blocked by the top style.
72+
* 3. Explicitly removes those forbidden styles from the bottom paragraph *before* the merge occurs.
73+
* 4. Performs the merge (deletes the newline).
74+
*
75+
* @return YES if the newline backspace was handled and sanitized; NO otherwise.
76+
*/
7377
+ (BOOL)handleNewlineBackspaceInRange:(NSRange)range replacementText:(NSString *)text input:(id)input {
7478
EnrichedTextInputView *typedInput = (EnrichedTextInputView *)input;
7579
if(typedInput == nullptr) {

ios/utils/ZeroWidthSpaceUtils.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ + (void)removeSpacesIfNeededinInput:(EnrichedTextInputView *)input {
4444
BlockQuoteStyle *bqStyle = input->stylesDict[@([BlockQuoteStyle getStyleType])];
4545
CodeBlockStyle *cbStyle = input->stylesDict[@([CodeBlockStyle getStyleType])];
4646

47-
// zero width spaces with no lists/blockquote styles on them get removed
47+
// zero width spaces with no lists/blockquotes/codeblocks on them get removed
4848
if(![ulStyle detectStyle:characterRange] && ![olStyle detectStyle:characterRange] && ![bqStyle detectStyle:characterRange] && ![cbStyle detectStyle:characterRange]) {
4949
[indexesToBeRemoved addObject:@(characterRange.location)];
5050
}

0 commit comments

Comments
 (0)