@@ -86,12 +86,8 @@ export default class ContentWrap extends Component {
8686 change . origin !== 'setValue'
8787 ) ;
8888
89- let now = new Date ( ) ;
90- console . log ( 'post to iframe:' + this . cmCodes . js , now . toLocaleTimeString ( ) + `.${ now . getMilliseconds ( ) } ` )
91-
92- document . getElementById ( 'demo-frame' ) . contentWindow . postMessage ( this . cmCodes . js , window . location . href ) ;
93- now = new Date ( ) ;
94- console . log ( 'posted to iframe:' + this . cmCodes . js , now . toLocaleTimeString ( ) + `.${ now . getMilliseconds ( ) } ` )
89+ const targetWindow = this . detachedWindow || document . getElementById ( 'demo-frame' ) . contentWindow ;
90+ targetWindow . postMessage ( { code : this . cmCodes . js } , '*' ) ;
9591
9692 // this.onCodeChange(editor, change);
9793 }
@@ -120,6 +116,7 @@ export default class ContentWrap extends Component {
120116 } , this . updateDelay ) ;
121117 }
122118
119+ // Called for both detached window and non-detached window
123120 createPreviewFile ( html , css , js ) {
124121 // isNotChrome
125122 const shouldInlineJs =
@@ -138,17 +135,22 @@ export default class ContentWrap extends Component {
138135 trackEvent ( 'fn' , 'hasCode' ) ;
139136 trackEvent . hasTrackedCode = true ;
140137 }
138+ const that = this ;
139+ that . frame . onload = function ( ) {
140+ that . frame . contentWindow . postMessage ( { code : that . cmCodes . js } , '*' )
141+ }
141142
142143 if ( shouldInlineJs ) {
143144 if ( this . detachedWindow ) {
144145 log ( '✉️ Sending message to detached window' ) ;
145146 this . detachedWindow . postMessage ( { contents } , '*' ) ;
147+
146148 } else {
147149 this . frame . src = this . frame . src ;
148150 setTimeout ( ( ) => {
149- this . frame . contentDocument . open ( ) ;
150- this . frame . contentDocument . write ( contents ) ;
151- this . frame . contentDocument . close ( ) ;
151+ that . frame . contentDocument . open ( ) ;
152+ that . frame . contentDocument . write ( contents ) ;
153+ that . frame . contentDocument . close ( ) ;
152154 } , 10 ) ;
153155 }
154156 } else {
@@ -565,10 +567,14 @@ export default class ContentWrap extends Component {
565567 'ZenUML' ,
566568 `width=${ iframeWidth } ,height=${ iframeHeight } ,resizable,scrollbars=yes,status=1`
567569 ) ;
568- // Trigger initial render in detached window
569- setTimeout ( ( ) => {
570- this . setPreviewContent ( true ) ;
571- } , 1500 ) ;
570+ const that = this ;
571+ this . detachedWindow . onload = function ( ) {
572+ that . setPreviewContent ( true ) ;
573+ const frm = that . detachedWindow . document . querySelector ( 'iframe' )
574+ frm . onload = function ( ) {
575+ that . detachedWindow . postMessage ( { code : that . cmCodes . js } , '*' ) ;
576+ }
577+ }
572578
573579 var intervalID = window . setInterval ( checkWindow => {
574580 if ( this . detachedWindow && this . detachedWindow . closed ) {
0 commit comments