Skip to content

Commit 09a5b90

Browse files
committed
Post message when the iFrame is loaded for the first time.
1 parent 1c15322 commit 09a5b90

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/components/ContentWrap.jsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

src/computes.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,12 @@ export function computeJs(
189189
) {
190190
console.log('Is it recomputing?')
191191
let code = `window.addEventListener('message', (e) => {
192-
const now = new Date();
193-
console.log('Received:' + e.data, now.toLocaleTimeString() + '.' + now.getMilliseconds())
194-
195-
app.$store.commit('code', e.data);
196-
// app.$store.state.code = e.data
192+
const code = e.data && e.data.code;
193+
194+
if (!code) {
195+
return;
196+
}
197+
app.$store.commit('code', code);
197198
}, false);`;
198199

199200
var d = deferred();

src/detached-window.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ window.addEventListener('message', e => {
77
frame.contentDocument.write(e.data.contents);
88
frame.contentDocument.close();
99
}, 10);
10+
} else if (e.data && e.data.code) {
11+
const frame = document.querySelector('iframe');
12+
frame.contentWindow.postMessage(e.data, '*')
1013
} else {
1114
document.querySelector('iframe').src = e.data;
1215
}

0 commit comments

Comments
 (0)