-
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 5 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 |
---|---|---|
|
@@ -4,20 +4,26 @@ import UIKit | |
open class ParagraphStyle: NSMutableParagraphStyle { | ||
|
||
var textList: TextList? | ||
var blockquote: Blockquote? | ||
|
||
override init() { | ||
textList = nil | ||
blockquote = nil | ||
super.init() | ||
} | ||
|
||
public required init?(coder aDecoder: NSCoder) { | ||
textList = nil | ||
blockquote = nil | ||
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 comment as above |
||
if aDecoder.containsValue(forKey: String(describing: TextList.self)) { | ||
let styleRaw = aDecoder.decodeInteger(forKey: String(describing: TextList.self)) | ||
if let style = TextList.Style(rawValue:styleRaw) { | ||
textList = TextList(style: style) | ||
} | ||
} | ||
if aDecoder.containsValue(forKey: String(describing:Blockquote.self)) { | ||
blockquote = aDecoder.decodeObject(forKey: String(describing:Blockquote.self)) as? Blockquote | ||
} | ||
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 space between |
||
super.init(coder: aDecoder) | ||
} | ||
|
||
|
@@ -26,11 +32,16 @@ open class ParagraphStyle: NSMutableParagraphStyle { | |
if let textListSet = textList { | ||
aCoder.encode(textListSet.style.rawValue, forKey: String(describing: TextList.self)) | ||
} | ||
|
||
if let blockquote = self.blockquote { | ||
aCoder.encode(blockquote, forKey: String(describing:Blockquote.self)) | ||
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 |
||
} | ||
} | ||
|
||
override open func setParagraphStyle(_ obj: NSParagraphStyle) { | ||
if let paragrahStyle = obj as? ParagraphStyle { | ||
textList = paragrahStyle.textList | ||
blockquote = paragrahStyle.blockquote | ||
} | ||
super.setParagraphStyle(obj) | ||
} | ||
|
@@ -69,21 +80,14 @@ open class ParagraphStyle: NSMutableParagraphStyle { | |
return false | ||
} | ||
|
||
if textList == nil || otherParagraph.textList == nil { | ||
return super.isEqual(object) | ||
} | ||
|
||
if textList == nil && otherParagraph.textList != nil { | ||
if textList != otherParagraph.textList { | ||
return false | ||
} | ||
|
||
if textList != nil && otherParagraph.textList == nil { | ||
if blockquote != otherParagraph.blockquote { | ||
return false | ||
} | ||
|
||
if textList! != otherParagraph.textList! { | ||
return false | ||
} | ||
|
||
return super.isEqual(object) | ||
} | ||
|
@@ -97,7 +101,7 @@ open class ParagraphStyle: NSMutableParagraphStyle { | |
let thisResult = ParagraphStyle() | ||
thisResult.setParagraphStyle(result as! NSParagraphStyle) | ||
thisResult.textList = textList | ||
|
||
thisResult.blockquote = blockquote | ||
return thisResult | ||
} | ||
|
||
|
@@ -106,7 +110,7 @@ open class ParagraphStyle: NSMutableParagraphStyle { | |
let thisResult = ParagraphStyle() | ||
thisResult.setParagraphStyle(result as! NSParagraphStyle) | ||
thisResult.textList = textList | ||
|
||
thisResult.blockquote = blockquote | ||
return thisResult | ||
} | ||
|
||
|
@@ -115,6 +119,6 @@ open class ParagraphStyle: NSMutableParagraphStyle { | |
} | ||
|
||
open override var description:String { | ||
return super.description + "\nTextList:\(textList?.style)" | ||
return super.description + "\nTextList:\(textList?.style)\nBlockquote:\(blockquote)" | ||
} | ||
} |
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,59 @@ 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) { | ||
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. Aaand same as above! |
||
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.
Optionals are nil by default. You don't really need these two lines