Commit 14b5e11a by Daniel Dahan

editor: tweaks to Editor for performance testing

parent 918b3f00
......@@ -101,26 +101,6 @@ public func CharacterAttributeToValue(attribute: CharacterAttribute) -> String {
}
}
extension NSAttributedString {
/**
Retrieves the character attributes that are currently applied.
- Parameter at location: An Int.
- Parameter effectiveRange: An optional NSRangePointer.
- Returns: A Dictionary of CharacterAttribute type keys and Any type values.
*/
open func characterAttributes(at location: Int, effectiveRange: NSRangePointer?) -> [CharacterAttribute: Any] {
var ca = [CharacterAttribute: Any]()
attributes(at: location, effectiveRange: effectiveRange).forEach { (key, value) in
guard let attribute = CharacterAttribute.init(rawValue: key) else {
return
}
ca[attribute] = value
}
return ca
}
}
extension NSMutableAttributedString {
/**
Adds a character attribute to a given range.
......
......@@ -56,10 +56,10 @@ public protocol TextStorageDelegate: NSTextStorageDelegate {
}
open class TextStorage: NSTextStorage {
/// A storage facility for attributed text.
/// A storage facility for attributed text.
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?
/// Initializer.
......@@ -70,13 +70,13 @@ open class TextStorage: NSTextStorage {
/// Initializer.
public override init() {
super.init()
prepareStore()
prepareStorage()
}
}
extension TextStorage {
/// Prepare the store.
fileprivate func prepareStore() {
/// Prepare the storage.
fileprivate func prepareStorage() {
storage = NSMutableAttributedString()
}
}
......@@ -97,6 +97,8 @@ extension TextStorage {
(self.delegate as? TextStorageDelegate)?.textStorage?(textStorage: self, didProcessEditing: self.string, result: result, flags: flags, stop: stop)
}
storage.fixAttributes(in: range)
super.processEditing()
}
......@@ -136,4 +138,27 @@ extension TextStorage {
storage.setAttributes(attrs, range: range)
edited(.editedAttributes, range: range, changeInLength: 0)
}
/**
Adds an individual attribute.
- Parameter _ name: Attribute name.
- Parameter value: An Any type.
- Parameter range: A range of characters that will have their
attributes added.
*/
open override func addAttribute(_ name: String, value: Any, range: NSRange) {
storage.addAttribute(name, value: value, range: range)
edited(.editedAttributes, range: range, changeInLength: 0)
}
/**
Removes an individual attribute.
- Parameter _ name: Attribute name.
- Parameter range: A range of characters that will have their
attributes removed.
*/
open override func removeAttribute(_ name: String, range: NSRange) {
storage.removeAttribute(name, 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