Build Status Platforms

文本故事 (TextStory)

TextStory 是一小组类和协议,旨在更轻松地使用 NSTextStorage 和相关系统。

集成

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/ChimeHQ/TextStory")
]

TSYTextStorage

一个最小化的 NSTextStorage 子类,为更强大的委托行为提供钩子。非常适合将逻辑/行为与存储对象分离。因为它被构建为包装另一个 NSTextStorage 实例,所以更易于组合。

遗憾的是,到目前为止,我还没有找到一种用 Swift 构建这个类的方法。如果您有使用 NSTextStorage 和 Swift/ObjC 自动 NSString/String 桥接的经验,我很乐意听取您的意见。

- (void)textStorage:(TSYTextStorage *)textStorage willReplaceCharactersInRange:(NSRange)range withString:(NSString *)string;
- (void)textStorage:(TSYTextStorage *)textStorage didReplaceCharactersInRange:(NSRange)range withString:(NSString *)string;
- (void)textStorageProcessEditingComplete:(TSYTextStorage *)textStorage;

// available for macOS only
- (NSRange)textStorage:(TSYTextStorage *)textStorage doubleClickRangeForLocation:(NSUInteger)location;
- (NSUInteger)textStorage:(TSYTextStorage *)textStorage nextWordIndexFromLocation:(NSUInteger)location direction:(BOOL)forward;

BufferingTextStorage

一个 NSTextStorage 子类,它以低内存和性能开销维护文本更改的历史记录。这个类使文本更改处理与 Cocoa 文本显示系统解耦成为可能,使它们彼此异步。即使更改的处理速度可能很慢,这也非常有助于保持 UI 的快速和响应性。

// interact with the buffered view of text
func bufferedSubstring(from range: NSRange) -> String
var bufferedLength: Int

// commit one change in the queue, allowing precise control over how the buffered view changes
func applyNextChange()

TextStoring

一个简单的协议,用于抽象字符串存储。这对于标准化 NSTextStorage 和您可能用于文本操作的其他对象之间的行为非常有用。特别方便用于测试和将系统与 Apple 类的行为/API 解耦。

为了保持灵活性并与 NSTextStorage 匹配,TextStoring 不是 actor 隔离的。

TextStorageAdapter

不可能使 NSTextView/UITextView 遵循 TextStoring,因为它们是 MainActor 隔离的类。此适配器旨在帮助将视图与 TextStoring 一起使用。

TextStoringMonitor

需要观察和响应 TextStoring 实例中更改的系统的标准接口。

LazyTextStoringMonitor

一个具体的 TextStoringMonitor 类,它实现了对包装的 TextStoringMonitor 的渐进式按需访问。这使得在现有的 TextStoringMonitor 之上添加延迟语义变得容易,这对于处理大型文档非常有帮助。

CompositeTextStoringMonitor

一种将 TextStoringMonitor 实例集合组合在一起并将它们视为单个单元的简便方法。

TextMutationEventRouter

这个类可以接受和路由 TSYTextStorage 委托回调到多个 TextStoringMonitor 实例。这对于扇出这些调用非常方便。

贡献和协作

我很乐意听到您的声音!通过 issue 或 pull request 联系。

我更喜欢协作,如果您有类似的项目,我很乐意找到合作方式。

为了提高可访问性,我更喜欢使用制表符进行缩进。但是,我宁愿您使用您想要的系统并提出 PR,而不是因为空格而犹豫不决。

参与本项目即表示您同意遵守贡献者行为准则