Commit a9eafa58 by Daniel Dahan

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

parent c693872f
......@@ -33,36 +33,26 @@ import UIKit
@objc(EditorDelegate)
public protocol EditorDelegate {
/**
An optional delegation method that is executed when
text will be processed during editing.
- Parameter text: The Text instance assodicated with the
delegation object.
- Parameter textStorage: The TextStorage instance
associated with the delegation object.
- Parameter string: The string value that is currently
being edited.
- Parameter range: The range of characters that are being
edited.
A delegation method that is executed when text will be
processed during editing.
- Parameter editor: An Editor.
- Parameter willProcessEditing textStorage: A TextStorage.
- Parameter text: A String.
- Parameter range: A NSRange.
*/
@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
the edit processing has completed.
- Parameter text: The Text instance assodicated with the
delegation object.
- Parameter textStorage: The TextStorage instance
associated with the delegation object.
- 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.
A delegation method that is executed when text has been
processed after editing.
- Parameter editor: An Editor.
- Parameter didProcessEditing textStorage: A TextStorage.
- Parameter text: A String.
- Parameter range: A NSRange.
*/
@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 {
......@@ -124,6 +114,7 @@ open class Editor: View {
prepareTextContainer()
prepareLayoutManager()
prepareTextStorage()
prepareRegularExpression()
prepareTextView()
}
}
......@@ -161,10 +152,14 @@ extension Editor {
extension Editor: TextStorageDelegate {
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>) {
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 {
open class TextStorage: NSTextStorage {
/// 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.
open var expression: NSRegularExpression?
......@@ -72,34 +72,29 @@ open class TextStorage: NSTextStorage {
super.init()
prepareStore()
}
}
extension TextStorage {
/// Prepare the store.
fileprivate func prepareStore() {
store = NSMutableAttributedString()
storage = NSMutableAttributedString()
}
}
extension TextStorage {
/// A String value of the attirbutedString property.
open override var string: String {
return store.string
return storage.string
}
/// Processes the text when editing.
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)
expression!.enumerateMatches(in: string, options: [], range: range) { [weak self] (result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
guard let s = self else {
return
}
(s.delegate as? TextStorageDelegate)?.textStorage!(textStorage: s, didProcessEditing: s.string, result: result, flags: flags, stop: stop)
expression?.enumerateMatches(in: string, options: [], range: range) { [unowned self] (result: NSTextCheckingResult?, flags: NSRegularExpression.MatchingFlags, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
(self.delegate as? TextStorageDelegate)?.textStorage?(textStorage: self, didProcessEditing: self.string, result: result, flags: flags, stop: stop)
}
super.processEditing()
......@@ -107,9 +102,8 @@ extension TextStorage {
/**
Returns the attributes for the character at a given index.
- Parameter location: The index for which to return attributes.
This value must lie within the bounds of the receiver.
- Parameter range: Upon return, the range over which the
- Parameter location: An Int
- Parameter effectiveRange range: Upon return, the range over which the
attributes and values are the same as those at index. This range
isn’t necessarily the maximum range covered, and its extent is
implementation-dependent. If you need the maximum range, use
......@@ -117,8 +111,8 @@ extension TextStorage {
If you don't need this value, pass NULL.
- Returns: The attributes for the character at index.
*/
open override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String : Any] {
return store.attributes(at: location, effectiveRange: range)
open override func attributes(at location: Int, effectiveRange range: NSRangePointer?) -> [String: Any] {
return storage.attributes(at: location, effectiveRange: range)
}
/**
......@@ -128,8 +122,7 @@ extension TextStorage {
will be replaced with.
*/
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)
}
......@@ -140,8 +133,7 @@ extension TextStorage {
attributes updated.
*/
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)
}
}
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