@@ -10,12 +10,6 @@ import {
1010
1111export type { TranscriptChildMetadata , TranscriptChildRole } from '../../utils/transcript-component-metadata' ;
1212
13- interface RenderedChild {
14- readonly child : Component ;
15- readonly metadata : TranscriptChildMetadata ;
16- readonly rows : readonly string [ ] ;
17- }
18-
1913export class TranscriptContainer extends GutterContainer {
2014 private readonly leftGutter : number ;
2115 private readonly rightGutter : number ;
@@ -66,78 +60,58 @@ export class TranscriptContainer extends GutterContainer {
6660 throw new Error ( 'Transcript child was added without metadata' ) ;
6761 }
6862 const inner = Math . max ( 1 , width - this . leftGutter - this . rightGutter ) ;
69- const following = this . children . slice ( index + 1 ) . map ( ( followingChild ) => {
63+ let rows = 0 ;
64+ let previousDurable = isDurable ( metadata . role ) ;
65+ for ( let childIndex = index + 1 ; childIndex < this . children . length ; childIndex += 1 ) {
66+ const followingChild = this . children [ childIndex ] ! ;
7067 const followingMetadata = getTranscriptChildMetadata ( followingChild ) ;
7168 if ( followingMetadata === undefined ) {
7269 throw new Error ( 'Transcript child was added without metadata' ) ;
7370 }
74- return {
75- child : followingChild ,
76- metadata : followingMetadata ,
77- rows : this . normalizeRows ( followingChild . render ( inner ) , followingMetadata ) ,
78- } ;
79- } ) ;
80- const firstVisible = following . find ( ( segment ) => segment . rows . length > 0 ) ;
81- const separator =
82- firstVisible !== undefined &&
83- isDurable ( metadata . role ) &&
84- isDurable ( firstVisible . metadata . role )
85- ? 1
86- : 0 ;
87- return separator + this . rowsForSegments ( following ) . length ;
71+ const followingRows = this . normalizeRows (
72+ followingChild . render ( inner ) ,
73+ followingMetadata ,
74+ ) ;
75+ if ( followingRows . length === 0 ) continue ;
76+ if ( previousDurable && isDurable ( followingMetadata . role ) ) rows += 1 ;
77+ rows += followingRows . length ;
78+ previousDurable = isDurable ( followingMetadata . role ) ;
79+ }
80+ return rows ;
8881 }
8982
9083 override render ( width : number ) : string [ ] {
91- return this . rowsForSegments ( this . renderedChildren ( width ) ) . map ( ( row ) => {
92- return ' ' . repeat ( this . leftGutter ) + row ;
93- } ) ;
94- }
95-
96-
97- private renderedChildren ( width : number ) : RenderedChild [ ] {
9884 const inner = Math . max ( 1 , width - this . leftGutter - this . rightGutter ) ;
99- return this . children . map ( ( child ) => {
85+ const lead = ' ' . repeat ( this . leftGutter ) ;
86+ const rows : string [ ] = [ ] ;
87+ let hasVisible = false ;
88+ let previousDurable = false ;
89+ for ( const child of this . children ) {
10090 const metadata = getTranscriptChildMetadata ( child ) ;
10191 if ( metadata === undefined ) {
10292 throw new Error ( 'Transcript child was added without metadata' ) ;
10393 }
104- return {
105- child,
106- metadata,
107- rows : this . normalizeRows ( child . render ( inner ) , metadata ) ,
108- } ;
109- } ) ;
94+ const childRows = this . normalizeRows ( child . render ( inner ) , metadata ) ;
95+ if ( childRows . length === 0 ) continue ;
96+ if ( hasVisible && previousDurable && isDurable ( metadata . role ) ) rows . push ( lead ) ;
97+ for ( const row of childRows ) rows . push ( lead + row ) ;
98+ hasVisible = true ;
99+ previousDurable = isDurable ( metadata . role ) ;
100+ }
101+ return rows ;
110102 }
111103
112104 private normalizeRows (
113105 rows : readonly string [ ] ,
114106 metadata : TranscriptChildMetadata ,
115107 ) : readonly string [ ] {
116- if ( metadata . edgeBlankPolicy === 'preserve' ) return [ ... rows ] ;
108+ if ( metadata . edgeBlankPolicy === 'preserve' ) return rows ;
117109 let start = 0 ;
118110 let end = rows . length ;
119111 while ( start < end && isPlainBlank ( rows [ start ] ! ) ) start += 1 ;
120112 while ( end > start && isPlainBlank ( rows [ end - 1 ] ! ) ) end -= 1 ;
121113 return rows . slice ( start , end ) ;
122114 }
123-
124- private rowsForSegments ( segments : readonly RenderedChild [ ] ) : string [ ] {
125- const rows : string [ ] = [ ] ;
126- const visibleSegments = segments . filter ( ( segment ) => segment . rows . length > 0 ) ;
127- for ( let index = 0 ; index < visibleSegments . length ; index += 1 ) {
128- const segment = visibleSegments [ index ] ! ;
129- rows . push ( ...segment . rows ) ;
130- const next = visibleSegments [ index + 1 ] ;
131- if (
132- next !== undefined &&
133- isDurable ( segment . metadata . role ) &&
134- isDurable ( next . metadata . role )
135- ) {
136- rows . push ( '' ) ;
137- }
138- }
139- return rows ;
140- }
141115}
142116
143117function isPlainBlank ( value : string ) : boolean {
0 commit comments