Commit a9eafa58 by Daniel Dahan

editor: fixed issue where regular expression was not set upon initialization

parent c693872f
...@@ -33,36 +33,26 @@ import UIKit ...@@ -33,36 +33,26 @@ import UIKit
@objc(EditorDelegate) @objc(EditorDelegate)
public protocol EditorDelegate { public protocol EditorDelegate {
/** /**
An optional delegation method that is executed when A delegation method that is executed when text will be
text will be processed during editing. processed during editing.
- Parameter text: The Text instance assodicated with the - Parameter editor: An Editor.
delegation object. - Parameter willProcessEditing textStorage: A TextStorage.
- Parameter textStorage: The TextStorage instance - Parameter text: A String.
associated with the delegation object. - Parameter range: A NSRange.
- Parameter string: The string value that is currently
being edited.
- Parameter range: The range of characters that are being
edited.
*/ */
@objc @objc
optional func editor(editor: Editor, willProcess textStorage: TextStorage, string: String, range: NSRange) optional func editor(editor: Editor, willProcessEditing textStorage: TextStorage, text: String, range: NSRange)
/** /**
An optional delegation method that is executed after A delegation method that is executed when text has been
the edit processing has completed. processed after editing.
- Parameter text: The Text instance assodicated with the - Parameter editor: An Editor.
delegation object. - Parameter didProcessEditing textStorage: A TextStorage.
- Parameter textStorage: The TextStorage instance - Parameter text: A String.
associated with the delegation object. - Parameter range: A NSRange.
- Parameter string: The string value that was edited.
- Parameter result: A NSTextCheckingResult associated
with the processing result.
- Parameter flags: Matching flags.
- Parameter stop: Halts a service which is either
publishing or resolving.
*/ */
@objc @objc
optional func editor(editor: Editor, didProcess textStorage: TextStorage, string: String, result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) optional func editor(editor: Editor, didProcessEditing textStorage: TextStorage, text: String, range: NSRange)
} }
open class Editor: View { open class Editor: View {
...@@ -124,6 +114,7 @@ open class Editor: View { ...@@ -124,6 +114,7 @@ open class Editor: View {
prepareTextContainer() prepareTextContainer()
prepareLayoutManager() prepareLayoutManager()
prepareTextStorage() prepareTextStorage()
prepareRegularExpression()
prepareTextView() prepareTextView()
} }
} }
...@@ -161,10 +152,14 @@ extension Editor { ...@@ -161,10 +152,14 @@ extension Editor {
extension Editor: TextStorageDelegate { extension Editor: TextStorageDelegate {
open func textStorage(textStorage: TextStorage, willProcessEditing text: String, range: NSRange) { open func textStorage(textStorage: TextStorage, willProcessEditing text: String, range: NSRange) {
delegate?.editor?(editor: self, willProcessEditing: textStorage, text: string, range: range)
} }
open func textStorage(textStorage: TextStorage, didProcessEditing text: String, result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) { open func textStorage(textStorage: TextStorage, didProcessEditing text: String, result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) {
guard let range = result?.range else {
return
}
delegate?.editor?(editor: self, didProcessEditing: textStorage, text: string, range: range)
} }
} }
...@@ -57,7 +57,7 @@ public protocol TextStorageDelegate: NSTextStorageDelegate { ...@@ -57,7 +57,7 @@ public protocol TextStorageDelegate: NSTextStorageDelegate {
open class TextStorage: NSTextStorage { open class TextStorage: NSTextStorage {
/// A storage facility for attributed text. /// A storage facility for attributed text.
open fileprivate(set) var store: NSMutableAttributedString! open fileprivate(set) var storage: NSMutableAttributedString!
/// The regular expression to match text fragments against. /// The regular expression to match text fragments against.
open var expression: NSRegularExpression? open var expression: NSRegularExpression?
...@@ -72,34 +72,29 @@ open class TextStorage: NSTextStorage { ...@@ -72,34 +72,29 @@ open class TextStorage: NSTextStorage {
super.init() super.init()
prepareStore() prepareStore()
} }
} }
extension TextStorage { extension TextStorage {
/// Prepare the store. /// Prepare the store.
fileprivate func prepareStore() { fileprivate func prepareStore() {
store = NSMutableAttributedString() storage = NSMutableAttributedString()
} }
} }
extension TextStorage { extension TextStorage {
/// A String value of the attirbutedString property. /// A String value of the attirbutedString property.
open override var string: String { open override var string: String {
return store.string return storage.string
} }
/// Processes the text when editing. /// Processes the text when editing.
open override func processEditing() { open override func processEditing() {
let range: NSRange = (string as NSString).paragraphRange(for: editedRange) let range = (string as NSString).paragraphRange(for: editedRange)
(delegate as? TextStorageDelegate)?.textStorage?(textStorage: self, willProcessEditing: string, range: range) (delegate as? TextStorageDelegate)?.textStorage?(textStorage: self, willProcessEditing: string, range: range)
expression!.enumerateMatches(in: string, options: [], range: range) { [weak self] (result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) -> Void in expression?.enumerateMatches(in: string, options: [], range: range) { [unowned self] (result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
guard let s = self else { (self.delegate as? TextStorageDelegate)?.textStorage?(textStorage: self, didProcessEditing: self.string, result: result, flags: flags, stop: stop)
return
}
(s.delegate as? TextStorageDelegate)?.textStorage!(textStorage: s, didProcessEditing: s.string, result: result, flags: flags, stop: stop)
} }
super.processEditing() super.processEditing()
...@@ -107,9 +102,8 @@ extension TextStorage { ...@@ -107,9 +102,8 @@ extension TextStorage {
/** /**
Returns the attributes for the character at a given index. Returns the attributes for the character at a given index.
- Parameter location: The index for which to return attributes. - Parameter location: An Int
This value must lie within the bounds of the receiver. - Parameter effectiveRange range: Upon return, the range over which the
- Parameter range: Upon return, the range over which the
attributes and values are the same as those at index. This range attributes and values are the same as those at index. This range
isn’t necessarily the maximum range covered, and its extent is isn’t necessarily the maximum range covered, and its extent is
implementation-dependent. If you need the maximum range, use implementation-dependent. If you need the maximum range, use
...@@ -117,8 +111,8 @@ extension TextStorage { ...@@ -117,8 +111,8 @@ extension TextStorage {
If you don't need this value, pass NULL. If you don't need this value, pass NULL.
- Returns: The attributes for the character at index. - Returns: The attributes for the character at index.
*/ */
open override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String : Any] { open override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String: Any] {
return store.attributes(at: location, effectiveRange: range) return storage.attributes(at: location, effectiveRange: range)
} }
/** /**
...@@ -128,8 +122,7 @@ extension TextStorage { ...@@ -128,8 +122,7 @@ extension TextStorage {
will be replaced with. will be replaced with.
*/ */
open override func replaceCharacters(in range: NSRange, with str: String) { open override func replaceCharacters(in range: NSRange, with str: String) {
store.replaceCharacters(in: range, with: str) storage.replaceCharacters(in: range, with: str)
edited(.editedCharacters, range: range, changeInLength: str.utf16.count - range.length) edited(.editedCharacters, range: range, changeInLength: str.utf16.count - range.length)
} }
...@@ -140,8 +133,7 @@ extension TextStorage { ...@@ -140,8 +133,7 @@ extension TextStorage {
attributes updated. attributes updated.
*/ */
open override func setAttributes(_ attrs: [String : Any]?, range: NSRange) { open override func setAttributes(_ attrs: [String : Any]?, range: NSRange) {
store.setAttributes(attrs, range: range) storage.setAttributes(attrs, range: range)
edited(.editedAttributes, range: range, changeInLength: 0) edited(.editedAttributes, range: range, changeInLength: 0)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment