Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import { untrack } from 'svelte';import { Virtualizer, elementScroll, observeElementOffset, observeElementRect, type VirtualItem, type VirtualizerOptions,} from '@tanstack/virtual-core';
type Options = Omit< VirtualizerOptions<HTMLElement, HTMLElement>, | 'getScrollElement' | 'scrollToFn' | 'observeElementRect' | 'observeElementOffset' | 'onChange'> & { scroller: HTMLElement | undefined };
/** * A TanStack virtualizer read through runes. `options` is read again * whenever what it reads changes, before the DOM updates, so rows added * above keep the place of the rows drawn before them; `items` and `size` * change with the scroll position and measured heights. */export class VirtualList { readonly virtualizer: Virtualizer<HTMLElement, HTMLElement>; #version = $state(0);
constructor(options: () => Options) { const settings = (): VirtualizerOptions<HTMLElement, HTMLElement> => { const { scroller, ...rest } = options(); return { ...rest, getScrollElement: () => scroller ?? null, scrollToFn: elementScroll, observeElementRect, observeElementOffset, onChange: () => this.#version++, }; }; this.virtualizer = new Virtualizer(untrack(settings)); $effect.pre(() => { this.virtualizer.setOptions(settings()); untrack(() => this.#version++); }); $effect(() => this.virtualizer._didMount()); // After the DOM updates: takes the scroll element and applies the scroll // a change of rows held back for it. $effect(() => { void this.#version; this.virtualizer._willUpdate(); }); }
get items(): VirtualItem[] { void this.#version; return this.virtualizer.getVirtualItems(); }
get size(): number { void this.#version; return this.virtualizer.getTotalSize(); }
/** Attach to each row, which carries `data-index`, to measure it as it draws and resizes. */ measure = (row: HTMLElement) => { this.virtualizer.measureElement(row); };}