editor.getViewport() not working (for me?)


the getViewport method returns for me the from/to of the items in “cache”. And not the currently shown items on screen. I tested this with Chrome and Firefox for latest master branch and v3 branch. From documentation i understood that it should return the lines currently rendered visible to the user or?

How i did test: open one of the “demo” files - i took “activeline.html”. Add a reference to jQuery:

<script src="https://cdn.jsdelivr.net/jquery/1.11.1/jquery.min.js"></script>

Added a event subscription to “scroll” event and added a debug point there.

    editor.on("scroll", function () {
        var vp = editor.getViewport();

The editor shows line 5 to 19 but the result is 0 to 40 (which is the number in editor.display.renderedView).

Am i doing something wrong or am i understanding it wrong? How can i get the correct line number of the currently shown start and end?


The ‘viewport’ concept, in CodeMirror, is exactly that – the lines that have been rendered. You can use the lineAtHeight method to find the precise lines at the top and bottom of the editor DIV.

1 Like

thanks fro the fast answer! So using the following works:

var start = editor.lineAtHeight(editor.display.scroller.getBoundingClientRect().top);