@@ -13,7 +13,7 @@ interface CodeSegment {
1313}
1414
1515/** The `support-agent.ts` contents, split into tone-colored typewriter segments. */
16- const CODE_LINES : CodeSegment [ ] [ ] = [
16+ const RAW_CODE_LINES : CodeSegment [ ] [ ] = [
1717 [
1818 { text : 'import' , tone : 'muted' } ,
1919 { text : ' ' } ,
@@ -52,8 +52,24 @@ const CODE_LINES: CodeSegment[][] = [
5252 [ { text : ' })' } ] ,
5353]
5454
55+ const codeLineOccurrences = new Map < string , number > ( )
56+ const CODE_LINES = RAW_CODE_LINES . map ( ( segments ) => {
57+ const text = segments . map ( ( segment ) => segment . text ) . join ( '' )
58+ const occurrence = codeLineOccurrences . get ( text ) ?? 0
59+ codeLineOccurrences . set ( text , occurrence + 1 )
60+ let sourceOffset = 0
61+ return {
62+ id : `${ text } :${ occurrence } ` ,
63+ segments : segments . map ( ( segment ) => {
64+ const id = `${ sourceOffset } :${ segment . text . length } `
65+ sourceOffset += segment . text . length
66+ return { ...segment , id }
67+ } ) ,
68+ }
69+ } )
70+
5571const CODE_LINE_LENGTHS = CODE_LINES . map ( ( line ) =>
56- line . reduce ( ( total , segment ) => total + segment . text . length , 0 )
72+ line . segments . reduce ( ( total , segment ) => total + segment . text . length , 0 )
5773)
5874const CODE_LINE_STARTS = CODE_LINE_LENGTHS . map ( ( _ , index ) =>
5975 CODE_LINE_LENGTHS . slice ( 0 , index ) . reduce ( ( total , length ) => total + length , 0 )
@@ -93,13 +109,13 @@ const SEGMENT_TONE_CLASS = {
93109} as const
94110
95111/** Renders one code line clipped to the number of characters typed so far. */
96- function renderCodeLine ( segments : CodeSegment [ ] , visibleChars : number ) {
112+ function renderCodeLine ( segments : Array < CodeSegment & { id : string } > , visibleChars : number ) {
97113 const rendered = [ ]
98114 let remaining = visibleChars
99115 for ( let index = 0 ; index < segments . length && remaining > 0 ; index ++ ) {
100116 const segment = segments [ index ]
101117 rendered . push (
102- < span key = { index } className = { segment . tone && SEGMENT_TONE_CLASS [ segment . tone ] } >
118+ < span key = { segment . id } className = { segment . tone && SEGMENT_TONE_CLASS [ segment . tone ] } >
103119 { segment . text . slice ( 0 , remaining ) }
104120 </ span >
105121 )
@@ -255,12 +271,12 @@ export function BuildMethodsGraphic() {
255271 < div className = 'min-h-[190px] space-y-2 p-4 font-mono text-caption leading-[1.7]' >
256272 { CODE_LINES . map ( ( line , index ) =>
257273 typedCodeChars > CODE_LINE_STARTS [ index ] ? (
258- < div key = { index } className = 'flex gap-3' >
274+ < div key = { line . id } className = 'flex gap-3' >
259275 < span className = 'w-3 select-none text-right text-[var(--text-muted)]' >
260276 { index + 1 }
261277 </ span >
262278 < code >
263- { renderCodeLine ( line , typedCodeChars - CODE_LINE_STARTS [ index ] ) }
279+ { renderCodeLine ( line . segments , typedCodeChars - CODE_LINE_STARTS [ index ] ) }
264280 { codeTypingActive && index === lastStartedLine && (
265281 < span className = 'ml-px inline-block h-[1.1em] w-px translate-y-[2px] animate-pulse bg-[var(--text-primary)] align-text-bottom' />
266282 ) }
0 commit comments