Skip to content

Commit 888b124

Browse files
author
Isaac
committed
Filter consecutive newlines
1 parent 09ca26b commit 888b124

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

submodules/TelegramUI/Components/Chat/ChatTextInputPanelNode/Sources/ChatTextInputPanelComponent.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public final class ChatTextInputPanelComponent: Component {
182182
let insets: UIEdgeInsets
183183
let maxHeight: CGFloat
184184
let maxLength: Int?
185+
let allowConsecutiveNewlines: Bool
185186
let sendAction: (() -> Void)?
186187
let sendContextAction: ((UIView, ContextGesture) -> Void)?
187188

@@ -206,6 +207,7 @@ public final class ChatTextInputPanelComponent: Component {
206207
insets: UIEdgeInsets,
207208
maxHeight: CGFloat,
208209
maxLength: Int?,
210+
allowConsecutiveNewlines: Bool,
209211
sendAction: (() -> Void)?,
210212
sendContextAction: ((UIView, ContextGesture) -> Void)?
211213
) {
@@ -229,6 +231,7 @@ public final class ChatTextInputPanelComponent: Component {
229231
self.insets = insets
230232
self.maxHeight = maxHeight
231233
self.maxLength = maxLength
234+
self.allowConsecutiveNewlines = allowConsecutiveNewlines
232235
self.sendAction = sendAction
233236
self.sendContextAction = sendContextAction
234237
}
@@ -294,6 +297,9 @@ public final class ChatTextInputPanelComponent: Component {
294297
if lhs.maxLength != rhs.maxLength {
295298
return false
296299
}
300+
if lhs.allowConsecutiveNewlines != rhs.allowConsecutiveNewlines {
301+
return false
302+
}
297303
if (lhs.sendAction == nil) != (rhs.sendAction == nil) {
298304
return false
299305
}
@@ -1013,6 +1019,8 @@ public final class ChatTextInputPanelComponent: Component {
10131019
}
10141020
}
10151021

1022+
panelNode.allowConsecutiveNewlines = component.allowConsecutiveNewlines
1023+
10161024
if let resetInputState = component.externalState.resetInputState {
10171025
component.externalState.resetInputState = nil
10181026
let _ = resetInputState

submodules/TelegramUI/Components/Chat/ChatTextInputPanelNode/Sources/ChatTextInputPanelNode.swift

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ public class ChatTextInputPanelNode: ChatInputPanelNode, ASEditableTextNodeDeleg
407407
public var customSendIsDisabled: Bool = false
408408
public var customInputTextMaxLength: Int?
409409
public var customSwitchToKeyboard: (() -> Void)?
410+
public var allowConsecutiveNewlines = true
410411

411412
private var starReactionButton: ComponentView<Empty>?
412413
private var liveMicrophoneButton: ComponentView<Empty>?
@@ -4952,24 +4953,41 @@ public class ChatTextInputPanelNode: ChatInputPanelNode, ASEditableTextNodeDeleg
49524953
}
49534954
}
49544955
}
4955-
4956+
4957+
let string = NSMutableAttributedString(attributedString: editableTextNode.attributedText ?? NSAttributedString())
4958+
var textColor: UIColor = .black
4959+
var accentTextColor: UIColor = .blue
4960+
var baseFontSize: CGFloat = 17.0
4961+
if let presentationInterfaceState = self.presentationInterfaceState {
4962+
textColor = presentationInterfaceState.theme.chat.inputPanel.inputTextColor
4963+
accentTextColor = presentationInterfaceState.theme.chat.inputPanel.panelControlAccentColor
4964+
baseFontSize = max(minInputFontSize, presentationInterfaceState.fontSize.baseDisplaySize)
4965+
if "".isEmpty {
4966+
baseFontSize = 17.0
4967+
}
4968+
}
4969+
let cleanReplacementString = textAttributedStringForStateText(context: context, stateText: NSAttributedString(string: cleanText), fontSize: baseFontSize, textColor: textColor, accentTextColor: accentTextColor, writingDirection: nil, spoilersRevealed: self.spoilersRevealed, availableEmojis: (self.context?.animatedEmojiStickersValue.keys).flatMap(Set.init) ?? Set(), emojiViewProvider: self.emojiViewProvider, makeCollapsedQuoteAttachment: { text, attributes in
4970+
return ChatInputTextCollapsedQuoteAttachmentImpl(text: text, attributes: attributes)
4971+
})
4972+
string.replaceCharacters(in: range, with: cleanReplacementString)
4973+
4974+
var resetText = false
49564975
if cleanText != text {
4957-
let string = NSMutableAttributedString(attributedString: editableTextNode.attributedText ?? NSAttributedString())
4958-
var textColor: UIColor = .black
4959-
var accentTextColor: UIColor = .blue
4960-
var baseFontSize: CGFloat = 17.0
4961-
if let presentationInterfaceState = self.presentationInterfaceState {
4962-
textColor = presentationInterfaceState.theme.chat.inputPanel.inputTextColor
4963-
accentTextColor = presentationInterfaceState.theme.chat.inputPanel.panelControlAccentColor
4964-
baseFontSize = max(minInputFontSize, presentationInterfaceState.fontSize.baseDisplaySize)
4965-
if "".isEmpty {
4966-
baseFontSize = 17.0
4976+
resetText = true
4977+
}
4978+
4979+
if !self.allowConsecutiveNewlines {
4980+
while string.string.range(of: "\n\n") != nil {
4981+
if let range = string.string.range(of: "\n\n") {
4982+
let rawRange = NSRange(range, in: string.string)
4983+
let firstNewline = string.attributedSubstring(from: NSRange(location: rawRange.location, length: 1))
4984+
string.replaceCharacters(in: rawRange, with: firstNewline)
4985+
resetText = true
49674986
}
49684987
}
4969-
let cleanReplacementString = textAttributedStringForStateText(context: context, stateText: NSAttributedString(string: cleanText), fontSize: baseFontSize, textColor: textColor, accentTextColor: accentTextColor, writingDirection: nil, spoilersRevealed: self.spoilersRevealed, availableEmojis: (self.context?.animatedEmojiStickersValue.keys).flatMap(Set.init) ?? Set(), emojiViewProvider: self.emojiViewProvider, makeCollapsedQuoteAttachment: { text, attributes in
4970-
return ChatInputTextCollapsedQuoteAttachmentImpl(text: text, attributes: attributes)
4971-
})
4972-
string.replaceCharacters(in: range, with: cleanReplacementString)
4988+
}
4989+
4990+
if resetText {
49734991
self.textInputNode?.attributedText = string
49744992
self.textInputNode?.selectedRange = NSMakeRange(range.lowerBound + cleanReplacementString.length, 0)
49754993
self.updateTextNodeText(animated: true)

submodules/TelegramUI/Components/MessageInputPanelComponent/Sources/MessageInputPanelComponent.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,7 @@ public final class MessageInputPanelComponent: Component {
11251125
insets: UIEdgeInsets(top: 0.0, left: 0.0, bottom: component.bottomInset, right: 0.0),
11261126
maxHeight: availableSize.height,
11271127
maxLength: component.maxLength,
1128+
allowConsecutiveNewlines: false,
11281129
sendAction: { [weak self] in
11291130
guard let self, let component = self.component else {
11301131
return

0 commit comments

Comments
 (0)