Skip to content

Commit 892839e

Browse files
committed
Fix scrollSelectionToVisible, Scroll to Undone Text
1 parent 7bacce8 commit 892839e

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

Sources/CodeEditTextView/TextView/TextView+ScrollToVisible.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@ extension TextView {
1414

1515
/// Scrolls the upmost selection to the visible rect if `scrollView` is not `nil`.
1616
public func scrollSelectionToVisible() {
17-
guard let scrollView, let selection = getSelection() else {
17+
guard let scrollView else {
1818
return
1919
}
2020

21-
let offsetToScrollTo = offsetNotPivot(selection)
22-
2321
// There's a bit of a chicken-and-the-egg issue going on here. We need to know the rect to scroll to, but we
2422
// can't know the exact rect to make visible without laying out the text. Then, once text is laid out the
2523
// selection rect may be different again. To solve this, we loop until the frame doesn't change after a layout
2624
// pass and scroll to that rect.
2725

2826
var lastFrame: CGRect = .zero
29-
while let boundingRect = layoutManager.rectForOffset(offsetToScrollTo), lastFrame != boundingRect {
27+
while let boundingRect = getSelection()?.boundingRect, lastFrame != boundingRect {
3028
lastFrame = boundingRect
3129
layoutManager.layoutLines()
3230
selectionManager.updateSelectionViews()
3331
selectionManager.drawSelections(in: visibleRect)
3432
}
3533
if lastFrame != .zero {
3634
scrollView.contentView.scrollToVisible(lastFrame)
35+
scrollView.reflectScrolledClipView(scrollView.contentView)
3736
}
3837
}
3938

Sources/CodeEditTextView/Utils/CEUndoManager.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class CEUndoManager: UndoManager {
8585
textView.textStorage.endEditing()
8686

8787
updateSelectionsForMutations(mutations: item.mutations.map { $0.mutation })
88+
textView.scrollSelectionToVisible()
8889

8990
NotificationCenter.default.post(name: .NSUndoManagerDidUndoChange, object: self)
9091
redoStack.append(item)
@@ -112,6 +113,7 @@ public class CEUndoManager: UndoManager {
112113
textView.textStorage.endEditing()
113114

114115
updateSelectionsForMutations(mutations: item.mutations.map { $0.inverse })
116+
textView.scrollSelectionToVisible()
115117

116118
NotificationCenter.default.post(name: .NSUndoManagerDidRedoChange, object: self)
117119
undoStack.append(item)

0 commit comments

Comments
 (0)