Skip to content

Commit

Permalink
Don't anchor scroll position to bottom if the editor isn't scrolled
Browse files Browse the repository at this point in the history
FIX: Fix a bug where adding content to the editor could inappropriately move
the scroll position.

Closes codemirror/dev#1200
  • Loading branch information
marijnh committed Jul 4, 2023
1 parent 5bd7ac0 commit 94ba222
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,7 @@ export function atElementStart(doc: HTMLElement, selection: SelectionRange) {
}
}
}

export function isScrolledToBottom(elt: HTMLElement) {
return elt.scrollTop > Math.max(1, elt.scrollHeight - elt.clientHeight - 4)
}
5 changes: 3 additions & 2 deletions src/editorview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {StyleModule, StyleSpec} from "style-mod"
import {DocView} from "./docview"
import {ContentView} from "./contentview"
import {InputState, focusChangeTransaction, isFocusChange} from "./input"
import {Rect, focusPreventScroll, flattenRect, getRoot, ScrollStrategy, dispatchKey} from "./dom"
import {Rect, focusPreventScroll, flattenRect, getRoot, ScrollStrategy,
isScrolledToBottom, dispatchKey} from "./dom"
import {posAtCoords, moveByChar, moveToLineBoundary, byGroup, moveVertically, skipAtoms} from "./cursor"
import {BlockInfo} from "./heightmap"
import {ViewState} from "./viewstate"
Expand Down Expand Up @@ -384,7 +385,7 @@ export class EditorView {
try {
for (let i = 0;; i++) {
if (scrollAnchorHeight < 0) {
if (scrollTop > sDOM.scrollHeight - sDOM.clientHeight - 4) {
if (isScrolledToBottom(sDOM)) {
scrollAnchorPos = -1
scrollAnchorHeight = this.viewState.heightMap.height
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/viewstate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Text, EditorState, ChangeSet, ChangeDesc, RangeSet, EditorSelection} from "@codemirror/state"
import {Rect} from "./dom"
import {Rect, isScrolledToBottom} from "./dom"
import {HeightMap, HeightOracle, BlockInfo, MeasuredHeights, QueryType, heightRelevantDecoChanges} from "./heightmap"
import {decorations, ViewUpdate, UpdateFlag, ChangedRange, ScrollTarget, nativeSelectionHidden,
contentAttributes} from "./extension"
Expand Down Expand Up @@ -263,7 +263,7 @@ export class ViewState {
this.scrollAnchorHeight = -1
this.scrollTop = view.scrollDOM.scrollTop
}
this.scrolledToBottom = this.scrollTop > view.scrollDOM.scrollHeight - view.scrollDOM.clientHeight - 4
this.scrolledToBottom = isScrolledToBottom(view.scrollDOM)

// Pixel viewport
let pixelViewport = (this.printing ? fullPixelRange : visiblePixelRange)(dom, this.paddingTop)
Expand Down

0 comments on commit 94ba222

Please sign in to comment.