Skip to content

Commit 7392a6c

Browse files
author
Avaer Kazmer
committed
Add load/save scene messaging
1 parent be49cba commit 7392a6c

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

app.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@
188188
_enter2d();
189189
break;
190190
}
191+
case 'loadScene': {
192+
const {html} = data;
193+
if (html) {
194+
root.setHTML(html);
195+
}
196+
break;
197+
}
191198
}
192199
});
193200

@@ -3622,6 +3629,15 @@
36223629
} else {
36233630
console.log('paste fail');
36243631
}
3632+
} else if (id === 'action-load-scene') {
3633+
window.parentPostMessage({
3634+
method: 'loadScene',
3635+
});
3636+
} else if (id === 'action-save-scene') {
3637+
window.parentPostMessage({
3638+
method: 'saveScene',
3639+
html: root.outerHTML,
3640+
});
36253641
} else if (href) {
36263642
const xrIframe = document.createElement('xr-iframe');
36273643
xrIframe.src = href;

index.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,41 @@ <h3>This beta requires login.</h3>
581581
if (navigator.serviceWorker.controller) { // avoid FOUC during reload
582582
xrScene = document.createElement('xr-scene');
583583
xrScene.src = 'app.html';
584+
xrScene.addEventListener('message', async e => {
585+
const {data} = e;
586+
const {method} = data;
587+
if (method === 'loadScene') {
588+
const res = await fetch('/.s/scene');
589+
if (res.status >= 200 && res.status < 300) {
590+
const html = await res.text();
591+
xrScene.postMessage({
592+
method: 'loadScene',
593+
html,
594+
});
595+
} else if (res.status === 404) {
596+
xrScene.postMessage({
597+
method: 'loadScene',
598+
html: null,
599+
});
600+
} else {
601+
console.warn(`invalid status code: ${res.status}`);
602+
}
603+
} else if (method === 'saveScene') {
604+
const {html} = data;
605+
const res = await fetch('/.s/scene', {
606+
method: 'PUT',
607+
headers: {
608+
'Content-Type': 'text/html',
609+
},
610+
body: html,
611+
});
612+
if (res.status >= 200 && res.status < 300) {
613+
// nothing
614+
} else {
615+
console.warn(`invalid status code: ${res.status}`);
616+
}
617+
}
618+
});
584619
document.body.appendChild(xrScene);
585620

586621
const {email, code} = q;

0 commit comments

Comments
 (0)