-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42210a0
commit 5c752f3
Showing
8 changed files
with
129 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Foundation | ||
|
||
#if os(macOS) && !targetEnvironment(macCatalyst) | ||
import AppKit | ||
|
||
typealias TextStorageEditActions = NSTextStorageEditActions | ||
#elseif os(iOS) || os(visionOS) | ||
import UIKit | ||
|
||
typealias TextStorageEditActions = NSTextStorage.EditActions | ||
#endif | ||
|
||
import RangeState | ||
|
||
#if os(macOS) || os(iOS) || os(visionOS) | ||
final class TextStorageDelegate: NSObject { | ||
typealias ChangeHandler = (NSRange, Int) -> Void | ||
|
||
public var storage: NSTextStorage? { | ||
didSet { | ||
oldValue?.delegate = nil | ||
storage?.delegate = self | ||
} | ||
} | ||
public var willChangeContent: ChangeHandler = { _, _ in } | ||
public var didChangeContent: ChangeHandler = { _, _ in } | ||
} | ||
|
||
extension TextStorageDelegate: NSTextStorageDelegate { | ||
public func textStorage( | ||
_ textStorage: NSTextStorage, | ||
willProcessEditing editedMask: TextStorageEditActions, | ||
range editedRange: NSRange, | ||
changeInLength delta: Int | ||
) { | ||
guard editedMask.contains(.editedCharacters) else { return } | ||
|
||
willChangeContent(editedRange, delta) | ||
} | ||
|
||
public func textStorage( | ||
_ textStorage: NSTextStorage, | ||
didProcessEditing editedMask: TextStorageEditActions, | ||
range editedRange: NSRange, | ||
changeInLength delta: Int | ||
) { | ||
// it's important to filter these, as attribute changes can be caused by styling. This will result in an infinite loop. | ||
guard editedMask.contains(.editedCharacters) else { return } | ||
|
||
didChangeContent(editedRange, delta) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters