Skip to content

Commit 6264e69

Browse files
author
Avaer Kazmer
committed
Add xr-site custom element
1 parent 3d411bb commit 6264e69

File tree

1 file changed

+65
-16
lines changed

1 file changed

+65
-16
lines changed

app.html

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<script src="file-type.js"></script>
2121
</head>
2222
<body>
23+
<xr-site id=root></xr-site>
2324
<script type=module>
2425
(async () => {
2526

@@ -1204,11 +1205,7 @@
12041205
const moveMesh = currentMoveMeshes[i];
12051206
if (moveMesh) {
12061207
const {xrIframe} = moveMesh;
1207-
layers.splice(layers.indexOf(xrIframe), 1);
1208-
xrIframe.destroy();
1209-
scene.remove(moveMesh);
1210-
scene.remove(moveMesh.labelMesh);
1211-
moveMeshes.splice(moveMeshes.indexOf(moveMesh), 1);
1208+
xrIframe.parentNode.removeChild(xrIframe);
12121209
currentMoveMeshes[i] = null;
12131210
}
12141211
}
@@ -3119,6 +3116,65 @@
31193116
// const trayMesh = _makeTrayMesh();
31203117
// scene.add(trayMesh);
31213118

3119+
class XRSite extends HTMLElement {
3120+
constructor() {
3121+
super();
3122+
3123+
this.observer = new MutationObserver(mutations => {
3124+
this.handleMutations(mutations);
3125+
});
3126+
this.observer.observe(this, {
3127+
childList: true,
3128+
});
3129+
}
3130+
update() {
3131+
this.handleMutations(this.observer.takeRecords());
3132+
}
3133+
handleMutations(mutations) {
3134+
for (let i = 0; i < mutations.length; i++) {
3135+
const {addedNodes, removedNodes} = mutations[i];
3136+
3137+
for (let j = 0; j < removedNodes.length; j++) {
3138+
const removedNode = removedNodes[j];
3139+
3140+
if (removedNode instanceof XRIFrame) {
3141+
const xrIframe = removedNode;
3142+
3143+
layers.splice(layers.indexOf(xrIframe), 1);
3144+
xrIframe.destroy();
3145+
3146+
const {moveMesh} = xrIframe;
3147+
scene.remove(moveMesh);
3148+
scene.remove(moveMesh.labelMesh);
3149+
moveMeshes.splice(moveMeshes.indexOf(moveMesh), 1);
3150+
}
3151+
}
3152+
for (let j = 0; j < addedNodes.length; j++) {
3153+
const addedNode = addedNodes[j];
3154+
3155+
if (addedNode instanceof XRIFrame) {
3156+
const xrIframe = addedNode;
3157+
3158+
layers.push(xrIframe);
3159+
3160+
const moveMesh = _makeMoveMesh('volume');
3161+
moveMesh.xrIframe = xrIframe;
3162+
moveMesh.startTime = Date.now();
3163+
scene.add(moveMesh);
3164+
moveMeshes.push(moveMesh);
3165+
xrIframe.moveMesh = moveMesh;
3166+
3167+
const labelMesh = _makeLabelMesh(xrIframe.name || xrIframe.src);
3168+
scene.add(labelMesh);
3169+
moveMesh.labelMesh = labelMesh;
3170+
}
3171+
}
3172+
}
3173+
}
3174+
}
3175+
customElements.define('xr-site', XRSite);
3176+
const root = document.getElementById('root');
3177+
31223178
const uiSize = 2048;
31233179
let renderIds = 0;
31243180
const _renderUi = (searchString, selectedTab) => new Promise((accept, reject) => {
@@ -3485,18 +3541,11 @@
34853541
} else if (href) {
34863542
const xrIframe = document.createElement('xr-iframe');
34873543
xrIframe.src = href;
3488-
layers.push(xrIframe);
3489-
3490-
const moveMesh = _makeMoveMesh('volume');
3491-
moveMesh.xrIframe = xrIframe;
3492-
moveMesh.startTime = Date.now();
3493-
scene.add(moveMesh);
3494-
moveMeshes.push(moveMesh);
3495-
currentMoveMeshes[controllerIndex] = moveMesh;
3544+
xrIframe.name = name;
3545+
root.appendChild(xrIframe);
3546+
root.update(); // force mutation observer to run
34963547

3497-
const labelMesh = _makeLabelMesh(name || href);
3498-
scene.add(labelMesh);
3499-
moveMesh.labelMesh = labelMesh;
3548+
currentMoveMeshes[controllerIndex] = xrIframe.moveMesh;
35003549
} /* else {
35013550
console.log('no match', intersectionSpec, new Error().stack);
35023551
} */

0 commit comments

Comments
 (0)