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
+18 -1
View File
@@ -26,7 +26,7 @@ await runSmoke("links/sectionLinks", async ({ page }) => {
const url = new URL(smokeUrl).href;
const filler = Array.from({ length: 50 }, (_, i) => `Paragraph ${i}. Some text before the destination.\n`).join('\n');
const source = '# Install and start\n\nChoose [Windows setup](#windows-installer), [macOS setup](#macos-installer-apple-silicon), or [Linux setup](#linux-intelamd-64-bit).\n\n[Other document](other.md#caf%C3%A9-setup-1)\n\n[Missing section](#no-such-section)\n\n' + filler + '\n## Windows installer\n\nWindows instructions.\n\n' + filler + '\n## macOS installer (Apple Silicon)\n\nMac instructions.\n\n' + filler + '\n## Linux (Intel/AMD 64-bit)\n\nLinux instructions.\n';
const other = '# Other\n\n## Café setup\n\nFirst.\n\n' + filler + '\n## Café setup\n\nSecond.\n';
const other = '# Other\n\n## Café setup\n\nFirst.\n\n' + filler + '\n## Café setup\n\nSecond.\n\n' + filler;
async function writerJump(label, line) {
const link = page.locator('.cm-content .writer-link').filter({ hasText: label }).first();
await link.click({ modifiers: ['Control'] });
@@ -57,6 +57,16 @@ await runSmoke("links/sectionLinks", async ({ page }) => {
return !heading.hidden && rect.top >= box.top && rect.bottom <= box.bottom;
}, text);
}
// A heading link puts the heading at the top of the view, below a gap of about 24 pixels.
// The last heading of a short document cannot reach the top, so the checks use headings with text below them.
async function headingAtTop(selector, text, scroller) {
await page.waitForFunction(({ selector, text, scroller }) => [...document.querySelectorAll(selector)].some(node => {
const offset = node.textContent.includes(text) ? node.getBoundingClientRect().top - node.closest(scroller).getBoundingClientRect().top : -1;
return offset >= 0 && offset <= 60;
}), { selector, text, scroller });
}
const writerHeadingAtTop = text => headingAtTop('.cm-line.writer-heading', text, '.cm-scroller');
const readerHeadingAtTop = text => headingAtTop('.reading h2', text, '.md-page');
for (const theme of ['dark', 'light']) for (const width of [1440, 620]) {
await page.setViewportSize({ width, height:1000 });
await page.goto(url);
@@ -72,10 +82,12 @@ await runSmoke("links/sectionLinks", async ({ page }) => {
await page.getByRole('button', { name:'Hide outline', exact:true }).click();
}
await writerJump('Windows setup', await jumpLine('## Windows installer'));
await writerHeadingAtTop('Windows installer');
await top();
await writerJump('Windows setup', await jumpLine('## Windows installer'));
await top();
await writerJump('macOS setup', await jumpLine('## macOS installer'));
await writerHeadingAtTop('macOS installer (Apple Silicon)');
await top();
await writerJump('Linux setup', await jumpLine('## Linux'));
await page.screenshot({ path:`${output}/writer-${theme}-${width}.png` });
@@ -91,14 +103,19 @@ await runSmoke("links/sectionLinks", async ({ page }) => {
const view = node ? EditorView.findFromDOM(node) : null;
return view && view.state.doc.lineAt(view.state.selection.main.head).text === '## Café setup' && view.state.selection.main.head > 100;
});
// The card case: a link to a heading in another file.
await writerHeadingAtTop('Café setup');
await page.screenshot({ path:`${output}/other-file-${theme}-${width}.png` });
assert.equal(await page.getByRole('tab', { name:'a.md', exact:true }).count(), 1);
await page.getByRole('tab', { name:'a.md', exact:true }).click();
await top();
await chooseView(page, 'Read');
await readerJump('Windows setup', 'Windows installer');
await readerHeadingAtTop('Windows installer');
await page.screenshot({ path:`${output}/reading-${theme}-${width}.png` });
await page.locator('.md-page').evaluate(node => { node.scrollTop = 0; });
await readerJump('macOS setup', 'macOS installer (Apple Silicon)');
await readerHeadingAtTop('macOS installer (Apple Silicon)');
await page.locator('.md-page').evaluate(node => { node.scrollTop = 0; });
await readerJump('Linux setup', 'Linux (Intel/AMD 64-bit)');
await chooseView(page, 'Split');