added automatically fitting commits

This commit is contained in:
2026-11-10 17:27:48 +00:00
parent aceee88132
commit c55d9daaa2
2 changed files with 109 additions and 32 deletions

View File

@@ -179,7 +179,12 @@
<Content {items} />
{/await}
</div>
<div class="main-container" style="--container-color:var(--ctp-green);">
<!-- The wrapper contributes no height, so the row is sized by the Content column alone. -->
<div class="relative min-h-0">
<div
class="main-container absolute inset-0 flex flex-col overflow-hidden"
style="--container-color:var(--ctp-green);"
>
<div class="section-title fira-mono-bold mr-3 flex py-2 text-4xl">
Commits
<div class="flex w-full flex-row-reverse items-center gap-4 pr-4">
@@ -188,6 +193,7 @@
</a>
</div>
</div>
<div class="min-h-0 flex-1 overflow-hidden">
{#await data.streamed.latestCommits}
<p>loading commits...</p>
{:then commits}
@@ -196,4 +202,6 @@
{/await}
</div>
</div>
</div>
</div>
</div>

View File

@@ -1,26 +1,93 @@
<script lang="ts">
import { tick } from 'svelte';
import type { GitCommit } from './types';
let { items }: { items: GitCommit[] } = $props();
let containerElement: HTMLDivElement;
let cardElements = $state<HTMLElement[]>([]);
let containerElement = $state<HTMLDivElement>();
let listElement = $state<HTMLDivElement>();
let graphHeight = $state(0);
let nodePositions = $state<{ x: number; y: number }[]>([]);
// Measure the actual centre of every article card.
$effect(() => {
const currentItems = items;
const cards = cardElements.filter(Boolean);
const container = containerElement;
// Only render commits that fit entirely inside the available height.
let visibleCount = $state(0);
let measurementVersion = 0;
if (!container || cards.length !== currentItems.length) return;
const visibleItems = $derived(items.slice(0, visibleCount));
async function fitVisibleItems() {
const version = ++measurementVersion;
const allItems = items;
const list = listElement;
const parent = containerElement?.parentElement;
if (!list || !parent) return;
// Render every commit first so each row's full height can be measured.
visibleCount = allItems.length;
await tick();
if (version !== measurementVersion) return;
const rows = Array.from(list.children) as HTMLElement[];
if (rows.length !== allItems.length) return;
const available = parent.getBoundingClientRect().bottom - list.getBoundingClientRect().top;
let used = 0;
let fit = 0;
for (const row of rows) {
const height = row.getBoundingClientRect().height;
if (used + height > available) break;
used += height;
fit += 1;
}
if (version !== measurementVersion) return;
visibleCount = fit;
}
// Re-measure whenever the items or the available space change.
$effect(() => {
void items;
const parent = containerElement?.parentElement;
if (!parent) return;
void fitVisibleItems();
const observer = new ResizeObserver(() => void fitVisibleItems());
observer.observe(parent);
return () => observer.disconnect();
});
// Measure the actual centre of every visible article card.
$effect(() => {
const currentItems = visibleItems;
const container = containerElement;
const list = listElement;
if (!container || !list) return;
const containerEl: HTMLDivElement = container;
const listEl: HTMLDivElement = list;
function updatePositions() {
const containerRect = container.getBoundingClientRect();
const rows = Array.from(listEl.children) as HTMLElement[];
nodePositions = cards.map((card) => {
if (rows.length !== currentItems.length) return;
const containerRect = containerEl.getBoundingClientRect();
nodePositions = rows.map((row) => {
const card = row.children[1] as HTMLElement;
const rect = card.getBoundingClientRect();
return {
@@ -29,7 +96,7 @@
};
});
graphHeight = container.scrollHeight;
graphHeight = containerEl.scrollHeight;
}
// Initial measurement.
@@ -38,8 +105,8 @@
// Watch for changes in card or container dimensions.
const observer = new ResizeObserver(updatePositions);
observer.observe(container);
cards.forEach((card) => observer.observe(card));
observer.observe(containerEl);
observer.observe(listEl);
// Also handle viewport changes.
window.addEventListener('resize', updatePositions);
@@ -69,8 +136,10 @@
y2={nodePositions[index + 1].y}
stroke="currentColor"
stroke-width="2"
stroke-dasharray={items[index + 1]?.parentRepo != items[index].parentRepo ? '5,4' : '0'}
class={items[index + 1]?.parentRepo == items[index].parentRepo
stroke-dasharray={visibleItems[index + 1]?.parentRepo != visibleItems[index]?.parentRepo
? '5,4'
: '0'}
class={visibleItems[index + 1]?.parentRepo == visibleItems[index]?.parentRepo
? 'text-ctp-green'
: 'text-ctp-surface2'}
stroke-linecap="round"
@@ -92,15 +161,15 @@
{/each}
</svg>
<div class="mt-3 mr-3 grid grid-flow-row auto-rows-min">
{#each items as item, index (item.hash)}
<div bind:this={listElement} class="mt-3 mr-3 grid grid-flow-row auto-rows-min">
{#each visibleItems as item, index (item.hash)}
<div class="relative z-10 grid grid-cols-[1.25rem_minmax(0,1fr)] gap-x-3.5 gap-y-2 pb-2">
<!-- Timeline space -->
<div></div>
<div bind:this={cardElements[index]} class="min-w-0">
<div class="min-w-0">
<a href={item.link} class="group block">
{#if items[index - 1]?.parentRepo != item.parentRepo}
{#if visibleItems[index - 1]?.parentRepo != item.parentRepo}
<p class="flex flex-row pt-2">
<svg
xmlns="http://www.w3.org/2000/svg"