Keep the view in place while scrolling through tables and images

The view jumped to other text in large documents. A short line with
another height, such as a code fence, became the CodeMirror reference
for the line height. CodeMirror then estimated all heights again, and
its layout loop stopped before it restored the scroll position. Each new
table or image height started this check.

- A mark span keeps lines with another height out of that reading.
- A resize observer records the preview heights. A measure request from
  `toDOM` restarted the layout loop.
- A hidden layer measures each table before its first draw.
- Images keep their space from their natural size, in Write and Read.

The scrollStability smoke test and the imageSizes unit test check this.
Real wheel input on large documents gave no jumps in Write and Read.
Card be7df1de.
This commit is contained in:
Knotty Wyvern
2026-11-04 21:41:20 -07:00
parent cbdaa12c84
commit 2b28707ba0
9 changed files with 370 additions and 16 deletions
+9
View File
@@ -5,6 +5,7 @@ import { attachTooltip } from "../ui/tooltip";
const tooltipCleanups = new WeakMap<HTMLElement, () => void>();
import type { ActionMenuItem } from "../ui/actionMenu";
import { showActionMenu, type MenuHandle } from "../ui/managedActionMenu";
import { imageSize, rememberImageSize } from "../workspace/imageSizes";
export type ImageActions = {
edit?: () => void;
@@ -56,6 +57,8 @@ function zoomImage(original: HTMLImageElement, trigger: HTMLButtonElement) {
viewport.className = "image-lightbox-viewport";
const image = original.cloneNode() as HTMLImageElement;
image.removeAttribute("loading");
image.removeAttribute("width");
image.removeAttribute("height");
image.draggable = false;
let scale = 0;
const status = document.createElement("span");
@@ -91,6 +94,12 @@ export function imageControls(root: HTMLElement, actions: ImageActions = {}, mea
const cleanup: (() => void)[] = [];
for (const image of root.querySelectorAll<HTMLImageElement>("img[data-md-src]")) {
if (image.closest(".md-image-frame")) continue;
// A remote image gives its size when it loads. The next view of this image keeps its space from the start.
const remember = () => rememberImageSize(image.src, image.naturalWidth, image.naturalHeight);
image.addEventListener("load", remember);
cleanup.push(() => image.removeEventListener("load", remember));
const known = !image.getAttribute("width") && imageSize(image.src);
if (known) { image.width = known.width; image.height = known.height; }
const frame = document.createElement("span");
frame.className = "md-image-frame";
frame.contentEditable = "false";