When is the view's scrollDom actually "ready"?

I want to get the view’s scrollDom and modify its scrollTop value, but if i don’t wait for at least 50ms, the value i m trying to scroll will be less than the one i actually passed to scrollTop(maybe the virtual rows haven’t been loaded yet? just a guess atm). Want to mention that i’m trying to modify the scrollDom.scrollTop, right after newing up the EditorView and settings the initial state.

Is there a way to know when the scrollDom was initialized/rendered, so that i can listen for scroll events, and then reuse its scrollTop value when the editor gets reinitialized?

1 Like

The editor will register an animation frame in which it finishes measuring its dimensions and content. You could use requestMeasure on the newly created editor and put your scrollTop update in the write callback.

not doing anything with the return of read but it still doesn’t work b/c the scrollHeight will be different after the editor loads the hidden contents of the document(when document size is large).

if i scroll the editor to the bottom, go back and change the scrollTop, it will calculate the proper values.

editor.view.requestMeasure({
          read: (view) => {
            const el = view.scrollDOM
            return [el.scrollHeight, el.offsetHeight]
          },

          write: (measure, view) => {
              const targetEl =
                window.document.querySelectorAll('.cm-scroller')[0]

              if (targetEl) {
                targetEl.scrollTop = measure[0] - measure[1]
              }
          },
        })

The final precise height requires DOM measurement, so indeed, that won’t be available until the editor has been scrolled to the bottom. But scrolling to a given position with scrollTo should be reliable regardless.