-
Notifications
You must be signed in to change notification settings - Fork 146
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
Implement blockquotes with paragraph style #168
Changes from all commits
221b420
183362f
4370a08
5b62cb3
71eada6
795149d
d821f46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,6 +108,7 @@ open class TextView: UITextView { | |
super.insertText(text) | ||
insertionRange.length = 1 | ||
refreshListAfterInsertionOf(text: text, range: insertionRange) | ||
refreshBlockquoteAfterInsertionOf(text: text, range: insertionRange) | ||
} | ||
|
||
open override func deleteBackward() { | ||
|
@@ -126,15 +127,9 @@ open class TextView: UITextView { | |
if storage.string.isEmpty { | ||
return | ||
} | ||
if deletedString.string == "\n" || deletionRange.location == 0 { | ||
var isPreviousLocationList = false | ||
if (selectedRange.location > 0) { | ||
isPreviousLocationList = storage.textListAttribute(atIndex: selectedRange.location - 1) != nil | ||
} | ||
if !isPreviousLocationList { | ||
removeList(aroundRange: selectedRange) | ||
} | ||
} | ||
|
||
refreshListAfterDeletionOf(text: deletedString, atRange: deletionRange) | ||
refreshBlockquoteAfterDeletionOf(text: deletedString, atRange: deletionRange) | ||
} | ||
|
||
// MARK: - UIView Overrides | ||
|
@@ -454,6 +449,22 @@ open class TextView: UITextView { | |
} | ||
} | ||
|
||
/// Refresh Lists attributes when text is deleted in the specified range | ||
/// | ||
/// - Parameters: | ||
/// - text: the text being added | ||
/// - range: the range of the insertion of the new text | ||
private func refreshListAfterDeletionOf(text deletedText: NSAttributedString, atRange range:NSRange) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
guard deletedText.textListAttribute(atIndex: 0) != nil, | ||
deletedText.string == "\n" || range.location == 0 else { | ||
return | ||
} | ||
|
||
if (range.location == 0) { | ||
removeList(aroundRange: range) | ||
} | ||
} | ||
|
||
fileprivate func removeList(aroundRange range: NSRange) { | ||
let formatter = TextListFormatter() | ||
formatter.removeList(inString: storage, atRange: range) | ||
|
@@ -495,6 +506,61 @@ open class TextView: UITextView { | |
formatter.toggleAttribute(inTextView: self, atRange: range) | ||
} | ||
|
||
/// Refresh Lists attributes when text is deleted in the specified range | ||
/// | ||
/// - Parameters: | ||
/// - text: the text being added | ||
/// - range: the range of the insertion of the new text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||
/// | ||
private func refreshBlockquoteAfterDeletionOf(text deletedText: NSAttributedString, atRange range:NSRange) { | ||
let formatter = BlockquoteFormatter() | ||
guard formatter.attribute(inTextView: self, at: range.location), | ||
deletedText.string == "\n" || range.location == 0 else { | ||
return | ||
} | ||
|
||
if (range.location == 0) { | ||
formatter.toggleAttribute(inTextView: self, atRange: range) | ||
} | ||
} | ||
|
||
/// Refresh blockquotes attributes when inserting new text in the specified range | ||
/// | ||
/// - Parameters: | ||
/// - text: the text being added | ||
/// - range: the range of the insertion of the new text | ||
/// | ||
private func refreshBlockquoteAfterInsertionOf(text: String, range:NSRange) { | ||
let formatter = BlockquoteFormatter() | ||
guard formatter.attribute(inTextView: self, at: range.location) else { | ||
return | ||
} | ||
|
||
let afterRange = NSRange(location: range.location + 1, length: 1) | ||
let beforeRange = NSRange(location: range.location - 1, length: 1) | ||
|
||
var afterString = "\n" | ||
var beforeString = "\n" | ||
if beforeRange.location >= 0 { | ||
beforeString = storage.attributedSubstring(from: beforeRange).string | ||
} | ||
if afterRange.endLocation < storage.length { | ||
afterString = storage.attributedSubstring(from: afterRange).string | ||
} | ||
|
||
let isBegginingOfListItem = storage.isStartOfNewLine(atLocation: range.location) | ||
|
||
if text == "\n" && beforeString == "\n" && afterString == "\n" && isBegginingOfListItem { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if this could be refactored (anyhow) using |
||
formatter.toggleAttribute(inTextView: self, atRange: range) | ||
if afterRange.endLocation < storage.length { | ||
formatter.toggleAttribute(inTextView: self, atRange: afterRange) | ||
deleteBackward() | ||
} else { | ||
selectedRange = NSRange(location: range.location, length: 0) | ||
} | ||
} | ||
} | ||
|
||
|
||
// MARK: - Links | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing space between
describing:
and the class kind