import { padStart } from "vdom-utils" ; import { Renderer } from "../renderSync" ; export interface Options { indent : number ; } export default class JsxRenderer implements Renderer < string > { indent : number ; output : string = "" ; constructor ( options = { } ) { const defaultOpts = { indent : 2 } ; Object . assign ( this , defaultOpts , options ) ; } reset ( ) { this . output = "" ; } onProp ( name , value , depth ) { const indent = ( depth + 1 ) * this . indent ; this . output += "\n" + padStart ( name + '="' + value + '"' , indent ) ; } onOpenTag ( name , hasAttributes , isVoid , depth ) { this . output += padStart ( "<" + name , depth * this . indent ) ; if ( ! hasAttributes ) { this . output += isVoid ? " />\n" : "" ; } } onOpenTagClose ( name , hasAttributes , isVoid , hasChildren , depth ) { if ( hasAttributes ) { const closer = isVoid ? "/>\n" : ">" ; this . output += "\n" + padStart ( closer , depth * this . indent ) ; } else if ( ! isVoid ) { this . output += ">" ; } if ( hasChildren ) { this . output += "\n" ; } } onTextNode ( text , depth ) { this . output += padStart ( text + "\n" , depth * this . indent ) ; } onCloseTag ( name , isVoid , depth ) { if ( ! isVoid ) { this . output += padStart ( "</" + name + ">" , depth * this . indent ) + "\n" ; } } onDangerousInnerHTML ( html ) { this . output += html ; } }