1- import { applyFilter } from "./filter .js" ;
2- import { $ , cloneTemplate , highlightText } from "./lib .js" ;
3- import { maybeAddTag } from "./tags .js" ;
1+ import { $ } from "./utils/lib .js" ;
2+ import { createLogStore } from "./utils/log-store .js" ;
3+ import { createVirtualScroller , addLogs as addLogsToScroller , rerender } from "./utils/virtual-scroller .js" ;
44
55/** @typedef {import('../types.d.ts').CliInput } CliInput */
66
7- /** @type {CliInput[] } */
8- const logs = [ ] ;
9-
107const logContainer = $ ( ".container" ) ;
118
12- /** @param {CliInput } cliInput */
13- async function getLogEl ( { input, date, id } ) {
14- const logEl = cloneTemplate ( ".log" ) ;
15- const elements = await highlightText ( input ) ;
16- logEl . append ( ...elements ) ;
17- maybeAddTag ( logEl ) ;
18-
19- logEl . setAttribute ( "data-id" , id ) ;
20- logEl . setAttribute (
21- "data-date" ,
22- new Date ( date ) . toLocaleDateString ( "en-US" , {
23- hour : "numeric" ,
24- minute : "numeric" ,
25- second : "numeric" ,
26- } )
27- ) ;
28-
29- applyFilter ( logEl ) ;
30-
31- return logEl ;
32- }
33-
34- /** @param {Element[] } logEls */
35- async function appendLog ( ...logEls ) {
36- const lastElement = logContainer . lastElementChild ;
37-
38- const shouldScrollDown = ( ( ) => {
39- if ( ! lastElement ) return false ;
40- const logBottom = lastElement ?. getBoundingClientRect ( ) . bottom ;
41- const parentBottom = logContainer . getBoundingClientRect ( ) . bottom ;
42-
43- return Math . abs ( parentBottom - logBottom ) < 10 ;
44- } ) ( ) ;
45-
46- logContainer . append ( ...logEls ) ;
47- if ( shouldScrollDown ) {
48- lastElement . scrollIntoView ( ) ;
49- }
50- }
9+ // Create data store and virtual scroller
10+ const store = createLogStore ( ) ;
11+ const scroller = createVirtualScroller ( logContainer , store , {
12+ estimatedHeight : 30 ,
13+ buffer : 5 ,
14+ debounceMs : 16 ,
15+ } ) ;
5116
5217/** @param {CliInput[] } newLogs */
5318export async function addLogs ( newLogs ) {
54- logs . push ( ...newLogs ) ;
55- $ ( ".log-count .total" ) . textContent = `(${ logs . length } )` ;
56-
57- const logEls = await Promise . all ( newLogs . map ( ( log ) => getLogEl ( log ) ) ) ;
58- await appendLog ( ...logEls ) ;
19+ await addLogsToScroller ( scroller , newLogs ) ;
20+ $ ( ".log-count .total" ) . textContent = `(${ store . logs . length } )` ;
5921}
6022
6123export async function reAddAllLogs ( ) {
62- logContainer . innerHTML = '' ;
63- const logEls = await Promise . all ( logs . map ( ( log ) => getLogEl ( log ) ) ) ;
64- await appendLog ( ...logEls ) ;
65- }
24+ await rerender ( scroller ) ;
25+ }
26+
27+ // Export store and scroller for use in other modules
28+ export { store , scroller } ;
0 commit comments