oomi-ai 0.2.39 → 0.2.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +5 -3
  2. package/lib/personaRuntimeManager.js +21 -17
  3. package/lib/personaRuntimeProcess.js +417 -49
  4. package/lib/scaffold.js +14 -14
  5. package/openclaw.plugin.json +1 -1
  6. package/package.json +10 -8
  7. package/templates/persona-app/package.json +6 -4
  8. package/templates/persona-app/src/App.css +562 -81
  9. package/templates/persona-app/src/App.tsx +2 -2
  10. package/templates/persona-app/src/main.tsx +13 -0
  11. package/templates/persona-app/src/pages/HomePage.tsx +117 -36
  12. package/templates/persona-app/src/pages/ScenePage.tsx +2 -15
  13. package/templates/persona-app/src/persona/notes.ts +3 -1
  14. package/templates/persona-app/src/spatial.ts +82 -0
  15. package/templates/persona-app/template.json +1 -1
  16. package/templates/persona-app/vendor/webspatial/FORK.md +6 -0
  17. package/templates/persona-app/vendor/webspatial/core-sdk/LICENSE +21 -0
  18. package/templates/persona-app/vendor/webspatial/core-sdk/dist/iife/index.d.ts +906 -0
  19. package/templates/persona-app/vendor/webspatial/core-sdk/dist/iife/index.global.js +75 -0
  20. package/templates/persona-app/vendor/webspatial/core-sdk/dist/iife/index.global.js.map +1 -0
  21. package/templates/persona-app/vendor/webspatial/core-sdk/dist/index.d.ts +906 -0
  22. package/templates/persona-app/vendor/webspatial/core-sdk/dist/index.js +3131 -0
  23. package/templates/persona-app/vendor/webspatial/core-sdk/dist/index.js.map +1 -0
  24. package/templates/persona-app/vendor/webspatial/core-sdk/package.json +45 -0
  25. package/templates/persona-app/vendor/webspatial/react-sdk/LICENSE +21 -0
  26. package/templates/persona-app/vendor/webspatial/react-sdk/dist/default/index.d.ts +365 -0
  27. package/templates/persona-app/vendor/webspatial/react-sdk/dist/default/index.js +4167 -0
  28. package/templates/persona-app/vendor/webspatial/react-sdk/dist/default/index.js.map +1 -0
  29. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.d.ts +82 -0
  30. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.js +66 -0
  31. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.js.map +1 -0
  32. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.web.d.ts +2 -0
  33. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.web.js +18 -0
  34. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-dev-runtime.web.js.map +1 -0
  35. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.d.ts +5 -0
  36. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.js +66 -0
  37. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.js.map +1 -0
  38. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.web.d.ts +1 -0
  39. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.web.js +18 -0
  40. package/templates/persona-app/vendor/webspatial/react-sdk/dist/jsx/jsx-runtime.web.js.map +1 -0
  41. package/templates/persona-app/vendor/webspatial/react-sdk/dist/web/index.d.ts +365 -0
  42. package/templates/persona-app/vendor/webspatial/react-sdk/dist/web/index.js +4207 -0
  43. package/templates/persona-app/vendor/webspatial/react-sdk/dist/web/index.js.map +1 -0
  44. package/templates/persona-app/vendor/webspatial/react-sdk/package.json +94 -0
  45. package/templates/persona-app/vite.config.ts +13 -0
@@ -0,0 +1,4167 @@
1
+
2
+ (function(){
3
+ if(typeof window === 'undefined') return;
4
+ if(!window.__webspatialsdk__) window.__webspatialsdk__ = {}
5
+ window.__webspatialsdk__['react-sdk-version'] = "1.2.1"
6
+ window.__webspatialsdk__['XR_ENV'] = "avp"
7
+ })()
8
+
9
+
10
+ // src/spatialized-container/hooks/useDomProxy.ts
11
+ import { useCallback, useEffect, useRef } from "react";
12
+
13
+ // src/spatialized-container/types.ts
14
+ var SpatialCustomStyleVars = {
15
+ back: "--xr-back",
16
+ depth: "--xr-depth",
17
+ backgroundMaterial: "--xr-background-material",
18
+ xrZIndex: "--xr-z-index"
19
+ };
20
+
21
+ // src/spatialized-container/utils.ts
22
+ function getInheritedStyleProps(computedStyle) {
23
+ var propNames = [
24
+ "azimuth",
25
+ "borderCollapse",
26
+ "borderSpacing",
27
+ "captionSide",
28
+ "color",
29
+ "cursor",
30
+ "direction",
31
+ // 'elevation',
32
+ "emptyCells",
33
+ "fontFamily",
34
+ "fontSize",
35
+ "fontStyle",
36
+ "fontVariant",
37
+ "fontWeight",
38
+ "font",
39
+ "letterSpacing",
40
+ "lineHeight",
41
+ "listStyleImage",
42
+ "listStylePosition",
43
+ "listStyleType",
44
+ "listStyle",
45
+ "orphans",
46
+ // 'pitchRange',
47
+ // 'pitch',
48
+ "quotes",
49
+ // 'richness',
50
+ // 'speakHeader',
51
+ // 'speakNumeral',
52
+ // 'speakPunctuation',
53
+ // 'speak',
54
+ // 'speechRate',
55
+ // 'stress',
56
+ "textAlign",
57
+ "textIndent",
58
+ "textTransform",
59
+ "visibility",
60
+ // 'voiceFamily',
61
+ // 'volume',
62
+ "whiteSpace",
63
+ "widows",
64
+ "wordSpacing",
65
+ // background also need to be synced
66
+ "background",
67
+ // position also need to be synced
68
+ "position",
69
+ "width",
70
+ "height",
71
+ "display",
72
+ // content-visibility also need to be synced
73
+ "contentVisibility"
74
+ ];
75
+ var props = {};
76
+ for (var cssName of propNames) {
77
+ if (computedStyle[cssName]) {
78
+ props[cssName] = computedStyle[cssName];
79
+ }
80
+ }
81
+ return props;
82
+ }
83
+ function parseTransformOrigin(computedStyle) {
84
+ const transformOriginProperty = computedStyle.getPropertyValue("transform-origin");
85
+ const [x, y] = transformOriginProperty.split(" ").map(parseFloat);
86
+ const width = parseFloat(computedStyle.getPropertyValue("width"));
87
+ const height = parseFloat(computedStyle.getPropertyValue("height"));
88
+ return {
89
+ x: width > 0 ? x / width : 0.5,
90
+ y: height > 0 ? y / height : 0.5,
91
+ z: 0.5
92
+ };
93
+ }
94
+ function parseBorderRadius(borderProperty, width) {
95
+ if (borderProperty === "") {
96
+ return 0;
97
+ }
98
+ if (borderProperty.endsWith("%")) {
99
+ return width * parseFloat(borderProperty) / 100;
100
+ }
101
+ return parseFloat(borderProperty);
102
+ }
103
+ function parseCornerRadius(computedStyle) {
104
+ const width = parseFloat(computedStyle.getPropertyValue("width"));
105
+ const topLeftPropertyValue = computedStyle.getPropertyValue(
106
+ "border-top-left-radius"
107
+ );
108
+ const topRightPropertyValue = computedStyle.getPropertyValue(
109
+ "border-top-right-radius"
110
+ );
111
+ const bottomLeftPropertyValue = computedStyle.getPropertyValue(
112
+ "border-bottom-left-radius"
113
+ );
114
+ const bottomRightPropertyValue = computedStyle.getPropertyValue(
115
+ "border-bottom-right-radius"
116
+ );
117
+ const cornerRadius = {
118
+ topLeading: parseBorderRadius(topLeftPropertyValue, width),
119
+ bottomLeading: parseBorderRadius(bottomLeftPropertyValue, width),
120
+ topTrailing: parseBorderRadius(topRightPropertyValue, width),
121
+ bottomTrailing: parseBorderRadius(bottomRightPropertyValue, width)
122
+ };
123
+ return cornerRadius;
124
+ }
125
+ function extractAndRemoveCustomProperties(cssText, properties) {
126
+ if (!cssText) {
127
+ return { extractedValues: {}, filteredCssText: "" };
128
+ }
129
+ const extractedValues = {};
130
+ const rules = cssText.split(";");
131
+ const filteredRules = rules.filter((rule) => {
132
+ const [key, value] = rule.split(":").map((part) => part.trim());
133
+ if (properties.includes(key)) {
134
+ extractedValues[key] = value;
135
+ return false;
136
+ }
137
+ return true;
138
+ });
139
+ const filteredCssText = filteredRules.join(";").trim();
140
+ return { extractedValues, filteredCssText };
141
+ }
142
+ function joinToCSSText(cssKV) {
143
+ const rules = Object.entries(cssKV).map(([key, value]) => `${key}: ${value}`);
144
+ return rules.join(";");
145
+ }
146
+
147
+ // src/spatialized-container/hooks/useDomProxy.ts
148
+ function makeOriginalKey(key) {
149
+ return `__original_${key}`;
150
+ }
151
+ var SpatialContainerRefProxy = class {
152
+ transformVisibilityTaskContainerDom = null;
153
+ ref;
154
+ domProxy;
155
+ styleProxy;
156
+ // extre ref props, used to add extra props to ref
157
+ extraRefProps;
158
+ constructor(ref, extraRefProps) {
159
+ this.ref = ref;
160
+ this.extraRefProps = extraRefProps;
161
+ }
162
+ updateStandardSpatializedContainerDom(dom) {
163
+ const self = this;
164
+ if (dom) {
165
+ let cacheExtraRefProps;
166
+ const domProxy = new Proxy(
167
+ dom,
168
+ {
169
+ get(target, prop) {
170
+ if (prop === "__raw") {
171
+ return target;
172
+ }
173
+ if (prop === "clientDepth") {
174
+ return target.style.getPropertyValue(SpatialCustomStyleVars.depth);
175
+ }
176
+ if (prop === "offsetBack") {
177
+ return target.style.getPropertyValue(SpatialCustomStyleVars.back);
178
+ }
179
+ if (prop === "getBoundingClientRect") {
180
+ return dom.__getBoundingClientRect;
181
+ }
182
+ if (prop === "getBoundingClientCube") {
183
+ return dom.__getBoundingClientCube;
184
+ }
185
+ if (prop === "style") {
186
+ if (!self.styleProxy) {
187
+ self.styleProxy = new Proxy(target.style, {
188
+ get(target2, prop2) {
189
+ if (prop2 === "visibility" || prop2 === "transform") {
190
+ return self.transformVisibilityTaskContainerDom?.style.getPropertyValue(
191
+ prop2
192
+ );
193
+ }
194
+ const value2 = Reflect.get(target2, prop2);
195
+ if (typeof value2 === "function") {
196
+ if (prop2 === "setProperty" || prop2 === "removeProperty" || prop2 === "getPropertyValue") {
197
+ return function(...args) {
198
+ const validProperties = ["visibility", "transform"];
199
+ const [property] = args;
200
+ if (validProperties.includes(property)) {
201
+ if (prop2 === "setProperty") {
202
+ const [, kValue] = args;
203
+ self.transformVisibilityTaskContainerDom?.style.setProperty(
204
+ property,
205
+ kValue
206
+ );
207
+ } else if (prop2 === "removeProperty") {
208
+ self.transformVisibilityTaskContainerDom?.style.removeProperty(
209
+ property
210
+ );
211
+ } else if (prop2 === "getPropertyValue") {
212
+ return self.transformVisibilityTaskContainerDom?.style.getPropertyValue(
213
+ property
214
+ );
215
+ }
216
+ } else {
217
+ return value2.apply(this, args);
218
+ }
219
+ }.bind(target2);
220
+ } else {
221
+ return value2.bind(target2);
222
+ }
223
+ } else {
224
+ return value2;
225
+ }
226
+ },
227
+ set(target2, prop2, value2) {
228
+ if (prop2 === "visibility") {
229
+ self.transformVisibilityTaskContainerDom?.style.setProperty(
230
+ "visibility",
231
+ value2
232
+ );
233
+ return true;
234
+ }
235
+ if (prop2 === "transform") {
236
+ self.transformVisibilityTaskContainerDom?.style.setProperty(
237
+ "transform",
238
+ value2
239
+ );
240
+ return true;
241
+ }
242
+ if (prop2 === SpatialCustomStyleVars.backgroundMaterial) {
243
+ target2.setProperty(
244
+ SpatialCustomStyleVars.backgroundMaterial,
245
+ value2
246
+ );
247
+ } else if (prop2 === SpatialCustomStyleVars.back) {
248
+ target2.setProperty(
249
+ SpatialCustomStyleVars.back,
250
+ value2
251
+ );
252
+ } else if (prop2 === SpatialCustomStyleVars.xrZIndex) {
253
+ target2.setProperty(
254
+ SpatialCustomStyleVars.xrZIndex,
255
+ value2
256
+ );
257
+ } else if (prop2 === SpatialCustomStyleVars.depth) {
258
+ target2.setProperty(
259
+ SpatialCustomStyleVars.depth,
260
+ value2
261
+ );
262
+ } else if (prop2 === "cssText") {
263
+ const toFilteredCSSProperties = [
264
+ "transform",
265
+ "visibility"
266
+ ];
267
+ const { extractedValues, filteredCssText } = extractAndRemoveCustomProperties(
268
+ value2,
269
+ toFilteredCSSProperties
270
+ );
271
+ toFilteredCSSProperties.forEach((key) => {
272
+ if (extractedValues[key]) {
273
+ self.transformVisibilityTaskContainerDom?.style.setProperty(
274
+ key,
275
+ extractedValues[key]
276
+ );
277
+ } else {
278
+ target2.removeProperty(key);
279
+ }
280
+ });
281
+ const appendedCSSText = joinToCSSText({
282
+ transform: "none",
283
+ visibility: "hidden"
284
+ });
285
+ return Reflect.set(
286
+ target2,
287
+ prop2,
288
+ [appendedCSSText, filteredCssText].join(";")
289
+ );
290
+ }
291
+ return Reflect.set(target2, prop2, value2);
292
+ }
293
+ });
294
+ }
295
+ return self.styleProxy;
296
+ }
297
+ if (typeof prop === "string" && self.extraRefProps) {
298
+ if (!cacheExtraRefProps) {
299
+ cacheExtraRefProps = self.extraRefProps(domProxy);
300
+ }
301
+ const extraProps = cacheExtraRefProps;
302
+ if (extraProps.hasOwnProperty(prop)) {
303
+ return extraProps[prop];
304
+ }
305
+ }
306
+ const value = Reflect.get(target, prop);
307
+ if (typeof value === "function") {
308
+ if ("removeAttribute" === prop) {
309
+ return function(...args) {
310
+ const [property] = args;
311
+ if (property === "style") {
312
+ dom.style.cssText = "visibility: hidden; transition: none; transform: none;";
313
+ if (self.transformVisibilityTaskContainerDom) {
314
+ self.transformVisibilityTaskContainerDom.style.visibility = "";
315
+ self.transformVisibilityTaskContainerDom.style.transform = "";
316
+ }
317
+ return true;
318
+ }
319
+ if (property === "class") {
320
+ domProxy.className = "xr-spatial-default";
321
+ return true;
322
+ }
323
+ };
324
+ }
325
+ return value.bind(target);
326
+ }
327
+ return value;
328
+ },
329
+ set(target, prop, value) {
330
+ if (prop === "className") {
331
+ if (value && value.indexOf("xr-spatial-default") === -1) {
332
+ value = value + " xr-spatial-default";
333
+ }
334
+ if (self.transformVisibilityTaskContainerDom) {
335
+ self.transformVisibilityTaskContainerDom.className = value;
336
+ }
337
+ }
338
+ if (typeof prop === "string" && self.extraRefProps) {
339
+ if (!cacheExtraRefProps) {
340
+ cacheExtraRefProps = self.extraRefProps(domProxy);
341
+ }
342
+ cacheExtraRefProps[prop] = value;
343
+ }
344
+ return Reflect.set(target, prop, value);
345
+ }
346
+ }
347
+ );
348
+ this.domProxy = domProxy;
349
+ const domClassList = dom.classList;
350
+ const domClassMethodKeys = ["add", "remove", "toggle", "replace"];
351
+ domClassMethodKeys.forEach((key) => {
352
+ const hiddenKey = makeOriginalKey(key);
353
+ const hiddenKeyExist = domClassList[hiddenKey] !== void 0;
354
+ const originalMethod = hiddenKeyExist ? domClassList[hiddenKey] : domClassList[key].bind(domClassList);
355
+ domClassList[hiddenKey] = originalMethod;
356
+ domClassList[key] = function(...args) {
357
+ const result = originalMethod(...args);
358
+ if (self.transformVisibilityTaskContainerDom) {
359
+ self.transformVisibilityTaskContainerDom.className = dom.className;
360
+ }
361
+ return result;
362
+ };
363
+ });
364
+ this.styleProxy = void 0;
365
+ this.updateDomProxyToRef();
366
+ Object.assign(dom, {
367
+ __targetProxy: domProxy
368
+ });
369
+ }
370
+ }
371
+ updateTransformVisibilityTaskContainerDom(dom) {
372
+ this.transformVisibilityTaskContainerDom = dom;
373
+ this.updateDomProxyToRef();
374
+ }
375
+ updateDomProxyToRef() {
376
+ const ref = this.ref;
377
+ if (!ref) {
378
+ return;
379
+ }
380
+ if (this.domProxy && this.transformVisibilityTaskContainerDom) {
381
+ if (typeof ref === "function") {
382
+ ref(this.domProxy);
383
+ } else {
384
+ ref.current = this.domProxy;
385
+ }
386
+ } else {
387
+ if (typeof ref === "function") {
388
+ ref(null);
389
+ } else {
390
+ ref.current = null;
391
+ }
392
+ }
393
+ }
394
+ updateRef(ref) {
395
+ this.ref = ref;
396
+ }
397
+ };
398
+ function hijackGetComputedStyle() {
399
+ const rawFn = window.getComputedStyle.bind(window);
400
+ window.getComputedStyle = (element, pseudoElt) => {
401
+ const dom = element.__raw;
402
+ if (dom) {
403
+ return rawFn(dom, pseudoElt);
404
+ }
405
+ return rawFn(element, pseudoElt);
406
+ };
407
+ }
408
+ function useDomProxy(ref, extraRefProps) {
409
+ const spatialContainerRefProxy = useRef(
410
+ new SpatialContainerRefProxy(ref, extraRefProps)
411
+ );
412
+ useEffect(() => {
413
+ spatialContainerRefProxy.current.updateRef(ref);
414
+ }, [ref]);
415
+ const transformVisibilityTaskContainerCallback = useCallback(
416
+ (el) => {
417
+ spatialContainerRefProxy.current.updateTransformVisibilityTaskContainerDom(
418
+ el
419
+ );
420
+ },
421
+ []
422
+ );
423
+ const standardSpatializedContainerCallback = useCallback(
424
+ (el) => {
425
+ spatialContainerRefProxy.current.updateStandardSpatializedContainerDom(el);
426
+ },
427
+ []
428
+ );
429
+ return {
430
+ transformVisibilityTaskContainerCallback,
431
+ standardSpatializedContainerCallback,
432
+ spatialContainerRefProxy
433
+ };
434
+ }
435
+
436
+ // src/spatialized-container/hooks/use2DFrameDetector.ts
437
+ import {
438
+ useContext,
439
+ useLayoutEffect,
440
+ useEffect as useEffect2,
441
+ useCallback as useCallback2
442
+ } from "react";
443
+
444
+ // src/spatialized-container/context/SpatializedContainerContext.ts
445
+ import { createContext } from "react";
446
+
447
+ // src/spatialized-container/SpatialID.ts
448
+ var SpatialID = "data-spatial-id";
449
+
450
+ // src/spatialized-container/context/SpatializedContainerContext.ts
451
+ var SpatializedContainerObject = class {
452
+ dom = null;
453
+ domSpatialId = null;
454
+ fns = {};
455
+ // cache dom for each spatialId
456
+ spatialId2dom = {};
457
+ spatialId2parentSpatialDom = {};
458
+ // layer : [standardInstance sequence, portalInstance sequence]
459
+ layerSequences = {};
460
+ notify2DFramePlaceHolderChange(dom) {
461
+ this.dom = dom;
462
+ this.domSpatialId = dom.getAttribute(SpatialID);
463
+ Object.values(this.fns).forEach((fn) => fn());
464
+ }
465
+ spatialId2transformVisibility = {};
466
+ updateSpatialTransformVisibility(spatialId, spatialTransformVisibility) {
467
+ this.spatialId2transformVisibility[spatialId] = spatialTransformVisibility;
468
+ this.fnsForSpatialTransformVisibility[spatialId]?.forEach(
469
+ (fn) => fn(spatialTransformVisibility)
470
+ );
471
+ }
472
+ // this is used by onSpatialEvent.currentTarget property
473
+ spatialId2ContainerRefProxy = {};
474
+ // this is called in sub standardInstance env
475
+ updateSpatialContainerRefProxyInfo(spatialId, spatialContainerRefProxy) {
476
+ this.spatialId2ContainerRefProxy[spatialId] = spatialContainerRefProxy;
477
+ }
478
+ getSpatialContainerRefProxyBySpatialId(spatialId) {
479
+ return this.spatialId2ContainerRefProxy[spatialId];
480
+ }
481
+ // notify when TransformVisibilityTaskContainer data change
482
+ fnsForSpatialTransformVisibility = {};
483
+ // used by StandardSpatializedContainer and PortalSpatializedContainer
484
+ onSpatialTransformVisibilityChange(spatialId, fn) {
485
+ if (!this.fnsForSpatialTransformVisibility[spatialId]) {
486
+ this.fnsForSpatialTransformVisibility[spatialId] = [];
487
+ }
488
+ this.fnsForSpatialTransformVisibility[spatialId].push(fn);
489
+ if (this.spatialId2transformVisibility[spatialId]) {
490
+ fn(this.spatialId2transformVisibility[spatialId]);
491
+ }
492
+ }
493
+ offSpatialTransformVisibilityChange(spatialId, fn) {
494
+ const fns = this.fnsForSpatialTransformVisibility[spatialId];
495
+ if (fns) {
496
+ this.fnsForSpatialTransformVisibility[spatialId] = fns.filter(
497
+ (f) => f !== fn
498
+ );
499
+ }
500
+ }
501
+ on2DFrameChange(spatialId, fn) {
502
+ this.fns[spatialId] = fn;
503
+ if (this.dom) {
504
+ fn();
505
+ }
506
+ }
507
+ off2DFrameChange(spatialId) {
508
+ delete this.fns[spatialId];
509
+ delete this.spatialId2dom[spatialId];
510
+ delete this.spatialId2parentSpatialDom[spatialId];
511
+ }
512
+ querySpatialDomBySpatialId(spatialId) {
513
+ if (this.domSpatialId === spatialId) {
514
+ return this.dom;
515
+ }
516
+ if (!this.dom) {
517
+ return null;
518
+ }
519
+ if (!this.spatialId2dom[spatialId]) {
520
+ const spatialDom = this.dom.querySelector(`[${SpatialID}="${spatialId}"]`);
521
+ if (spatialDom) {
522
+ this.spatialId2dom[spatialId] = spatialDom;
523
+ }
524
+ }
525
+ return this.spatialId2dom[spatialId];
526
+ }
527
+ queryParentSpatialDomBySpatialId(spatialId) {
528
+ if (this.domSpatialId === spatialId) {
529
+ return null;
530
+ }
531
+ if (this.spatialId2parentSpatialDom[spatialId]) {
532
+ return this.spatialId2parentSpatialDom[spatialId];
533
+ }
534
+ let spatialDom = this.querySpatialDomBySpatialId(spatialId);
535
+ if (spatialDom) {
536
+ if (spatialDom === this.dom) return null;
537
+ let parentSpatialDom = spatialDom.parentElement;
538
+ while (parentSpatialDom && spatialDom !== this.dom) {
539
+ if (parentSpatialDom.hasAttribute(SpatialID)) {
540
+ break;
541
+ } else {
542
+ parentSpatialDom = parentSpatialDom.parentElement;
543
+ }
544
+ }
545
+ this.spatialId2parentSpatialDom[spatialId] = parentSpatialDom;
546
+ return parentSpatialDom;
547
+ }
548
+ return null;
549
+ }
550
+ getSpatialId(layer, isInStandardInstance, name = "") {
551
+ if (this.layerSequences[layer] === void 0) {
552
+ this.layerSequences[layer] = [0, 0];
553
+ }
554
+ const idx = isInStandardInstance ? 0 : 1;
555
+ const sequenceId = this.layerSequences[layer][idx];
556
+ this.layerSequences[layer][idx] = sequenceId + 1;
557
+ const spatialId = `${name}_${layer}_${sequenceId}`;
558
+ return spatialId;
559
+ }
560
+ };
561
+ var SpatializedContainerContext = createContext(null);
562
+
563
+ // src/spatialized-container/hooks/use2DFrameDetector.ts
564
+ function use2DFrameDetector(ref) {
565
+ const spatializedContainerObject = useContext(
566
+ SpatializedContainerContext
567
+ );
568
+ const notify2DFrameChange = useCallback2(() => {
569
+ ref.current && spatializedContainerObject.notify2DFramePlaceHolderChange(ref.current);
570
+ }, [ref.current, spatializedContainerObject]);
571
+ useLayoutEffect(notify2DFrameChange, [notify2DFrameChange]);
572
+ useEffect2(() => {
573
+ if (!ref.current || !spatializedContainerObject) {
574
+ console.warn(
575
+ "Ref is not attached to the DOM or spatializedContainerObject is not available"
576
+ );
577
+ return;
578
+ }
579
+ window.addEventListener("resize", notify2DFrameChange);
580
+ return () => {
581
+ window.removeEventListener("resize", notify2DFrameChange);
582
+ };
583
+ }, []);
584
+ useEffect2(() => {
585
+ if (!ref.current) {
586
+ console.warn("Ref is not attached to the DOM");
587
+ return;
588
+ }
589
+ const ro = new ResizeObserver(notify2DFrameChange);
590
+ ro.observe(ref.current);
591
+ return () => {
592
+ ro.disconnect();
593
+ };
594
+ }, []);
595
+ useEffect2(() => {
596
+ if (!ref.current) {
597
+ console.warn("Ref is not attached to the DOM");
598
+ return;
599
+ }
600
+ const ro = new MutationObserver(notify2DFrameChange);
601
+ ro.observe(ref.current, {
602
+ attributeFilter: ["class", "style"],
603
+ subtree: true
604
+ });
605
+ return () => {
606
+ ro.disconnect();
607
+ };
608
+ }, []);
609
+ }
610
+
611
+ // src/spatialized-container/StandardSpatializedContainer.tsx
612
+ import {
613
+ forwardRef,
614
+ useCallback as useCallback3,
615
+ useContext as useContext2,
616
+ useEffect as useEffect3,
617
+ useRef as useRef2,
618
+ useState
619
+ } from "react";
620
+ import { jsx } from "react/jsx-runtime";
621
+ function useSpatialTransformVisibilityWatcher(spatialId) {
622
+ const [transformExist, setTransformExist] = useState(false);
623
+ const spatializedContainerObject = useContext2(SpatializedContainerContext);
624
+ useEffect3(() => {
625
+ const fn = (spatialTransform) => {
626
+ setTransformExist(spatialTransform.transform !== "none");
627
+ };
628
+ spatializedContainerObject.onSpatialTransformVisibilityChange(spatialId, fn);
629
+ return () => {
630
+ spatializedContainerObject.offSpatialTransformVisibilityChange(
631
+ spatialId,
632
+ fn
633
+ );
634
+ };
635
+ }, [spatialId, spatializedContainerObject]);
636
+ return transformExist;
637
+ }
638
+ function useInternalRef(ref) {
639
+ const refInternal = useRef2(null);
640
+ const refInternalCallback = useCallback3(
641
+ (node) => {
642
+ refInternal.current = node;
643
+ if (typeof ref === "function") {
644
+ ref(node);
645
+ } else if (ref) {
646
+ ref.current = node;
647
+ }
648
+ },
649
+ [ref]
650
+ );
651
+ return { refInternal, refInternalCallback };
652
+ }
653
+ function StandardSpatializedContainerBase(props, ref) {
654
+ const {
655
+ component: Component,
656
+ style: inStyle = {},
657
+ className,
658
+ inStandardSpatializedContainer = false,
659
+ ...restProps
660
+ } = props;
661
+ const { refInternal, refInternalCallback } = useInternalRef(ref);
662
+ if (!inStandardSpatializedContainer) {
663
+ use2DFrameDetector(refInternal);
664
+ }
665
+ const transformExist = useSpatialTransformVisibilityWatcher(props[SpatialID]);
666
+ const extraStyle = {
667
+ visibility: "hidden",
668
+ transition: "none",
669
+ transform: transformExist ? "translateZ(0)" : "none"
670
+ };
671
+ const style = { ...inStyle, ...extraStyle };
672
+ const classNames = className ? `${className} xr-spatial-default` : "xr-spatial-default";
673
+ return /* @__PURE__ */ jsx(
674
+ Component,
675
+ {
676
+ ref: refInternalCallback,
677
+ style,
678
+ className: classNames,
679
+ ...restProps
680
+ }
681
+ );
682
+ }
683
+ var StandardSpatializedContainer = forwardRef(
684
+ StandardSpatializedContainerBase
685
+ );
686
+ function injectSpatialDefaultStyle() {
687
+ const styleElement = document.createElement("style");
688
+ styleElement.type = "text/css";
689
+ styleElement.innerHTML = " :where(.xr-spatial-default) { --xr-back: 0; --xr-depth: 0; --xr-z-index: 0; --xr-background-material: none; } ";
690
+ document.head.appendChild(styleElement);
691
+ }
692
+
693
+ // src/spatialized-container/TransformVisibilityTaskContainer.tsx
694
+ import {
695
+ forwardRef as forwardRef2,
696
+ useCallback as useCallback5,
697
+ useRef as useRef3
698
+ } from "react";
699
+ import { createPortal } from "react-dom";
700
+
701
+ // src/spatialized-container/hooks/useSpatialTransformVisibility.ts
702
+ import { useCallback as useCallback4, useContext as useContext3, useEffect as useEffect4 } from "react";
703
+
704
+ // src/notifyUpdateStandInstanceLayout.ts
705
+ function notifyUpdateStandInstanceLayout() {
706
+ document.dispatchEvent(
707
+ new CustomEvent("standInstanceLayout" /* standInstanceLayout */, {
708
+ detail: {}
709
+ })
710
+ );
711
+ }
712
+ function notifyDOMUpdate(mutationsList) {
713
+ document.dispatchEvent(
714
+ new CustomEvent("domUpdated" /* domUpdated */, {
715
+ detail: mutationsList
716
+ })
717
+ );
718
+ }
719
+
720
+ // src/spatialized-container/hooks/useSpatialTransformVisibility.ts
721
+ function parseTransformAndVisibilityProperties(node) {
722
+ const computedStyle = getComputedStyle(node);
723
+ const transform = computedStyle.getPropertyValue("transform");
724
+ const visibility = computedStyle.getPropertyValue("visibility");
725
+ return {
726
+ visibility,
727
+ transform
728
+ };
729
+ }
730
+ function useSpatialTransformVisibility(spatialId, ref) {
731
+ const spatializedContainerObject = useContext3(SpatializedContainerContext);
732
+ const checkSpatialStyleUpdate = useCallback4(() => {
733
+ if (!ref.current) {
734
+ return;
735
+ }
736
+ const spatialTransformVisibility = parseTransformAndVisibilityProperties(
737
+ ref.current
738
+ );
739
+ spatializedContainerObject.updateSpatialTransformVisibility(
740
+ spatialId,
741
+ spatialTransformVisibility
742
+ );
743
+ }, []);
744
+ useEffect4(() => {
745
+ checkSpatialStyleUpdate();
746
+ }, [checkSpatialStyleUpdate]);
747
+ useEffect4(() => {
748
+ const observer = new MutationObserver((mutationsList) => {
749
+ checkSpatialStyleUpdate();
750
+ });
751
+ const config = {
752
+ childList: false,
753
+ subtree: false,
754
+ attributes: true,
755
+ // attributeOldValue: true,
756
+ attributeFilter: ["style", "class"]
757
+ };
758
+ observer.observe(ref.current, config);
759
+ return () => {
760
+ observer.disconnect();
761
+ };
762
+ }, []);
763
+ useEffect4(() => {
764
+ const headObserver = new MutationObserver((mutations) => {
765
+ checkSpatialStyleUpdate();
766
+ });
767
+ headObserver.observe(document.head, { childList: true, subtree: true });
768
+ return () => {
769
+ headObserver.disconnect();
770
+ };
771
+ }, []);
772
+ useEffect4(() => {
773
+ const onDomUpdated = (event) => {
774
+ checkSpatialStyleUpdate();
775
+ };
776
+ document.addEventListener(
777
+ "domUpdated" /* domUpdated */,
778
+ onDomUpdated
779
+ );
780
+ return () => {
781
+ document.removeEventListener(
782
+ "domUpdated" /* domUpdated */,
783
+ onDomUpdated
784
+ );
785
+ };
786
+ }, []);
787
+ }
788
+
789
+ // src/spatialized-container/TransformVisibilityTaskContainer.tsx
790
+ import { jsx as jsx2 } from "react/jsx-runtime";
791
+ var cssParserDivContainer = null;
792
+ function initCSSParserDivContainer() {
793
+ cssParserDivContainer = document?.createElement("div");
794
+ if (cssParserDivContainer) {
795
+ cssParserDivContainer.style.position = "absolute";
796
+ cssParserDivContainer.setAttribute("data-id", "css-parser-div-container");
797
+ }
798
+ }
799
+ function createOrGetCSSParserDivContainer() {
800
+ if (cssParserDivContainer && !cssParserDivContainer.parentElement) {
801
+ document?.body.appendChild(cssParserDivContainer);
802
+ }
803
+ return cssParserDivContainer;
804
+ }
805
+ function useInternalRef2(ref) {
806
+ const refInternal = useRef3(null);
807
+ const refInternalCallback = useCallback5(
808
+ (node) => {
809
+ refInternal.current = node;
810
+ if (typeof ref === "function") {
811
+ ref(node);
812
+ } else if (ref) {
813
+ ref.current = node;
814
+ }
815
+ },
816
+ [ref]
817
+ );
818
+ return { refInternal, refInternalCallback };
819
+ }
820
+ function TransformVisibilityTaskContainerBase(props, ref) {
821
+ const { style: inStyle, ...restProps } = props;
822
+ const extraStyle = {
823
+ // when width/height equal to zero, transform: translateX(-50%) won't work
824
+ // to make sure the element is not visible, we set left/top to a very large negative value
825
+ left: -1e4,
826
+ top: -1e4,
827
+ pointerEvents: "none",
828
+ opacity: 0,
829
+ // width: 0,
830
+ // height: 0,
831
+ padding: 0,
832
+ transition: "none",
833
+ position: "absolute"
834
+ };
835
+ const { refInternal, refInternalCallback } = useInternalRef2(ref);
836
+ const style = { ...inStyle, ...extraStyle };
837
+ useSpatialTransformVisibility(props[SpatialID], refInternal);
838
+ const cssParserDivContainer2 = createOrGetCSSParserDivContainer();
839
+ if (!cssParserDivContainer2) {
840
+ return null;
841
+ }
842
+ return createPortal(
843
+ /* @__PURE__ */ jsx2("div", { ref: refInternalCallback, style, ...restProps }),
844
+ cssParserDivContainer2
845
+ );
846
+ }
847
+ var TransformVisibilityTaskContainer = forwardRef2(
848
+ TransformVisibilityTaskContainerBase
849
+ );
850
+
851
+ // src/spatialized-container/SpatializedContainer.tsx
852
+ import { forwardRef as forwardRef4, useContext as useContext7, useEffect as useEffect10, useMemo as useMemo2 } from "react";
853
+
854
+ // src/utils/getSession.ts
855
+ import { isSSREnv, Spatial } from "@webspatial/core-sdk";
856
+ var spatial = null;
857
+ var _currentSession = null;
858
+ function getSession() {
859
+ if (isSSREnv()) return null;
860
+ if (!spatial) {
861
+ spatial = new Spatial();
862
+ }
863
+ if (!spatial.isSupported()) {
864
+ return null;
865
+ }
866
+ if (_currentSession) {
867
+ return _currentSession;
868
+ }
869
+ _currentSession = spatial.requestSession();
870
+ return _currentSession;
871
+ }
872
+
873
+ // src/spatialized-container/context/SpatialLayerContext.ts
874
+ import { createContext as createContext2 } from "react";
875
+ var SpatialLayerContext = createContext2(0);
876
+
877
+ // src/spatialized-container/PortalSpatializedContainer.tsx
878
+ import { useMemo, useContext as useContext4, useEffect as useEffect7 } from "react";
879
+
880
+ // src/spatialized-container/context/PortalInstanceContext.ts
881
+ import { createContext as createContext3 } from "react";
882
+
883
+ // src/utils/debugTool.ts
884
+ import { isSSREnv as isSSREnv2 } from "@webspatial/core-sdk";
885
+ async function inspectCurrentSpatialScene() {
886
+ const spatialScene = getSession().getSpatialScene();
887
+ return spatialScene.inspect();
888
+ }
889
+ function getSpatialized2DElement(spatialized2DElement) {
890
+ return spatialized2DElement.__innerSpatializedElement?.();
891
+ }
892
+ function enableDebugTool() {
893
+ if (isSSREnv2()) return;
894
+ Object.assign(window, {
895
+ inspectCurrentSpatialScene,
896
+ getSpatialized2DElement
897
+ });
898
+ }
899
+
900
+ // src/utils/androidBitmapCapture.ts
901
+ var snapdomModule = null;
902
+ var snapdomChecked = false;
903
+ var snapdomAvailable = false;
904
+ var html2canvasModule = null;
905
+ var html2canvasChecked = false;
906
+ var html2canvasAvailable = false;
907
+ async function loadSnapdom() {
908
+ if (snapdomModule) return snapdomModule;
909
+ if (snapdomChecked && !snapdomAvailable) return null;
910
+ console.log("[WebSpatial] Checking for snapdom...", {
911
+ windowExists: typeof window !== "undefined",
912
+ snapdomOnWindow: typeof window?.snapdom,
913
+ html2canvasOnWindow: typeof window?.html2canvas
914
+ });
915
+ for (let attempt = 0; attempt < 3; attempt++) {
916
+ if (typeof window !== "undefined" && window.snapdom) {
917
+ snapdomModule = window.snapdom;
918
+ snapdomChecked = true;
919
+ snapdomAvailable = true;
920
+ console.log("[WebSpatial] Using globally provided snapdom (fast mode)");
921
+ return snapdomModule;
922
+ }
923
+ if (attempt < 2) {
924
+ console.log(`[WebSpatial] snapdom not on window, retry ${attempt + 1}/3...`);
925
+ await new Promise((resolve) => setTimeout(resolve, 100));
926
+ }
927
+ }
928
+ try {
929
+ console.log("[WebSpatial] Trying dynamic import of @zumer/snapdom...");
930
+ const moduleName = "@zumer/snapdom";
931
+ const dynamicImport = new Function("moduleName", "return import(moduleName)");
932
+ const module = await dynamicImport(moduleName);
933
+ snapdomModule = module.snapdom || module.default || module;
934
+ snapdomChecked = true;
935
+ snapdomAvailable = true;
936
+ console.log("[WebSpatial] Loaded snapdom via dynamic import (fast mode)");
937
+ return snapdomModule;
938
+ } catch (error) {
939
+ snapdomChecked = true;
940
+ snapdomAvailable = false;
941
+ console.log("[WebSpatial] snapdom not available:", error.message);
942
+ console.log("[WebSpatial] Falling back to html2canvas");
943
+ return null;
944
+ }
945
+ }
946
+ async function loadHtml2Canvas() {
947
+ if (html2canvasModule) return html2canvasModule;
948
+ if (html2canvasChecked && !html2canvasAvailable) return null;
949
+ if (typeof window !== "undefined" && window.html2canvas) {
950
+ html2canvasModule = window.html2canvas;
951
+ html2canvasChecked = true;
952
+ html2canvasAvailable = true;
953
+ console.log("[WebSpatial] Using globally provided html2canvas (fallback mode)");
954
+ return html2canvasModule;
955
+ }
956
+ try {
957
+ const moduleName = "html2canvas";
958
+ const dynamicImport = new Function("moduleName", "return import(moduleName)");
959
+ const module = await dynamicImport(moduleName);
960
+ html2canvasModule = module.default || module;
961
+ html2canvasChecked = true;
962
+ html2canvasAvailable = true;
963
+ console.log("[WebSpatial] Loaded html2canvas via dynamic import (fallback mode)");
964
+ return html2canvasModule;
965
+ } catch (error) {
966
+ html2canvasChecked = true;
967
+ html2canvasAvailable = false;
968
+ console.warn(
969
+ "[WebSpatial] Neither snapdom nor html2canvas available. Bitmap capture for Android XR is disabled. Install @zumer/snapdom (recommended) or html2canvas."
970
+ );
971
+ return null;
972
+ }
973
+ }
974
+ function isAndroidPlatform() {
975
+ if (typeof window === "undefined") return false;
976
+ const ua = window.navigator.userAgent;
977
+ const hasWebSpatialBridge = typeof window.webspatialBridge !== "undefined";
978
+ return hasWebSpatialBridge && (ua.includes("Android") || ua.includes("Linux"));
979
+ }
980
+ function getAndroidRenderMode() {
981
+ if (typeof window === "undefined") {
982
+ return null;
983
+ }
984
+ const bridgeMode = window.webspatialBridge?.getRenderMode?.();
985
+ if (bridgeMode === "live-window" || bridgeMode === "bitmap-capture") {
986
+ return bridgeMode;
987
+ }
988
+ const configuredMode = window.__WebSpatialAndroidConfig?.renderMode;
989
+ if (configuredMode === "live-window" || configuredMode === "bitmap-capture") {
990
+ return configuredMode;
991
+ }
992
+ if (!isAndroidPlatform()) {
993
+ return null;
994
+ }
995
+ return "bitmap-capture";
996
+ }
997
+ function supportsAndroidLiveWindowProxy() {
998
+ return getAndroidRenderMode() === "live-window";
999
+ }
1000
+ function usesAndroidBitmapCapture() {
1001
+ return isAndroidPlatform() && !supportsAndroidLiveWindowProxy();
1002
+ }
1003
+ var DEFAULT_CAPTURE_BACKGROUND = "#1a1a2e";
1004
+ function hasTransparentBackground(element) {
1005
+ const style = window.getComputedStyle(element);
1006
+ const bg = style.backgroundColor;
1007
+ const bgImage = style.backgroundImage;
1008
+ if (bg === "transparent" || bg === "rgba(0, 0, 0, 0)" || bg === "" || bg === "initial") {
1009
+ if (bgImage === "none" || bgImage === "" || bgImage === "initial") {
1010
+ return true;
1011
+ }
1012
+ }
1013
+ return false;
1014
+ }
1015
+ function injectCaptureBackground(element, backgroundColor = DEFAULT_CAPTURE_BACKGROUND) {
1016
+ const restoreFunctions = [];
1017
+ const wasTransparent = hasTransparentBackground(element);
1018
+ if (wasTransparent) {
1019
+ const originalBg = element.style.backgroundColor;
1020
+ element.style.backgroundColor = backgroundColor;
1021
+ restoreFunctions.push(() => {
1022
+ element.style.backgroundColor = originalBg;
1023
+ });
1024
+ }
1025
+ const shouldInjectDescendantBackground = (candidate) => {
1026
+ if (!hasTransparentBackground(candidate)) {
1027
+ return false;
1028
+ }
1029
+ const style = window.getComputedStyle(candidate);
1030
+ if (style.display === "inline" || style.display === "contents") {
1031
+ return false;
1032
+ }
1033
+ const rect = candidate.getBoundingClientRect();
1034
+ const hasMeaningfulBox = rect.width >= 32 && rect.height >= 32;
1035
+ if (!hasMeaningfulBox) {
1036
+ return false;
1037
+ }
1038
+ const hasNestedLayout = candidate.children.length > 0;
1039
+ const hasVisualContainerTraits = style.borderRadius !== "0px" || style.boxShadow !== "none" || style.backdropFilter !== "none" || style.overflow !== "visible" || style.borderStyle !== "none";
1040
+ return hasNestedLayout || hasVisualContainerTraits;
1041
+ };
1042
+ const allDescendants = element.querySelectorAll("*");
1043
+ let injectedCount = 0;
1044
+ allDescendants.forEach((el) => {
1045
+ const htmlEl = el;
1046
+ if (shouldInjectDescendantBackground(htmlEl)) {
1047
+ const childOriginalBg = htmlEl.style.backgroundColor;
1048
+ htmlEl.style.backgroundColor = backgroundColor;
1049
+ injectedCount++;
1050
+ restoreFunctions.push(() => {
1051
+ htmlEl.style.backgroundColor = childOriginalBg;
1052
+ });
1053
+ }
1054
+ });
1055
+ console.log(
1056
+ `[WebSpatial] Injected background ${backgroundColor} for capture (parent transparent: ${wasTransparent}, ${injectedCount} children)`
1057
+ );
1058
+ return () => {
1059
+ restoreFunctions.forEach((restore) => restore());
1060
+ };
1061
+ }
1062
+ var initialRenderDelayApplied = false;
1063
+ async function waitForContent(element, imageTimeoutMs = 2e3) {
1064
+ if (!initialRenderDelayApplied) {
1065
+ initialRenderDelayApplied = true;
1066
+ console.log("[WebSpatial] Applying initial render delay (1500ms) for first capture");
1067
+ await new Promise((resolve) => setTimeout(resolve, 1500));
1068
+ }
1069
+ try {
1070
+ await Promise.race([
1071
+ document.fonts.ready,
1072
+ new Promise((resolve) => setTimeout(resolve, 500))
1073
+ ]);
1074
+ } catch {
1075
+ }
1076
+ const images = element.querySelectorAll("img");
1077
+ console.log(`[WebSpatial] Found ${images.length} images in element`);
1078
+ if (images.length > 0) {
1079
+ Array.from(images).forEach((img, i) => {
1080
+ const src = img.src?.substring(0, 80) || "no-src";
1081
+ console.log(
1082
+ `[WebSpatial] Image ${i}: complete=${img.complete}, naturalWidth=${img.naturalWidth}, src=${src}...`
1083
+ );
1084
+ });
1085
+ const incompleteImages = Array.from(images).filter((img) => !img.complete);
1086
+ if (incompleteImages.length > 0) {
1087
+ console.log(`[WebSpatial] Waiting for ${incompleteImages.length} images to load (timeout: ${imageTimeoutMs}ms)`);
1088
+ const imagePromises = incompleteImages.map((img) => {
1089
+ return new Promise((resolve) => {
1090
+ const handler = () => resolve();
1091
+ img.addEventListener("load", handler, { once: true });
1092
+ img.addEventListener("error", handler, { once: true });
1093
+ });
1094
+ });
1095
+ await Promise.race([
1096
+ Promise.all(imagePromises),
1097
+ new Promise((resolve) => setTimeout(resolve, imageTimeoutMs))
1098
+ ]);
1099
+ const stillIncomplete = incompleteImages.filter((img) => !img.complete).length;
1100
+ console.log(`[WebSpatial] Image wait complete. ${stillIncomplete} images still loading.`);
1101
+ } else {
1102
+ console.log(`[WebSpatial] All ${images.length} images already complete`);
1103
+ }
1104
+ }
1105
+ const textContent = element.innerText?.trim() || "";
1106
+ if (textContent.length < 100) {
1107
+ console.log(`[WebSpatial] Element has minimal content (${textContent.length} chars), waiting 500ms more`);
1108
+ await new Promise((resolve) => setTimeout(resolve, 500));
1109
+ }
1110
+ }
1111
+ var MAX_BITMAP_DIMENSION = 2048;
1112
+ function resizeCanvasIfNeeded(canvas) {
1113
+ const { width, height } = canvas;
1114
+ if (width <= MAX_BITMAP_DIMENSION && height <= MAX_BITMAP_DIMENSION) {
1115
+ return canvas;
1116
+ }
1117
+ const scaleFactor = Math.min(
1118
+ MAX_BITMAP_DIMENSION / width,
1119
+ MAX_BITMAP_DIMENSION / height
1120
+ );
1121
+ const newWidth = Math.round(width * scaleFactor);
1122
+ const newHeight = Math.round(height * scaleFactor);
1123
+ console.log(
1124
+ `[WebSpatial] Resizing bitmap from ${width}x${height} to ${newWidth}x${newHeight}`
1125
+ );
1126
+ const resizedCanvas = document.createElement("canvas");
1127
+ resizedCanvas.width = newWidth;
1128
+ resizedCanvas.height = newHeight;
1129
+ const ctx = resizedCanvas.getContext("2d");
1130
+ if (ctx) {
1131
+ ctx.imageSmoothingEnabled = true;
1132
+ ctx.imageSmoothingQuality = "high";
1133
+ ctx.drawImage(canvas, 0, 0, newWidth, newHeight);
1134
+ }
1135
+ return resizedCanvas;
1136
+ }
1137
+ async function captureWithSnapdom(snapdom, element, scale) {
1138
+ try {
1139
+ const cappedScale = Math.min(scale, 1.5);
1140
+ const rect = element.getBoundingClientRect();
1141
+ console.log(`[WebSpatial] snapdom capturing: rect=(${rect.x.toFixed(0)},${rect.y.toFixed(0)},${rect.width.toFixed(0)},${rect.height.toFixed(0)}), scale=${cappedScale}`);
1142
+ const result = await snapdom(element, {
1143
+ scale: cappedScale,
1144
+ embedFonts: false
1145
+ });
1146
+ let canvas = await result.toCanvas();
1147
+ canvas = resizeCanvasIfNeeded(canvas);
1148
+ const dataUrl = canvas.toDataURL("image/webp", 0.85);
1149
+ return dataUrl;
1150
+ } catch (error) {
1151
+ console.error("[WebSpatial] snapdom capture failed:", error);
1152
+ return null;
1153
+ }
1154
+ }
1155
+ function createVisibleCaptureClone(element) {
1156
+ const rect = element.getBoundingClientRect();
1157
+ const sandbox = document.createElement("div");
1158
+ sandbox.setAttribute("aria-hidden", "true");
1159
+ sandbox.style.position = "fixed";
1160
+ sandbox.style.left = "-10000px";
1161
+ sandbox.style.top = "0px";
1162
+ sandbox.style.pointerEvents = "none";
1163
+ sandbox.style.zIndex = "-1";
1164
+ sandbox.style.contain = "layout style paint";
1165
+ sandbox.style.opacity = "1";
1166
+ const clone = element.cloneNode(true);
1167
+ const makeCloneVisible = (node) => {
1168
+ node.style.visibility = "visible";
1169
+ node.style.opacity = "1";
1170
+ node.style.transition = "none";
1171
+ node.style.animation = "none";
1172
+ node.style.transform = "none";
1173
+ node.style.top = "0px";
1174
+ node.style.left = "0px";
1175
+ Array.from(node.children).forEach((child) => {
1176
+ if (child instanceof HTMLElement) {
1177
+ makeCloneVisible(child);
1178
+ }
1179
+ });
1180
+ };
1181
+ makeCloneVisible(clone);
1182
+ clone.style.position = "relative";
1183
+ clone.style.margin = "0px";
1184
+ clone.style.width = `${Math.ceil(rect.width)}px`;
1185
+ clone.style.minHeight = `${Math.ceil(rect.height)}px`;
1186
+ sandbox.appendChild(clone);
1187
+ document.body.appendChild(sandbox);
1188
+ return {
1189
+ clone,
1190
+ cleanup: () => sandbox.remove()
1191
+ };
1192
+ }
1193
+ async function captureWithHtml2Canvas(html2canvas, element, scale, backgroundColor) {
1194
+ try {
1195
+ const rect = element.getBoundingClientRect();
1196
+ console.log(
1197
+ `[WebSpatial] html2canvas capturing via visible clone: rect=(${rect.x.toFixed(0)},${rect.y.toFixed(0)},${rect.width.toFixed(0)},${rect.height.toFixed(0)})`
1198
+ );
1199
+ const scrollY = window.scrollY || window.pageYOffset || 0;
1200
+ const viewportTop = scrollY;
1201
+ const viewportBottom = scrollY + window.innerHeight;
1202
+ const elementTop = rect.y + scrollY;
1203
+ const elementBottom = elementTop + rect.height;
1204
+ console.log(
1205
+ `[WebSpatial] Capture context: viewport=(${viewportTop}-${viewportBottom}), element=(${elementTop}-${elementBottom}), innerHeight=${window.innerHeight}`
1206
+ );
1207
+ let canvas;
1208
+ const captureClone = createVisibleCaptureClone(element);
1209
+ const restoreBackground = injectCaptureBackground(
1210
+ captureClone.clone,
1211
+ backgroundColor || DEFAULT_CAPTURE_BACKGROUND
1212
+ );
1213
+ try {
1214
+ await new Promise(
1215
+ (resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))
1216
+ );
1217
+ const cloneRect = captureClone.clone.getBoundingClientRect();
1218
+ console.log(
1219
+ `[WebSpatial] Visible clone ready: rect=(${cloneRect.x.toFixed(0)},${cloneRect.y.toFixed(0)},${cloneRect.width.toFixed(0)},${cloneRect.height.toFixed(0)})`
1220
+ );
1221
+ canvas = await html2canvas(captureClone.clone, {
1222
+ backgroundColor,
1223
+ logging: true,
1224
+ // Enable logging to debug
1225
+ scale: Math.min(scale, 1.5),
1226
+ useCORS: true,
1227
+ allowTaint: true,
1228
+ imageTimeout: 5e3,
1229
+ removeContainer: true,
1230
+ foreignObjectRendering: false
1231
+ });
1232
+ } finally {
1233
+ restoreBackground();
1234
+ captureClone.cleanup();
1235
+ }
1236
+ const ctx = canvas.getContext("2d");
1237
+ if (ctx) {
1238
+ const xPositions = [50, Math.floor(canvas.width / 4), Math.floor(canvas.width / 2), Math.floor(canvas.width * 3 / 4)];
1239
+ const yPositions = [50, 100, 200, 400, 600, 800, 1e3, 1200, 1400];
1240
+ const samples = [];
1241
+ yPositions.forEach((y) => {
1242
+ if (y < canvas.height) {
1243
+ xPositions.forEach((x) => {
1244
+ if (x < canvas.width) {
1245
+ samples.push({ name: `(${x},${y})`, x, y });
1246
+ }
1247
+ });
1248
+ }
1249
+ });
1250
+ console.log(`[WebSpatial] Canvas size: ${canvas.width}x${canvas.height}, scale=${scale}`);
1251
+ let bgCount = 0;
1252
+ let contentCount = 0;
1253
+ let contentPixels = [];
1254
+ samples.forEach((s) => {
1255
+ const pixel = ctx.getImageData(s.x, s.y, 1, 1).data;
1256
+ const isBackground = pixel[0] === 26 && pixel[1] === 26 && pixel[2] === 46;
1257
+ if (isBackground) {
1258
+ bgCount++;
1259
+ } else {
1260
+ contentCount++;
1261
+ contentPixels.push(`${s.name}=rgba(${pixel[0]},${pixel[1]},${pixel[2]})`);
1262
+ }
1263
+ });
1264
+ console.log(`[WebSpatial] Grid sample: ${bgCount} BG, ${contentCount} CONTENT`);
1265
+ if (contentPixels.length > 0) {
1266
+ console.log(`[WebSpatial] Content pixels: ${contentPixels.slice(0, 10).join(", ")}`);
1267
+ }
1268
+ if (contentCount === 0) {
1269
+ console.log(`[WebSpatial] No content in grid sample - scanning center column...`);
1270
+ for (let y = 0; y < canvas.height; y += 30) {
1271
+ const pixel = ctx.getImageData(Math.floor(canvas.width / 2), y, 1, 1).data;
1272
+ const isBackground = pixel[0] === 26 && pixel[1] === 26 && pixel[2] === 46;
1273
+ if (!isBackground) {
1274
+ console.log(`[WebSpatial] First content at Y=${y}: rgba(${pixel[0]},${pixel[1]},${pixel[2]})`);
1275
+ break;
1276
+ }
1277
+ }
1278
+ }
1279
+ }
1280
+ canvas = resizeCanvasIfNeeded(canvas);
1281
+ const dataUrl = canvas.toDataURL("image/webp", 0.85);
1282
+ return dataUrl;
1283
+ } catch (error) {
1284
+ console.error("[WebSpatial] html2canvas capture failed:", error);
1285
+ return null;
1286
+ }
1287
+ }
1288
+ async function captureElementBitmap(element, options) {
1289
+ if (!usesAndroidBitmapCapture()) {
1290
+ return null;
1291
+ }
1292
+ const scale = options?.scale ?? (window.devicePixelRatio || 1);
1293
+ const startTime = performance.now();
1294
+ if (options?.waitForImages !== false) {
1295
+ await waitForContent(element, 500);
1296
+ }
1297
+ let result = null;
1298
+ const html2canvas = await loadHtml2Canvas();
1299
+ if (html2canvas) {
1300
+ console.log("[WebSpatial] Using html2canvas (primary)");
1301
+ result = await captureWithHtml2Canvas(
1302
+ html2canvas,
1303
+ element,
1304
+ scale,
1305
+ options?.backgroundColor ?? DEFAULT_CAPTURE_BACKGROUND
1306
+ );
1307
+ if (result) {
1308
+ const elapsed = Math.round(performance.now() - startTime);
1309
+ console.log(`[WebSpatial] Capture complete (html2canvas, ${elapsed}ms)`);
1310
+ return result;
1311
+ }
1312
+ }
1313
+ const snapdom = await loadSnapdom();
1314
+ if (snapdom) {
1315
+ console.log("[WebSpatial] Falling back to snapdom");
1316
+ result = await captureWithSnapdom(snapdom, element, scale);
1317
+ if (result) {
1318
+ const elapsed = Math.round(performance.now() - startTime);
1319
+ console.log(`[WebSpatial] Capture complete (snapdom, ${elapsed}ms)`);
1320
+ return result;
1321
+ }
1322
+ }
1323
+ console.error("[WebSpatial] No capture library available");
1324
+ return null;
1325
+ }
1326
+ function observeContentChanges(element, onContentChange) {
1327
+ const observer = new MutationObserver((_mutations) => {
1328
+ onContentChange();
1329
+ });
1330
+ observer.observe(element, {
1331
+ childList: true,
1332
+ subtree: true,
1333
+ characterData: true,
1334
+ attributes: true,
1335
+ attributeFilter: ["class", "style"]
1336
+ });
1337
+ let resizeObserver = null;
1338
+ if (typeof ResizeObserver !== "undefined") {
1339
+ resizeObserver = new ResizeObserver((_entries) => {
1340
+ onContentChange();
1341
+ });
1342
+ resizeObserver.observe(element);
1343
+ }
1344
+ return () => {
1345
+ observer.disconnect();
1346
+ resizeObserver?.disconnect();
1347
+ };
1348
+ }
1349
+
1350
+ // src/utils/BitmapCaptureCoordinator.ts
1351
+ var BitmapCaptureCoordinatorClass = class {
1352
+ // Track completed captures by element UUID
1353
+ capturedElements = /* @__PURE__ */ new Set();
1354
+ // Track in-flight capture requests
1355
+ pendingCaptures = /* @__PURE__ */ new Map();
1356
+ // Minimum time between recaptures of the same element (ms)
1357
+ recaptureThrottleMs = 750;
1358
+ // Capture queue for serialized processing (prevents thread contention)
1359
+ captureQueue = [];
1360
+ isProcessingQueue = false;
1361
+ /**
1362
+ * Request a bitmap capture for an element.
1363
+ * Returns null immediately if the element has already been captured.
1364
+ * Deduplicates concurrent requests for the same element.
1365
+ * Captures are serialized to prevent thread contention.
1366
+ *
1367
+ * @param elementId Unique element ID (UUID, not spatialId)
1368
+ * @param dom The DOM element to capture
1369
+ * @returns Promise resolving to bitmap data URL, or null if already captured
1370
+ */
1371
+ async requestCapture(elementId, dom) {
1372
+ if (this.capturedElements.has(elementId)) {
1373
+ console.log(`[WebSpatial] Skipping capture for ${elementId} (already captured)`);
1374
+ return null;
1375
+ }
1376
+ const pending = this.pendingCaptures.get(elementId);
1377
+ if (pending) {
1378
+ console.log(`[WebSpatial] Joining existing capture for ${elementId}`);
1379
+ return pending.promise;
1380
+ }
1381
+ this.capturedElements.add(elementId);
1382
+ const promise = new Promise((resolve, reject) => {
1383
+ this.captureQueue.push({ elementId, dom, resolve, reject });
1384
+ console.log(`[WebSpatial] Queued capture for ${elementId} (queue size: ${this.captureQueue.length})`);
1385
+ });
1386
+ this.pendingCaptures.set(elementId, {
1387
+ promise,
1388
+ timestamp: Date.now()
1389
+ });
1390
+ promise.finally(() => {
1391
+ this.pendingCaptures.delete(elementId);
1392
+ });
1393
+ this.processQueue();
1394
+ return promise;
1395
+ }
1396
+ /**
1397
+ * Process the capture queue one at a time.
1398
+ * This prevents thread contention and ensures consistent capture performance.
1399
+ */
1400
+ async processQueue() {
1401
+ if (this.isProcessingQueue) return;
1402
+ this.isProcessingQueue = true;
1403
+ while (this.captureQueue.length > 0) {
1404
+ const item = this.captureQueue.shift();
1405
+ const { elementId, dom, resolve, reject } = item;
1406
+ try {
1407
+ console.log(`[WebSpatial] Processing capture for ${elementId} (${this.captureQueue.length} remaining)`);
1408
+ const bitmap = await this.doCapture(elementId, dom);
1409
+ resolve(bitmap);
1410
+ } catch (error) {
1411
+ reject(error);
1412
+ }
1413
+ await new Promise((r) => setTimeout(r, 50));
1414
+ }
1415
+ this.isProcessingQueue = false;
1416
+ }
1417
+ /**
1418
+ * Force a recapture of an element (e.g., after content change).
1419
+ * Respects throttling to prevent excessive recaptures.
1420
+ */
1421
+ async requestRecapture(elementId, dom) {
1422
+ const pending = this.pendingCaptures.get(elementId);
1423
+ if (pending && Date.now() - pending.timestamp < this.recaptureThrottleMs) {
1424
+ console.log(`[WebSpatial] Throttling recapture for ${elementId}`);
1425
+ return pending.promise;
1426
+ }
1427
+ this.capturedElements.delete(elementId);
1428
+ return this.requestCapture(elementId, dom);
1429
+ }
1430
+ /**
1431
+ * Perform the actual capture.
1432
+ */
1433
+ async doCapture(elementId, dom) {
1434
+ console.log(`[WebSpatial] Starting capture for ${elementId}`);
1435
+ this.logElementDiagnostics(elementId, dom);
1436
+ try {
1437
+ const bitmap = await captureElementBitmap(dom);
1438
+ if (bitmap) {
1439
+ const sizeKB = Math.round(bitmap.length / 1024);
1440
+ console.log(`[WebSpatial] Capture complete for ${elementId} (${sizeKB}KB)`);
1441
+ } else {
1442
+ console.log(`[WebSpatial] Capture returned null for ${elementId}`);
1443
+ }
1444
+ return bitmap;
1445
+ } catch (error) {
1446
+ console.error(`[WebSpatial] Capture failed for ${elementId}:`, error);
1447
+ this.capturedElements.delete(elementId);
1448
+ return null;
1449
+ }
1450
+ }
1451
+ /**
1452
+ * Clear the capture state for an element (e.g., when destroyed).
1453
+ */
1454
+ clearElement(elementId) {
1455
+ this.capturedElements.delete(elementId);
1456
+ this.pendingCaptures.delete(elementId);
1457
+ }
1458
+ /**
1459
+ * Clear all capture state (e.g., on page navigation).
1460
+ */
1461
+ clearAll() {
1462
+ this.capturedElements.clear();
1463
+ this.pendingCaptures.clear();
1464
+ }
1465
+ /**
1466
+ * Check if an element has been captured.
1467
+ */
1468
+ hasCaptured(elementId) {
1469
+ return this.capturedElements.has(elementId);
1470
+ }
1471
+ /**
1472
+ * Log detailed diagnostics about an element before capture.
1473
+ * This helps debug issues like content being shifted or hidden.
1474
+ */
1475
+ logElementDiagnostics(elementId, dom) {
1476
+ const rect = dom.getBoundingClientRect();
1477
+ const style = window.getComputedStyle(dom);
1478
+ console.log(`[WebSpatial] === CAPTURE DIAGNOSTICS: ${elementId} ===`);
1479
+ console.log(`[WebSpatial] Element: ${dom.tagName}.${dom.className}`);
1480
+ console.log(`[WebSpatial] BoundingRect: (${rect.x.toFixed(0)}, ${rect.y.toFixed(0)}, ${rect.width.toFixed(0)}, ${rect.height.toFixed(0)})`);
1481
+ console.log(`[WebSpatial] Position: ${style.position}, Top: ${style.top}, Left: ${style.left}`);
1482
+ console.log(`[WebSpatial] Padding: ${style.paddingTop} / ${style.paddingRight} / ${style.paddingBottom} / ${style.paddingLeft}`);
1483
+ console.log(`[WebSpatial] Margin: ${style.marginTop} / ${style.marginRight} / ${style.marginBottom} / ${style.marginLeft}`);
1484
+ console.log(`[WebSpatial] Visibility: ${style.visibility}, Display: ${style.display}`);
1485
+ console.log(`[WebSpatial] Overflow: ${style.overflow}, OverflowY: ${style.overflowY}`);
1486
+ const parent = dom.parentElement;
1487
+ if (parent) {
1488
+ const parentStyle = window.getComputedStyle(parent);
1489
+ const parentRect = parent.getBoundingClientRect();
1490
+ console.log(`[WebSpatial] Parent: ${parent.tagName}.${parent.className}`);
1491
+ console.log(`[WebSpatial] Parent BoundingRect: (${parentRect.x.toFixed(0)}, ${parentRect.y.toFixed(0)}, ${parentRect.width.toFixed(0)}, ${parentRect.height.toFixed(0)})`);
1492
+ console.log(`[WebSpatial] Parent Position: ${parentStyle.position}, Top: ${parentStyle.top}`);
1493
+ }
1494
+ const children = dom.children;
1495
+ console.log(`[WebSpatial] Children count: ${children.length}`);
1496
+ for (let i = 0; i < Math.min(5, children.length); i++) {
1497
+ const child = children[i];
1498
+ const childRect = child.getBoundingClientRect();
1499
+ const childStyle = window.getComputedStyle(child);
1500
+ console.log(`[WebSpatial] Child ${i}: ${child.tagName}.${child.className?.substring(0, 30)}...`);
1501
+ console.log(`[WebSpatial] Rect: (${childRect.x.toFixed(0)}, ${childRect.y.toFixed(0)}, ${childRect.width.toFixed(0)}, ${childRect.height.toFixed(0)})`);
1502
+ console.log(`[WebSpatial] Position: ${childStyle.position}, Visibility: ${childStyle.visibility}`);
1503
+ }
1504
+ console.log(`[WebSpatial] === END DIAGNOSTICS ===`);
1505
+ }
1506
+ };
1507
+ var BitmapCaptureCoordinator = new BitmapCaptureCoordinatorClass();
1508
+
1509
+ // src/spatialized-container/transform-utils.ts
1510
+ function toSceneSpatial(point, spatializedElement) {
1511
+ return spatializedElement.__toSceneSpace(point);
1512
+ }
1513
+ function toLocalSpace(point, spatializedElement) {
1514
+ return spatializedElement.__toLocalSpace(point);
1515
+ }
1516
+ function convertDOMRectToSceneSpace(originalRect, matrix) {
1517
+ const topLeft = new DOMPoint(originalRect.left, originalRect.top);
1518
+ const topRight = new DOMPoint(originalRect.right, originalRect.top);
1519
+ const bottomRight = new DOMPoint(originalRect.right, originalRect.bottom);
1520
+ const bottomLeft = new DOMPoint(originalRect.left, originalRect.bottom);
1521
+ const transformedTopLeft = matrix.transformPoint(topLeft);
1522
+ const transformedTopRight = matrix.transformPoint(topRight);
1523
+ const transformedBottomRight = matrix.transformPoint(bottomRight);
1524
+ const transformedBottomLeft = matrix.transformPoint(bottomLeft);
1525
+ const allPoints = [
1526
+ transformedTopLeft,
1527
+ transformedTopRight,
1528
+ transformedBottomRight,
1529
+ transformedBottomLeft
1530
+ ];
1531
+ const xCoords = allPoints.map((point) => point.x);
1532
+ const yCoords = allPoints.map((point) => point.y);
1533
+ const newMinX = Math.min(...xCoords);
1534
+ const newMaxX = Math.max(...xCoords);
1535
+ const newMinY = Math.min(...yCoords);
1536
+ const newMaxY = Math.max(...yCoords);
1537
+ return new DOMRect(newMinX, newMinY, newMaxX - newMinX, newMaxY - newMinY);
1538
+ }
1539
+
1540
+ // src/spatialized-container/context/PortalInstanceContext.ts
1541
+ var PortalInstanceObject = class {
1542
+ spatialId;
1543
+ spatializedContainerObject;
1544
+ parentPortalInstanceObject;
1545
+ spatializedElement;
1546
+ // cachedDomInfo used for cache dom info
1547
+ // when dom is updated, this property should be updated as well
1548
+ cachedDomInfo;
1549
+ get dom() {
1550
+ return this.cachedDomInfo?.dom;
1551
+ }
1552
+ get computedStyle() {
1553
+ return this.cachedDomInfo?.computedStyle;
1554
+ }
1555
+ get isFixedPosition() {
1556
+ return this.cachedDomInfo?.isFixedPosition;
1557
+ }
1558
+ // cachedDomRect used for cache dom rect
1559
+ cachedDomRect;
1560
+ get domRect() {
1561
+ return this.cachedDomRect;
1562
+ }
1563
+ // cachedTransformVisibilityInfo used for cache transform visibility info
1564
+ cachedTransformVisibilityInfo;
1565
+ get transformMatrix() {
1566
+ return this.cachedTransformVisibilityInfo?.transformMatrix;
1567
+ }
1568
+ get visibility() {
1569
+ return this.cachedTransformVisibilityInfo?.visibility;
1570
+ }
1571
+ // spatializedElementPromise used for get spatialized element
1572
+ // SpatializedElement is when attachSpatializedElement is called
1573
+ spatializedElementPromise;
1574
+ spatializedElementResolver;
1575
+ // used for get extra spatialized element properties
1576
+ getExtraSpatializedElementProperties;
1577
+ // Bitmap capture state for Android
1578
+ pendingBitmapCapture = null;
1579
+ // Initial delay before first capture (0ms = start immediately, content detection handles fonts/images)
1580
+ bitmapCaptureInitialDelayMs = 0;
1581
+ // Track if capture has been requested via coordinator
1582
+ captureRequested = false;
1583
+ observedContentDom = null;
1584
+ stopObservingContentChanges = null;
1585
+ constructor(spatialId, spatializedContainerObject, parentPortalInstanceObject, getExtraSpatializedElementProperties2) {
1586
+ this.spatialId = spatialId;
1587
+ this.spatializedContainerObject = spatializedContainerObject;
1588
+ this.parentPortalInstanceObject = parentPortalInstanceObject;
1589
+ this.getExtraSpatializedElementProperties = getExtraSpatializedElementProperties2;
1590
+ this.spatializedElementPromise = new Promise(
1591
+ (resolve) => {
1592
+ this.spatializedElementResolver = resolve;
1593
+ }
1594
+ );
1595
+ }
1596
+ // called when PortalSpatializedContainer is mounted
1597
+ init() {
1598
+ this.spatializedContainerObject.onSpatialTransformVisibilityChange(
1599
+ this.spatialId,
1600
+ this.onSpatialTransformVisibilityChange
1601
+ );
1602
+ }
1603
+ // called when PortalSpatializedContainer is unmounted
1604
+ destroy() {
1605
+ this.spatializedContainerObject.offSpatialTransformVisibilityChange(
1606
+ this.spatialId,
1607
+ this.onSpatialTransformVisibilityChange
1608
+ );
1609
+ if (this.pendingBitmapCapture) {
1610
+ clearTimeout(this.pendingBitmapCapture);
1611
+ this.pendingBitmapCapture = null;
1612
+ }
1613
+ if (this.spatializedElement) {
1614
+ BitmapCaptureCoordinator.clearElement(this.spatializedElement.id);
1615
+ }
1616
+ this.stopObservingContentChanges?.();
1617
+ this.observedContentDom = null;
1618
+ this.stopObservingContentChanges = null;
1619
+ }
1620
+ onSpatialTransformVisibilityChange = (spatialTransform) => {
1621
+ this.cachedTransformVisibilityInfo = {
1622
+ transformMatrix: new DOMMatrix(spatialTransform.transform),
1623
+ visibility: spatialTransform.visibility
1624
+ };
1625
+ this.updateSpatializedElementProperties();
1626
+ };
1627
+ // called when 2D frame change
1628
+ notify2DFrameChange() {
1629
+ const dom = this.spatializedContainerObject.querySpatialDomBySpatialId(
1630
+ this.spatialId
1631
+ );
1632
+ if (!dom) {
1633
+ return;
1634
+ }
1635
+ const computedStyle = getComputedStyle(dom);
1636
+ this.cachedDomInfo = {
1637
+ dom,
1638
+ computedStyle,
1639
+ isFixedPosition: computedStyle.getPropertyValue("position") === "fixed"
1640
+ };
1641
+ if (usesAndroidBitmapCapture()) {
1642
+ this.ensureContentObserver(dom);
1643
+ }
1644
+ this.updateSpatializedElementProperties();
1645
+ const __getBoundingClientCube = () => {
1646
+ return this.spatializedElement?.cubeInfo;
1647
+ };
1648
+ const __getBoundingClientRect = () => {
1649
+ if (!this.spatializedElement?.transform) {
1650
+ return null;
1651
+ }
1652
+ const domRect = new DOMRect(
1653
+ 0,
1654
+ 0,
1655
+ this.domRect?.width,
1656
+ this.domRect?.height
1657
+ );
1658
+ return convertDOMRectToSceneSpace(
1659
+ domRect,
1660
+ this.spatializedElement?.transform
1661
+ );
1662
+ };
1663
+ const __toSceneSpace = (point) => {
1664
+ return new DOMPoint(point.x, point.y, point.z).matrixTransform(
1665
+ this.spatializedElement?.transform
1666
+ );
1667
+ };
1668
+ const __toLocalSpace = (point) => {
1669
+ return new DOMPoint(point.x, point.y, point.z).matrixTransform(
1670
+ this.spatializedElement?.transformInv
1671
+ );
1672
+ };
1673
+ const __innerSpatializedElement = () => this.spatializedElement;
1674
+ Object.assign(dom, {
1675
+ __getBoundingClientCube,
1676
+ __getBoundingClientRect,
1677
+ __toSceneSpace,
1678
+ __toLocalSpace,
1679
+ __innerSpatializedElement
1680
+ });
1681
+ }
1682
+ ensureContentObserver(dom) {
1683
+ if (!usesAndroidBitmapCapture()) {
1684
+ return;
1685
+ }
1686
+ if (this.observedContentDom === dom && this.stopObservingContentChanges) {
1687
+ return;
1688
+ }
1689
+ this.stopObservingContentChanges?.();
1690
+ this.observedContentDom = dom;
1691
+ this.stopObservingContentChanges = observeContentChanges(dom, () => {
1692
+ this.scheduleBitmapCapture(true);
1693
+ });
1694
+ }
1695
+ async getSpatializedElement() {
1696
+ return this.spatializedElementPromise;
1697
+ }
1698
+ // called when SpatializedElement is created
1699
+ attachSpatializedElement(spatializedElement) {
1700
+ this.spatializedElement = spatializedElement;
1701
+ this.addToParent(spatializedElement);
1702
+ this.spatializedElementResolver?.(spatializedElement);
1703
+ this.updateSpatializedElementProperties();
1704
+ }
1705
+ inAddingToParent = false;
1706
+ async addToParent(spatializedElement) {
1707
+ if (this.inAddingToParent) {
1708
+ return;
1709
+ }
1710
+ this.inAddingToParent = true;
1711
+ if (this.isFixedPosition || !this.parentPortalInstanceObject) {
1712
+ var spatialScene = await getSession().getSpatialScene();
1713
+ await spatialScene.addSpatializedElement(spatializedElement);
1714
+ } else {
1715
+ const parentSpatialized2DElement = await this.parentPortalInstanceObject.getSpatializedElement();
1716
+ parentSpatialized2DElement.addSpatializedElement(spatializedElement);
1717
+ }
1718
+ this.inAddingToParent = false;
1719
+ }
1720
+ /**
1721
+ * Captures the DOM element as a bitmap for Android XR rendering.
1722
+ * Uses BitmapCaptureCoordinator to prevent duplicate captures across instances.
1723
+ * The initial capture is delayed to allow images to load.
1724
+ */
1725
+ scheduleBitmapCapture(forceRecapture = false) {
1726
+ if (!usesAndroidBitmapCapture()) return;
1727
+ if (!this.dom || !this.spatializedElement) return;
1728
+ const elementId = this.spatializedElement.id;
1729
+ if (this.captureRequested) {
1730
+ return;
1731
+ }
1732
+ this.captureRequested = true;
1733
+ if (this.pendingBitmapCapture) {
1734
+ clearTimeout(this.pendingBitmapCapture);
1735
+ }
1736
+ console.log(
1737
+ `[WebSpatial] Scheduling capture for: ${elementId} (in ${this.bitmapCaptureInitialDelayMs}ms)`
1738
+ );
1739
+ this.pendingBitmapCapture = setTimeout(async () => {
1740
+ this.pendingBitmapCapture = null;
1741
+ if (!this.dom || !this.spatializedElement) {
1742
+ console.log(`[WebSpatial] Capture cancelled - element gone: ${elementId}`);
1743
+ return;
1744
+ }
1745
+ try {
1746
+ const captureStyleId = "__webspatial_capture_style__";
1747
+ let captureStyle = document.getElementById(captureStyleId);
1748
+ if (!captureStyle) {
1749
+ captureStyle = document.createElement("style");
1750
+ captureStyle.id = captureStyleId;
1751
+ document.head.appendChild(captureStyle);
1752
+ }
1753
+ captureStyle.textContent = `
1754
+ .xr-spatial-default,
1755
+ [enable-xr],
1756
+ .xr-spatial-default * {
1757
+ visibility: visible !important;
1758
+ }
1759
+ `;
1760
+ const originalVisibility = this.dom.style.visibility;
1761
+ const originalCssText = this.dom.style.cssText;
1762
+ this.dom.style.setProperty("visibility", "visible", "important");
1763
+ const nestedSpatialElements = this.dom.querySelectorAll(".xr-spatial-default");
1764
+ const nestedOriginalVisibilities = [];
1765
+ nestedSpatialElements.forEach((el) => {
1766
+ const htmlEl = el;
1767
+ nestedOriginalVisibilities.push({
1768
+ element: htmlEl,
1769
+ visibility: htmlEl.style.visibility,
1770
+ cssText: htmlEl.style.cssText
1771
+ });
1772
+ htmlEl.style.setProperty("visibility", "visible", "important");
1773
+ });
1774
+ const fixedElements = [];
1775
+ this.dom.querySelectorAll("*").forEach((el) => {
1776
+ const htmlEl = el;
1777
+ const style = window.getComputedStyle(htmlEl);
1778
+ if (style.position === "fixed") {
1779
+ fixedElements.push({
1780
+ element: htmlEl,
1781
+ display: htmlEl.style.display
1782
+ });
1783
+ htmlEl.style.display = "none";
1784
+ }
1785
+ });
1786
+ console.log(
1787
+ `[WebSpatial] Capturing ${elementId} with ${nestedSpatialElements.length} nested spatial elements made visible, ${fixedElements.length} fixed elements hidden`
1788
+ );
1789
+ const bitmap = forceRecapture || BitmapCaptureCoordinator.hasCaptured(elementId) ? await BitmapCaptureCoordinator.requestRecapture(
1790
+ elementId,
1791
+ this.dom
1792
+ ) : await BitmapCaptureCoordinator.requestCapture(
1793
+ elementId,
1794
+ this.dom
1795
+ );
1796
+ const captureStyleToRemove = document.getElementById("__webspatial_capture_style__");
1797
+ if (captureStyleToRemove) {
1798
+ captureStyleToRemove.textContent = "";
1799
+ }
1800
+ this.dom.style.cssText = originalCssText;
1801
+ if (originalVisibility) {
1802
+ this.dom.style.visibility = originalVisibility;
1803
+ }
1804
+ nestedOriginalVisibilities.forEach(({ element, visibility, cssText }) => {
1805
+ element.style.cssText = cssText;
1806
+ if (visibility) {
1807
+ element.style.visibility = visibility;
1808
+ }
1809
+ });
1810
+ fixedElements.forEach(({ element, display }) => {
1811
+ element.style.display = display;
1812
+ });
1813
+ if (bitmap) {
1814
+ this.spatializedElement.updateProperties({ bitmap });
1815
+ }
1816
+ } catch (error) {
1817
+ console.error(`[WebSpatial] Capture failed: ${elementId}`, error);
1818
+ } finally {
1819
+ this.captureRequested = false;
1820
+ }
1821
+ }, this.bitmapCaptureInitialDelayMs);
1822
+ }
1823
+ updateSpatializedElementProperties() {
1824
+ const dom = this.dom;
1825
+ const spatializedElement = this.spatializedElement;
1826
+ const visibility = this.visibility;
1827
+ if (!dom || !spatializedElement || !visibility || !this.transformMatrix) {
1828
+ return;
1829
+ }
1830
+ const computedStyle = this.computedStyle;
1831
+ const isFixedPosition = this.isFixedPosition;
1832
+ let domRect = dom.getBoundingClientRect();
1833
+ let { x, y } = domRect;
1834
+ if (!isFixedPosition) {
1835
+ const parentDom = this.spatializedContainerObject.queryParentSpatialDomBySpatialId(
1836
+ this.spatialId
1837
+ );
1838
+ if (parentDom) {
1839
+ const parentDomRect = parentDom.getBoundingClientRect();
1840
+ x -= parentDomRect.x;
1841
+ y -= parentDomRect.y;
1842
+ } else {
1843
+ x += window.scrollX;
1844
+ y += window.scrollY;
1845
+ }
1846
+ }
1847
+ this.cachedDomRect = {
1848
+ x: domRect.x,
1849
+ y: domRect.y,
1850
+ width: domRect.width,
1851
+ height: domRect.height
1852
+ };
1853
+ const width = domRect.width;
1854
+ const height = domRect.height;
1855
+ const opacity = parseFloat(computedStyle.getPropertyValue("opacity"));
1856
+ const scrollWithParent = !isFixedPosition;
1857
+ const display = computedStyle.getPropertyValue("display");
1858
+ const visible = visibility === "visible" && display !== "none";
1859
+ const zIndex = parseFloat(
1860
+ computedStyle.getPropertyValue(SpatialCustomStyleVars.xrZIndex)
1861
+ ) || 0;
1862
+ const backOffset = parseFloat(computedStyle.getPropertyValue(SpatialCustomStyleVars.back)) || 0;
1863
+ const depth = parseFloat(
1864
+ computedStyle.getPropertyValue(SpatialCustomStyleVars.depth)
1865
+ ) || 0;
1866
+ const rotationAnchor = parseTransformOrigin(computedStyle);
1867
+ const extraProperties = this.getExtraSpatializedElementProperties?.(computedStyle) || {};
1868
+ spatializedElement.updateProperties({
1869
+ clientX: x,
1870
+ clientY: y,
1871
+ width,
1872
+ height,
1873
+ depth,
1874
+ opacity,
1875
+ scrollWithParent,
1876
+ zIndex,
1877
+ visible,
1878
+ backOffset,
1879
+ rotationAnchor,
1880
+ ...extraProperties
1881
+ });
1882
+ spatializedElement.updateTransform(this.transformMatrix);
1883
+ Object.assign(this.dom, {
1884
+ __spatializedElement: spatializedElement
1885
+ });
1886
+ if (usesAndroidBitmapCapture()) {
1887
+ this.scheduleBitmapCapture();
1888
+ }
1889
+ }
1890
+ };
1891
+ var PortalInstanceContext = createContext3(
1892
+ null
1893
+ );
1894
+
1895
+ // src/spatialized-container/hooks/useSync2DFrame.ts
1896
+ import { useEffect as useEffect5, useState as useState2 } from "react";
1897
+ function useForceUpdate() {
1898
+ const [, setToggle] = useState2(false);
1899
+ return () => setToggle((toggle) => !toggle);
1900
+ }
1901
+ function useSync2DFrame(spatialId, portalInstanceObject, spatializedContainerObject) {
1902
+ const forceUpdate = useForceUpdate();
1903
+ useEffect5(() => {
1904
+ spatializedContainerObject.on2DFrameChange(spatialId, () => {
1905
+ portalInstanceObject.notify2DFrameChange();
1906
+ forceUpdate();
1907
+ });
1908
+ return () => {
1909
+ spatializedContainerObject.off2DFrameChange(spatialId);
1910
+ };
1911
+ }, []);
1912
+ }
1913
+
1914
+ // src/spatialized-container/hooks/useSpatializedElement.ts
1915
+ import { useEffect as useEffect6, useState as useState3 } from "react";
1916
+ function useSpatializedElement(createSpatializedElement2, portalInstanceObject) {
1917
+ const [spatializedElement, setSpatializedElement] = useState3();
1918
+ useEffect6(() => {
1919
+ let isDestroyed = false;
1920
+ let spatializedElement2;
1921
+ createSpatializedElement2().then(
1922
+ (inSpatializedElement) => {
1923
+ if (!isDestroyed) {
1924
+ spatializedElement2 = inSpatializedElement;
1925
+ portalInstanceObject.attachSpatializedElement(spatializedElement2);
1926
+ setSpatializedElement(spatializedElement2);
1927
+ } else {
1928
+ inSpatializedElement?.destroy();
1929
+ }
1930
+ }
1931
+ );
1932
+ return () => {
1933
+ isDestroyed = true;
1934
+ if (spatializedElement2) {
1935
+ spatializedElement2.destroy();
1936
+ spatializedElement2 = void 0;
1937
+ }
1938
+ };
1939
+ }, [createSpatializedElement2, portalInstanceObject]);
1940
+ return spatializedElement;
1941
+ }
1942
+
1943
+ // src/spatialized-container/PortalSpatializedContainer.tsx
1944
+ import { Fragment, jsx as jsx3, jsxs } from "react/jsx-runtime";
1945
+ function renderPlaceholderInSubPortal(portalInstanceObject, El) {
1946
+ const spatialId = portalInstanceObject.spatialId;
1947
+ const inPortalInstanceEnv = !!portalInstanceObject.parentPortalInstanceObject;
1948
+ const position = portalInstanceObject.computedStyle?.getPropertyValue("position");
1949
+ const shouldRenderPlaceHolder = inPortalInstanceEnv && portalInstanceObject && portalInstanceObject.domRect && position !== "absolute" && position !== "fixed";
1950
+ if (!shouldRenderPlaceHolder) {
1951
+ return /* @__PURE__ */ jsx3(Fragment, {});
1952
+ }
1953
+ const { width, height } = portalInstanceObject.domRect;
1954
+ const display = portalInstanceObject.computedStyle.getPropertyPriority("display");
1955
+ const spatialIdProps = { [SpatialID]: spatialId };
1956
+ return /* @__PURE__ */ jsx3(
1957
+ El,
1958
+ {
1959
+ ...spatialIdProps,
1960
+ style: {
1961
+ position: "relative",
1962
+ width: `${width}px`,
1963
+ height: `${height}px`,
1964
+ visibility: "hidden",
1965
+ display
1966
+ }
1967
+ }
1968
+ );
1969
+ }
1970
+ function PortalSpatializedContainer(props) {
1971
+ const {
1972
+ spatializedContent: Content,
1973
+ createSpatializedElement: createSpatializedElement2,
1974
+ getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
1975
+ onSpatialTap,
1976
+ onSpatialDragStart,
1977
+ onSpatialDrag,
1978
+ onSpatialDragEnd,
1979
+ onSpatialRotate,
1980
+ onSpatialRotateEnd,
1981
+ onSpatialMagnify,
1982
+ onSpatialMagnifyEnd,
1983
+ [SpatialID]: spatialId,
1984
+ ...restProps
1985
+ } = props;
1986
+ const spatializedContainerObject = useContext4(
1987
+ SpatializedContainerContext
1988
+ );
1989
+ const parentPortalInstanceObject = useContext4(PortalInstanceContext);
1990
+ const portalInstanceObject = useMemo(
1991
+ () => new PortalInstanceObject(
1992
+ spatialId,
1993
+ spatializedContainerObject,
1994
+ parentPortalInstanceObject,
1995
+ getExtraSpatializedElementProperties2
1996
+ ),
1997
+ []
1998
+ );
1999
+ useEffect7(() => {
2000
+ portalInstanceObject.init();
2001
+ return () => {
2002
+ portalInstanceObject.destroy();
2003
+ };
2004
+ }, []);
2005
+ useSync2DFrame(spatialId, portalInstanceObject, spatializedContainerObject);
2006
+ const spatializedElement = useSpatializedElement(
2007
+ createSpatializedElement2,
2008
+ portalInstanceObject
2009
+ );
2010
+ const PlaceholderEl = renderPlaceholderInSubPortal(
2011
+ portalInstanceObject,
2012
+ props.component
2013
+ );
2014
+ useEffect7(() => {
2015
+ if (spatializedElement) {
2016
+ spatializedElement.onSpatialTap = onSpatialTap;
2017
+ }
2018
+ }, [spatializedElement, onSpatialTap]);
2019
+ useEffect7(() => {
2020
+ if (spatializedElement) {
2021
+ spatializedElement.onSpatialDrag = onSpatialDrag;
2022
+ }
2023
+ }, [spatializedElement, onSpatialDrag]);
2024
+ useEffect7(() => {
2025
+ if (spatializedElement) {
2026
+ spatializedElement.onSpatialDragEnd = onSpatialDragEnd;
2027
+ }
2028
+ }, [spatializedElement, onSpatialDragEnd]);
2029
+ useEffect7(() => {
2030
+ if (spatializedElement) {
2031
+ spatializedElement.onSpatialRotate = onSpatialRotate;
2032
+ }
2033
+ }, [spatializedElement, onSpatialRotate]);
2034
+ useEffect7(() => {
2035
+ if (spatializedElement) {
2036
+ spatializedElement.onSpatialRotateEnd = onSpatialRotateEnd;
2037
+ }
2038
+ }, [spatializedElement, onSpatialRotateEnd]);
2039
+ useEffect7(() => {
2040
+ if (spatializedElement) {
2041
+ spatializedElement.onSpatialMagnify = onSpatialMagnify;
2042
+ }
2043
+ }, [spatializedElement, onSpatialMagnify]);
2044
+ useEffect7(() => {
2045
+ if (spatializedElement) {
2046
+ spatializedElement.onSpatialMagnifyEnd = onSpatialMagnifyEnd;
2047
+ }
2048
+ }, [spatializedElement, onSpatialMagnifyEnd]);
2049
+ useEffect7(() => {
2050
+ if (spatializedElement) {
2051
+ spatializedElement.onSpatialDragStart = onSpatialDragStart;
2052
+ }
2053
+ }, [spatializedElement, onSpatialDragStart]);
2054
+ return /* @__PURE__ */ jsxs(PortalInstanceContext.Provider, { value: portalInstanceObject, children: [
2055
+ spatializedElement && portalInstanceObject.dom && /* @__PURE__ */ jsx3(Content, { spatializedElement, ...restProps }),
2056
+ PlaceholderEl
2057
+ ] });
2058
+ }
2059
+
2060
+ // src/reality/context/InsideAttachmentContext.tsx
2061
+ import { createContext as createContext4, useContext as useContext5 } from "react";
2062
+ var InsideAttachmentContext = createContext4(false);
2063
+ var useInsideAttachment = () => useContext5(InsideAttachmentContext);
2064
+
2065
+ // src/spatialized-container/hooks/useSpatialEvents.ts
2066
+ function createEventProxy(event, currentTargetGetter, offsetXGetter, offsetYGetter, offsetZGetter, clientXGetter, clientYGetter, clientZGetter, translationXGetter, translationYGetter, translationZGetter, quaternionGetter, magnificationGetter) {
2067
+ return new Proxy(event, {
2068
+ get(target, prop) {
2069
+ if (prop === "currentTarget") {
2070
+ return currentTargetGetter();
2071
+ }
2072
+ if (prop === "isTrusted") {
2073
+ return true;
2074
+ }
2075
+ if (prop === "bubbles") {
2076
+ return false;
2077
+ }
2078
+ if (prop === "offsetX" && offsetXGetter) {
2079
+ return offsetXGetter(target) ?? 0;
2080
+ }
2081
+ if (prop === "offsetY" && offsetYGetter) {
2082
+ return offsetYGetter(target) ?? 0;
2083
+ }
2084
+ if (prop === "offsetZ" && offsetZGetter) {
2085
+ return offsetZGetter(target) ?? 0;
2086
+ }
2087
+ if (prop === "clientX" && clientXGetter) {
2088
+ return clientXGetter(target) ?? 0;
2089
+ }
2090
+ if (prop === "clientY" && clientYGetter) {
2091
+ return clientYGetter(target) ?? 0;
2092
+ }
2093
+ if (prop === "clientZ" && clientZGetter) {
2094
+ return clientZGetter(target) ?? 0;
2095
+ }
2096
+ if (prop === "translationX" && translationXGetter) {
2097
+ return translationXGetter(target) ?? 0;
2098
+ }
2099
+ if (prop === "translationY" && translationYGetter) {
2100
+ return translationYGetter(target) ?? 0;
2101
+ }
2102
+ if (prop === "translationZ" && translationZGetter) {
2103
+ return translationZGetter(target) ?? 0;
2104
+ }
2105
+ if (prop === "quaternion" && quaternionGetter) {
2106
+ return quaternionGetter(target) ?? { x: 0, y: 0, z: 0, w: 1 };
2107
+ }
2108
+ if (prop === "magnification" && magnificationGetter) {
2109
+ return magnificationGetter(target) ?? 1;
2110
+ }
2111
+ return Reflect.get(target, prop);
2112
+ }
2113
+ });
2114
+ }
2115
+ function createEventHandler(handler, currentTargetGetter, offsetXGetter, offsetYGetter, offsetZGetter, clientXGetter, clientYGetter, clientZGetter, translationXGetter, translationYGetter, translationZGetter, quaternionGetter, magnificationGetter) {
2116
+ return handler ? (event) => {
2117
+ const proxyEvent = createEventProxy(
2118
+ event,
2119
+ currentTargetGetter,
2120
+ offsetXGetter,
2121
+ offsetYGetter,
2122
+ offsetZGetter,
2123
+ clientXGetter,
2124
+ clientYGetter,
2125
+ clientZGetter,
2126
+ translationXGetter,
2127
+ translationYGetter,
2128
+ translationZGetter,
2129
+ quaternionGetter,
2130
+ magnificationGetter
2131
+ );
2132
+ handler(proxyEvent);
2133
+ } : void 0;
2134
+ }
2135
+ function useSpatialEventsBase(spatialEvents, currentTargetGetter) {
2136
+ const onSpatialTap = createEventHandler(
2137
+ spatialEvents.onSpatialTap,
2138
+ currentTargetGetter,
2139
+ // offsetX/Y/Z come from local coordinates
2140
+ (ev) => ev.detail?.location3D?.x,
2141
+ (ev) => ev.detail?.location3D?.y,
2142
+ (ev) => ev.detail?.location3D?.z,
2143
+ // clientX/Y/Z come from global scene coordinates
2144
+ (ev) => ev.detail?.globalLocation3D?.x,
2145
+ (ev) => ev.detail?.globalLocation3D?.y,
2146
+ (ev) => ev.detail?.globalLocation3D?.z
2147
+ );
2148
+ const onSpatialDrag = createEventHandler(
2149
+ spatialEvents.onSpatialDrag,
2150
+ currentTargetGetter,
2151
+ void 0,
2152
+ void 0,
2153
+ void 0,
2154
+ void 0,
2155
+ void 0,
2156
+ void 0,
2157
+ (ev) => ev.detail?.translation3D?.x,
2158
+ (ev) => ev.detail?.translation3D?.y,
2159
+ (ev) => ev.detail?.translation3D?.z
2160
+ );
2161
+ const onSpatialDragEnd = createEventHandler(
2162
+ spatialEvents.onSpatialDragEnd,
2163
+ currentTargetGetter
2164
+ );
2165
+ const onSpatialRotate = createEventHandler(
2166
+ spatialEvents.onSpatialRotate,
2167
+ currentTargetGetter,
2168
+ void 0,
2169
+ void 0,
2170
+ void 0,
2171
+ void 0,
2172
+ void 0,
2173
+ void 0,
2174
+ void 0,
2175
+ void 0,
2176
+ void 0,
2177
+ (ev) => ev.detail?.quaternion
2178
+ );
2179
+ const onSpatialRotateEnd = createEventHandler(
2180
+ spatialEvents.onSpatialRotateEnd,
2181
+ currentTargetGetter
2182
+ );
2183
+ const onSpatialMagnify = createEventHandler(
2184
+ spatialEvents.onSpatialMagnify,
2185
+ currentTargetGetter,
2186
+ void 0,
2187
+ void 0,
2188
+ void 0,
2189
+ void 0,
2190
+ void 0,
2191
+ void 0,
2192
+ void 0,
2193
+ void 0,
2194
+ void 0,
2195
+ void 0,
2196
+ (ev) => ev.detail?.magnification
2197
+ );
2198
+ const onSpatialMagnifyEnd = createEventHandler(
2199
+ spatialEvents.onSpatialMagnifyEnd,
2200
+ currentTargetGetter
2201
+ );
2202
+ const onSpatialDragStart = createEventHandler(
2203
+ spatialEvents.onSpatialDragStart,
2204
+ currentTargetGetter,
2205
+ (ev) => ev.detail?.startLocation3D?.x,
2206
+ (ev) => ev.detail?.startLocation3D?.y,
2207
+ (ev) => ev.detail?.startLocation3D?.z,
2208
+ (ev) => ev.detail?.globalLocation3D?.x,
2209
+ (ev) => ev.detail?.globalLocation3D?.y,
2210
+ (ev) => ev.detail?.globalLocation3D?.z
2211
+ );
2212
+ return {
2213
+ onSpatialTap,
2214
+ onSpatialDragStart,
2215
+ onSpatialDrag,
2216
+ onSpatialDragEnd,
2217
+ onSpatialRotate,
2218
+ onSpatialRotateEnd,
2219
+ onSpatialMagnify,
2220
+ onSpatialMagnifyEnd
2221
+ };
2222
+ }
2223
+ function useSpatialEvents(spatialEvents, spatialContainerRefProxy) {
2224
+ return useSpatialEventsBase(
2225
+ spatialEvents,
2226
+ () => spatialContainerRefProxy.current?.domProxy
2227
+ );
2228
+ }
2229
+ function useSpatialEventsWhenSpatializedContainerExist(spatialEvents, spatialId, spatializedContainerObject) {
2230
+ return useSpatialEventsBase(spatialEvents, () => {
2231
+ const spatialContainerRefProxy = spatializedContainerObject.getSpatialContainerRefProxyBySpatialId(
2232
+ spatialId
2233
+ );
2234
+ return spatialContainerRefProxy?.domProxy;
2235
+ });
2236
+ }
2237
+
2238
+ // src/ssr/SSRContext.tsx
2239
+ import { createContext as createContext5, useState as useState4, useEffect as useEffect8 } from "react";
2240
+ import { jsx as jsx4 } from "react/jsx-runtime";
2241
+ var SSRContext = createContext5(false);
2242
+ var SSRProvider = ({
2243
+ isSSR: initialIsSSR = true,
2244
+ children
2245
+ }) => {
2246
+ const [isSSR, setIsSSR] = useState4(initialIsSSR);
2247
+ useEffect8(() => {
2248
+ if (isSSR) {
2249
+ setIsSSR(false);
2250
+ }
2251
+ }, []);
2252
+ return /* @__PURE__ */ jsx4(SSRContext.Provider, { value: isSSR, children });
2253
+ };
2254
+
2255
+ // src/ssr/withSSRSupported.tsx
2256
+ import { forwardRef as forwardRef3 } from "react";
2257
+
2258
+ // src/ssr/useSSRPhase.tsx
2259
+ import { useContext as useContext6, useState as useState5, useEffect as useEffect9 } from "react";
2260
+ function useSSRPhase() {
2261
+ const isSSRContext = useContext6(SSRContext);
2262
+ const isServer = typeof window === "undefined";
2263
+ const [hydrated, setHydrated] = useState5(false);
2264
+ useEffect9(() => setHydrated(true), []);
2265
+ if (isServer) {
2266
+ return "ssr";
2267
+ }
2268
+ if (isSSRContext) {
2269
+ return hydrated ? "after-hydrate" : "hydrate";
2270
+ } else {
2271
+ return "after-hydrate";
2272
+ }
2273
+ }
2274
+
2275
+ // src/ssr/withSSRSupported.tsx
2276
+ import { jsx as jsx5 } from "react/jsx-runtime";
2277
+ function withSSRSupported(Component) {
2278
+ const ClientOnlyComponent = (props, ref) => {
2279
+ const phase = useSSRPhase();
2280
+ let renderType = "real";
2281
+ if (phase === "ssr" || phase === "hydrate") {
2282
+ renderType = "fake";
2283
+ }
2284
+ if (renderType === "fake") {
2285
+ const { style, className } = props;
2286
+ return /* @__PURE__ */ jsx5("div", { style, className, ref });
2287
+ } else {
2288
+ return /* @__PURE__ */ jsx5(Component, { ...props, ref });
2289
+ }
2290
+ };
2291
+ ClientOnlyComponent.displayName = `withClientOnly(${Component.displayName || Component.name || "Component"})`;
2292
+ return forwardRef3(ClientOnlyComponent);
2293
+ }
2294
+
2295
+ // src/spatialized-container/SpatializedContainer.tsx
2296
+ import { jsx as jsx6, jsxs as jsxs2 } from "react/jsx-runtime";
2297
+ function DegradedContainer({
2298
+ innerRef,
2299
+ ...inprops
2300
+ }) {
2301
+ const {
2302
+ component: Component,
2303
+ children,
2304
+ ["enable-xr"]: _enableXR,
2305
+ onSpatialTap: _onSpatialTap,
2306
+ onSpatialDragStart: _onSpatialDragStart,
2307
+ onSpatialDrag: _onSpatialDrag,
2308
+ onSpatialDragEnd: _onSpatialDragEnd,
2309
+ onSpatialRotate: _onSpatialRotate,
2310
+ onSpatialRotateEnd: _onSpatialRotateEnd,
2311
+ onSpatialMagnify: _onSpatialMagnify,
2312
+ onSpatialMagnifyEnd: _onSpatialMagnifyEnd,
2313
+ spatializedContent: _content,
2314
+ createSpatializedElement: _create,
2315
+ getExtraSpatializedElementProperties: _getExtra,
2316
+ extraRefProps: _extraRef,
2317
+ sizingMode: _sizingMode,
2318
+ ...restProps
2319
+ } = inprops;
2320
+ return /* @__PURE__ */ jsx6(Component, { ref: innerRef, ...restProps, children });
2321
+ }
2322
+ function SpatializedContainerBase(inprops, ref) {
2323
+ const isWebSpatialEnv = getSession() !== null;
2324
+ const insideAttachment = useInsideAttachment();
2325
+ if (!isWebSpatialEnv || insideAttachment) {
2326
+ if (insideAttachment) {
2327
+ console.warn(
2328
+ `[WebSpatial] ${inprops.component || "Spatial element"} cannot be used inside AttachmentAsset. Rendering as plain HTML.`
2329
+ );
2330
+ }
2331
+ return /* @__PURE__ */ jsx6(DegradedContainer, { ...inprops, innerRef: ref });
2332
+ }
2333
+ const layer = useContext7(SpatialLayerContext) + 1;
2334
+ const rootSpatializedContainerObject = useContext7(
2335
+ SpatializedContainerContext
2336
+ );
2337
+ const inSpatializedContainer = !!rootSpatializedContainerObject;
2338
+ const portalInstanceObject = useContext7(PortalInstanceContext);
2339
+ const inPortalInstanceEnv = !!portalInstanceObject;
2340
+ const isInStandardInstance = !inPortalInstanceEnv;
2341
+ const spatialId = useMemo2(() => {
2342
+ return !inSpatializedContainer ? `root_container` : rootSpatializedContainerObject.getSpatialId(layer, isInStandardInstance);
2343
+ }, []);
2344
+ const spatialIdProps = {
2345
+ [SpatialID]: spatialId
2346
+ };
2347
+ const {
2348
+ onSpatialTap,
2349
+ onSpatialDragStart,
2350
+ onSpatialDrag,
2351
+ onSpatialDragEnd,
2352
+ onSpatialRotate,
2353
+ onSpatialRotateEnd,
2354
+ onSpatialMagnify,
2355
+ onSpatialMagnifyEnd,
2356
+ extraRefProps,
2357
+ ...props
2358
+ } = inprops;
2359
+ if (inSpatializedContainer) {
2360
+ if (inPortalInstanceEnv) {
2361
+ const spatialEvents = useSpatialEventsWhenSpatializedContainerExist(
2362
+ {
2363
+ onSpatialTap,
2364
+ onSpatialDragStart,
2365
+ onSpatialDrag,
2366
+ onSpatialDragEnd,
2367
+ onSpatialRotate,
2368
+ onSpatialRotateEnd,
2369
+ onSpatialMagnify,
2370
+ onSpatialMagnifyEnd
2371
+ },
2372
+ spatialId,
2373
+ rootSpatializedContainerObject
2374
+ );
2375
+ return /* @__PURE__ */ jsx6(SpatialLayerContext.Provider, { value: layer, children: /* @__PURE__ */ jsx6(
2376
+ PortalSpatializedContainer,
2377
+ {
2378
+ ...spatialIdProps,
2379
+ ...props,
2380
+ ...spatialEvents
2381
+ }
2382
+ ) });
2383
+ } else {
2384
+ const {
2385
+ transformVisibilityTaskContainerCallback,
2386
+ standardSpatializedContainerCallback,
2387
+ spatialContainerRefProxy
2388
+ } = useDomProxy(ref, extraRefProps);
2389
+ useEffect10(() => {
2390
+ rootSpatializedContainerObject.updateSpatialContainerRefProxyInfo(
2391
+ spatialId,
2392
+ spatialContainerRefProxy.current
2393
+ );
2394
+ }, [spatialContainerRefProxy.current]);
2395
+ const {
2396
+ spatializedContent,
2397
+ createSpatializedElement: createSpatializedElement2,
2398
+ getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
2399
+ ...restProps
2400
+ } = props;
2401
+ return /* @__PURE__ */ jsxs2(SpatialLayerContext.Provider, { value: layer, children: [
2402
+ /* @__PURE__ */ jsx6(
2403
+ StandardSpatializedContainer,
2404
+ {
2405
+ ref: standardSpatializedContainerCallback,
2406
+ ...spatialIdProps,
2407
+ ...restProps,
2408
+ inStandardSpatializedContainer: true
2409
+ }
2410
+ ),
2411
+ /* @__PURE__ */ jsx6(
2412
+ TransformVisibilityTaskContainer,
2413
+ {
2414
+ ref: transformVisibilityTaskContainerCallback,
2415
+ ...spatialIdProps,
2416
+ className: props.className,
2417
+ style: props.style
2418
+ }
2419
+ )
2420
+ ] });
2421
+ }
2422
+ } else {
2423
+ const {
2424
+ transformVisibilityTaskContainerCallback,
2425
+ standardSpatializedContainerCallback,
2426
+ spatialContainerRefProxy
2427
+ } = useDomProxy(ref, extraRefProps);
2428
+ const spatialEvents = useSpatialEvents(
2429
+ {
2430
+ onSpatialTap,
2431
+ onSpatialDragStart,
2432
+ onSpatialDrag,
2433
+ onSpatialDragEnd,
2434
+ onSpatialRotate,
2435
+ onSpatialRotateEnd,
2436
+ onSpatialMagnify,
2437
+ onSpatialMagnifyEnd
2438
+ },
2439
+ spatialContainerRefProxy
2440
+ );
2441
+ const spatializedContainerObject = useMemo2(
2442
+ () => new SpatializedContainerObject(),
2443
+ []
2444
+ );
2445
+ const {
2446
+ spatializedContent,
2447
+ createSpatializedElement: createSpatializedElement2,
2448
+ getExtraSpatializedElementProperties: getExtraSpatializedElementProperties2,
2449
+ ...restProps
2450
+ } = props;
2451
+ return /* @__PURE__ */ jsx6(SpatialLayerContext.Provider, { value: layer, children: /* @__PURE__ */ jsxs2(
2452
+ SpatializedContainerContext.Provider,
2453
+ {
2454
+ value: spatializedContainerObject,
2455
+ children: [
2456
+ /* @__PURE__ */ jsx6(
2457
+ StandardSpatializedContainer,
2458
+ {
2459
+ ref: standardSpatializedContainerCallback,
2460
+ ...spatialIdProps,
2461
+ ...restProps,
2462
+ inStandardSpatializedContainer: false
2463
+ }
2464
+ ),
2465
+ /* @__PURE__ */ jsx6(
2466
+ PortalSpatializedContainer,
2467
+ {
2468
+ ...spatialIdProps,
2469
+ ...props,
2470
+ ...spatialEvents
2471
+ }
2472
+ ),
2473
+ /* @__PURE__ */ jsx6(
2474
+ TransformVisibilityTaskContainer,
2475
+ {
2476
+ ref: transformVisibilityTaskContainerCallback,
2477
+ ...spatialIdProps,
2478
+ className: props.className,
2479
+ style: props.style
2480
+ }
2481
+ )
2482
+ ]
2483
+ }
2484
+ ) });
2485
+ }
2486
+ }
2487
+ var SpatializedContainer = withSSRSupported(
2488
+ forwardRef4(SpatializedContainerBase)
2489
+ );
2490
+
2491
+ // src/spatialized-container/Spatialized2DElementContainer.tsx
2492
+ import { createPortal as createPortal2 } from "react-dom";
2493
+ import {
2494
+ forwardRef as forwardRef5,
2495
+ useContext as useContext8,
2496
+ useEffect as useEffect12
2497
+ } from "react";
2498
+
2499
+ // src/utils/windowStyleSync.ts
2500
+ function ensureWindowDocumentStructure(openedWindow) {
2501
+ try {
2502
+ const { document: document2 } = openedWindow;
2503
+ let documentElement = document2.documentElement;
2504
+ if (!documentElement) {
2505
+ documentElement = document2.createElement("html");
2506
+ document2.appendChild(documentElement);
2507
+ }
2508
+ let head = document2.head;
2509
+ if (!head) {
2510
+ head = document2.createElement("head");
2511
+ if (documentElement.firstChild) {
2512
+ documentElement.insertBefore(head, documentElement.firstChild);
2513
+ } else {
2514
+ documentElement.appendChild(head);
2515
+ }
2516
+ }
2517
+ let body = document2.body;
2518
+ if (!body) {
2519
+ body = document2.createElement("body");
2520
+ documentElement.appendChild(body);
2521
+ }
2522
+ return {
2523
+ document: document2,
2524
+ documentElement,
2525
+ head,
2526
+ body
2527
+ };
2528
+ } catch (error) {
2529
+ console.warn(
2530
+ "[WebSpatial] Failed to ensure child window document structure",
2531
+ error
2532
+ );
2533
+ return null;
2534
+ }
2535
+ }
2536
+ function asyncLoadStyleToChildWindow(childWindow, link, isCurrent) {
2537
+ return new Promise((resolve) => {
2538
+ const { href } = link;
2539
+ const sep = href.includes("?") ? "&" : "?";
2540
+ link.href = `${href}${sep}uniqueURL=${Math.random()}`;
2541
+ let finished = false;
2542
+ const finish = (ok) => {
2543
+ if (finished) return;
2544
+ finished = true;
2545
+ resolve(ok);
2546
+ };
2547
+ link.onerror = () => {
2548
+ finish(false);
2549
+ };
2550
+ link.onload = () => {
2551
+ if (!isCurrent()) {
2552
+ link.parentNode?.removeChild(link);
2553
+ finish(false);
2554
+ return;
2555
+ }
2556
+ finish(true);
2557
+ };
2558
+ setTimeout(() => {
2559
+ if (!isCurrent()) {
2560
+ finish(false);
2561
+ return;
2562
+ }
2563
+ const childDocument = ensureWindowDocumentStructure(childWindow);
2564
+ if (!childDocument) {
2565
+ finish(false);
2566
+ return;
2567
+ }
2568
+ childDocument.head.appendChild(link);
2569
+ }, 50);
2570
+ });
2571
+ }
2572
+ var WEBSPATIAL_SYNC_ATTR = "data-webspatial-sync";
2573
+ var WEBSPATIAL_SYNC_KEY_ATTR = "data-webspatial-sync-key";
2574
+ function setOpenWindowStyle(openedWindow) {
2575
+ const childDocument = ensureWindowDocumentStructure(openedWindow);
2576
+ if (!childDocument) return;
2577
+ childDocument.documentElement.style.cssText += document.documentElement.style.cssText;
2578
+ childDocument.documentElement.style.backgroundColor = "transparent";
2579
+ childDocument.body.style.margin = "0px";
2580
+ childDocument.body.style.display = "inline-block";
2581
+ childDocument.body.style.minWidth = "auto";
2582
+ childDocument.body.style.minHeight = "auto";
2583
+ childDocument.body.style.maxWidth = "fit-content";
2584
+ childDocument.body.style.minWidth = "fit-content";
2585
+ childDocument.body.style.background = "transparent";
2586
+ }
2587
+ var controllers = /* @__PURE__ */ new WeakMap();
2588
+ function getController(childWindow) {
2589
+ const prev = controllers.get(childWindow);
2590
+ if (prev) return prev;
2591
+ const next = { version: 0 };
2592
+ controllers.set(childWindow, next);
2593
+ return next;
2594
+ }
2595
+ async function syncParentHeadToChild(childWindow) {
2596
+ const controller = getController(childWindow);
2597
+ const version2 = ++controller.version;
2598
+ const styleLoadedPromises = [];
2599
+ const childDocument = ensureWindowDocumentStructure(childWindow);
2600
+ if (!childDocument) {
2601
+ return [];
2602
+ }
2603
+ const { head } = childDocument;
2604
+ const isCurrent = () => controller.version === version2;
2605
+ const parentStyles = Array.from(document.head.querySelectorAll("style"));
2606
+ const parentStylesheets = Array.from(
2607
+ document.head.querySelectorAll('link[rel="stylesheet"][href]')
2608
+ );
2609
+ const desiredStylesheetKeys = /* @__PURE__ */ new Set();
2610
+ for (const link of parentStylesheets) {
2611
+ if (link.href) desiredStylesheetKeys.add(link.href);
2612
+ }
2613
+ const existingSyncedLinks = Array.from(
2614
+ head.querySelectorAll(
2615
+ `link[rel="stylesheet"][${WEBSPATIAL_SYNC_ATTR}="1"]`
2616
+ )
2617
+ );
2618
+ for (const link of existingSyncedLinks) {
2619
+ const key = link.getAttribute(WEBSPATIAL_SYNC_KEY_ATTR) ?? link.href;
2620
+ if (!desiredStylesheetKeys.has(key)) link.parentNode?.removeChild(link);
2621
+ }
2622
+ const prevSyncedStyles = head.querySelectorAll(
2623
+ `style[${WEBSPATIAL_SYNC_ATTR}="1"]`
2624
+ );
2625
+ prevSyncedStyles.forEach((n) => n.parentNode?.removeChild(n));
2626
+ for (const styleEl of parentStyles) {
2627
+ const node = styleEl.cloneNode(true);
2628
+ node.setAttribute(WEBSPATIAL_SYNC_ATTR, "1");
2629
+ head.appendChild(node);
2630
+ }
2631
+ const currentKeys = /* @__PURE__ */ new Set();
2632
+ const currentSyncedLinks = Array.from(
2633
+ head.querySelectorAll(
2634
+ `link[rel="stylesheet"][${WEBSPATIAL_SYNC_ATTR}="1"]`
2635
+ )
2636
+ );
2637
+ for (const link of currentSyncedLinks) {
2638
+ currentKeys.add(link.getAttribute(WEBSPATIAL_SYNC_KEY_ATTR) ?? link.href);
2639
+ }
2640
+ for (const link of parentStylesheets) {
2641
+ const key = link.href;
2642
+ if (!key || currentKeys.has(key)) continue;
2643
+ const node = link.cloneNode(true);
2644
+ node.setAttribute(WEBSPATIAL_SYNC_ATTR, "1");
2645
+ node.setAttribute(WEBSPATIAL_SYNC_KEY_ATTR, key);
2646
+ styleLoadedPromises.push(
2647
+ asyncLoadStyleToChildWindow(childWindow, node, isCurrent)
2648
+ );
2649
+ }
2650
+ childDocument.documentElement.className = document.documentElement.className;
2651
+ return Promise.all(styleLoadedPromises);
2652
+ }
2653
+
2654
+ // src/utils/useSyncHeadStyles.ts
2655
+ import { useEffect as useEffect11 } from "react";
2656
+ function defaultShouldSync(mutations) {
2657
+ if (!Array.isArray(mutations) || mutations.length === 0) return false;
2658
+ for (const mutation of mutations) {
2659
+ const nodes = [
2660
+ ...Array.from(mutation.addedNodes),
2661
+ ...Array.from(mutation.removedNodes)
2662
+ ];
2663
+ for (const node of nodes) {
2664
+ if (!(node instanceof Element)) continue;
2665
+ const tag = node.tagName;
2666
+ if (tag === "STYLE") return true;
2667
+ if (tag === "LINK") {
2668
+ const { rel } = node;
2669
+ if (rel && rel.toLowerCase() === "stylesheet") return true;
2670
+ }
2671
+ }
2672
+ }
2673
+ return false;
2674
+ }
2675
+ function useSyncHeadStyles(childWindow, options) {
2676
+ const delayMs = 100;
2677
+ const subtree = options?.subtree ?? false;
2678
+ const immediate = options?.immediate ?? true;
2679
+ useEffect11(() => {
2680
+ if (!childWindow) return;
2681
+ let timer;
2682
+ const scheduleSync = () => {
2683
+ if (timer) window.clearTimeout(timer);
2684
+ timer = window.setTimeout(() => {
2685
+ syncParentHeadToChild(childWindow);
2686
+ }, delayMs);
2687
+ };
2688
+ if (immediate) scheduleSync();
2689
+ const observer = new MutationObserver((mutations) => {
2690
+ if (!defaultShouldSync(mutations)) return;
2691
+ scheduleSync();
2692
+ });
2693
+ observer.observe(document.head, { childList: true, subtree });
2694
+ return () => {
2695
+ if (timer) window.clearTimeout(timer);
2696
+ observer.disconnect();
2697
+ };
2698
+ }, [childWindow, delayMs, subtree, immediate]);
2699
+ }
2700
+
2701
+ // src/spatialized-container/Spatialized2DElementContainer.tsx
2702
+ import { jsx as jsx7 } from "react/jsx-runtime";
2703
+ function getJSXPortalInstance(inProps, portalInstanceObject) {
2704
+ const { component: El, style: inStyle = {}, ...props } = inProps;
2705
+ const extraStyle = {
2706
+ visibility: "visible",
2707
+ position: "relative",
2708
+ top: "0px",
2709
+ left: "0px",
2710
+ margin: "0px",
2711
+ marginLeft: "0px",
2712
+ marginRight: "0px",
2713
+ marginTop: "0px",
2714
+ marginBottom: "0px",
2715
+ borderRadius: "0px",
2716
+ // overflow: '',
2717
+ transform: "none"
2718
+ };
2719
+ const computedStyle = portalInstanceObject.computedStyle;
2720
+ const inheritedPortalStyle = getInheritedStyleProps(computedStyle);
2721
+ const style = {
2722
+ ...inStyle,
2723
+ ...inheritedPortalStyle,
2724
+ ...extraStyle
2725
+ };
2726
+ return /* @__PURE__ */ jsx7(El, { style, ...props });
2727
+ }
2728
+ function useSyncDocumentTitle(windowProxy, spatializedElement, name) {
2729
+ useEffect12(() => {
2730
+ const childDocument = ensureWindowDocumentStructure(windowProxy);
2731
+ if (!childDocument) return;
2732
+ childDocument.document.title = name;
2733
+ spatializedElement.updateProperties({
2734
+ name
2735
+ });
2736
+ }, [name]);
2737
+ }
2738
+ function SpatializedContent(props) {
2739
+ const { spatializedElement, ...restProps } = props;
2740
+ const spatialized2DElement = spatializedElement;
2741
+ const { windowProxy } = spatialized2DElement;
2742
+ const isAndroidBitmapMode = usesAndroidBitmapCapture();
2743
+ useSyncHeadStyles(isAndroidBitmapMode ? null : windowProxy, {
2744
+ subtree: false
2745
+ });
2746
+ const name = restProps["data-name"] || "";
2747
+ useSyncDocumentTitle(windowProxy, spatialized2DElement, name);
2748
+ const portalInstanceObject = useContext8(
2749
+ PortalInstanceContext
2750
+ );
2751
+ if (isAndroidBitmapMode) {
2752
+ return null;
2753
+ }
2754
+ const childDocument = ensureWindowDocumentStructure(windowProxy);
2755
+ if (!childDocument?.body) {
2756
+ return null;
2757
+ }
2758
+ const JSXPortalInstance = getJSXPortalInstance(
2759
+ restProps,
2760
+ portalInstanceObject
2761
+ );
2762
+ return createPortal2(JSXPortalInstance, childDocument.body);
2763
+ }
2764
+ function getExtraSpatializedElementProperties(computedStyle) {
2765
+ const overflow = computedStyle.getPropertyValue("overflow");
2766
+ const scrollPageEnabled = ["visible", "hidden", "clip"].indexOf(overflow) >= 0;
2767
+ const material = computedStyle.getPropertyValue(
2768
+ SpatialCustomStyleVars.backgroundMaterial
2769
+ );
2770
+ const properties = {};
2771
+ properties.scrollPageEnabled = scrollPageEnabled;
2772
+ properties.cornerRadius = parseCornerRadius(computedStyle);
2773
+ if (material) {
2774
+ properties.material = material;
2775
+ }
2776
+ return properties;
2777
+ }
2778
+ async function createSpatializedElement() {
2779
+ const spatializedElement = await getSession().createSpatialized2DElement();
2780
+ const windowProxy = spatializedElement.windowProxy;
2781
+ if (usesAndroidBitmapCapture()) {
2782
+ console.log(
2783
+ "[WebSpatial] Android: Skipping WindowProxy setup, using bitmap capture"
2784
+ );
2785
+ return spatializedElement;
2786
+ }
2787
+ setOpenWindowStyle(windowProxy);
2788
+ await syncParentHeadToChild(windowProxy);
2789
+ const childDocument = ensureWindowDocumentStructure(windowProxy);
2790
+ if (!childDocument) {
2791
+ return spatializedElement;
2792
+ }
2793
+ const viewport = childDocument.document.querySelector('meta[name="viewport"]');
2794
+ if (viewport) {
2795
+ viewport?.setAttribute(
2796
+ "content",
2797
+ " initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
2798
+ );
2799
+ } else {
2800
+ const meta = childDocument.document.createElement("meta");
2801
+ meta.name = "viewport";
2802
+ meta.content = "initial-scale=1.0, maximum-scale=1.0, user-scalable=no";
2803
+ childDocument.head.appendChild(meta);
2804
+ }
2805
+ return spatializedElement;
2806
+ }
2807
+ function Spatialized2DElementContainerBase(props, ref) {
2808
+ return /* @__PURE__ */ jsx7(
2809
+ SpatializedContainer,
2810
+ {
2811
+ ref,
2812
+ createSpatializedElement,
2813
+ getExtraSpatializedElementProperties,
2814
+ spatializedContent: SpatializedContent,
2815
+ ...props
2816
+ }
2817
+ );
2818
+ }
2819
+ var Spatialized2DElementContainer = forwardRef5(
2820
+ Spatialized2DElementContainerBase
2821
+ );
2822
+
2823
+ // src/spatialized-container/SpatializedStatic3DElementContainer.tsx
2824
+ import {
2825
+ forwardRef as forwardRef6,
2826
+ useCallback as useCallback6,
2827
+ useContext as useContext9,
2828
+ useEffect as useEffect13,
2829
+ useMemo as useMemo3,
2830
+ useRef as useRef4
2831
+ } from "react";
2832
+ import { Fragment as Fragment2, jsx as jsx8 } from "react/jsx-runtime";
2833
+ function getAbsoluteURL(url) {
2834
+ if (!url) {
2835
+ return "";
2836
+ }
2837
+ try {
2838
+ return new URL(url, document.baseURI).toString();
2839
+ } catch {
2840
+ return url;
2841
+ }
2842
+ }
2843
+ function createLoadEvent(type, targetGetter) {
2844
+ const event = new CustomEvent(type, {
2845
+ bubbles: false,
2846
+ cancelable: false
2847
+ });
2848
+ const proxyEvent = new Proxy(event, {
2849
+ get(target, prop) {
2850
+ if (prop === "target") {
2851
+ return targetGetter();
2852
+ }
2853
+ return Reflect.get(target, prop);
2854
+ }
2855
+ });
2856
+ return proxyEvent;
2857
+ }
2858
+ function createLoadFailureEvent(targetGetter) {
2859
+ return createLoadEvent("modelloadfailed", targetGetter);
2860
+ }
2861
+ function createLoadSuccessEvent(targetGetter) {
2862
+ return createLoadEvent("modelloaded", targetGetter);
2863
+ }
2864
+ function SpatializedContent2(props) {
2865
+ const { src, spatializedElement, onLoad, onError } = props;
2866
+ const spatializedStatic3DElement = spatializedElement;
2867
+ const portalInstanceObject = useContext9(
2868
+ PortalInstanceContext
2869
+ );
2870
+ const currentSrc = useMemo3(() => getAbsoluteURL(src), [src]);
2871
+ useEffect13(() => {
2872
+ if (src) {
2873
+ spatializedStatic3DElement.updateProperties({ modelURL: currentSrc });
2874
+ }
2875
+ }, [currentSrc]);
2876
+ useEffect13(() => {
2877
+ if (onLoad) {
2878
+ spatializedStatic3DElement.onLoadCallback = () => {
2879
+ onLoad(
2880
+ createLoadSuccessEvent(
2881
+ () => portalInstanceObject.dom.__targetProxy
2882
+ )
2883
+ );
2884
+ };
2885
+ } else {
2886
+ spatializedStatic3DElement.onLoadCallback = void 0;
2887
+ }
2888
+ }, [onLoad]);
2889
+ useEffect13(() => {
2890
+ if (onError) {
2891
+ spatializedStatic3DElement.onLoadFailureCallback = () => {
2892
+ onError(
2893
+ createLoadFailureEvent(
2894
+ () => portalInstanceObject.dom.__targetProxy
2895
+ )
2896
+ );
2897
+ };
2898
+ } else {
2899
+ spatializedStatic3DElement.onLoadFailureCallback = void 0;
2900
+ }
2901
+ }, [onError]);
2902
+ return /* @__PURE__ */ jsx8(Fragment2, {});
2903
+ }
2904
+ function SpatializedStatic3DElementContainerBase(props, ref) {
2905
+ const promiseRef = useRef4(null);
2906
+ const createSpatializedElement2 = useCallback6(() => {
2907
+ const url = getAbsoluteURL(props.src);
2908
+ promiseRef.current = getSession().createSpatializedStatic3DElement(url);
2909
+ return promiseRef.current;
2910
+ }, []);
2911
+ const extraRefProps = useCallback6(
2912
+ (domProxy) => {
2913
+ let modelTransform = new DOMMatrixReadOnly();
2914
+ return {
2915
+ get currentSrc() {
2916
+ return getAbsoluteURL(props.src);
2917
+ },
2918
+ get ready() {
2919
+ return promiseRef.current.then((spatializedElement) => spatializedElement.ready).then((success) => {
2920
+ if (success) return createLoadSuccessEvent(() => domProxy);
2921
+ throw createLoadFailureEvent(() => domProxy);
2922
+ });
2923
+ },
2924
+ get entityTransform() {
2925
+ return modelTransform;
2926
+ },
2927
+ set entityTransform(value) {
2928
+ modelTransform = value;
2929
+ const spatializedElement = domProxy.__spatializedElement;
2930
+ spatializedElement?.updateModelTransform(modelTransform);
2931
+ }
2932
+ };
2933
+ },
2934
+ []
2935
+ );
2936
+ return /* @__PURE__ */ jsx8(
2937
+ SpatializedContainer,
2938
+ {
2939
+ ref,
2940
+ component: "div",
2941
+ createSpatializedElement: createSpatializedElement2,
2942
+ spatializedContent: SpatializedContent2,
2943
+ extraRefProps,
2944
+ ...props
2945
+ }
2946
+ );
2947
+ }
2948
+ var SpatializedStatic3DElementContainer = forwardRef6(
2949
+ SpatializedStatic3DElementContainerBase
2950
+ );
2951
+
2952
+ // src/spatialized-container/Spatialized2DElementContainerFactory.tsx
2953
+ import { forwardRef as forwardRef7 } from "react";
2954
+ import { jsx as jsx9 } from "react/jsx-runtime";
2955
+ var CachedSpatialized2DElementContainerType = /* @__PURE__ */ new Map();
2956
+ function withSpatialized2DElementContainer(Component) {
2957
+ if (CachedSpatialized2DElementContainerType.has(Component)) {
2958
+ return CachedSpatialized2DElementContainerType.get(Component);
2959
+ } else {
2960
+ const TypedSpatialized2DElementContainer = forwardRef7(
2961
+ (givenProps, ref) => {
2962
+ const { component: ignoreComponent, ...props } = givenProps;
2963
+ return /* @__PURE__ */ jsx9(
2964
+ Spatialized2DElementContainer,
2965
+ {
2966
+ component: Component,
2967
+ ...props,
2968
+ ref
2969
+ }
2970
+ );
2971
+ }
2972
+ );
2973
+ CachedSpatialized2DElementContainerType.set(
2974
+ Component,
2975
+ TypedSpatialized2DElementContainer
2976
+ );
2977
+ CachedSpatialized2DElementContainerType.set(
2978
+ TypedSpatialized2DElementContainer,
2979
+ TypedSpatialized2DElementContainer
2980
+ );
2981
+ return TypedSpatialized2DElementContainer;
2982
+ }
2983
+ }
2984
+
2985
+ // src/spatialized-container/index.ts
2986
+ function initPolyfill() {
2987
+ hijackGetComputedStyle();
2988
+ injectSpatialDefaultStyle();
2989
+ initCSSParserDivContainer();
2990
+ }
2991
+
2992
+ // src/initScene.ts
2993
+ function initScene(name, callback, options) {
2994
+ return getSession()?.initScene(name, callback, options);
2995
+ }
2996
+
2997
+ // src/spatialized-container-monitor/withSpatialMonitor.tsx
2998
+ import { forwardRef as forwardRef9 } from "react";
2999
+
3000
+ // src/spatialized-container-monitor/useMonitorDomChange.tsx
3001
+ import { useRef as useRef5, useEffect as useEffect14, useMemo as useMemo4 } from "react";
3002
+ function useMonitorDomChange(inRef) {
3003
+ const ref = useRef5(null);
3004
+ useEffect14(() => {
3005
+ const observer = new MutationObserver((mutationsList) => {
3006
+ notifyDOMUpdate(mutationsList);
3007
+ });
3008
+ const config = {
3009
+ childList: true,
3010
+ subtree: true,
3011
+ attributes: true,
3012
+ attributeFilter: ["style", "class"]
3013
+ };
3014
+ ref.current && observer.observe(ref.current, config);
3015
+ return () => {
3016
+ observer.disconnect();
3017
+ };
3018
+ }, []);
3019
+ const proxyRef = useMemo4(
3020
+ () => new Proxy(ref, {
3021
+ set: function(target, key, value) {
3022
+ if (key === "current") {
3023
+ if (inRef) {
3024
+ if (typeof inRef === "function") {
3025
+ inRef(value);
3026
+ } else if (inRef) {
3027
+ inRef.current = value;
3028
+ }
3029
+ }
3030
+ }
3031
+ return Reflect.set(target, key, value);
3032
+ }
3033
+ }),
3034
+ []
3035
+ );
3036
+ return proxyRef;
3037
+ }
3038
+
3039
+ // src/spatialized-container-monitor/useMonitorDocumentHeaderChange.tsx
3040
+ import { useEffect as useEffect15 } from "react";
3041
+ function useMonitorDocumentHeaderChange() {
3042
+ useEffect15(() => {
3043
+ const observer = new MutationObserver((mutationsList) => {
3044
+ notifyUpdateStandInstanceLayout();
3045
+ });
3046
+ const config = {
3047
+ childList: true,
3048
+ subtree: true,
3049
+ attributes: true
3050
+ };
3051
+ observer.observe(document.head, config);
3052
+ return () => {
3053
+ observer.disconnect();
3054
+ };
3055
+ }, []);
3056
+ }
3057
+
3058
+ // src/spatialized-container-monitor/SpatialMonitor.tsx
3059
+ import { forwardRef as forwardRef8 } from "react";
3060
+ import { jsx as jsx10 } from "react/jsx-runtime";
3061
+ function SpatialMonitorBase(inProps, inRef) {
3062
+ const { El = "div", ...props } = inProps;
3063
+ const ref = useMonitorDomChange(inRef);
3064
+ useMonitorDocumentHeaderChange();
3065
+ return /* @__PURE__ */ jsx10(El, { ...props, ref });
3066
+ }
3067
+ var SpatialMonitor = forwardRef8(SpatialMonitorBase);
3068
+
3069
+ // src/spatialized-container-monitor/withSpatialMonitor.tsx
3070
+ import { jsx as jsx11 } from "react/jsx-runtime";
3071
+ var cachedWithSpatialMonitorType = /* @__PURE__ */ new Map();
3072
+ function withSpatialMonitor(El) {
3073
+ if (cachedWithSpatialMonitorType.has(El)) {
3074
+ return cachedWithSpatialMonitorType.get(El);
3075
+ } else {
3076
+ const WithSpatialMonitorComponent = forwardRef9(
3077
+ (givenProps, givenRef) => {
3078
+ const {
3079
+ El: _,
3080
+ ...props
3081
+ } = givenProps;
3082
+ return /* @__PURE__ */ jsx11(SpatialMonitor, { El, ...props, ref: givenRef });
3083
+ }
3084
+ );
3085
+ WithSpatialMonitorComponent.displayName = `WithSpatialMonitor(${typeof El === "string" ? El : El.displayName || El.name})`;
3086
+ cachedWithSpatialMonitorType.set(El, WithSpatialMonitorComponent);
3087
+ cachedWithSpatialMonitorType.set(
3088
+ cachedWithSpatialMonitorType,
3089
+ cachedWithSpatialMonitorType
3090
+ );
3091
+ return WithSpatialMonitorComponent;
3092
+ }
3093
+ }
3094
+
3095
+ // src/reality/components/Entity.tsx
3096
+ import { forwardRef as forwardRef11 } from "react";
3097
+
3098
+ // src/reality/components/BaseEntity.tsx
3099
+ import { forwardRef as forwardRef10 } from "react";
3100
+
3101
+ // src/reality/context/RealityContext.tsx
3102
+ import { createContext as createContext6, useContext as useContext10 } from "react";
3103
+ var RealityContext = createContext6(null);
3104
+ var useRealityContext = () => useContext10(RealityContext);
3105
+
3106
+ // src/reality/context/ParentContext.tsx
3107
+ import { createContext as createContext7, useContext as useContext11 } from "react";
3108
+ var ParentContext = createContext7(null);
3109
+ var useParentContext = () => useContext11(ParentContext);
3110
+
3111
+ // src/reality/context/AttachmentContext.tsx
3112
+ import { createContext as createContext8, useContext as useContext12 } from "react";
3113
+ var AttachmentRegistry = class {
3114
+ // name → (instanceId → container)
3115
+ containers = /* @__PURE__ */ new Map();
3116
+ listeners = /* @__PURE__ */ new Map();
3117
+ addContainer(name, instanceId, container) {
3118
+ if (!this.containers.has(name)) {
3119
+ this.containers.set(name, /* @__PURE__ */ new Map());
3120
+ }
3121
+ this.containers.get(name).set(instanceId, container);
3122
+ this.notifyListeners(name);
3123
+ }
3124
+ removeContainer(name, instanceId) {
3125
+ this.containers.get(name)?.delete(instanceId);
3126
+ if (this.containers.get(name)?.size === 0) {
3127
+ this.containers.delete(name);
3128
+ }
3129
+ this.notifyListeners(name);
3130
+ }
3131
+ getContainers(name) {
3132
+ const map = this.containers.get(name);
3133
+ return map ? Array.from(map.values()) : [];
3134
+ }
3135
+ onContainersChange(name, cb) {
3136
+ const current = this.getContainers(name);
3137
+ if (current.length > 0) {
3138
+ cb(current);
3139
+ }
3140
+ const prev = this.listeners.get(name);
3141
+ if (prev) prev([]);
3142
+ this.listeners.set(name, cb);
3143
+ return () => {
3144
+ if (this.listeners.get(name) === cb) {
3145
+ this.listeners.delete(name);
3146
+ }
3147
+ };
3148
+ }
3149
+ notifyListeners(name) {
3150
+ const cs = this.getContainers(name);
3151
+ this.listeners.get(name)?.(cs);
3152
+ }
3153
+ destroy() {
3154
+ this.containers.clear();
3155
+ this.listeners.clear();
3156
+ }
3157
+ };
3158
+ var AttachmentContext = createContext8(null);
3159
+
3160
+ // src/reality/hooks/useEntityTransform.tsx
3161
+ import { useEffect as useEffect16, useRef as useRef6 } from "react";
3162
+
3163
+ // src/reality/utils/ResourceRegistry.ts
3164
+ var ResourceRegistry = class {
3165
+ resources = /* @__PURE__ */ new Map();
3166
+ add(id, resource) {
3167
+ this.resources.set(id, resource);
3168
+ }
3169
+ remove(id) {
3170
+ this.resources.delete(id);
3171
+ }
3172
+ // Remove the resource by id and destroy it once resolved
3173
+ // This does not cancel in-flight creation; it schedules destruction after resolution
3174
+ removeAndDestroy(id) {
3175
+ const p = this.resources.get(id);
3176
+ if (p) {
3177
+ p.then((spatialObj) => spatialObj.destroy()).catch(() => {
3178
+ });
3179
+ }
3180
+ this.resources.delete(id);
3181
+ }
3182
+ get(id) {
3183
+ return this.resources.get(id);
3184
+ }
3185
+ destroy() {
3186
+ const pending = Array.from(this.resources.values());
3187
+ this.resources.clear();
3188
+ pending.forEach(
3189
+ (promise) => promise.then((spatialObj) => spatialObj.destroy()).catch(() => {
3190
+ })
3191
+ );
3192
+ }
3193
+ };
3194
+
3195
+ // src/reality/utils/equal.ts
3196
+ function shallowEqualVec3(a, b) {
3197
+ if (a === b) return true;
3198
+ if (!a || !b) return false;
3199
+ return a.x === b.x && a.y === b.y && a.z === b.z;
3200
+ }
3201
+ function shallowEqualRotation(a, b) {
3202
+ if (a === b) return true;
3203
+ if (!a || !b) return false;
3204
+ return a.x === b.x && a.y === b.y && a.z === b.z && ("w" in a ? a.w === b.w : true);
3205
+ }
3206
+
3207
+ // src/reality/utils/AbortResourceManager.ts
3208
+ var AbortResourceManager = class {
3209
+ constructor(signal) {
3210
+ this.signal = signal;
3211
+ signal.addEventListener("abort", () => {
3212
+ this.aborted = true;
3213
+ void this.dispose();
3214
+ });
3215
+ }
3216
+ resources = [];
3217
+ aborted = false;
3218
+ async addResource(factory) {
3219
+ if (this.aborted) throw new DOMException("Aborted", "AbortError");
3220
+ const resource = await factory();
3221
+ if (this.aborted) {
3222
+ await resource.destroy();
3223
+ throw new DOMException("Aborted", "AbortError");
3224
+ }
3225
+ this.resources.push(resource);
3226
+ return resource;
3227
+ }
3228
+ async dispose() {
3229
+ const resources = this.resources.splice(0);
3230
+ for (const r of resources) {
3231
+ try {
3232
+ await r.destroy();
3233
+ } catch (e) {
3234
+ console.error("AbortResourceManager dispose error:", e, r);
3235
+ }
3236
+ }
3237
+ }
3238
+ };
3239
+
3240
+ // src/reality/hooks/useEntityTransform.tsx
3241
+ function useEntityTransform(entity, { position, rotation, scale }) {
3242
+ const last = useRef6({});
3243
+ useEffect16(() => {
3244
+ if (!entity) return;
3245
+ const shouldUpdate = !shallowEqualVec3(last.current.position, position) || !shallowEqualRotation(last.current.rotation, rotation) || !shallowEqualVec3(last.current.scale, scale);
3246
+ if (!shouldUpdate) return;
3247
+ last.current = { position, rotation, scale };
3248
+ const updateTransform = async () => {
3249
+ try {
3250
+ await entity.updateTransform({ position, rotation, scale });
3251
+ } catch (err) {
3252
+ console.error("[useEntityTransform] Failed to update transform:", err);
3253
+ }
3254
+ };
3255
+ updateTransform();
3256
+ return () => {
3257
+ };
3258
+ }, [entity, position, rotation, scale]);
3259
+ }
3260
+
3261
+ // src/reality/hooks/useEntityEvent.tsx
3262
+ import { useEffect as useEffect18, useRef as useRef8 } from "react";
3263
+
3264
+ // src/reality/type.ts
3265
+ var eventMap = {
3266
+ // tap
3267
+ onSpatialTap: "spatialtap",
3268
+ // drag
3269
+ onSpatialDragStart: "spatialdragstart",
3270
+ onSpatialDrag: "spatialdrag",
3271
+ onSpatialDragEnd: "spatialdragend",
3272
+ // rotate
3273
+ onSpatialRotateStart: "spatialrotatestart",
3274
+ onSpatialRotate: "spatialrotate",
3275
+ onSpatialRotateEnd: "spatialrotateend",
3276
+ // magnify
3277
+ onSpatialMagnifyStart: "spatialmagnifystart",
3278
+ onSpatialMagnify: "spatialmagnify",
3279
+ onSpatialMagnifyEnd: "spatialmagnifyend"
3280
+ };
3281
+
3282
+ // src/reality/hooks/useEntityRef.tsx
3283
+ import { useImperativeHandle } from "react";
3284
+ var useEntityRef = (ref, instance) => {
3285
+ useImperativeHandle(ref, () => instance);
3286
+ };
3287
+ var EntityRef = class {
3288
+ _entity;
3289
+ _ctx;
3290
+ constructor(entity = null, ctx = null) {
3291
+ this._entity = entity;
3292
+ this._ctx = ctx;
3293
+ }
3294
+ updateEntity(entity) {
3295
+ if (entity) this._entity = entity;
3296
+ }
3297
+ updateCtx(ctx) {
3298
+ if (ctx) this._ctx = ctx;
3299
+ }
3300
+ destroy() {
3301
+ this._entity?.destroy();
3302
+ }
3303
+ get entity() {
3304
+ return this._entity;
3305
+ }
3306
+ get id() {
3307
+ return this._entity?.userData?.id;
3308
+ }
3309
+ get name() {
3310
+ return this._entity?.userData?.name;
3311
+ }
3312
+ async convertFromEntityToEntity(fromEntityId, toEntityId, position) {
3313
+ if (!this._entity) return position;
3314
+ try {
3315
+ const fromEnt = await this._ctx?.resourceRegistry.get(fromEntityId);
3316
+ const toEnt = await this._ctx?.resourceRegistry.get(toEntityId);
3317
+ if (!fromEnt || !toEnt) return position;
3318
+ const ret = await this._entity.convertFromEntityToEntity(
3319
+ fromEnt.id,
3320
+ toEnt.id,
3321
+ position
3322
+ );
3323
+ return ret?.data ?? position;
3324
+ } catch {
3325
+ return position;
3326
+ }
3327
+ }
3328
+ async convertFromEntityToReality(entityId, position) {
3329
+ if (!this._entity) return position;
3330
+ try {
3331
+ const ent = await this._ctx?.resourceRegistry.get(entityId);
3332
+ if (!ent) return position;
3333
+ const ret = await this._entity.convertFromEntityToScene(ent.id, position);
3334
+ return ret?.data ?? position;
3335
+ } catch {
3336
+ return position;
3337
+ }
3338
+ }
3339
+ async convertFromRealityToEntity(entityId, position) {
3340
+ if (!this._entity) return position;
3341
+ try {
3342
+ const ent = await this._ctx?.resourceRegistry.get(entityId);
3343
+ if (!ent) return position;
3344
+ const ret = await this._entity.convertFromSceneToEntity(ent.id, position);
3345
+ return ret?.data ?? position;
3346
+ } catch {
3347
+ return position;
3348
+ }
3349
+ }
3350
+ };
3351
+
3352
+ // src/reality/hooks/useEntityEvent.tsx
3353
+ function createEventProxy2(ev, instance) {
3354
+ return new Proxy(ev, {
3355
+ get(target, prop) {
3356
+ if (prop === "currentTarget") {
3357
+ return instance;
3358
+ }
3359
+ if (prop === "target") {
3360
+ const origin = target.__origin;
3361
+ if (origin) {
3362
+ return new EntityRef(origin, null);
3363
+ }
3364
+ return instance;
3365
+ }
3366
+ if (prop === "bubbles") {
3367
+ return true;
3368
+ }
3369
+ if (prop === "offsetX") {
3370
+ const type = target.type;
3371
+ if (type === "spatialtap") {
3372
+ return target.detail?.location3D?.x ?? 0;
3373
+ }
3374
+ if (type === "spatialdragstart") {
3375
+ return target.detail?.startLocation3D?.x ?? 0;
3376
+ }
3377
+ return void 0;
3378
+ }
3379
+ if (prop === "offsetY") {
3380
+ const type = target.type;
3381
+ if (type === "spatialtap") {
3382
+ return target.detail?.location3D?.y ?? 0;
3383
+ }
3384
+ if (type === "spatialdragstart") {
3385
+ return target.detail?.startLocation3D?.y ?? 0;
3386
+ }
3387
+ return void 0;
3388
+ }
3389
+ if (prop === "offsetZ") {
3390
+ const type = target.type;
3391
+ if (type === "spatialtap") {
3392
+ return target.detail?.location3D?.z ?? 0;
3393
+ }
3394
+ if (type === "spatialdragstart") {
3395
+ return target.detail?.startLocation3D?.z ?? 0;
3396
+ }
3397
+ return void 0;
3398
+ }
3399
+ if (prop === "translationX") {
3400
+ const type = target.type;
3401
+ if (type === "spatialdrag") {
3402
+ return target.detail?.translation3D?.x ?? 0;
3403
+ }
3404
+ return void 0;
3405
+ }
3406
+ if (prop === "translationY") {
3407
+ const type = target.type;
3408
+ if (type === "spatialdrag") {
3409
+ return target.detail?.translation3D?.y ?? 0;
3410
+ }
3411
+ return void 0;
3412
+ }
3413
+ if (prop === "translationZ") {
3414
+ const type = target.type;
3415
+ if (type === "spatialdrag") {
3416
+ return target.detail?.translation3D?.z ?? 0;
3417
+ }
3418
+ return void 0;
3419
+ }
3420
+ if (prop === "quaternion") {
3421
+ const type = target.type;
3422
+ if (type === "spatialrotate") {
3423
+ return target.detail?.quaternion ?? {
3424
+ x: 0,
3425
+ y: 0,
3426
+ z: 0,
3427
+ w: 1
3428
+ };
3429
+ }
3430
+ return void 0;
3431
+ }
3432
+ if (prop === "magnification") {
3433
+ const type = target.type;
3434
+ if (type === "spatialmagnify") {
3435
+ return target.detail?.magnification ?? 1;
3436
+ }
3437
+ return void 0;
3438
+ }
3439
+ if (prop === "clientX") {
3440
+ const type = target.type;
3441
+ if (type === "spatialtap" || type === "spatialdragstart") {
3442
+ return target.detail?.globalLocation3D?.x ?? 0;
3443
+ }
3444
+ return void 0;
3445
+ }
3446
+ if (prop === "clientY") {
3447
+ const type = target.type;
3448
+ if (type === "spatialtap" || type === "spatialdragstart") {
3449
+ return target.detail?.globalLocation3D?.y ?? 0;
3450
+ }
3451
+ return void 0;
3452
+ }
3453
+ if (prop === "clientZ") {
3454
+ const type = target.type;
3455
+ if (type === "spatialtap" || type === "spatialdragstart") {
3456
+ return target.detail?.globalLocation3D?.z ?? 0;
3457
+ }
3458
+ return void 0;
3459
+ }
3460
+ const val = target[prop];
3461
+ return typeof val === "function" ? val.bind(target) : val;
3462
+ }
3463
+ });
3464
+ }
3465
+ var useEntityEvent = ({ instance, ...handlers }) => {
3466
+ const eventsSetRef = useRef8(/* @__PURE__ */ new Set());
3467
+ useEffect18(() => {
3468
+ const entity = instance.entity;
3469
+ if (!entity) return;
3470
+ Object.entries(eventMap).forEach(([reactKey, spatialEvent]) => {
3471
+ const handlerFn = handlers[reactKey];
3472
+ if (!handlerFn) return;
3473
+ const wrapped = (ev) => handlerFn(createEventProxy2(ev, instance));
3474
+ entity.addEvent(spatialEvent, wrapped);
3475
+ eventsSetRef.current.add(reactKey);
3476
+ });
3477
+ return () => {
3478
+ };
3479
+ }, [instance.entity, ...Object.values(handlers)]);
3480
+ useEffect18(() => {
3481
+ const entity = instance.entity;
3482
+ if (!entity) return;
3483
+ return () => {
3484
+ for (let x of eventsSetRef.current) {
3485
+ entity.removeEvent(x);
3486
+ }
3487
+ eventsSetRef.current.clear();
3488
+ };
3489
+ }, [instance.entity]);
3490
+ return null;
3491
+ };
3492
+
3493
+ // src/reality/hooks/useEntityId.tsx
3494
+ import { useEffect as useEffect19 } from "react";
3495
+ var useEntityId = ({ id, entity }) => {
3496
+ const ctx = useRealityContext();
3497
+ useEffect19(() => {
3498
+ if (!id || !entity || !ctx) return;
3499
+ ctx.resourceRegistry.add(id, Promise.resolve(entity));
3500
+ return () => {
3501
+ ctx.resourceRegistry.remove(id);
3502
+ };
3503
+ }, [id, entity, ctx]);
3504
+ return null;
3505
+ };
3506
+
3507
+ // src/reality/hooks/useEntity.tsx
3508
+ import { useEffect as useEffect20, useRef as useRef9 } from "react";
3509
+ var useEntity = ({
3510
+ ref,
3511
+ id,
3512
+ position,
3513
+ rotation,
3514
+ scale,
3515
+ onSpatialTap,
3516
+ onSpatialDragStart,
3517
+ onSpatialDrag,
3518
+ onSpatialDragEnd,
3519
+ // onSpatialRotateStart,
3520
+ onSpatialRotate,
3521
+ onSpatialRotateEnd,
3522
+ // onSpatialMagnifyStart,
3523
+ onSpatialMagnify,
3524
+ onSpatialMagnifyEnd,
3525
+ // TODO: add other event handlers
3526
+ createEntity
3527
+ }) => {
3528
+ const ctx = useRealityContext();
3529
+ const parent = useParentContext();
3530
+ const instanceRef = useRef9(new EntityRef(null, ctx));
3531
+ const forceUpdate = useForceUpdate2();
3532
+ useEffect20(() => {
3533
+ if (!ctx) return;
3534
+ const controller = new AbortController();
3535
+ const init = async () => {
3536
+ try {
3537
+ const ent = await createEntity(controller.signal);
3538
+ if (!ent) return;
3539
+ if (controller.signal.aborted) {
3540
+ ent.destroy();
3541
+ return;
3542
+ }
3543
+ if (parent) {
3544
+ const result = await parent.addEntity(ent);
3545
+ if (!result.success) throw new Error("parent.addEntity failed");
3546
+ } else {
3547
+ const result = await ctx.reality.addEntity(ent);
3548
+ if (!result.success) throw new Error("ctx.reality.addEntity failed");
3549
+ }
3550
+ instanceRef.current?.updateEntity(ent);
3551
+ forceUpdate();
3552
+ } catch (error) {
3553
+ console.error("useEntity init ~ error:", error);
3554
+ }
3555
+ };
3556
+ init();
3557
+ return () => {
3558
+ controller.abort();
3559
+ instanceRef.current?.destroy();
3560
+ };
3561
+ }, [ctx, parent]);
3562
+ useEntityId({ id, entity: instanceRef.current.entity });
3563
+ useEntityTransform(instanceRef.current.entity, { position, rotation, scale });
3564
+ useEntityRef(ref, instanceRef.current);
3565
+ useEntityEvent({
3566
+ instance: instanceRef.current,
3567
+ onSpatialTap,
3568
+ onSpatialDragStart,
3569
+ onSpatialDrag,
3570
+ onSpatialDragEnd,
3571
+ // onSpatialRotateStart,
3572
+ onSpatialRotate,
3573
+ onSpatialRotateEnd,
3574
+ // onSpatialMagnifyStart,
3575
+ onSpatialMagnify,
3576
+ onSpatialMagnifyEnd
3577
+ });
3578
+ return instanceRef.current.entity;
3579
+ };
3580
+
3581
+ // src/reality/hooks/useForceUpdate.tsx
3582
+ import { useCallback as useCallback7, useState as useState7 } from "react";
3583
+ var useForceUpdate2 = () => {
3584
+ const [, setTick] = useState7(0);
3585
+ return useCallback7(() => setTick((tick) => tick + 1), []);
3586
+ };
3587
+
3588
+ // src/reality/components/BaseEntity.tsx
3589
+ import { jsx as jsx12 } from "react/jsx-runtime";
3590
+ var BaseEntity = forwardRef10(
3591
+ ({ children, createEntity, ...rest }, ref) => {
3592
+ const ctx = useRealityContext();
3593
+ const entity = useEntity({
3594
+ ...rest,
3595
+ ref,
3596
+ createEntity: (signal) => createEntity(ctx, signal)
3597
+ });
3598
+ if (!entity) return null;
3599
+ return /* @__PURE__ */ jsx12(ParentContext.Provider, { value: entity, children });
3600
+ }
3601
+ );
3602
+
3603
+ // src/reality/components/Entity.tsx
3604
+ import { jsx as jsx13 } from "react/jsx-runtime";
3605
+ var Entity = forwardRef11((props, ref) => {
3606
+ const { id, name, children, ...rest } = props;
3607
+ return /* @__PURE__ */ jsx13(
3608
+ BaseEntity,
3609
+ {
3610
+ ...rest,
3611
+ id,
3612
+ ref,
3613
+ createEntity: async (ctxVal) => ctxVal.session.createEntity({ id, name }),
3614
+ children
3615
+ }
3616
+ );
3617
+ });
3618
+
3619
+ // src/reality/components/BoxEntity.tsx
3620
+ import { forwardRef as forwardRef13 } from "react";
3621
+
3622
+ // src/reality/components/GeometryEntity.tsx
3623
+ import { forwardRef as forwardRef12 } from "react";
3624
+ import { jsx as jsx14 } from "react/jsx-runtime";
3625
+ var GeometryEntity = forwardRef12(
3626
+ ({ id, children, name, materials, geometryOptions, createGeometry, ...rest }, ref) => {
3627
+ return /* @__PURE__ */ jsx14(
3628
+ BaseEntity,
3629
+ {
3630
+ ...rest,
3631
+ id,
3632
+ ref,
3633
+ createEntity: async (ctx, signal) => {
3634
+ const manager = new AbortResourceManager(signal);
3635
+ try {
3636
+ const ent = await manager.addResource(
3637
+ () => ctx.session.createEntity({ id, name })
3638
+ );
3639
+ const geometry = await manager.addResource(
3640
+ () => createGeometry(geometryOptions)
3641
+ );
3642
+ const materialList = await Promise.all(
3643
+ materials?.map((id2) => ctx.resourceRegistry.get(id2)).filter(Boolean) ?? []
3644
+ );
3645
+ const modelComponent = await manager.addResource(
3646
+ () => ctx.session.createModelComponent({
3647
+ mesh: geometry,
3648
+ materials: materialList
3649
+ })
3650
+ );
3651
+ await ent.addComponent(modelComponent);
3652
+ return ent;
3653
+ } catch (error) {
3654
+ await manager.dispose();
3655
+ return null;
3656
+ }
3657
+ },
3658
+ children
3659
+ }
3660
+ );
3661
+ }
3662
+ );
3663
+
3664
+ // src/reality/components/BoxEntity.tsx
3665
+ import { jsx as jsx15 } from "react/jsx-runtime";
3666
+ var BoxEntity = forwardRef13(
3667
+ ({ children, ...props }, ref) => {
3668
+ const ctx = useRealityContext();
3669
+ return /* @__PURE__ */ jsx15(
3670
+ GeometryEntity,
3671
+ {
3672
+ ...props,
3673
+ ref,
3674
+ createGeometry: (options) => ctx.session.createBoxGeometry(options),
3675
+ geometryOptions: {
3676
+ width: props.width,
3677
+ height: props.height,
3678
+ depth: props.depth,
3679
+ cornerRadius: props.cornerRadius,
3680
+ splitFaces: props.splitFaces
3681
+ },
3682
+ children
3683
+ }
3684
+ );
3685
+ }
3686
+ );
3687
+
3688
+ // src/reality/components/UnlitMaterial.tsx
3689
+ import { useEffect as useEffect21, useRef as useRef10 } from "react";
3690
+ var UnlitMaterial = ({ children, ...options }) => {
3691
+ const ctx = useRealityContext();
3692
+ const materialRef = useRef10();
3693
+ useEffect21(() => {
3694
+ if (!ctx) return;
3695
+ const { session, reality, resourceRegistry } = ctx;
3696
+ const init = async () => {
3697
+ const materialPromise = session.createUnlitMaterial(options);
3698
+ resourceRegistry.add(options.id, materialPromise);
3699
+ try {
3700
+ const mat = await materialPromise;
3701
+ materialRef.current = mat;
3702
+ } catch (error) {
3703
+ console.error(" ~ UnlitMaterial ~ error:", error);
3704
+ }
3705
+ };
3706
+ init();
3707
+ return () => {
3708
+ resourceRegistry.removeAndDestroy(options.id);
3709
+ };
3710
+ }, [ctx]);
3711
+ return null;
3712
+ };
3713
+
3714
+ // src/reality/components/SphereEntity.tsx
3715
+ import { forwardRef as forwardRef14 } from "react";
3716
+ import { jsx as jsx16 } from "react/jsx-runtime";
3717
+ var SphereEntity = forwardRef14(
3718
+ ({ children, ...props }, ref) => {
3719
+ const ctx = useRealityContext();
3720
+ return /* @__PURE__ */ jsx16(
3721
+ GeometryEntity,
3722
+ {
3723
+ ...props,
3724
+ ref,
3725
+ createGeometry: (options) => ctx.session.createSphereGeometry(options),
3726
+ geometryOptions: {
3727
+ radius: props.radius
3728
+ },
3729
+ children
3730
+ }
3731
+ );
3732
+ }
3733
+ );
3734
+
3735
+ // src/reality/components/ConeEntity.tsx
3736
+ import { forwardRef as forwardRef15 } from "react";
3737
+ import { jsx as jsx17 } from "react/jsx-runtime";
3738
+ var ConeEntity = forwardRef15(
3739
+ ({ children, ...props }, ref) => {
3740
+ const ctx = useRealityContext();
3741
+ return /* @__PURE__ */ jsx17(
3742
+ GeometryEntity,
3743
+ {
3744
+ ...props,
3745
+ ref,
3746
+ createGeometry: (options) => ctx.session.createConeGeometry(options),
3747
+ geometryOptions: {
3748
+ radius: props.radius,
3749
+ height: props.height
3750
+ },
3751
+ children
3752
+ }
3753
+ );
3754
+ }
3755
+ );
3756
+
3757
+ // src/reality/components/CylinderEntity.tsx
3758
+ import { forwardRef as forwardRef16 } from "react";
3759
+ import { jsx as jsx18 } from "react/jsx-runtime";
3760
+ var CylinderEntity = forwardRef16(
3761
+ ({ children, ...props }, ref) => {
3762
+ const ctx = useRealityContext();
3763
+ return /* @__PURE__ */ jsx18(
3764
+ GeometryEntity,
3765
+ {
3766
+ ...props,
3767
+ ref,
3768
+ createGeometry: (options) => ctx.session.createCylinderGeometry(options),
3769
+ geometryOptions: {
3770
+ radius: props.radius,
3771
+ height: props.height
3772
+ },
3773
+ children
3774
+ }
3775
+ );
3776
+ }
3777
+ );
3778
+
3779
+ // src/reality/components/PlaneEntity.tsx
3780
+ import { forwardRef as forwardRef17 } from "react";
3781
+ import { jsx as jsx19 } from "react/jsx-runtime";
3782
+ var PlaneEntity = forwardRef17(
3783
+ ({ children, ...props }, ref) => {
3784
+ const ctx = useRealityContext();
3785
+ return /* @__PURE__ */ jsx19(
3786
+ GeometryEntity,
3787
+ {
3788
+ ...props,
3789
+ ref,
3790
+ createGeometry: (options) => ctx.session.createPlaneGeometry(options),
3791
+ geometryOptions: {
3792
+ width: props.width,
3793
+ height: props.height,
3794
+ cornerRadius: props.cornerRadius
3795
+ },
3796
+ children
3797
+ }
3798
+ );
3799
+ }
3800
+ );
3801
+
3802
+ // src/reality/components/SceneGraph.tsx
3803
+ import { jsx as jsx20 } from "react/jsx-runtime";
3804
+ var SceneGraph = ({ children }) => {
3805
+ return /* @__PURE__ */ jsx20(ParentContext.Provider, { value: null, children });
3806
+ };
3807
+
3808
+ // src/reality/components/ModelAsset.tsx
3809
+ import { useEffect as useEffect22, useRef as useRef11 } from "react";
3810
+ var resolveAssetUrl = (url) => {
3811
+ if (url.startsWith("http://") || url.startsWith("https://")) {
3812
+ return url;
3813
+ }
3814
+ return new URL(url, window.location.href).href;
3815
+ };
3816
+ var ModelAsset = ({ children, ...options }) => {
3817
+ const ctx = useRealityContext();
3818
+ const materialRef = useRef11();
3819
+ useEffect22(() => {
3820
+ const controller = new AbortController();
3821
+ if (!ctx) return;
3822
+ const { session, reality, resourceRegistry } = ctx;
3823
+ const init = async () => {
3824
+ try {
3825
+ const resolvedUrl = resolveAssetUrl(options.src);
3826
+ const modelAssetPromise = session.createModelAsset({ url: resolvedUrl });
3827
+ resourceRegistry.add(options.id, modelAssetPromise);
3828
+ const mat = await modelAssetPromise;
3829
+ if (controller.signal.aborted) {
3830
+ mat.destroy();
3831
+ return;
3832
+ }
3833
+ materialRef.current = mat;
3834
+ options.onLoad?.();
3835
+ } catch (error) {
3836
+ options.onError?.(error);
3837
+ }
3838
+ };
3839
+ init();
3840
+ return () => {
3841
+ controller.abort();
3842
+ materialRef.current?.destroy();
3843
+ };
3844
+ }, [ctx]);
3845
+ return null;
3846
+ };
3847
+
3848
+ // src/reality/components/ModelEntity.tsx
3849
+ import { forwardRef as forwardRef18 } from "react";
3850
+ import { jsx as jsx21 } from "react/jsx-runtime";
3851
+ var ModelEntity = forwardRef18(
3852
+ ({ id, model, children, name, ...rest }, ref) => {
3853
+ return /* @__PURE__ */ jsx21(
3854
+ BaseEntity,
3855
+ {
3856
+ ...rest,
3857
+ id,
3858
+ ref,
3859
+ createEntity: async (ctx, signal) => {
3860
+ try {
3861
+ const modelAsset = await ctx.resourceRegistry.get(model);
3862
+ if (!modelAsset)
3863
+ throw new Error(`ModelEntity: model not found ${model}`);
3864
+ if (signal.aborted) return null;
3865
+ return ctx.session.createSpatialModelEntity(
3866
+ {
3867
+ modelAssetId: modelAsset.id,
3868
+ name
3869
+ },
3870
+ { id, name }
3871
+ );
3872
+ } catch (error) {
3873
+ return null;
3874
+ }
3875
+ },
3876
+ children
3877
+ }
3878
+ );
3879
+ }
3880
+ );
3881
+
3882
+ // src/reality/components/Reality.tsx
3883
+ import {
3884
+ forwardRef as forwardRef19,
3885
+ useCallback as useCallback8,
3886
+ useEffect as useEffect23,
3887
+ useRef as useRef12,
3888
+ useState as useState8
3889
+ } from "react";
3890
+ import { Fragment as Fragment3, jsx as jsx22, jsxs as jsxs3 } from "react/jsx-runtime";
3891
+ var Reality = forwardRef19(
3892
+ function RealityBase({ children, ...inProps }, ref) {
3893
+ const insideAttachment = useInsideAttachment();
3894
+ if (insideAttachment) {
3895
+ console.warn(
3896
+ "[WebSpatial] Reality cannot be used inside AttachmentAsset."
3897
+ );
3898
+ return null;
3899
+ }
3900
+ const {
3901
+ onSpatialTap,
3902
+ onSpatialDragStart,
3903
+ onSpatialDrag,
3904
+ onSpatialDragEnd,
3905
+ onSpatialRotate,
3906
+ onSpatialRotateEnd,
3907
+ onSpatialMagnify,
3908
+ onSpatialMagnifyEnd,
3909
+ ...props
3910
+ } = inProps;
3911
+ const ctxRef = useRef12(null);
3912
+ const creationId = useRef12(0);
3913
+ const [isReady, setIsReady] = useState8(false);
3914
+ const cleanupReality = useCallback8(() => {
3915
+ ctxRef.current?.attachmentRegistry.destroy();
3916
+ ctxRef.current?.resourceRegistry.destroy();
3917
+ ctxRef.current?.reality.destroy();
3918
+ ctxRef.current = null;
3919
+ setIsReady(false);
3920
+ }, []);
3921
+ useEffect23(() => {
3922
+ return () => {
3923
+ creationId.current++;
3924
+ cleanupReality();
3925
+ };
3926
+ }, [cleanupReality]);
3927
+ const createReality = useCallback8(async () => {
3928
+ const id = ++creationId.current;
3929
+ const resourceRegistry = new ResourceRegistry();
3930
+ const attachmentRegistry = new AttachmentRegistry();
3931
+ const session = await getSession();
3932
+ if (!session) {
3933
+ resourceRegistry.destroy();
3934
+ attachmentRegistry.destroy();
3935
+ return null;
3936
+ }
3937
+ const reality = await session.createSpatializedDynamic3DElement();
3938
+ const isCancelled = () => id !== creationId.current;
3939
+ if (isCancelled()) {
3940
+ resourceRegistry.destroy();
3941
+ attachmentRegistry.destroy();
3942
+ reality.destroy();
3943
+ return null;
3944
+ }
3945
+ try {
3946
+ const result = await session.getSpatialScene().addSpatializedElement(reality);
3947
+ if (!result.success || isCancelled()) {
3948
+ resourceRegistry.destroy();
3949
+ attachmentRegistry.destroy();
3950
+ reality.destroy();
3951
+ return null;
3952
+ }
3953
+ cleanupReality();
3954
+ ctxRef.current = {
3955
+ session,
3956
+ reality,
3957
+ resourceRegistry,
3958
+ attachmentRegistry
3959
+ };
3960
+ setIsReady(true);
3961
+ return reality;
3962
+ } catch (err) {
3963
+ console.error("[createReality] failed", err);
3964
+ resourceRegistry.destroy();
3965
+ attachmentRegistry.destroy();
3966
+ reality.destroy();
3967
+ return null;
3968
+ }
3969
+ }, [cleanupReality]);
3970
+ const content = useCallback8(() => /* @__PURE__ */ jsx22(Fragment3, {}), []);
3971
+ return /* @__PURE__ */ jsxs3(RealityContext.Provider, { value: ctxRef.current, children: [
3972
+ /* @__PURE__ */ jsx22(
3973
+ SpatializedContainer,
3974
+ {
3975
+ component: "div",
3976
+ ref,
3977
+ createSpatializedElement: createReality,
3978
+ spatializedContent: content,
3979
+ ...props
3980
+ }
3981
+ ),
3982
+ isReady && children
3983
+ ] });
3984
+ }
3985
+ );
3986
+
3987
+ // src/reality/components/AttachmentAsset.tsx
3988
+ import { useEffect as useEffect24, useState as useState9 } from "react";
3989
+ import { createPortal as createPortal3 } from "react-dom";
3990
+ import { jsx as jsx23 } from "react/jsx-runtime";
3991
+ var AttachmentAsset = ({
3992
+ name,
3993
+ children
3994
+ }) => {
3995
+ const ctx = useRealityContext();
3996
+ const [containers, setContainers] = useState9([]);
3997
+ useEffect24(() => {
3998
+ if (!ctx) return;
3999
+ return ctx.attachmentRegistry.onContainersChange(name, setContainers);
4000
+ }, [ctx, name]);
4001
+ if (!containers.length) return null;
4002
+ return /* @__PURE__ */ jsx23(InsideAttachmentContext.Provider, { value: true, children: containers.map((c, idx) => createPortal3(children, c, `${name}-${idx}`)) });
4003
+ };
4004
+
4005
+ // src/reality/components/AttachmentEntity.tsx
4006
+ import { useEffect as useEffect25, useRef as useRef13, useState as useState10 } from "react";
4007
+ var instanceCounter = 0;
4008
+ var AttachmentEntity = ({
4009
+ attachment: attachmentName,
4010
+ position,
4011
+ size
4012
+ }) => {
4013
+ const ctx = useRealityContext();
4014
+ const parent = useParentContext();
4015
+ const attachmentRef = useRef13(null);
4016
+ const parentIdRef = useRef13(null);
4017
+ const instanceIdRef = useRef13(`att_${++instanceCounter}`);
4018
+ const attachmentNameRef = useRef13(attachmentName);
4019
+ const [childWindow, setChildWindow] = useState10(null);
4020
+ useEffect25(() => {
4021
+ if (!ctx || !parent) return;
4022
+ const parentId = parent.id;
4023
+ parentIdRef.current = parentId;
4024
+ let cancelled = false;
4025
+ const init = async () => {
4026
+ try {
4027
+ const att = await ctx.session.createAttachmentEntity({
4028
+ parentEntityId: parentId,
4029
+ position: position ?? [0, 0, 0],
4030
+ size
4031
+ });
4032
+ if (cancelled) {
4033
+ att.destroy();
4034
+ return;
4035
+ }
4036
+ const windowProxy = att.getWindowProxy();
4037
+ setOpenWindowStyle(windowProxy);
4038
+ windowProxy.document.body.style.display = "block";
4039
+ windowProxy.document.body.style.minWidth = "100%";
4040
+ windowProxy.document.body.style.maxWidth = "100%";
4041
+ windowProxy.document.body.style.minHeight = "100%";
4042
+ await syncParentHeadToChild(windowProxy);
4043
+ const viewport = windowProxy.document.querySelector(
4044
+ 'meta[name="viewport"]'
4045
+ );
4046
+ if (!viewport) {
4047
+ const meta = windowProxy.document.createElement("meta");
4048
+ meta.name = "viewport";
4049
+ meta.content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no";
4050
+ windowProxy.document.head.appendChild(meta);
4051
+ }
4052
+ const base = windowProxy.document.createElement("base");
4053
+ base.href = document.baseURI;
4054
+ windowProxy.document.head.appendChild(base);
4055
+ attachmentRef.current = att;
4056
+ setChildWindow(windowProxy);
4057
+ ctx.attachmentRegistry.addContainer(
4058
+ attachmentNameRef.current,
4059
+ instanceIdRef.current,
4060
+ att.getContainer()
4061
+ );
4062
+ } catch (error) {
4063
+ console.error("[AttachmentEntity] init error:", error);
4064
+ }
4065
+ };
4066
+ init();
4067
+ return () => {
4068
+ cancelled = true;
4069
+ const att = attachmentRef.current;
4070
+ if (att) {
4071
+ ctx.attachmentRegistry.removeContainer(
4072
+ attachmentNameRef.current,
4073
+ instanceIdRef.current
4074
+ );
4075
+ att.destroy();
4076
+ attachmentRef.current = null;
4077
+ setChildWindow(null);
4078
+ }
4079
+ };
4080
+ }, [ctx, parent]);
4081
+ useEffect25(() => {
4082
+ if (!ctx) return;
4083
+ const att = attachmentRef.current;
4084
+ const prevName = attachmentNameRef.current;
4085
+ if (att && prevName !== attachmentName) {
4086
+ ctx.attachmentRegistry.removeContainer(prevName, instanceIdRef.current);
4087
+ ctx.attachmentRegistry.addContainer(
4088
+ attachmentName,
4089
+ instanceIdRef.current,
4090
+ att.getContainer()
4091
+ );
4092
+ attachmentNameRef.current = attachmentName;
4093
+ } else {
4094
+ attachmentNameRef.current = attachmentName;
4095
+ }
4096
+ }, [ctx, attachmentName]);
4097
+ useSyncHeadStyles(childWindow, { subtree: false });
4098
+ useEffect25(() => {
4099
+ if (!attachmentRef.current) return;
4100
+ attachmentRef.current.update({ position, size });
4101
+ }, [position?.[0], position?.[1], position?.[2], size?.width, size?.height]);
4102
+ return null;
4103
+ };
4104
+
4105
+ // src/Model.tsx
4106
+ import { forwardRef as forwardRef20 } from "react";
4107
+ import { Spatial as Spatial2 } from "@webspatial/core-sdk";
4108
+ import { jsx as jsx24 } from "react/jsx-runtime";
4109
+ var spatial2 = new Spatial2();
4110
+ function ModelBase(props, ref) {
4111
+ const insideAttachment = useInsideAttachment();
4112
+ const { "enable-xr": enableXR, ...restProps } = props;
4113
+ if (!enableXR || !spatial2.runInSpatialWeb() || insideAttachment) {
4114
+ const {
4115
+ onSpatialTap,
4116
+ onSpatialDragStart,
4117
+ onSpatialDrag,
4118
+ onSpatialDragEnd,
4119
+ onSpatialRotate,
4120
+ onSpatialRotateEnd,
4121
+ onSpatialMagnify,
4122
+ onSpatialMagnifyEnd,
4123
+ ...modelProps
4124
+ } = restProps;
4125
+ return /* @__PURE__ */ jsx24("model", { ref, ...modelProps });
4126
+ }
4127
+ return /* @__PURE__ */ jsx24(SpatializedStatic3DElementContainer, { ref, ...restProps });
4128
+ }
4129
+ var Model = withSSRSupported(forwardRef20(ModelBase));
4130
+ Model.displayName = "Model";
4131
+
4132
+ // src/index.ts
4133
+ var version = "1.2.1";
4134
+ if (typeof window !== "undefined") {
4135
+ initPolyfill();
4136
+ }
4137
+ export {
4138
+ AttachmentAsset,
4139
+ AttachmentEntity,
4140
+ BoxEntity,
4141
+ ConeEntity,
4142
+ CylinderEntity,
4143
+ Entity,
4144
+ Model,
4145
+ ModelAsset,
4146
+ ModelEntity,
4147
+ PlaneEntity,
4148
+ Reality,
4149
+ SSRProvider,
4150
+ SceneGraph,
4151
+ SpatialMonitor,
4152
+ Spatialized2DElementContainer,
4153
+ SpatializedContainer,
4154
+ SpatializedStatic3DElementContainer,
4155
+ SphereEntity,
4156
+ UnlitMaterial,
4157
+ enableDebugTool,
4158
+ eventMap,
4159
+ initPolyfill,
4160
+ initScene,
4161
+ toLocalSpace,
4162
+ toSceneSpatial,
4163
+ version,
4164
+ withSpatialMonitor,
4165
+ withSpatialized2DElementContainer
4166
+ };
4167
+ //# sourceMappingURL=index.js.map