@@ -5,7 +5,7 @@ import _ from 'lodash'
55import { widthParser } from "../../helpers/mjAttribute"
66import { UnknownMJMLElement } from '../../Error'
77
8- const getElementWidth = ( element , siblings ) => {
8+ const getElementWidth = ( { element, siblings, parentWidth } ) => {
99 const { elem } = element . props
1010 let { width } = element . props
1111
@@ -14,14 +14,14 @@ const getElementWidth = (element, siblings) => {
1414 }
1515
1616 if ( width == undefined ) {
17- return 600 / siblings
17+ return parentWidth / siblings
1818 }
1919
2020 const { width : parsedWidth , unit } = widthParser ( width )
2121
2222 switch ( unit ) {
2323 case '%' :
24- return parsedWidth * 6 // * 600 / 100
24+ return parsedWidth * parentWidth / 100
2525
2626 case 'px' :
2727 default :
@@ -39,7 +39,8 @@ function createComponent(ComposedComponent, defaultAttributes) {
3939 this . state = Immutable . fromJS ( {
4040 elem : _ . merge ( {
4141 children : [ ] ,
42- attributes : { }
42+ attributes : { } ,
43+ inheritedAttributes : [ ]
4344 } , defaultAttributes )
4445 } )
4546
@@ -62,49 +63,73 @@ function createComponent(ComposedComponent, defaultAttributes) {
6263 return this . state . getIn ( [ 'elem' , 'tagName' ] ) . substr ( 3 )
6364 }
6465
66+ inheritedAttributes ( ) {
67+ return _ . reduce ( this . state . getIn ( [ 'elem' , 'inheritedAttributes' ] ) . toJS ( ) , ( result , value ) => {
68+ result [ value ] = this . mjAttribute ( value )
69+ return result
70+ } , { } )
71+ }
72+
73+ isInheritedAttributes ( name ) {
74+ return _ . indexOf ( this . state . getIn ( [ 'elem' , 'inheritedAttributes' ] ) . toJS ( ) , name ) != - 1
75+ }
76+
6577 siblingsCount ( ) {
6678 const children = this . state . getIn ( [ 'elem' , 'children' ] )
6779
6880 return this . hasReactChildren ( ) ? React . Children . count ( children ) : this . state . getIn ( [ 'elem' , 'children' ] ) . size
6981 }
7082
83+ getWidth ( ) {
84+ return this . mjAttribute ( 'rawPxWidth' ) || this . mjAttribute ( 'width' )
85+ }
86+
7187 childDefaultProps ( id ) {
7288 return {
7389 id,
7490 key : id ,
7591 color : this . mjAttribute ( 'color' ) ,
76- parentWidth : this . mjAttribute ( 'rawPxWidth' ) ,
92+ parentWidth : this . getWidth ( ) ,
7793 verticalAlign : this . mjAttribute ( 'vertical-align' ) ,
7894 sibling : this . siblingsCount ( )
7995 }
8096 }
8197
8298 renderWrappedOutlookChildren ( ) {
8399 let elements = this . renderChildren ( )
84- const wrappedElements = [ ]
85- const prefix = `mj-${ this . mjElementName ( ) } -outlook`
86100
87101 if ( elements && elements . get ) {
88102 // had to break immutable here :(
89103 elements = elements . toArray ( )
90104 }
91105
92- if ( elements . length == 0 ) {
106+ const wrappedElements = [ ]
107+ const prefix = `mj-${ this . mjElementName ( ) } -outlook`
108+ const parentWidth = this . getWidth ( )
109+ const siblings = elements . length
110+ const elementsWidth = elements . map ( ( element ) => {
111+ if ( this . isInheritedAttributes ( 'width' ) ) {
112+ return parentWidth
113+ }
114+
115+ return getElementWidth ( { element, siblings, parentWidth} )
116+ } )
117+
118+ if ( siblings == 0 ) {
93119 return [ ]
94120 }
95121
96122 elements . forEach ( ( element , n ) => {
97- const width = getElementWidth ( element , elements . length )
98- wrappedElements . push ( React . cloneElement ( element , { rawPxWidth : width } ) )
123+ const width = elementsWidth [ n ]
99124
100- if ( n < elements . length - 1 ) {
101- const nextWidth = getElementWidth ( elements [ n + 1 ] , elements . length )
125+ wrappedElements . push ( React . cloneElement ( element , _ . merge ( { rawPxWidth : width } , this . inheritedAttributes ( ) ) ) )
102126
103- wrappedElements . push ( < div key = { `outlook-${ n } ` } className = { `${ prefix } -line` } data-width = { nextWidth } /> )
127+ if ( n < elements . length - 1 ) {
128+ wrappedElements . push ( < div key = { `outlook-${ n } ` } className = { `${ prefix } -line` } data-width = { elementsWidth [ n + 1 ] } /> )
104129 }
105130 } )
106131
107- const outlookOpenTag = < div key = "outlook-open" className = { `${ prefix } -open` } data-width = { getElementWidth ( elements [ 0 ] , elements . length ) } />
132+ const outlookOpenTag = < div key = "outlook-open" className = { `${ prefix } -open` } data-width = { elementsWidth [ 0 ] } />
108133 const outlookCloseTag = < div key = "outlook-close" className = { `${ prefix } -close` } />
109134
110135 return [ outlookOpenTag ] . concat ( wrappedElements , [ outlookCloseTag ] )
0 commit comments