Put a linked heading at the top of the view

A heading link showed its heading in the center of the view, also for a
heading in another file. The text to read then started low on the
screen. An outline jump used the same scroll.

- Write and Source put a heading target at the top, with a gap of 24
  pixels. Other targets, such as a search match or a backlink, stay in
  the center.
- Read puts the heading at the top. The scroll margin of the heading
  keeps the gap.

The sectionLinks smoke test checks the position in Write and Read, also
for a heading in another file. Card bf263f80.
This commit is contained in:
Knotty Wyvern
2026-11-03 00:23:17 -07:00
parent 0ab7fe0c64
commit 18031e34e4
4 changed files with 27 additions and 5 deletions
+6 -2
View File
@@ -13,6 +13,7 @@
import type { Chrome, OpenBuffer } from "$lib/workspace/types";
import { workspaceReadDataUrl, workspaceReveal } from "$lib/workspace/api";
import { linkCandidates } from "$lib/workspace/markdownLinks";
import { documentHeadings } from "$lib/workspace/headingLinks";
import { editImageSource, imageSource } from "./imageSource";
import { markdownImages, documentBodyStart } from "$lib/workspace/noteHtml";
import { isMarkdownPath, isRemoteImage, resolveImageRel, imageFiles, hasFileDrag, imageInsertion, markdownTitle, pastedImageSource } from "$lib/workspace/media";
@@ -434,8 +435,11 @@
const view = editor;
if (!view || line == null) return;
untrack(() => {
const anchor = view.state.doc.line(Math.min(view.state.doc.lines, Math.max(1, line))).from;
const effects = [EditorView.scrollIntoView(anchor, { y: "center" })];
const number = Math.min(view.state.doc.lines, Math.max(1, line));
const anchor = view.state.doc.line(number).from;
// A heading starts the text to read, so it goes to the top, as in Read. Other lines, such as a search match, stay in the center.
const heading = markdownDocument && documentHeadings(view.state.doc.toString()).some(entry => entry.line === number);
const effects = [EditorView.scrollIntoView(anchor, heading ? { y: "start", yMargin: 24 } : { y: "center" })];
foldedRanges(view.state).between(anchor, anchor, (from, to) => { effects.push(unfoldEffect.of({ from, to })); });
view.dispatch({ selection: { anchor }, effects });
view.focus();
+2 -1
View File
@@ -102,7 +102,8 @@
[...el.querySelectorAll<HTMLElement>("h1, h2, h3, h4, h5, h6")].filter(heading => !heading.classList.contains("document-title"))[index];
if (!target) return;
currentHeadings?.reveal(target);
target.scrollIntoView({ block: "center" });
// The heading starts the text to read, so it goes to the top. Its scroll margin keeps a gap above it (reading.css).
target.scrollIntoView({ block: "start" });
el.focus({ preventScroll: true });
});
return renderDiagrams(el, () => cancelled);