API feature request: Add `center` option to `applyScroll`

Use case or problem

Currently, the applyScroll() method of MarkdownSubView (and hence MarkdownPreviewView etc) accepts only scroll: number parameter (which I assume to be a line number rather than a screen coordinate).

When it is called, the view scrolls so that the specified position comes to the top of the view.

But in many use cases of mine, I want it to come to the center position of the view.

Proposed solution

Add a center: boolean parameter to applyScroll.

Current workaround

Currently, I have to do a little tricky thing to achieve this (link):

// view is a MarkdownView, and position is a Pos

// previewMode.applyScroll(position.start.line) doesn't center-align the target block, 
// so we have to do it manually.
const renderer = (view.previewMode as any).renderer as any;

// find the div corresponding the target block
const div = Array.from(renderer.sizerEl.querySelectorAll<HTMLElement>(':scope > div'))
    .find((div) => renderer.getSectionInfo(div)?.lineStart === position.start.line);
if (!div) return;

div.scrollIntoView({ block: 'center' });

I found this hidden API.

view.previewMode.renderer.applyScroll(lineNumber, {
    highlight: true,
    center: true
1 Like