Skip to content
Merged
2 changes: 1 addition & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2763,7 +2763,7 @@ SPEC CHECKSUMS:
FBLazyVector: a293a88992c4c33f0aee184acab0b64a08ff9458
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
glog: 5683914934d5b6e4240e497e0f4a3b42d1854183
hermes-engine: f4c793a1c5689b14d5974f3e19e9cf19e0c1f7f6
hermes-engine: e17ae6b2f75a4e61b67ed99ca68c1f2ed03a0181
RCT-Folly: 59ec0ac1f2f39672a0c6e6cecdd39383b764646f
RCTDeprecation: 2b70c6e3abe00396cefd8913efbf6a2db01a2b36
RCTRequired: f3540eee8094231581d40c5c6d41b0f170237a81
Expand Down
26 changes: 13 additions & 13 deletions ios/EnrichedTextInputView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ @implementation EnrichedTextInputView {
NSMutableSet<NSNumber *> *_activeStyles;
LinkData *_recentlyActiveLinkData;
NSRange _recentlyActiveLinkRange;
NSString *_recentlyEmittedString;
NSString *_recentInputString;
MentionParams *_recentlyActiveMentionParams;
NSRange _recentlyActiveMentionRange;
NSString *_recentlyEmittedHtml;
Expand Down Expand Up @@ -75,7 +75,7 @@ - (void)setDefaults {
_recentlyActiveLinkRange = NSMakeRange(0, 0);
_recentlyActiveMentionRange = NSMakeRange(0, 0);
recentlyChangedRange = NSMakeRange(0, 0);
_recentlyEmittedString = @"";
_recentInputString = @"";
_recentlyEmittedHtml = @"<html>\n<p></p>\n</html>";
_emitHtml = NO;
blockEmitting = NO;
Expand Down Expand Up @@ -1234,7 +1234,7 @@ - (void)manageSelectionBasedChanges {
// mention editing runs if only a selection was done (no text change)
// otherwise we would double-emit with a second call in the
// anyTextMayHaveBeenModified method
if ([_recentlyEmittedString
if ([_recentInputString
isEqualToString:[textView.textStorage.string copy]]) {
[mentionStyleClass manageMentionEditing];
}
Expand All @@ -1243,7 +1243,7 @@ - (void)manageSelectionBasedChanges {
// typing attributes for empty lines selection reset
NSString *currentString = [textView.textStorage.string copy];
if (textView.selectedRange.length == 0 &&
[_recentlyEmittedString isEqualToString:currentString]) {
[_recentInputString isEqualToString:currentString]) {
// no string change means only a selection changed with no character changes
NSRange paragraphRange = [textView.textStorage.string
paragraphRangeForRange:textView.selectedRange];
Expand Down Expand Up @@ -1287,7 +1287,7 @@ - (void)anyTextMayHaveBeenModified {

// emptying input typing attributes management
if (textView.textStorage.string.length == 0 &&
_recentlyEmittedString.length > 0) {
_recentInputString.length > 0) {
// reset typing attribtues
textView.typingAttributes = defaultTypingAttributes;
}
Expand Down Expand Up @@ -1336,7 +1336,7 @@ - (void)anyTextMayHaveBeenModified {
[self setPlaceholderLabelShown:YES];
}

if (![textView.textStorage.string isEqualToString:_recentlyEmittedString]) {
if (![textView.textStorage.string isEqualToString:_recentInputString]) {
// modified words handling
NSArray *modifiedWords =
[WordsUtils getAffectedWordsFromText:textView.textStorage.string
Expand All @@ -1355,16 +1355,16 @@ - (void)anyTextMayHaveBeenModified {
}
}

// emit string without zero width spaces
NSString *stringToBeEmitted = [[textView.textStorage.string
stringByReplacingOccurrencesOfString:@"\u200B"
withString:@""] copy];

// emit onChangeText event
auto emitter = [self getEventEmitter];
if (emitter != nullptr) {
// set the recently emitted string only if the emitter is defined
_recentlyEmittedString = stringToBeEmitted;
// set the recent input string only if the emitter is defined
_recentInputString = [textView.textStorage.string copy];

// emit string without zero width spaces
NSString *stringToBeEmitted = [[textView.textStorage.string
stringByReplacingOccurrencesOfString:@"\u200B"
withString:@""] copy];

emitter->onChangeText({.value = [stringToBeEmitted toCppString]});
}
Expand Down
56 changes: 54 additions & 2 deletions ios/inputParser/InputParser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
[result appendString:@"\n</ul>\n<br>"];
inUnorderedList = NO;
}
} else if (inBlockQuote) {
BlockQuoteStyle *bqStyle = _input->stylesDict[@(BlockQuote)];
BOOL detected =
[bqStyle detectStyle:NSMakeRange(currentRange.location, 0)];
if (detected) {
[result appendString:@"\n<br>"];
} else {
[result appendString:@"\n</blockquote>\n<br>"];
inBlockQuote = NO;
}
} else if (inCodeBlock) {
CodeBlockStyle *cbStyle = _input->stylesDict[@(CodeBlock)];
BOOL detected =
[cbStyle detectStyle:NSMakeRange(currentRange.location, 0)];
if (detected) {
[result appendString:@"\n<br>"];
} else {
[result appendString:@"\n</codeblock>\n<br>"];
inCodeBlock = NO;
}
} else {
[result appendString:@"\n<br>"];
}
Expand Down Expand Up @@ -386,11 +406,17 @@ - (NSString *)parseToHtmlFromRange:(NSRange)range {
[result appendString:@"\n</html>"];

// remove zero width spaces in the very end
NSRange resultRange = NSMakeRange(0, result.length);
[result replaceOccurrencesOfString:@"\u200B"
withString:@""
options:0
range:resultRange];
range:NSMakeRange(0, result.length)];

// replace empty <p></p> into <br> in the very end
[result replaceOccurrencesOfString:@"<p></p>"
withString:@"<br>"
options:0
range:NSMakeRange(0, result.length)];

return result;
}

Expand Down Expand Up @@ -640,6 +666,16 @@ - (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html {
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"</p></li>"
withString:@"</li>"];

// change <br/> to <br>
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<br/>"
withString:@"<br>"];

// remove <p> tags around <br>
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<p><br>"
withString:@"<br>"];
fixedHtml = [fixedHtml stringByReplacingOccurrencesOfString:@"<br></p>"
withString:@"<br>"];

// tags that have to be in separate lines
fixedHtml = [self stringByAddingNewlinesToTag:@"<br>"
inString:fixedHtml
Expand Down Expand Up @@ -721,6 +757,22 @@ - (NSString *_Nullable)initiallyProcessHtml:(NSString *_Nonnull)html {
inString:fixedHtml
leading:NO
trailing:YES];

// this is more like a hack but for some reason the last <br> in
// <blockquote> and <codeblock> are not properly changed into zero width
// space so we do that manually here
fixedHtml = [fixedHtml
stringByReplacingOccurrencesOfString:@"<br>\n</blockquote>"
withString:@"<p>\u200B</p>\n</blockquote>"];
fixedHtml = [fixedHtml
stringByReplacingOccurrencesOfString:@"<br>\n</codeblock>"
withString:@"<p>\u200B</p>\n</codeblock>"];

// replace "<br>" at the end with "<br>\n" if input is not empty to properly
// handle last <br> in html
if ([fixedHtml hasSuffix:@"<br>"] && fixedHtml.length != 4) {
fixedHtml = [fixedHtml stringByAppendingString:@"\n"];
}
}

return fixedHtml;
Expand Down
Loading