oomi-ai 0.2.49 → 0.3.0

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