diff --git a/README.md b/README.md index 81df825..c9a371f 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,22 @@ In UIKit, set `isSelectable` on `TranscriptTextView`. `TranscriptStyle` sets the body size, line, block and list spacing, indents, padding and the maximum image height. +### Reading the selection + +`onSelectionChange` tells the app when the selection changes. The range is in the document's flat UTF-16 offset +space, and it is nil when nothing is selected any more. `selectsAllWhenShown()` selects all of the text, with the +selection handles shown, when the view comes on screen: + +```swift +TranscriptText(document) + .selectsAllWhenShown() + .onSelectionChange { range in + if range == nil { dismiss() } // the person tapped the selection away + } +``` + +In UIKit, use `onSelectionChange`, `selectedRange` and `selectsAllWhenShown` on `TranscriptTextView`. + ## In this repository | Folder | Contents | diff --git a/Sources/TranscriptKit/iOS/TranscriptText.swift b/Sources/TranscriptKit/iOS/TranscriptText.swift index 494e049..79ce070 100644 --- a/Sources/TranscriptKit/iOS/TranscriptText.swift +++ b/Sources/TranscriptKit/iOS/TranscriptText.swift @@ -23,6 +23,8 @@ public struct TranscriptText: UIViewRepresentable { private var animationTiming: TranscriptAnimationTiming = .immediately private var isSelectable = true private var playTrigger: AnyHashable? + private var selectionChange: ((Range?) -> Void)? + private var selectsAll = false public init(_ document: TranscriptDocument, style: TranscriptStyle = .init()) { self.source = .document(document) @@ -83,6 +85,24 @@ public struct TranscriptText: UIViewRepresentable { return copy } + /// Called after the selection changes, with the selected range of the + /// document's flat UTF-16 offset space, or nil when nothing is selected + /// any more. `document.text(in:)` gives the selected text. See + /// `TranscriptTextView.onSelectionChange`. + public func onSelectionChange(_ action: @escaping (Range?) -> Void) -> TranscriptText { + var copy = self + copy.selectionChange = action + return copy + } + + /// Selects all of the text, with the selection handles shown, when the + /// view comes on screen; see `TranscriptTextView.selectsAllWhenShown`. + public func selectsAllWhenShown(_ selects: Bool = true) -> TranscriptText { + var copy = self + copy.selectsAll = selects + return copy + } + public final class Coordinator { var playTrigger: AnyHashable? } @@ -117,6 +137,8 @@ public struct TranscriptText: UIViewRepresentable { // with the `.environment(\.openURL, ...)` modifier. let openURL = context.environment.openURL view.openLink = { openURL($0) } + view.onSelectionChange = selectionChange + view.selectsAllWhenShown = selectsAll } public func sizeThatFits(_ proposal: ProposedViewSize, uiView: TranscriptTextView, context: Context) -> CGSize? { diff --git a/Sources/TranscriptKit/iOS/TranscriptTextView.swift b/Sources/TranscriptKit/iOS/TranscriptTextView.swift index 688dc0c..2b9b5a7 100644 --- a/Sources/TranscriptKit/iOS/TranscriptTextView.swift +++ b/Sources/TranscriptKit/iOS/TranscriptTextView.swift @@ -174,7 +174,44 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD /// The document on screen, which can be a little behind `document`. private var shownDocument: TranscriptDocument { currentLayout.document } - private var selection: Range? + private var selection: Range? { + didSet { + let range = selectedRange + guard range != reportedSelection else { return } + reportedSelection = range + onSelectionChange?(range) + } + } + private var reportedSelection: Range? + + /// The selected range of the document's flat UTF-16 offset space, or nil + /// when nothing is selected. `document.text(in:)` gives its text. + public var selectedRange: Range? { + guard let selection, !selection.isEmpty else { return nil } + return selection + } + + /// Called on the main thread after the selection changes, with the new + /// `selectedRange`: nil when nothing is selected any more. + public var onSelectionChange: ((Range?) -> Void)? + + /// Selects all of the text, with the selection handles shown, once the + /// view is in a window and has text. Turn it off and on to select again. + public var selectsAllWhenShown = false { + didSet { + guard selectsAllWhenShown != oldValue else { return } + didSelectAllWhenShown = false + selectAllIfNeeded() + } + } + private var didSelectAllWhenShown = false + + private func selectAllIfNeeded() { + guard selectsAllWhenShown, !didSelectAllWhenShown, window != nil, currentLayout.length > 0 else { return } + didSelectAllWhenShown = true + _ = becomeFirstResponder() + selectAll(nil) + } private let textInteraction = UITextInteraction(for: .nonEditable) private let emptyInputView = UIView() /// Holds the layers of tags' animations, above the tail layer. @@ -308,6 +345,7 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD } } updateCanvas() + selectAllIfNeeded() } /// The part of this view currently on screen, or all of it outside a scroll view. @@ -571,6 +609,7 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD selection = min(current.lowerBound, length)..