Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX Fix Alignment setting #131

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@ public extension RichTextAttributeWriter {
_ alignment: RichTextAlignment,
at range: NSRange
) {
let text = richText.string

// Text view has selected text
if range.length > 0 {
return setAlignment(alignment, at: range)
guard range.length == 0 else {
setAlignment(alignment, at: range)
return
}

let text = richText.string

// The cursor is at the beginning of the text
if range.location == 0 {
return setAlignment(alignment, atIndex: 0)
}

// The cursor is immediately after a newline
if let char = text.character(at: range.location - 1), char.isNewLineSeparator {
return setAlignment(alignment, atIndex: range.location)
// We dont want to set attributed string to previous line,
// isNewLineSeparator is unfortunately omitted by API.
Copy link
Collaborator Author

@DominikBucher12 DominikBucher12 Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielsaidi this was main reason the line above were being aligned as well as the current line.

return
}

// The cursor is immediately before a newline
Expand All @@ -55,6 +57,7 @@ public extension RichTextAttributeWriter {
let location = UInt(range.location)
let index = text.findIndexOfCurrentParagraph(from: location)
return setAlignment(alignment, atIndex: index)

}
}

Expand Down Expand Up @@ -85,7 +88,7 @@ private extension RichTextAttributeWriter {
_ alignment: RichTextAlignment,
atIndex index: Int
) {
guard let text = mutableRichText else { return }
guard let text = mutableRichText, !text.string.isEmpty else { return }
let range = NSRange(location: index, length: 1)
let safeRange = safeRange(for: range, isAttributeOperation: true)
var attributes = text.attributes(at: safeRange.location, effectiveRange: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ public extension RichTextViewComponent {
var attributes = richTextAttributes
attributes[.paragraphStyle] = style
typingAttributes = attributes
setRichTextAlignment(alignment, at: selectedRange)
} else {
setRichTextAlignment(alignment, at: selectedRange)
}
// Workaround to update caret position
setSelectedRange(selectedRange)
}
}