Skip to content

Commit

Permalink
#64 Fixed FontTraitToggleCommand when executed without text selection (
Browse files Browse the repository at this point in the history
…#65)

* #64 Fixed FontTraitToggleCommand when executed without text selection

* Fixed relaaying of attributes when editor is empty
  • Loading branch information
rajdeep committed Sep 17, 2020
1 parent d1a3c85 commit 79ae357
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Proton/Sources/Core/RichTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,14 @@ class RichTextView: AutogrowingTextView {
layoutManager.invalidateDisplay(forCharacterRange: range)
}

func resetTypingAttributes() {
self.typingAttributes = defaultTypingAttributes
}

override func deleteBackward() {
defer {
if contentLength == 0 {
self.typingAttributes = defaultTypingAttributes
resetTypingAttributes()
}
richTextViewDelegate?.richTextView(self, didReceive: .backspace, modifierFlags: [], at: selectedRange)
}
Expand Down
3 changes: 3 additions & 0 deletions Proton/Sources/Core/RichTextViewContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class RichTextViewContext: NSObject, UITextViewDelegate {

resetAttachmentSelection(textView)
guard range.length > 0 else {
if textView.attributedText.length == 0 {
richTextView.resetTypingAttributes()
}
var attributes = richTextView.typingAttributes
let contentType = attributes[.blockContentType] as? EditorContent.Name ?? .unknown
attributes[.blockContentType] = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,12 @@ public class FontTraitToggleCommand: EditorCommand {

public func execute(on editor: EditorView) {
let selectedText = editor.selectedText
if editor.isEmpty || editor.selectedRange == .zero {
if editor.isEmpty || editor.selectedRange == .zero || selectedText.length == 0 {
guard let font = editor.typingAttributes[.font] as? UIFont else { return }
editor.typingAttributes[.font] = font.toggled(trait: trait)
return
}

if selectedText.length == 0 {
guard let font = editor.attributedText.attribute(.font, at: editor.selectedRange.location - 1, effectiveRange: nil) as? UIFont else { return }
editor.typingAttributes[.font] = font.toggled(trait: trait)
return
}

guard let initFont = selectedText.attribute(.font, at: 0, effectiveRange: nil) as? UIFont else {
return
}
Expand Down

0 comments on commit 79ae357

Please sign in to comment.