1- import { close , open , text , type Op } from '@bomb.sh/tty' ;
1+ /* oxlint-disable bombshell-dev/max-params -- Layout helpers pass the element and rendering context explicitly. */
2+ import { close , open , rgba , text , type Op } from '@bomb.sh/tty' ;
3+ import { convert } from 'hueful' ;
24import { Element , Text } from './dom.ts' ;
5+ import type { StyleValues } from './style.ts' ;
6+
7+ type TTYContainerOptions = NonNullable < Parameters < typeof open > [ 1 ] > ;
8+ type TTYTextOptions = NonNullable < Parameters < typeof text > [ 1 ] > ;
39
410export class Layout {
511 #lastIdNumber = 0 ;
@@ -11,17 +17,27 @@ export class Layout {
1117 }
1218
1319 /** Serialize an element to TTY Ops. */
14- * layout ( element : Element = this . #root) : IterableIterator < Op > {
15- yield this . #open( element ) ;
20+ * layout ( element : Element = this . #root, inheritedColor ?: number ) : IterableIterator < Op > {
21+ const style = computeStyle ( element ) ;
22+ const color = resolveColorValue ( style . color . value , inheritedColor ) ;
23+ if ( style . display . value === 'inline' ) {
24+ const content = collectInlineText ( element ) ;
25+ if ( content !== '' ) {
26+ yield text ( content , ttyTextOptions ( color ) ) ;
27+ }
28+ return ;
29+ }
1630
17- const stack = [ element . childNodes [ Symbol . iterator ] ( ) ] ;
31+ const stack = [ { children : element . childNodes [ Symbol . iterator ] ( ) , color } ] ;
1832 try {
33+ yield this . #open( element , style , color ) ;
1934 let content = '' ;
2035 while ( stack . length > 0 ) {
21- const next = stack [ stack . length - 1 ] ! . next ( ) ;
36+ const frame = stack [ stack . length - 1 ] ! ;
37+ const next = frame . children . next ( ) ;
2238 if ( next . done ) {
2339 if ( content !== '' ) {
24- yield text ( content ) ;
40+ yield text ( content , ttyTextOptions ( frame . color ) ) ;
2541 content = '' ;
2642 }
2743 stack . pop ( ) ;
@@ -33,11 +49,20 @@ export class Layout {
3349 content += child . data ;
3450 } else if ( child instanceof Element ) {
3551 if ( content !== '' ) {
36- yield text ( content ) ;
52+ yield text ( content , ttyTextOptions ( frame . color ) ) ;
3753 content = '' ;
3854 }
39- yield this . #open( child ) ;
40- stack . push ( child . childNodes [ Symbol . iterator ] ( ) ) ;
55+ const childStyle = computeStyle ( child ) ;
56+ const childColor = resolveColorValue ( childStyle . color . value , frame . color ) ;
57+ if ( childStyle . display . value === 'inline' ) {
58+ const inlineContent = collectInlineText ( child ) ;
59+ if ( inlineContent !== '' ) {
60+ yield text ( inlineContent , ttyTextOptions ( childColor ) ) ;
61+ }
62+ } else {
63+ stack . push ( { children : child . childNodes [ Symbol . iterator ] ( ) , color : childColor } ) ;
64+ yield this . #open( child , childStyle , childColor ) ;
65+ }
4166 }
4267 }
4368 } finally {
@@ -48,12 +73,157 @@ export class Layout {
4873 }
4974 }
5075
51- #open( element : Element ) : Op {
76+ #open( element : Element , style : StyleValues , color ?: number ) : Op {
5277 let stableId = this . #stableIds. get ( element ) ;
5378 if ( ! stableId ) {
5479 stableId = `${ element . nodeName } : ${ this . #lastIdNumber++ } ` ;
5580 this . #stableIds. set ( element , stableId ) ;
5681 }
57- return open ( stableId ) ;
82+ return open ( stableId , ttyContainerOptions ( style , color ) ) ;
83+ }
84+ }
85+
86+ /** Inline elements collect descendant text using the outer element's styles. */
87+ function collectInlineText ( element : Element ) : string {
88+ let content = '' ;
89+ const stack = [ element . childNodes [ Symbol . iterator ] ( ) ] ;
90+ while ( stack . length > 0 ) {
91+ const next = stack [ stack . length - 1 ] ! . next ( ) ;
92+ if ( next . done ) {
93+ stack . pop ( ) ;
94+ } else if ( next . value instanceof Text ) {
95+ content += next . value . data ;
96+ } else if ( next . value instanceof Element ) {
97+ stack . push ( next . value . childNodes [ Symbol . iterator ] ( ) ) ;
98+ }
5899 }
100+ return content ;
101+ }
102+
103+ /**
104+ * Convert a color which is stored internally as a hueful `ParseResult` into
105+ * an RGB numeric value which is what TTY speaks natively.
106+ */
107+ function resolveColorValue (
108+ value : StyleValues [ 'color' ] [ 'value' ] ,
109+ inheritedColor ?: number ,
110+ ) : number | undefined {
111+ if ( value === 'currentColor' ) {
112+ return inheritedColor ;
113+ }
114+ const [ r , g , b , alpha = 1 ] = convert ( value . space , 'rgb' , value . value ) ;
115+ const channel = ( component : number ) : number => Math . round ( Math . max ( 0 , Math . min ( 255 , component ) ) ) ;
116+ return rgba ( channel ( r ) , channel ( g ) , channel ( b ) , channel ( alpha * 255 ) ) ;
117+ }
118+
119+ /**
120+ * Map a layout container element's styles to TTY options. The mapping is
121+ * sparse so that even though we have a completely known computed style,
122+ * we only send through to WASM enough information to render.
123+ */
124+ function ttyContainerOptions ( style : StyleValues , color ?: number ) : TTYContainerOptions {
125+ const props : TTYContainerOptions = { } ;
126+ const layout : NonNullable < typeof props . layout > = { } ;
127+ if ( style . flexDirection . value === 'column' ) {
128+ layout . direction = 'ttb' ;
129+ }
130+ if ( style . gap . value !== 0 ) {
131+ layout . gap = style . gap . value ;
132+ }
133+
134+ let top = style . paddingTop . value ;
135+ let right = style . paddingRight . value ;
136+ let bottom = style . paddingBottom . value ;
137+ let left = style . paddingLeft . value ;
138+ const borderTop = style . borderTopWidth . value ;
139+ const borderRight = style . borderRightWidth . value ;
140+ const borderBottom = style . borderBottomWidth . value ;
141+ const borderLeft = style . borderLeftWidth . value ;
142+ if (
143+ style . borderStyle . value === 'solid' &&
144+ ( borderTop || borderRight || borderBottom || borderLeft )
145+ ) {
146+ const borderColor = resolveColorValue ( style . borderColor . value , color ) ;
147+ // TTY requires an explicit border color. Keep the space when it cannot draw the border.
148+ if ( borderColor !== undefined ) {
149+ props . border = { color : borderColor } ;
150+ if ( borderTop !== 0 ) {
151+ props . border . top = borderTop ;
152+ }
153+ if ( borderRight !== 0 ) {
154+ props . border . right = borderRight ;
155+ }
156+ if ( borderBottom !== 0 ) {
157+ props . border . bottom = borderBottom ;
158+ }
159+ if ( borderLeft !== 0 ) {
160+ props . border . left = borderLeft ;
161+ }
162+ } else {
163+ // TTY cannot draw borders with the terminal-default foreground yet.
164+ // Reserve their space with padding until https://github.com/bombshell-dev/tty/issues/155
165+ // is implemented; then emit the border and remove this fallback.
166+ top += borderTop ;
167+ right += borderRight ;
168+ bottom += borderBottom ;
169+ left += borderLeft ;
170+ }
171+ }
172+
173+ if ( top || right || bottom || left ) {
174+ layout . padding = { } ;
175+ if ( top !== 0 ) {
176+ layout . padding . top = top ;
177+ }
178+ if ( right !== 0 ) {
179+ layout . padding . right = right ;
180+ }
181+ if ( bottom !== 0 ) {
182+ layout . padding . bottom = bottom ;
183+ }
184+ if ( left !== 0 ) {
185+ layout . padding . left = left ;
186+ }
187+ }
188+ // Even an empty layout object makes TTY pack the entire layout block.
189+ if ( Object . keys ( layout ) . length > 0 ) {
190+ props . layout = layout ;
191+ }
192+ return props ;
193+ }
194+
195+ function ttyTextOptions ( color ?: number ) : TTYTextOptions {
196+ return color === undefined ? { } : { color } ;
197+ }
198+
199+ function computeStyle ( element : Element ) : StyleValues {
200+ const defaults = computeDefaultStyles ( element ) ;
201+ return applyInlineStyles ( element , defaults ) ;
202+ }
203+
204+ function computeDefaultStyles ( element : Element ) : StyleValues {
205+ const display =
206+ element . namespaceURI === 'http://www.w3.org/1999/xhtml' && element . localName === 'span'
207+ ? 'inline'
208+ : 'flex' ;
209+ return {
210+ color : { value : 'currentColor' , representation : 'currentColor' } ,
211+ display : { value : display , representation : display } ,
212+ flexDirection : { value : 'row' , representation : 'row' } ,
213+ gap : { value : 0 , representation : '0px' } ,
214+ paddingTop : { value : 0 , representation : '0px' } ,
215+ paddingRight : { value : 0 , representation : '0px' } ,
216+ paddingBottom : { value : 0 , representation : '0px' } ,
217+ paddingLeft : { value : 0 , representation : '0px' } ,
218+ borderStyle : { value : 'none' , representation : 'none' } ,
219+ borderTopWidth : { value : 1 , representation : '1px' } ,
220+ borderRightWidth : { value : 1 , representation : '1px' } ,
221+ borderBottomWidth : { value : 1 , representation : '1px' } ,
222+ borderLeftWidth : { value : 1 , representation : '1px' } ,
223+ borderColor : { value : 'currentColor' , representation : 'currentColor' } ,
224+ } ;
225+ }
226+
227+ function applyInlineStyles ( element : Element , defaults : StyleValues ) : StyleValues {
228+ return { ...defaults , ...element . style . values } ;
59229}
0 commit comments