Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
22 changes: 22 additions & 0 deletions Sources/TranscriptKit/iOS/TranscriptText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>?) -> Void)?
private var selectsAll = false

public init(_ document: TranscriptDocument, style: TranscriptStyle = .init()) {
self.source = .document(document)
Expand Down Expand Up @@ -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<Int>?) -> 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?
}
Expand Down Expand Up @@ -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? {
Expand Down
41 changes: 40 additions & 1 deletion Sources/TranscriptKit/iOS/TranscriptTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>?
private var selection: Range<Int>? {
didSet {
let range = selectedRange
guard range != reportedSelection else { return }
reportedSelection = range
onSelectionChange?(range)
}
}
private var reportedSelection: Range<Int>?

/// 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<Int>? {
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<Int>?) -> 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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -571,6 +609,7 @@ public final class TranscriptTextView: UIView, UITextInput, UIGestureRecognizerD
selection = min(current.lowerBound, length)..<min(current.upperBound, length)
}
reportHeightIfChanged()
selectAllIfNeeded()

let moved = showTail(tail)
if tail != nil {
Expand Down