I’m trying to get the line numbers of the first and last visible lines in the editor, based on the current scroll.
I’ve looked at all of these answers so far:
- Get visible lines (with respect to current scroll)
- How do you iterate over visible editor lines? - #2 by marijn
but they all refer to deprecated APIs and don’t seem to have any info that is currently useful.
Accidentally asked this in a github issue and got an answer that left me with more questions than I had before: How to get currently visible lines · Issue #919 · codemirror/dev · GitHub
My code is currently this:
const rect = editor.getBoundingClientRect();
const topVisibleLineBlock = editor.lineBlockAtHeight(rect.top);
const bottomVisibleLineBlock = editor.lineBlockAtHeight(rect.bottom);
// Look up the line blocks in editor.viewState.viewportLines ...
I’m pretty sure that this is the wrong way to do this, because when I scroll around the values returned by the calls to editor.lineBlockAtHeight()
don’t change.
I’m also not sure if editor.viewState.viewportLines is the right place to look up the line blocks to get line #s, or what I should be comparing to do that lookup…