|
| 1 | +import type * as React from "react"; |
| 2 | + |
| 3 | +import { aspectRatioFromDim } from "@/shared/ui/markdown/utils"; |
| 4 | +import type { TimelineItem } from "./timelineItems"; |
| 5 | +import type { TimelineMessage } from "../types"; |
| 6 | +import { parseImetaTags } from "./parseImeta"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Estimate a timeline row's rendered height so its `content-visibility` |
| 10 | + * placeholder reserves credible space BEFORE first paint. A flat placeholder |
| 11 | + * makes a never-painted media/code row snap from the floor to its true height |
| 12 | + * as it realizes on scroll-up — the teleport. The browser's `auto` keyword |
| 13 | + * still refines the size once the row paints; this only has to make that first |
| 14 | + * realization land near-correct, not exact. |
| 15 | + * |
| 16 | + * Deliberately conservative and cheap (no DOM, no markdown parse): row chrome |
| 17 | + * + a line-count estimate for text/code + known media `dim`s. Over-reserving a |
| 18 | + * little is harmless (a small downward settle); under-reserving by a lot is the |
| 19 | + * jump we're killing. |
| 20 | + */ |
| 21 | + |
| 22 | +// Visual caps mirror the inline image/markdown styles. |
| 23 | +const MEDIA_MAX_WIDTH = 384; // max-w-[min(24rem,100%)] |
| 24 | +const MEDIA_MAX_HEIGHT = 256; // max-h-64 |
| 25 | +const TEXT_LINE_HEIGHT = 22; |
| 26 | +const CODE_LINE_HEIGHT = 19; |
| 27 | +const CHARS_PER_LINE = 64; // rough wrap width at the timeline column |
| 28 | +const ROW_CHROME = 34; // author/time header + row padding |
| 29 | +const REACTION_ROW = 28; |
| 30 | +const PREVIEW_CARD = 96; |
| 31 | +const MIN_ESTIMATE = 60; // never reserve less than the old flat floor |
| 32 | + |
| 33 | +function mediaHeightFromDim(dim: string | undefined): number { |
| 34 | + const ratio = aspectRatioFromDim(dim); |
| 35 | + if (!ratio || ratio <= 0) return MEDIA_MAX_HEIGHT; // unknown shape: reserve full box |
| 36 | + const widthBoundHeight = MEDIA_MAX_WIDTH / ratio; |
| 37 | + return Math.round(Math.min(MEDIA_MAX_HEIGHT, widthBoundHeight)); |
| 38 | +} |
| 39 | + |
| 40 | +function wrappedLineCount(text: string): number { |
| 41 | + let lines = 0; |
| 42 | + for (const raw of text.split("\n")) { |
| 43 | + lines += Math.max(1, Math.ceil(raw.length / CHARS_PER_LINE)); |
| 44 | + } |
| 45 | + return lines; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Strip fenced code blocks from the body, returning the prose remainder and the |
| 50 | + * total number of code lines (for separate mono line-height accounting). |
| 51 | + */ |
| 52 | +function splitFencedCode(body: string): { prose: string; codeLines: number } { |
| 53 | + const parts = body.split(/```/); |
| 54 | + // Even indices are prose, odd indices are inside a fence. |
| 55 | + let prose = ""; |
| 56 | + let codeLines = 0; |
| 57 | + for (let i = 0; i < parts.length; i += 1) { |
| 58 | + if (i % 2 === 1) { |
| 59 | + codeLines += parts[i].split("\n").length; |
| 60 | + } else { |
| 61 | + prose += parts[i]; |
| 62 | + } |
| 63 | + } |
| 64 | + return { prose, codeLines }; |
| 65 | +} |
| 66 | + |
| 67 | +// Image/video file extensions the markdown renderer turns into inline media. |
| 68 | +const MEDIA_URL_RE = |
| 69 | + /https?:\/\/\S+\.(?:png|jpe?g|gif|webp|avif|svg|mp4|webm|mov)(?:\?\S*)?$/i; |
| 70 | + |
| 71 | +/** |
| 72 | + * URLs in the body that the markdown renderer shows as inline `<img>`/`<video>`: |
| 73 | + * `` markdown images and bare media URLs on their own line. Used to |
| 74 | + * reserve media height for dim-less inline media (no imeta tag). |
| 75 | + */ |
| 76 | +function mediaUrlsInBody(body: string): string[] { |
| 77 | + const urls: string[] = []; |
| 78 | + for (const match of body.matchAll(/!\[[^\]]*\]\(([^)\s]+)\)/g)) { |
| 79 | + urls.push(match[1]); |
| 80 | + } |
| 81 | + for (const line of body.split("\n")) { |
| 82 | + const trimmed = line.trim(); |
| 83 | + if (MEDIA_URL_RE.test(trimmed)) urls.push(trimmed); |
| 84 | + } |
| 85 | + return urls; |
| 86 | +} |
| 87 | + |
| 88 | +export function estimateRowHeight(message: TimelineMessage): number { |
| 89 | + const body = message.body ?? ""; |
| 90 | + const { prose, codeLines } = splitFencedCode(body); |
| 91 | + |
| 92 | + let height = ROW_CHROME; |
| 93 | + height += |
| 94 | + wrappedLineCount(prose.trim() === "" ? "" : prose) * TEXT_LINE_HEIGHT; |
| 95 | + height += codeLines * CODE_LINE_HEIGHT; |
| 96 | + |
| 97 | + const imetaUrls = new Set<string>(); |
| 98 | + if (message.tags && message.tags.length > 0) { |
| 99 | + const imeta = parseImetaTags(message.tags); |
| 100 | + for (const entry of imeta.values()) { |
| 101 | + if (!entry.url) continue; |
| 102 | + imetaUrls.add(entry.url); |
| 103 | + height += mediaHeightFromDim(entry.dim); |
| 104 | + } |
| 105 | + } |
| 106 | + for (const url of mediaUrlsInBody(body)) { |
| 107 | + if (imetaUrls.has(url)) continue; // already counted via its imeta dim |
| 108 | + height += MEDIA_MAX_HEIGHT; // dim-less inline media: reserve the full box |
| 109 | + } |
| 110 | + |
| 111 | + // A bare non-media URL on its own line usually renders a link-preview card. |
| 112 | + const hasPreviewUrlLine = body |
| 113 | + .split("\n") |
| 114 | + .some( |
| 115 | + (line) => |
| 116 | + /^\s*https?:\/\/\S+\s*$/.test(line) && !MEDIA_URL_RE.test(line.trim()), |
| 117 | + ); |
| 118 | + if (hasPreviewUrlLine) height += PREVIEW_CARD; |
| 119 | + |
| 120 | + if (message.reactions && message.reactions.length > 0) height += REACTION_ROW; |
| 121 | + |
| 122 | + return Math.max(MIN_ESTIMATE, Math.round(height)); |
| 123 | +} |
| 124 | + |
| 125 | +// Dividers are short, fixed-height rows; reserving their true height keeps the |
| 126 | +// estimate honest without a content scan. |
| 127 | +const DIVIDER_HEIGHT = 32; |
| 128 | + |
| 129 | +/** |
| 130 | + * `contain-intrinsic-size` for a `timeline-row-cv` wrapper. A credible per-row |
| 131 | + * reserve replaces the flat 60px placeholder so a never-painted row realizes |
| 132 | + * near its true height instead of snapping the scroll position. `auto` keeps |
| 133 | + * refining once the row paints. |
| 134 | + */ |
| 135 | +export function timelineRowReserveStyle( |
| 136 | + item: TimelineItem, |
| 137 | +): React.CSSProperties { |
| 138 | + const height = |
| 139 | + item.kind === "message" || item.kind === "system" |
| 140 | + ? estimateRowHeight(item.entry.message) |
| 141 | + : DIVIDER_HEIGHT; |
| 142 | + return { containIntrinsicSize: `auto ${height}px` }; |
| 143 | +} |
0 commit comments