@webspatial/react-sdk 0.1.10 → 0.1.11

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.
@@ -1,8 +1,3488 @@
1
- // src/jsx/spatialPolyfill.ts
2
- import { getSession as getSession2, parseCornerRadius, XRApp as XRApp2 } from "@webspatial/react-sdk";
1
+ // src/spatial-react-components/SpatialPrimitive.tsx
2
+ import { forwardRef as forwardRef3 } from "react";
3
3
 
4
- // src/jsx/injectSceneHook.ts
5
- import { defaultSceneConfig, XRApp, getSession } from "@webspatial/react-sdk";
4
+ // src/spatial-react-components/primitives.ts
5
+ var primitives = [
6
+ "a",
7
+ "abbr",
8
+ "address",
9
+ "area",
10
+ "article",
11
+ "aside",
12
+ "audio",
13
+ "b",
14
+ "base",
15
+ "bdi",
16
+ "bdo",
17
+ "big",
18
+ "blockquote",
19
+ "body",
20
+ "br",
21
+ "button",
22
+ "canvas",
23
+ "caption",
24
+ "cite",
25
+ "code",
26
+ "col",
27
+ "colgroup",
28
+ "data",
29
+ "datalist",
30
+ "dd",
31
+ "del",
32
+ "details",
33
+ "dfn",
34
+ "dialog",
35
+ "div",
36
+ "dl",
37
+ "dt",
38
+ "em",
39
+ "embed",
40
+ "fieldset",
41
+ "figcaption",
42
+ "figure",
43
+ "footer",
44
+ "form",
45
+ "h1",
46
+ "h2",
47
+ "h3",
48
+ "h4",
49
+ "h5",
50
+ "h6",
51
+ "head",
52
+ "header",
53
+ "hgroup",
54
+ "hr",
55
+ "html",
56
+ "i",
57
+ "iframe",
58
+ "img",
59
+ "input",
60
+ "ins",
61
+ "kbd",
62
+ "keygen",
63
+ "label",
64
+ "legend",
65
+ "li",
66
+ "link",
67
+ "main",
68
+ "map",
69
+ "mark",
70
+ "menu",
71
+ "menuitem",
72
+ "meta",
73
+ "meter",
74
+ "nav",
75
+ "noscript",
76
+ "object",
77
+ "ol",
78
+ "optgroup",
79
+ "option",
80
+ "output",
81
+ "p",
82
+ "param",
83
+ "picture",
84
+ "pre",
85
+ "progress",
86
+ "q",
87
+ "rp",
88
+ "rt",
89
+ "ruby",
90
+ "s",
91
+ "samp",
92
+ "script",
93
+ "section",
94
+ "select",
95
+ "small",
96
+ "source",
97
+ "span",
98
+ "strong",
99
+ "style",
100
+ "sub",
101
+ "summary",
102
+ "sup",
103
+ "table",
104
+ "tbody",
105
+ "td",
106
+ "textarea",
107
+ "tfoot",
108
+ "th",
109
+ "thead",
110
+ "time",
111
+ "title",
112
+ "tr",
113
+ "track",
114
+ "u",
115
+ "ul",
116
+ "var",
117
+ "video",
118
+ "wbr",
119
+ // SVG
120
+ "circle",
121
+ "clipPath",
122
+ "defs",
123
+ "ellipse",
124
+ "foreignObject",
125
+ "g",
126
+ "image",
127
+ "line",
128
+ "linearGradient",
129
+ "mask",
130
+ "path",
131
+ "pattern",
132
+ "polygon",
133
+ "polyline",
134
+ "radialGradient",
135
+ "rect",
136
+ "stop",
137
+ "svg",
138
+ "text",
139
+ "tspan"
140
+ ];
141
+
142
+ // src/spatial-react-components/SpatialReactComponent/SpatialReactComponent.tsx
143
+ import { forwardRef as forwardRef2, useMemo as useMemo2, useContext as useContext4 } from "react";
144
+
145
+ // src/utils/getSession.ts
146
+ import { Spatial } from "@webspatial/core-sdk";
147
+ var spatial = null;
148
+ var _currentSession = null;
149
+ function getSession() {
150
+ if (!spatial) {
151
+ spatial = new Spatial();
152
+ }
153
+ if (!spatial.isSupported()) {
154
+ return null;
155
+ }
156
+ if (_currentSession) {
157
+ return _currentSession;
158
+ }
159
+ _currentSession = spatial.requestSession();
160
+ return _currentSession;
161
+ }
162
+
163
+ // src/spatial-react-components/SpatialReactComponent/StandardInstance.tsx
164
+ import {
165
+ useRef,
166
+ useLayoutEffect,
167
+ useEffect,
168
+ useContext,
169
+ forwardRef
170
+ } from "react";
171
+
172
+ // src/spatial-react-components/hooks/useForceUpdate.ts
173
+ import { useState } from "react";
174
+ function useForceUpdate() {
175
+ const [, setToggle] = useState(false);
176
+ return () => setToggle((toggle) => !toggle);
177
+ }
178
+
179
+ // src/spatial-react-components/SpatialReactComponent/SpatialIsStandardInstanceContext.ts
180
+ import { createContext } from "react";
181
+ var SpatialIsStandardInstanceContext = createContext(
182
+ null
183
+ );
184
+
185
+ // src/spatial-react-components/SpatialReactComponent/SpatialReactContext.ts
186
+ import { createContext as createContext2 } from "react";
187
+
188
+ // src/spatial-react-components/SpatialReactComponent/const.ts
189
+ var SpatialID = "data-spatial-id";
190
+
191
+ // src/spatial-react-components/SpatialReactComponent/SpatialReactContext.ts
192
+ var SpatialReactContextObject = class {
193
+ debugName;
194
+ constructor(debugName) {
195
+ this.debugName = debugName;
196
+ }
197
+ dom = null;
198
+ domSpatialId = null;
199
+ fns = {};
200
+ // cache dom for each spatialId
201
+ spatialId2dom = {};
202
+ spatialId2parentSpatialDom = {};
203
+ // layer : [standardInstance sequence, portalInstance sequence]
204
+ layerSequences = {};
205
+ getSpatialID(layer, isInStandardInstance, debugName = "") {
206
+ if (this.layerSequences[layer] === void 0) {
207
+ this.layerSequences[layer] = [0, 0];
208
+ }
209
+ const idx = isInStandardInstance ? 0 : 1;
210
+ const sequenceId = this.layerSequences[layer][idx];
211
+ this.layerSequences[layer][idx] = sequenceId + 1;
212
+ const spatialId = `${debugName}_${layer}_${sequenceId}`;
213
+ return spatialId;
214
+ }
215
+ onDomChange(spatialId, fn) {
216
+ this.fns[spatialId] = fn;
217
+ if (this.dom) {
218
+ fn();
219
+ }
220
+ }
221
+ offDomChange(spatialId) {
222
+ delete this.fns[spatialId];
223
+ delete this.spatialId2dom[spatialId];
224
+ delete this.spatialId2parentSpatialDom[spatialId];
225
+ }
226
+ notifyDomChange(dom) {
227
+ this.dom = dom;
228
+ this.domSpatialId = dom.getAttribute(SpatialID);
229
+ Object.values(this.fns).forEach((fn) => fn());
230
+ }
231
+ querySpatialDom(spatialId) {
232
+ if (this.domSpatialId === spatialId) {
233
+ return this.dom;
234
+ }
235
+ if (!this.dom) {
236
+ return null;
237
+ }
238
+ if (!this.spatialId2dom[spatialId]) {
239
+ const spatialDom = this.dom.querySelector(`[${SpatialID}="${spatialId}"]`);
240
+ if (spatialDom) {
241
+ this.spatialId2dom[spatialId] = spatialDom;
242
+ }
243
+ }
244
+ return this.spatialId2dom[spatialId];
245
+ }
246
+ queryParentSpatialDom(spatialId) {
247
+ if (this.domSpatialId === spatialId) {
248
+ return null;
249
+ }
250
+ if (this.spatialId2parentSpatialDom[spatialId]) {
251
+ return this.spatialId2parentSpatialDom[spatialId];
252
+ }
253
+ let spatialDom = this.querySpatialDom(spatialId);
254
+ if (spatialDom) {
255
+ if (spatialDom === this.dom) return null;
256
+ let parentSpatialDom = spatialDom.parentElement;
257
+ while (parentSpatialDom && spatialDom !== this.dom) {
258
+ if (parentSpatialDom.hasAttribute(SpatialID)) {
259
+ break;
260
+ } else {
261
+ parentSpatialDom = parentSpatialDom.parentElement;
262
+ }
263
+ }
264
+ this.spatialId2parentSpatialDom[spatialId] = parentSpatialDom;
265
+ return parentSpatialDom;
266
+ }
267
+ return null;
268
+ }
269
+ // Used for CSSModel3D
270
+ // layer : [standardInstance sequence, portalInstance sequence]
271
+ subDivLayerSequences = {};
272
+ subDivEventHandlers = {};
273
+ getSubDivSpatialID(layer, isInStandardInstance, prefix = "") {
274
+ if (this.subDivLayerSequences[layer] === void 0) {
275
+ this.subDivLayerSequences[layer] = [0, 0];
276
+ }
277
+ const idx = isInStandardInstance ? 0 : 1;
278
+ const sequenceId = this.subDivLayerSequences[layer][idx];
279
+ this.subDivLayerSequences[layer][idx] = sequenceId + 1;
280
+ const spatialId = `${prefix}_${layer}_${sequenceId}`;
281
+ return spatialId;
282
+ }
283
+ onSubDivEvent(subSpatialId, eventHandler) {
284
+ this.subDivEventHandlers[subSpatialId] = eventHandler;
285
+ }
286
+ offSubDivEvent(subSpatialId) {
287
+ delete this.subDivEventHandlers[subSpatialId];
288
+ }
289
+ notifySubDivEvent(subSpatialId, args) {
290
+ const eventHandler = this.subDivEventHandlers[subSpatialId];
291
+ if (eventHandler) {
292
+ eventHandler(args);
293
+ }
294
+ }
295
+ };
296
+ var SpatialReactContext = createContext2(null);
297
+
298
+ // src/spatial-react-components/SpatialReactComponent/StandardInstance.tsx
299
+ import { jsx } from "react/jsx-runtime";
300
+ function useDetectDomRectChange() {
301
+ const ref = useRef(null);
302
+ const forceUpdate = useForceUpdate();
303
+ const spatialReactContextObject = useContext(SpatialReactContext);
304
+ useLayoutEffect(() => {
305
+ ref.current && spatialReactContextObject?.notifyDomChange(ref.current);
306
+ });
307
+ useEffect(() => {
308
+ if (!ref.current || !spatialReactContextObject) {
309
+ console.warn(
310
+ "Ref is not attached to the DOM or spatialReactContextObject is not available"
311
+ );
312
+ return;
313
+ }
314
+ const handleResize = () => {
315
+ forceUpdate();
316
+ };
317
+ window.addEventListener("resize", handleResize);
318
+ return () => {
319
+ window.removeEventListener("resize", handleResize);
320
+ };
321
+ }, [spatialReactContextObject]);
322
+ useEffect(() => {
323
+ if (!ref.current) {
324
+ console.warn("Ref is not attached to the DOM");
325
+ return;
326
+ }
327
+ let ro = new ResizeObserver((elements) => {
328
+ forceUpdate();
329
+ });
330
+ ro.observe(ref.current);
331
+ return () => {
332
+ ro.disconnect();
333
+ };
334
+ }, []);
335
+ useEffect(() => {
336
+ if (!ref.current) {
337
+ console.warn("Ref is not attached to the DOM");
338
+ return;
339
+ }
340
+ let ro = new MutationObserver((elements) => {
341
+ forceUpdate();
342
+ });
343
+ ro.observe(ref.current, {
344
+ attributeFilter: ["class", "style"],
345
+ subtree: true
346
+ });
347
+ return () => {
348
+ ro.disconnect();
349
+ };
350
+ }, []);
351
+ return ref;
352
+ }
353
+ var StandardInstance = forwardRef(function(inProps, refIn) {
354
+ const { El, style: inStyle, debugShowStandardInstance, ...props } = inProps;
355
+ const extraStyle = {
356
+ visibility: debugShowStandardInstance ? void 0 : "hidden",
357
+ transition: "none"
358
+ };
359
+ const style = { ...inStyle, ...extraStyle };
360
+ var ref = useDetectDomRectChange();
361
+ const proxyRef = new Proxy(ref, {
362
+ get(target, prop, receiver) {
363
+ return Reflect.get(target, prop, receiver);
364
+ },
365
+ set(target, prop, value, receiver) {
366
+ if (prop === "current") {
367
+ const domElement = value;
368
+ if (refIn) {
369
+ if (typeof refIn === "function") {
370
+ refIn(domElement);
371
+ } else {
372
+ refIn.current = domElement;
373
+ }
374
+ }
375
+ }
376
+ return Reflect.set(target, prop, value, receiver);
377
+ }
378
+ });
379
+ return /* @__PURE__ */ jsx(SpatialIsStandardInstanceContext.Provider, { value: true, children: /* @__PURE__ */ jsx(El, { "data-standardinstance": true, ref: proxyRef, style, ...props }) });
380
+ });
381
+ StandardInstance.displayName = "StandardInstance";
382
+
383
+ // src/spatial-react-components/SpatialReactComponent/PortalInstance.tsx
384
+ import {
385
+ useRef as useRef3,
386
+ useCallback,
387
+ useEffect as useEffect3,
388
+ useState as useState2,
389
+ useContext as useContext3,
390
+ useMemo
391
+ } from "react";
392
+ import { createPortal } from "react-dom";
393
+
394
+ // src/spatial-react-components/SpatialReactComponent/usePortalContainer.tsx
395
+ import { useContext as useContext2, useRef as useRef2, useEffect as useEffect2 } from "react";
396
+
397
+ // src/spatial-react-components/SpatialReactComponent/SpatialWindowManager.ts
398
+ var SpatialWindowManager = class {
399
+ initPromise;
400
+ entity;
401
+ webview;
402
+ window = null;
403
+ parentSpatialWindowManager;
404
+ isFixedPosition;
405
+ constructor(options) {
406
+ this.isFixedPosition = options.isFixedPosition || false;
407
+ this.parentSpatialWindowManager = options.parentSpatialWindowManager;
408
+ }
409
+ setDebugName(debugName) {
410
+ this.entity?._setName(debugName);
411
+ }
412
+ async initInternal(url) {
413
+ this.entity = await getSession().createEntity();
414
+ this.webview = await getSession().createWindowComponent();
415
+ await this.webview.loadURL(url);
416
+ await this.entity.setCoordinateSpace("Dom");
417
+ await this.webview.setScrollWithParent(true);
418
+ await this.webview.setScrollEnabled(false);
419
+ await this.entity.setComponent(this.webview);
420
+ var wc = await getSession().getCurrentWindowComponent();
421
+ var ent = await wc.getEntity();
422
+ await this.entity.setParent(ent);
423
+ }
424
+ async initInternalFromWindow() {
425
+ var w = await getSession().createWindowContext();
426
+ this.window = w;
427
+ this.entity = await getSession().createEntity();
428
+ this.webview = await getSession().createWindowComponent();
429
+ await this.webview.setFromWindow(w);
430
+ await this.entity.setCoordinateSpace("Dom");
431
+ await this.webview.setScrollWithParent(true);
432
+ await this.webview.setScrollEnabled(false);
433
+ await this.entity.setComponent(this.webview);
434
+ this.setEntityParentByCSSPosition(this.isFixedPosition);
435
+ }
436
+ async updateCSSPosition(isFixedPosition) {
437
+ if (this.isFixedPosition === isFixedPosition) {
438
+ return;
439
+ } else {
440
+ this.isFixedPosition = isFixedPosition;
441
+ }
442
+ return this.setEntityParentByCSSPosition(isFixedPosition);
443
+ }
444
+ async setEntityParentByCSSPosition(isFixedPosition) {
445
+ if (this.initPromise) {
446
+ await this.initPromise;
447
+ if (isFixedPosition || !this.parentSpatialWindowManager) {
448
+ var wc = await getSession().getCurrentWindowComponent();
449
+ var ent = await wc.getEntity();
450
+ await this.entity.setParent(ent);
451
+ } else {
452
+ const parentSpatialWindowManager = this.parentSpatialWindowManager;
453
+ await parentSpatialWindowManager.initPromise;
454
+ this.entity.setParent(parentSpatialWindowManager.entity);
455
+ }
456
+ }
457
+ }
458
+ async init(url) {
459
+ this.initPromise = this.initInternal(url);
460
+ await this.initPromise;
461
+ }
462
+ async initFromWidow() {
463
+ this.initPromise = this.initInternalFromWindow();
464
+ await this.initPromise;
465
+ }
466
+ async resize(rect, offset, rotation = { x: 0, y: 0, z: 0, w: 1 }, scale = { x: 1, y: 1, z: 1 }, rotationOrigin = { x: 0, y: 0, z: 0 }, parrentOffset = 0) {
467
+ let targetPosX = rect.x + rect.width / 2;
468
+ let targetPosY = rect.y + rect.height / 2 + parrentOffset;
469
+ if (!this.webview) {
470
+ return;
471
+ }
472
+ var entity = this.entity;
473
+ entity.transform.position.x = targetPosX + (offset ? offset.x : 0);
474
+ entity.transform.position.y = targetPosY + (offset ? offset.y : 0);
475
+ entity.transform.position.z = offset ? offset.z : 0;
476
+ entity.transform.orientation.x = rotation.x;
477
+ entity.transform.orientation.y = rotation.y;
478
+ entity.transform.orientation.z = rotation.z;
479
+ entity.transform.orientation.w = rotation.w;
480
+ entity.transform.scale.x = scale.x;
481
+ entity.transform.scale.y = scale.y;
482
+ entity.transform.scale.z = scale.z;
483
+ await entity.updateTransform();
484
+ var webview = this.webview;
485
+ await webview.setResolution(rect.width, rect.height);
486
+ await webview.setRotationAnchor(rotationOrigin);
487
+ }
488
+ async setZIndex(zIndex) {
489
+ let entity = this.entity;
490
+ entity.updateZIndex(zIndex);
491
+ }
492
+ async destroy() {
493
+ if (this.initPromise) {
494
+ await this.initPromise;
495
+ this.entity?.destroy();
496
+ this.webview?.destroy();
497
+ this.window = null;
498
+ }
499
+ }
500
+ };
501
+
502
+ // src/spatial-react-components/SpatialReactComponent/SpatialWindowManagerContext.ts
503
+ import { createContext as createContext3 } from "react";
504
+ var SpatialWindowManagerContext = createContext3(null);
505
+
506
+ // src/spatial-react-components/SpatialReactComponent/usePortalContainer.tsx
507
+ function usePortalContainer(options) {
508
+ const isStandard = useContext2(SpatialIsStandardInstanceContext);
509
+ const parentSpatialWindowManager = useContext2(SpatialWindowManagerContext);
510
+ const forceUpdate = useForceUpdate();
511
+ const spatialWindowManagerRef = useRef2();
512
+ useEffect2(() => {
513
+ let isDestroyed = false;
514
+ let spawnedResult;
515
+ async function asyncCreatePortalContainer() {
516
+ const session = getSession();
517
+ let windowMgr = new SpatialWindowManager({
518
+ parentSpatialWindowManager: parentSpatialWindowManager || void 0
519
+ });
520
+ await windowMgr.initFromWidow();
521
+ if (isDestroyed) {
522
+ windowMgr.destroy();
523
+ return;
524
+ }
525
+ spawnedResult = await options.onContainerSpawned(windowMgr);
526
+ if (isDestroyed) {
527
+ options.onContainerDestroyed(windowMgr, spawnedResult);
528
+ windowMgr.destroy();
529
+ return;
530
+ }
531
+ spatialWindowManagerRef.current = windowMgr;
532
+ forceUpdate();
533
+ }
534
+ if (isStandard !== true) {
535
+ asyncCreatePortalContainer();
536
+ }
537
+ return () => {
538
+ isDestroyed = true;
539
+ const spatialWindowManager = spatialWindowManagerRef.current;
540
+ if (spatialWindowManager) {
541
+ options.onContainerDestroyed(spatialWindowManager, spawnedResult);
542
+ spatialWindowManager.destroy();
543
+ }
544
+ };
545
+ }, []);
546
+ return [spatialWindowManagerRef.current];
547
+ }
548
+
549
+ // src/spatial-react-components/SpatialReactComponent/utils.ts
550
+ function getInheritedStyleProps(computedStyle) {
551
+ var propNames = [
552
+ "azimuth",
553
+ "borderCollapse",
554
+ "borderSpacing",
555
+ "captionSide",
556
+ "color",
557
+ "cursor",
558
+ "direction",
559
+ "elevation",
560
+ "emptyCells",
561
+ "fontFamily",
562
+ "fontSize",
563
+ "fontStyle",
564
+ "fontVariant",
565
+ "fontWeight",
566
+ "font",
567
+ "letterSpacing",
568
+ "lineHeight",
569
+ "listStyleImage",
570
+ "listStylePosition",
571
+ "listStyleType",
572
+ "listStyle",
573
+ "orphans",
574
+ "pitchRange",
575
+ "pitch",
576
+ "quotes",
577
+ "richness",
578
+ "speakHeader",
579
+ "speakNumeral",
580
+ "speakPunctuation",
581
+ "speak",
582
+ "speechRate",
583
+ "stress",
584
+ "textAlign",
585
+ "textIndent",
586
+ "textTransform",
587
+ "visibility",
588
+ "voiceFamily",
589
+ "volume",
590
+ "whiteSpace",
591
+ "widows",
592
+ "wordSpacing",
593
+ // background also need to be synced
594
+ "background",
595
+ // position also need to be synced
596
+ "position",
597
+ "width",
598
+ "height"
599
+ ];
600
+ var props = {};
601
+ for (var cssName of propNames) {
602
+ if (computedStyle[cssName]) {
603
+ props[cssName] = computedStyle[cssName];
604
+ }
605
+ }
606
+ return props;
607
+ }
608
+ function domRect2rectType(from) {
609
+ return {
610
+ x: from.x,
611
+ y: from.y,
612
+ width: from.width,
613
+ height: from.height
614
+ };
615
+ }
616
+ function parseTransformOrigin(computedStyle) {
617
+ const transformOriginProperty = computedStyle.getPropertyValue("transform-origin");
618
+ const [x, y] = transformOriginProperty.split(" ").map(parseFloat);
619
+ const width = parseFloat(computedStyle.getPropertyValue("width"));
620
+ const height = parseFloat(computedStyle.getPropertyValue("height"));
621
+ return {
622
+ x: width > 0 ? x / width : 0.5,
623
+ y: height > 0 ? y / height : 0.5,
624
+ z: 0.5
625
+ };
626
+ }
627
+ function parseBorderRadius(borderProperty, width) {
628
+ if (borderProperty === "") {
629
+ return 0;
630
+ }
631
+ if (borderProperty.endsWith("%")) {
632
+ return width * parseFloat(borderProperty) / 100;
633
+ }
634
+ return parseFloat(borderProperty);
635
+ }
636
+ function parseCornerRadius(computedStyle) {
637
+ const width = parseFloat(computedStyle.getPropertyValue("width"));
638
+ const topLeftPropertyValue = computedStyle.getPropertyValue(
639
+ "border-top-left-radius"
640
+ );
641
+ const topRightPropertyValue = computedStyle.getPropertyValue(
642
+ "border-top-right-radius"
643
+ );
644
+ const bottomLeftPropertyValue = computedStyle.getPropertyValue(
645
+ "border-bottom-left-radius"
646
+ );
647
+ const bottomRightPropertyValue = computedStyle.getPropertyValue(
648
+ "border-bottom-right-radius"
649
+ );
650
+ const cornerRadius = {
651
+ topLeading: parseBorderRadius(topLeftPropertyValue, width),
652
+ bottomLeading: parseBorderRadius(bottomLeftPropertyValue, width),
653
+ topTrailing: parseBorderRadius(topRightPropertyValue, width),
654
+ bottomTrailing: parseBorderRadius(bottomRightPropertyValue, width)
655
+ };
656
+ return cornerRadius;
657
+ }
658
+
659
+ // src/spatial-react-components/SpatialReactComponent/SpatialDebugNameContext.ts
660
+ import { createContext as createContext4 } from "react";
661
+ var SpatialDebugNameContext = createContext4("");
662
+
663
+ // src/XRApp.ts
664
+ var defaultSceneConfig = {
665
+ defaultSize: {
666
+ width: 900,
667
+ height: 700
668
+ },
669
+ resizability: "automatic"
670
+ };
671
+ var CONTEXT_WINDOW_URL = "webspatial://createWindowContext";
672
+ var originalOpen = window.open;
673
+ var XRApp = class _XRApp {
674
+ static instance;
675
+ static getInstance() {
676
+ if (!_XRApp.instance) {
677
+ _XRApp.instance = new _XRApp();
678
+ }
679
+ return _XRApp.instance;
680
+ }
681
+ handleATag(event) {
682
+ const targetElement = event.target;
683
+ if (targetElement.tagName === "A") {
684
+ const link = targetElement;
685
+ const target = link.target;
686
+ const url = link.href;
687
+ if (target && target !== "_self") {
688
+ event.preventDefault();
689
+ window.open(url, target);
690
+ }
691
+ }
692
+ }
693
+ init() {
694
+ ;
695
+ window.open = this.open;
696
+ document.addEventListener("click", this.handleATag);
697
+ }
698
+ deinit() {
699
+ ;
700
+ window.open = originalOpen;
701
+ document.removeEventListener("click", this.handleATag);
702
+ }
703
+ configMap = {};
704
+ // name=>config
705
+ getConfig(name) {
706
+ if (name === void 0 || !this.configMap[name]) return void 0;
707
+ return this.configMap[name];
708
+ }
709
+ async show(window2, cfg) {
710
+ try {
711
+ let session = getSession();
712
+ await session._createScene(
713
+ "Plain",
714
+ // only support Plain for now
715
+ {
716
+ sceneData: {
717
+ method: "showRoot",
718
+ sceneConfig: cfg,
719
+ // url: url,
720
+ window: window2
721
+ }
722
+ }
723
+ );
724
+ } catch (error) {
725
+ console.error(error);
726
+ }
727
+ }
728
+ open = (url, target, features) => {
729
+ const newWindow = originalOpen(url, target, features);
730
+ if (url === CONTEXT_WINDOW_URL) return newWindow;
731
+ if (target === "_self" || target === "_parent" || target === "_top") {
732
+ return newWindow;
733
+ }
734
+ let cnt = 2;
735
+ let timer = setInterval(async () => {
736
+ cnt -= 1;
737
+ if (cnt < 0) {
738
+ clearInterval(timer);
739
+ return;
740
+ }
741
+ if (newWindow._webSpatialID) {
742
+ clearInterval(timer);
743
+ let session = getSession();
744
+ if (!session) {
745
+ console.error("no session");
746
+ } else {
747
+ const cfg = this.getConfig(target);
748
+ try {
749
+ await session._createScene(
750
+ "Plain",
751
+ // only support Plain for now
752
+ {
753
+ sceneData: {
754
+ method: "createRoot",
755
+ sceneConfig: cfg,
756
+ url,
757
+ window: newWindow
758
+ // windowID: (newWindow as any)._webSpatialID,
759
+ // windowContainerID: (newWindow as any)._webSpatialGroupID,
760
+ }
761
+ }
762
+ );
763
+ if (typeof target === "string" && this.configMap[target]) {
764
+ delete this.configMap[target];
765
+ }
766
+ } catch (error) {
767
+ console.error(error);
768
+ }
769
+ }
770
+ }
771
+ }, 0);
772
+ return newWindow;
773
+ };
774
+ initScene(name, callback) {
775
+ this.configMap[name] = callback({ ...defaultSceneConfig });
776
+ }
777
+ };
778
+
779
+ // src/spatial-react-components/SpatialReactComponent/PortalInstance.tsx
780
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
781
+ function renderJSXPortalInstance(inProps, inheritedPortalStyle, className) {
782
+ const { El, style: inStyle = {}, className: _, ...props } = inProps;
783
+ const extraStyle = {
784
+ visibility: "visible",
785
+ position: "",
786
+ top: "0px",
787
+ left: "0px",
788
+ margin: "0px",
789
+ marginLeft: "0px",
790
+ marginRight: "0px",
791
+ marginTop: "0px",
792
+ marginBottom: "0px",
793
+ borderRadius: "0px",
794
+ overflow: "",
795
+ display: "block"
796
+ };
797
+ const style = {
798
+ ...inStyle,
799
+ ...inheritedPortalStyle,
800
+ ...extraStyle
801
+ };
802
+ return /* @__PURE__ */ jsx2(El, { style, className, ...props });
803
+ }
804
+ function setOpenWindowStyle(openedWindow) {
805
+ openedWindow.document.documentElement.style.cssText += document.documentElement.style.cssText;
806
+ openedWindow.document.documentElement.style.backgroundColor = "transparent";
807
+ openedWindow.document.body.style.margin = "0px";
808
+ }
809
+ function handleOpenWindowDocumentClick(openedWindow) {
810
+ openedWindow.document.onclick = function(e) {
811
+ let element = e.target;
812
+ let found = false;
813
+ while (!found) {
814
+ if (element && element.tagName == "A") {
815
+ XRApp.getInstance().handleATag(e);
816
+ return false;
817
+ }
818
+ if (element && element.parentElement) {
819
+ element = element.parentElement;
820
+ } else {
821
+ break;
822
+ }
823
+ }
824
+ };
825
+ }
826
+ function asyncLoadStyleToChildWindow(childWindow, n, debugName) {
827
+ return new Promise((resolve) => {
828
+ n.href += "?uniqueURL=" + Math.random();
829
+ n.onerror = function() {
830
+ console.error(
831
+ "Failed to load style link",
832
+ debugName,
833
+ n.href
834
+ );
835
+ resolve(false);
836
+ };
837
+ n.onload = function() {
838
+ resolve(true);
839
+ };
840
+ childWindow.document.head.appendChild(n);
841
+ });
842
+ }
843
+ async function syncParentHeadToChild(childWindow, debugName) {
844
+ const styleLoadedPromises = [];
845
+ for (let i = 0; i < document.head.children.length; i++) {
846
+ let n = document.head.children[i].cloneNode(true);
847
+ if (n.nodeName == "LINK" && n.rel == "stylesheet" && n.href) {
848
+ const promise = asyncLoadStyleToChildWindow(
849
+ childWindow,
850
+ n,
851
+ debugName
852
+ );
853
+ styleLoadedPromises.push(promise);
854
+ } else {
855
+ childWindow.document.head.appendChild(n);
856
+ }
857
+ }
858
+ if (debugName) {
859
+ childWindow.document.title = debugName;
860
+ }
861
+ childWindow.document.documentElement.className = document.documentElement.className;
862
+ return Promise.all(styleLoadedPromises);
863
+ }
864
+ async function syncHeaderStyle(openedWindow, debugName) {
865
+ await syncParentHeadToChild(openedWindow, debugName);
866
+ const headObserver = new MutationObserver((mutations) => {
867
+ syncParentHeadToChild(openedWindow, debugName);
868
+ });
869
+ headObserver.observe(document.head, { childList: true, subtree: true });
870
+ return headObserver;
871
+ }
872
+ function syncDefaultSpatialStyle(openedWindow, debugName) {
873
+ const styleElement = document.createElement("style");
874
+ styleElement.type = "text/css";
875
+ styleElement.innerHTML = " .xr-spatial-default { --xr-back: 0.001; --xr-background-material: none } ";
876
+ openedWindow.document.head.appendChild(styleElement);
877
+ openedWindow.document.body.style.display = "inline-block";
878
+ openedWindow.document.body.style.minWidth = "auto";
879
+ openedWindow.document.body.style.minHeight = "auto";
880
+ openedWindow.document.body.style.maxWidth = "fit-content";
881
+ openedWindow.document.body.style.minWidth = "fit-content";
882
+ }
883
+ function useSyncSpatialProps(spatialWindowManager, props, domRect, anchor, cornerRadiusFromStyle, opacity, stylePosition, isSubPortal) {
884
+ let { allowScroll, scrollWithParent, style, spatialStyle = {} } = props;
885
+ let {
886
+ position = { x: 0, y: 0, z: 0 },
887
+ rotation = { x: 0, y: 0, z: 0, w: 1 },
888
+ scale = { x: 1, y: 1, z: 1 },
889
+ material = { type: "none" },
890
+ cornerRadius = cornerRadiusFromStyle,
891
+ zIndex = 0
892
+ } = spatialStyle;
893
+ let styleOverflow = style?.overflow;
894
+ const visible = useMemo(() => {
895
+ return spatialStyle.visible !== false && domRect.width > 0 && domRect.height > 0;
896
+ }, [spatialStyle.visible, domRect.width, domRect.height]);
897
+ if (position.x === void 0) position.x = 0;
898
+ if (position.y === void 0) position.y = 0;
899
+ if (position.z === void 0) position.z = 1;
900
+ if (scale.x === void 0) scale.x = 1;
901
+ if (scale.y === void 0) scale.y = 1;
902
+ if (scale.z === void 0) scale.z = 1;
903
+ const cornerRadiusObject = {
904
+ topLeading: 0,
905
+ bottomLeading: 0,
906
+ topTrailing: 0,
907
+ bottomTrailing: 0
908
+ };
909
+ if (typeof cornerRadius == "number") {
910
+ cornerRadiusObject.topLeading = cornerRadius;
911
+ cornerRadiusObject.bottomLeading = cornerRadius;
912
+ cornerRadiusObject.topTrailing = cornerRadius;
913
+ cornerRadiusObject.bottomTrailing = cornerRadius;
914
+ } else {
915
+ Object.assign(cornerRadiusObject, cornerRadius);
916
+ }
917
+ const isFixedPosition = stylePosition == "fixed";
918
+ useEffect3(() => {
919
+ if (spatialWindowManager && spatialWindowManager.webview) {
920
+ spatialWindowManager.updateCSSPosition(isFixedPosition);
921
+ }
922
+ }, [spatialWindowManager, isFixedPosition]);
923
+ useEffect3(() => {
924
+ if (spatialWindowManager && spatialWindowManager.webview) {
925
+ const webview = spatialWindowManager.webview;
926
+ (async function() {
927
+ webview.setStyle({
928
+ material: { type: material.type },
929
+ cornerRadius: cornerRadiusObject
930
+ });
931
+ })();
932
+ }
933
+ }, [
934
+ spatialWindowManager,
935
+ material.type,
936
+ cornerRadiusObject.topLeading,
937
+ cornerRadiusObject.bottomLeading,
938
+ cornerRadiusObject.topTrailing,
939
+ cornerRadiusObject.bottomTrailing
940
+ ]);
941
+ useEffect3(() => {
942
+ if (spatialWindowManager && spatialWindowManager.webview) {
943
+ const webview = spatialWindowManager.webview;
944
+ (async function() {
945
+ webview.setScrollEnabled(allowScroll || styleOverflow == "scroll");
946
+ const isFixed = scrollWithParent == false || isFixedPosition;
947
+ webview.setScrollWithParent(!isFixed);
948
+ })();
949
+ }
950
+ }, [
951
+ spatialWindowManager,
952
+ allowScroll,
953
+ scrollWithParent,
954
+ isFixedPosition,
955
+ styleOverflow
956
+ ]);
957
+ useEffect3(() => {
958
+ if (spatialWindowManager && spatialWindowManager.webview) {
959
+ const webview = spatialWindowManager.webview;
960
+ (async function() {
961
+ webview.setScrollEnabled(allowScroll || styleOverflow == "scroll");
962
+ const isFixed = scrollWithParent == false || stylePosition == "fixed";
963
+ webview.setScrollWithParent(!isFixed);
964
+ })();
965
+ }
966
+ }, [spatialWindowManager, allowScroll, scrollWithParent, stylePosition]);
967
+ useEffect3(() => {
968
+ if (spatialWindowManager) {
969
+ ;
970
+ (async function() {
971
+ await spatialWindowManager.resize(
972
+ domRect,
973
+ position,
974
+ rotation,
975
+ scale,
976
+ anchor,
977
+ isSubPortal ? 0 : window.scrollY
978
+ );
979
+ spatialWindowManager?.setZIndex(zIndex);
980
+ })();
981
+ }
982
+ }, [
983
+ spatialWindowManager,
984
+ domRect.x,
985
+ domRect.y,
986
+ domRect.width,
987
+ domRect.height,
988
+ position,
989
+ rotation,
990
+ scale,
991
+ anchor,
992
+ zIndex
993
+ ]);
994
+ useEffect3(() => {
995
+ if (spatialWindowManager && spatialWindowManager.webview) {
996
+ const webview = spatialWindowManager.webview;
997
+ webview.setOpacity(opacity);
998
+ }
999
+ }, [spatialWindowManager, opacity]);
1000
+ useEffect3(() => {
1001
+ if (spatialWindowManager) {
1002
+ spatialWindowManager.entity?.setVisible(visible);
1003
+ }
1004
+ }, [spatialWindowManager, visible]);
1005
+ useEffect3(() => {
1006
+ if (spatialWindowManager?.window && spatialWindowManager.webview) {
1007
+ ;
1008
+ (async function() {
1009
+ const bodyWidth = document.body.getBoundingClientRect().width;
1010
+ const viewport = spatialWindowManager.window?.document.querySelector(
1011
+ 'meta[name="viewport"]'
1012
+ );
1013
+ viewport?.setAttribute(
1014
+ "content",
1015
+ `width=${bodyWidth}, initial-scale=1.0 user-scalable=no`
1016
+ );
1017
+ await spatialWindowManager.webview?.setScrollEdgeInsets({
1018
+ top: 0,
1019
+ left: 0,
1020
+ bottom: 0,
1021
+ right: domRect.width - bodyWidth
1022
+ });
1023
+ })();
1024
+ }
1025
+ }, [spatialWindowManager, domRect.width]);
1026
+ }
1027
+ function useSyncDomRect(spatialId) {
1028
+ const [domRect, setDomRect] = useState2({
1029
+ x: 0,
1030
+ y: 0,
1031
+ width: 0,
1032
+ height: 0
1033
+ });
1034
+ const inheritedPortalStyleRef = useRef3({});
1035
+ const anchorRef = useRef3({
1036
+ x: 0.5,
1037
+ y: 0.5,
1038
+ z: 0.5
1039
+ });
1040
+ const cornerRadiusRef = useRef3({
1041
+ topLeading: 0,
1042
+ bottomLeading: 0,
1043
+ topTrailing: 0,
1044
+ bottomTrailing: 0
1045
+ });
1046
+ const opacityRef = useRef3(1);
1047
+ const spatialReactContextObject = useContext3(SpatialReactContext);
1048
+ const inheritedPortalClassNameRef = useRef3("");
1049
+ useEffect3(() => {
1050
+ const syncDomRect = () => {
1051
+ const dom = spatialReactContextObject?.querySpatialDom(spatialId);
1052
+ if (!dom) {
1053
+ return;
1054
+ }
1055
+ const computedStyle = getComputedStyle(dom);
1056
+ inheritedPortalStyleRef.current = getInheritedStyleProps(computedStyle);
1057
+ const stylePosition = inheritedPortalStyleRef.current.position;
1058
+ const isFixedPosition = stylePosition === "fixed";
1059
+ let domRect2 = dom.getBoundingClientRect();
1060
+ let rectType = domRect2rectType(domRect2);
1061
+ if (!isFixedPosition) {
1062
+ const parentDom = spatialReactContextObject?.queryParentSpatialDom(spatialId);
1063
+ if (parentDom) {
1064
+ const parentDomRect = parentDom.getBoundingClientRect();
1065
+ const parentRectType = domRect2rectType(parentDomRect);
1066
+ rectType.x -= parentRectType.x;
1067
+ rectType.y -= parentRectType.y;
1068
+ }
1069
+ }
1070
+ const anchor = parseTransformOrigin(computedStyle);
1071
+ anchorRef.current = anchor;
1072
+ const cornerRadius = parseCornerRadius(computedStyle);
1073
+ cornerRadiusRef.current = cornerRadius;
1074
+ const opacity = parseFloat(computedStyle.getPropertyValue("opacity"));
1075
+ opacityRef.current = opacity;
1076
+ inheritedPortalClassNameRef.current = dom.className;
1077
+ setDomRect(rectType);
1078
+ };
1079
+ spatialReactContextObject?.onDomChange(spatialId, syncDomRect);
1080
+ return () => {
1081
+ spatialReactContextObject?.offDomChange(spatialId);
1082
+ };
1083
+ }, []);
1084
+ return {
1085
+ domRect,
1086
+ inheritedPortalStyle: inheritedPortalStyleRef.current,
1087
+ anchor: anchorRef.current,
1088
+ cornerRadius: cornerRadiusRef.current,
1089
+ opacity: opacityRef.current,
1090
+ className: inheritedPortalClassNameRef.current
1091
+ };
1092
+ }
1093
+ function PortalInstance(inProps) {
1094
+ const { allowScroll, scrollWithParent, spatialStyle, isSubPortal, ...props } = inProps;
1095
+ const debugName = useContext3(SpatialDebugNameContext);
1096
+ const onContainerSpawned = useCallback(
1097
+ async (spatialWindowManager2) => {
1098
+ const openWindow = spatialWindowManager2.window;
1099
+ setOpenWindowStyle(openWindow);
1100
+ handleOpenWindowDocumentClick(openWindow);
1101
+ syncDefaultSpatialStyle(openWindow, debugName);
1102
+ const headObserver = await syncHeaderStyle(openWindow, debugName);
1103
+ const spawnedResult = {
1104
+ headObserver
1105
+ };
1106
+ spatialWindowManager2.setDebugName(debugName);
1107
+ return spawnedResult;
1108
+ },
1109
+ []
1110
+ );
1111
+ const onContainerDestroyed = useCallback(
1112
+ (spatialWindowManager2, spawnedResult) => {
1113
+ const { headObserver } = spawnedResult;
1114
+ headObserver.disconnect();
1115
+ },
1116
+ []
1117
+ );
1118
+ const [spatialWindowManager] = usePortalContainer({
1119
+ onContainerSpawned,
1120
+ onContainerDestroyed
1121
+ });
1122
+ const spatialId = props[SpatialID];
1123
+ const {
1124
+ domRect,
1125
+ inheritedPortalStyle,
1126
+ anchor,
1127
+ cornerRadius,
1128
+ opacity,
1129
+ className
1130
+ } = useSyncDomRect(spatialId);
1131
+ useSyncSpatialProps(
1132
+ spatialWindowManager,
1133
+ {
1134
+ style: props.style,
1135
+ allowScroll,
1136
+ scrollWithParent,
1137
+ spatialStyle
1138
+ },
1139
+ domRect,
1140
+ anchor,
1141
+ cornerRadius,
1142
+ opacity,
1143
+ inheritedPortalStyle.position,
1144
+ isSubPortal
1145
+ );
1146
+ const JSXPortalInstance = renderJSXPortalInstance(
1147
+ props,
1148
+ inheritedPortalStyle,
1149
+ className
1150
+ );
1151
+ const needRenderPlaceHolder = isSubPortal && inheritedPortalStyle.position !== "absolute" && inheritedPortalStyle.position !== "fixed";
1152
+ const El = props.El;
1153
+ return /* @__PURE__ */ jsxs(SpatialWindowManagerContext.Provider, { value: spatialWindowManager, children: [
1154
+ needRenderPlaceHolder && /* @__PURE__ */ jsx2(
1155
+ El,
1156
+ {
1157
+ className,
1158
+ "data-subportal-spatialid": spatialId,
1159
+ style: {
1160
+ position: "relative",
1161
+ width: `${domRect.width}px`,
1162
+ height: `${domRect.height}px`,
1163
+ visibility: "hidden",
1164
+ display: inheritedPortalStyle.display
1165
+ }
1166
+ }
1167
+ ),
1168
+ spatialWindowManager && spatialWindowManager.window && createPortal(
1169
+ JSXPortalInstance,
1170
+ spatialWindowManager.window.document.body
1171
+ )
1172
+ ] });
1173
+ }
1174
+ PortalInstance.displayName = "PortalInstance";
1175
+
1176
+ // src/spatial-react-components/SpatialReactComponent/SpatialLayerContext.ts
1177
+ import { createContext as createContext5 } from "react";
1178
+ var SpatialLayerContext = createContext5(0);
1179
+
1180
+ // src/spatial-react-components/SpatialReactComponent/SpatialReactComponent.tsx
1181
+ import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
1182
+ function parseProps(inProps) {
1183
+ const {
1184
+ debugShowStandardInstance,
1185
+ debugName = "",
1186
+ component,
1187
+ allowScroll,
1188
+ spatialStyle,
1189
+ scrollWithParent,
1190
+ ...props
1191
+ } = inProps;
1192
+ const El = component ? component : "div";
1193
+ const componentDesc = { El };
1194
+ const spatialDesc = { spatialStyle, allowScroll, scrollWithParent };
1195
+ const debugDesc = { debugName, debugShowStandardInstance };
1196
+ return { componentDesc, spatialDesc, debugDesc, props };
1197
+ }
1198
+ function renderWebReactComponent(inProps, ref) {
1199
+ const { componentDesc, props } = parseProps(inProps);
1200
+ const { El } = componentDesc;
1201
+ return /* @__PURE__ */ jsx3(El, { ...props, ref });
1202
+ }
1203
+ function renderSpatialReactComponent(inProps, ref) {
1204
+ const { componentDesc, spatialDesc, debugDesc, props } = parseProps(inProps);
1205
+ const standardInstanceProps = {
1206
+ ...props,
1207
+ ...componentDesc,
1208
+ debugShowStandardInstance: debugDesc.debugShowStandardInstance
1209
+ };
1210
+ const portalInstanceProps = {
1211
+ ...props,
1212
+ ...componentDesc,
1213
+ ...spatialDesc
1214
+ };
1215
+ const spatialReactContextObject = useMemo2(
1216
+ () => new SpatialReactContextObject(debugDesc.debugName),
1217
+ []
1218
+ );
1219
+ return /* @__PURE__ */ jsxs2(SpatialReactContext.Provider, { value: spatialReactContextObject, children: [
1220
+ /* @__PURE__ */ jsx3(StandardInstance, { ...standardInstanceProps, ref }),
1221
+ /* @__PURE__ */ jsx3(PortalInstance, { isSubPortal: false, ...portalInstanceProps })
1222
+ ] });
1223
+ }
1224
+ function renderSubPortalInstance(inProps) {
1225
+ const { componentDesc, spatialDesc, props } = parseProps(inProps);
1226
+ const portalInstanceProps = {
1227
+ ...props,
1228
+ ...componentDesc,
1229
+ ...spatialDesc
1230
+ };
1231
+ return /* @__PURE__ */ jsx3(PortalInstance, { isSubPortal: true, ...portalInstanceProps });
1232
+ }
1233
+ function SpatialReactComponentRefactor(inProps, ref) {
1234
+ const layer = useContext4(SpatialLayerContext) + 1;
1235
+ const parentSpatialReactContextObject = useContext4(SpatialReactContext);
1236
+ const isRootInstance = !parentSpatialReactContextObject;
1237
+ const isInStandardInstance = !!useContext4(SpatialIsStandardInstanceContext);
1238
+ const spatialID = useMemo2(() => {
1239
+ return isRootInstance ? layer.toString() : parentSpatialReactContextObject.getSpatialID(
1240
+ layer,
1241
+ isInStandardInstance,
1242
+ inProps.debugName
1243
+ );
1244
+ }, []);
1245
+ const props = { ...inProps, [SpatialID]: spatialID };
1246
+ const contentInLayer = renderContentInLayer(props, ref);
1247
+ return /* @__PURE__ */ jsx3(SpatialDebugNameContext.Provider, { value: inProps.debugName || "", children: /* @__PURE__ */ jsx3(SpatialLayerContext.Provider, { value: layer, children: contentInLayer }) });
1248
+ }
1249
+ function renderContentInLayer(inProps, ref) {
1250
+ const isInStandardInstance = useContext4(SpatialIsStandardInstanceContext);
1251
+ const isWebSpatialEnv2 = getSession() !== null;
1252
+ if (isInStandardInstance || !isWebSpatialEnv2) {
1253
+ return renderWebReactComponent(inProps, ref);
1254
+ } else {
1255
+ const parentSpatialReactContextObject = useContext4(SpatialReactContext);
1256
+ if (parentSpatialReactContextObject) {
1257
+ return renderSubPortalInstance(inProps);
1258
+ } else {
1259
+ return renderSpatialReactComponent(inProps, ref);
1260
+ }
1261
+ }
1262
+ }
1263
+ var SpatialReactComponent = forwardRef2(SpatialReactComponentRefactor);
1264
+ SpatialReactComponent.displayName = "SpatialReactComponent";
1265
+
1266
+ // src/spatial-react-components/SpatialPrimitive.tsx
1267
+ import { jsx as jsx4 } from "react/jsx-runtime";
1268
+ var cachedWithSpatialType = /* @__PURE__ */ new Map();
1269
+ function withSpatial(Component) {
1270
+ if (cachedWithSpatialType.has(Component)) {
1271
+ return cachedWithSpatialType.get(Component);
1272
+ } else {
1273
+ const WithSpatialComponent = forwardRef3(
1274
+ (givenProps, givenRef) => {
1275
+ const { component: ignoreComponent, ...props } = givenProps;
1276
+ return /* @__PURE__ */ jsx4(
1277
+ SpatialReactComponent,
1278
+ {
1279
+ component: Component,
1280
+ ...props,
1281
+ ref: givenRef
1282
+ }
1283
+ );
1284
+ }
1285
+ );
1286
+ WithSpatialComponent.displayName = `WithSpatial(${typeof Component === "string" ? Component : Component.displayName || Component.name})`;
1287
+ cachedWithSpatialType.set(Component, WithSpatialComponent);
1288
+ cachedWithSpatialType.set(WithSpatialComponent, WithSpatialComponent);
1289
+ return WithSpatialComponent;
1290
+ }
1291
+ }
1292
+ var SpatialPrimitive = {};
1293
+ (function createSpatialPrimitive(SpatialPrimitive2) {
1294
+ primitives.forEach((primitive) => {
1295
+ SpatialPrimitive2[primitive] = withSpatial(primitive);
1296
+ });
1297
+ })(SpatialPrimitive);
1298
+ var SpatialDiv = SpatialPrimitive.div;
1299
+
1300
+ // src/spatial-react-components/SpatialMonitor/withSpatialMonitor.tsx
1301
+ import { forwardRef as forwardRef5 } from "react";
1302
+
1303
+ // src/spatial-react-components/SpatialMonitor/useMonitorDomChange.tsx
1304
+ import { useRef as useRef4, useEffect as useEffect4, useMemo as useMemo3 } from "react";
1305
+
1306
+ // src/spatial-react-components/notifyUpdateStandInstanceLayout.ts
1307
+ function notifyUpdateStandInstanceLayout() {
1308
+ document.dispatchEvent(
1309
+ new CustomEvent("standInstanceLayout" /* standInstanceLayout */, {
1310
+ detail: {}
1311
+ })
1312
+ );
1313
+ }
1314
+ function notifyDOMUpdate(mutationsList) {
1315
+ document.dispatchEvent(
1316
+ new CustomEvent("domUpdated" /* domUpdated */, {
1317
+ detail: mutationsList
1318
+ })
1319
+ );
1320
+ }
1321
+
1322
+ // src/spatial-react-components/SpatialMonitor/useMonitorDomChange.tsx
1323
+ function useMonitorDomChange(inRef) {
1324
+ const ref = useRef4(null);
1325
+ useEffect4(() => {
1326
+ const observer = new MutationObserver((mutationsList) => {
1327
+ notifyDOMUpdate(mutationsList);
1328
+ });
1329
+ const config = {
1330
+ childList: true,
1331
+ subtree: true,
1332
+ attributes: true,
1333
+ attributeFilter: ["style", "class"]
1334
+ };
1335
+ ref.current && observer.observe(ref.current, config);
1336
+ return () => {
1337
+ observer.disconnect();
1338
+ };
1339
+ }, []);
1340
+ const proxyRef = useMemo3(
1341
+ () => new Proxy(ref, {
1342
+ set: function(target, key, value) {
1343
+ if (key === "current") {
1344
+ if (inRef) {
1345
+ if (typeof inRef === "function") {
1346
+ inRef(value);
1347
+ } else if (inRef) {
1348
+ inRef.current = value;
1349
+ }
1350
+ }
1351
+ }
1352
+ return Reflect.set(target, key, value);
1353
+ }
1354
+ }),
1355
+ []
1356
+ );
1357
+ return proxyRef;
1358
+ }
1359
+
1360
+ // src/spatial-react-components/SpatialMonitor/useMonitorDocumentHeaderChange.tsx
1361
+ import { useEffect as useEffect5 } from "react";
1362
+ function useMonitorDocumentHeaderChange() {
1363
+ useEffect5(() => {
1364
+ const observer = new MutationObserver((mutationsList) => {
1365
+ notifyUpdateStandInstanceLayout();
1366
+ });
1367
+ const config = {
1368
+ childList: true,
1369
+ subtree: true,
1370
+ attributes: true
1371
+ };
1372
+ observer.observe(document.head, config);
1373
+ return () => {
1374
+ observer.disconnect();
1375
+ };
1376
+ }, []);
1377
+ }
1378
+
1379
+ // src/spatial-react-components/SpatialMonitor/SpatialMonitor.tsx
1380
+ import { forwardRef as forwardRef4 } from "react";
1381
+ import { jsx as jsx5 } from "react/jsx-runtime";
1382
+ function SpatialMonitorBase(inProps, inRef) {
1383
+ const { El = "div", ...props } = inProps;
1384
+ const ref = useMonitorDomChange(inRef);
1385
+ useMonitorDocumentHeaderChange();
1386
+ return /* @__PURE__ */ jsx5(El, { ...props, ref });
1387
+ }
1388
+ var SpatialMonitor = forwardRef4(SpatialMonitorBase);
1389
+
1390
+ // src/spatial-react-components/SpatialMonitor/withSpatialMonitor.tsx
1391
+ import { jsx as jsx6 } from "react/jsx-runtime";
1392
+
1393
+ // src/spatial-react-components/CSSSpatialDiv/CSSSpatialDiv.tsx
1394
+ import { forwardRef as forwardRef7 } from "react";
1395
+
1396
+ // src/spatial-react-components/CSSSpatialDiv/CSSSpatialComponent.tsx
1397
+ import {
1398
+ forwardRef as forwardRef6,
1399
+ useContext as useContext5,
1400
+ useEffect as useEffect7,
1401
+ useMemo as useMemo4,
1402
+ useRef as useRef6
1403
+ } from "react";
1404
+
1405
+ // src/spatial-react-components/CSSSpatialDiv/useSpatialStyle.ts
1406
+ import { useCallback as useCallback2, useEffect as useEffect6, useRef as useRef5, useState as useState3 } from "react";
1407
+ import isEqual from "lodash.isequal";
1408
+
1409
+ // src/spatial-react-components/CSSSpatialDiv/math/index.ts
1410
+ import { Matrix4 } from "three/src/math/Matrix4.js";
1411
+ import { Vector3 } from "three/src/math/Vector3.js";
1412
+ import { Quaternion } from "three/src/math/Quaternion.js";
1413
+
1414
+ // src/spatial-react-components/CSSSpatialDiv/const.ts
1415
+ var SpatialCustomVars = {
1416
+ back: "--xr-back",
1417
+ backgroundMaterial: "--xr-background-material",
1418
+ xrZIndex: "--xr-z-index"
1419
+ };
1420
+ var BackgroundMaterialDefault = "none";
1421
+
1422
+ // src/spatial-react-components/CSSSpatialDiv/useSpatialStyle.ts
1423
+ function parse2dMatrix(transformDataArray) {
1424
+ const [n11, n21, n12, n22, n13, n23] = transformDataArray;
1425
+ const matrix4 = new Matrix4(
1426
+ n11,
1427
+ n12,
1428
+ 0,
1429
+ n13,
1430
+ n21,
1431
+ n22,
1432
+ 0,
1433
+ n23,
1434
+ 0,
1435
+ 0,
1436
+ 1,
1437
+ 0,
1438
+ 0,
1439
+ 0,
1440
+ 0,
1441
+ 1
1442
+ );
1443
+ return matrix4;
1444
+ }
1445
+ function parse3dMatrix(transformDataArray) {
1446
+ const matrix4 = new Matrix4().fromArray(transformDataArray);
1447
+ return matrix4;
1448
+ }
1449
+ function parseTransform(computedStyle) {
1450
+ let transform = computedStyle.getPropertyValue("transform");
1451
+ const matrixFlagString = "matrix(";
1452
+ const idxOfMatrix = transform.indexOf(matrixFlagString);
1453
+ if (idxOfMatrix !== -1) {
1454
+ const transformDataArray = transform.substring(matrixFlagString.length, transform.length - 1).split(",").map((item) => parseFloat(item));
1455
+ return parse2dMatrix(transformDataArray);
1456
+ } else {
1457
+ const matrix3dFlagString = "matrix3d(";
1458
+ const idxOfMatrix3d = transform.indexOf(matrix3dFlagString);
1459
+ if (idxOfMatrix3d !== -1) {
1460
+ const transform3dDataArray = transform.substring(matrix3dFlagString.length, transform.length - 1).split(",").map((item) => parseFloat(item));
1461
+ return parse3dMatrix(transform3dDataArray);
1462
+ } else {
1463
+ return new Matrix4();
1464
+ }
1465
+ }
1466
+ }
1467
+ function parseBack(computedStyle) {
1468
+ let backProperty = computedStyle.getPropertyValue(SpatialCustomVars.back);
1469
+ let back = void 0;
1470
+ try {
1471
+ back = parseFloat(backProperty);
1472
+ } catch (error) {
1473
+ }
1474
+ return new Matrix4().makeTranslation(0, 0, back || 0);
1475
+ }
1476
+ function parseXRZIndex(computedStyle) {
1477
+ let xrZIndexProperty = computedStyle.getPropertyValue(
1478
+ SpatialCustomVars.xrZIndex
1479
+ );
1480
+ let xrZIndex = 0;
1481
+ try {
1482
+ xrZIndex = parseFloat(xrZIndexProperty);
1483
+ } catch (error) {
1484
+ }
1485
+ return xrZIndex;
1486
+ }
1487
+ function parseSpatialStyle(node) {
1488
+ const computedStyle = getComputedStyle(node);
1489
+ const mat4ForBack = parseBack(computedStyle);
1490
+ const mat4ForTransform = parseTransform(computedStyle);
1491
+ const resultMatrix = new Matrix4();
1492
+ resultMatrix.multiplyMatrices(mat4ForBack, mat4ForTransform);
1493
+ const position = new Vector3();
1494
+ const quaternion = new Quaternion();
1495
+ const scale = new Vector3();
1496
+ resultMatrix.decompose(position, quaternion, scale);
1497
+ const zIndex = parseXRZIndex(computedStyle);
1498
+ const visible = computedStyle.getPropertyValue("visibility") === "visible";
1499
+ const backgroundMaterialType = computedStyle.getPropertyValue(
1500
+ SpatialCustomVars.backgroundMaterial
1501
+ );
1502
+ return {
1503
+ position: { x: position.x, y: position.y, z: position.z },
1504
+ rotation: {
1505
+ x: quaternion.x,
1506
+ y: quaternion.y,
1507
+ z: quaternion.z,
1508
+ w: quaternion.w
1509
+ },
1510
+ scale: { x: scale.x, y: scale.y, z: scale.z },
1511
+ zIndex,
1512
+ material: {
1513
+ type: backgroundMaterialType || "none"
1514
+ },
1515
+ visible
1516
+ };
1517
+ }
1518
+ function useSpatialStyle() {
1519
+ const ref = useRef5(null);
1520
+ const [spatialStyle, setSpatialStyle] = useState3({
1521
+ position: { x: 0, y: 0, z: 0 },
1522
+ rotation: { x: 0, y: 0, z: 0, w: 1 },
1523
+ scale: { x: 1, y: 1, z: 1 },
1524
+ zIndex: 0,
1525
+ material: {
1526
+ type: "none"
1527
+ },
1528
+ visible: true
1529
+ });
1530
+ const [ready, setReady] = useState3(false);
1531
+ const checkSpatialStyleUpdate = useCallback2(() => {
1532
+ const nextSpatialStyle = parseSpatialStyle(ref.current);
1533
+ if (!isEqual(spatialStyle, nextSpatialStyle)) {
1534
+ setSpatialStyle(nextSpatialStyle);
1535
+ }
1536
+ }, []);
1537
+ useEffect6(() => {
1538
+ if (!ref.current) {
1539
+ return;
1540
+ }
1541
+ const spatialStyle2 = parseSpatialStyle(ref.current);
1542
+ setSpatialStyle(spatialStyle2);
1543
+ setReady(true);
1544
+ }, []);
1545
+ useEffect6(() => {
1546
+ if (!ref.current) {
1547
+ return;
1548
+ }
1549
+ const observer = new MutationObserver((mutationsList) => {
1550
+ checkSpatialStyleUpdate();
1551
+ });
1552
+ const config = {
1553
+ childList: true,
1554
+ subtree: true,
1555
+ attributes: true,
1556
+ // attributeOldValue: true,
1557
+ attributeFilter: ["style", "class"]
1558
+ };
1559
+ observer.observe(ref.current, config);
1560
+ return () => {
1561
+ observer.disconnect();
1562
+ };
1563
+ }, []);
1564
+ useEffect6(() => {
1565
+ const headObserver = new MutationObserver((mutations) => {
1566
+ checkSpatialStyleUpdate();
1567
+ });
1568
+ headObserver.observe(document.head, { childList: true, subtree: true });
1569
+ return () => {
1570
+ headObserver.disconnect();
1571
+ };
1572
+ }, []);
1573
+ useEffect6(() => {
1574
+ if (!ref.current) {
1575
+ return;
1576
+ }
1577
+ function isDescendant(child, parent) {
1578
+ if (child === parent) {
1579
+ return true;
1580
+ }
1581
+ let node = child;
1582
+ while (node) {
1583
+ if (node === parent) {
1584
+ return true;
1585
+ }
1586
+ node = node.parentElement;
1587
+ }
1588
+ return false;
1589
+ }
1590
+ const onDomUpdated = (event) => {
1591
+ const mutationsList = event.detail;
1592
+ const spatialReactComponentDiv = ref.current.previousElementSibling;
1593
+ const targets = mutationsList.map((m) => m.target).filter(
1594
+ (node) => node !== ref.current && !isDescendant(node, spatialReactComponentDiv)
1595
+ );
1596
+ if (targets.length > 0) {
1597
+ checkSpatialStyleUpdate();
1598
+ }
1599
+ };
1600
+ document.addEventListener(
1601
+ "domUpdated" /* domUpdated */,
1602
+ onDomUpdated
1603
+ );
1604
+ return () => {
1605
+ document.removeEventListener(
1606
+ "domUpdated" /* domUpdated */,
1607
+ onDomUpdated
1608
+ );
1609
+ };
1610
+ }, []);
1611
+ return { ref, ready, spatialStyle };
1612
+ }
1613
+
1614
+ // src/spatial-react-components/CSSSpatialDiv/CSSSpatialDebugNameContext.ts
1615
+ import { createContext as createContext6 } from "react";
1616
+ var CSSSpatialDebugNameContext = createContext6("");
1617
+
1618
+ // src/spatial-react-components/CSSSpatialDiv/useHijackSpatialDivRef.ts
1619
+ import { useCallback as useCallback3 } from "react";
1620
+
1621
+ // src/spatial-react-components/CSSSpatialDiv/injectClassStyle.ts
1622
+ var InjectClassName = "xr-css-spatial-default";
1623
+ function injectClassStyle() {
1624
+ const style = document.createElement("style");
1625
+ style.innerHTML = `
1626
+ .${InjectClassName} {
1627
+ ${SpatialCustomVars.backgroundMaterial}: ${BackgroundMaterialDefault};
1628
+ }
1629
+ `;
1630
+ document.head.prepend(style);
1631
+ }
1632
+ injectClassStyle();
1633
+
1634
+ // src/spatial-react-components/CSSSpatialDiv/utils.ts
1635
+ function extractAndRemoveCustomProperties(cssText, properties) {
1636
+ if (!cssText) {
1637
+ return { extractedValues: {}, filteredCssText: "" };
1638
+ }
1639
+ const extractedValues = {};
1640
+ const rules = cssText.split(";");
1641
+ const filteredRules = rules.filter((rule) => {
1642
+ const [key, value] = rule.split(":").map((part) => part.trim());
1643
+ if (properties.includes(key)) {
1644
+ extractedValues[key] = value;
1645
+ return false;
1646
+ }
1647
+ return true;
1648
+ });
1649
+ const filteredCssText = filteredRules.join(";").trim();
1650
+ return { extractedValues, filteredCssText };
1651
+ }
1652
+ function joinToCSSText(cssKV) {
1653
+ const rules = Object.entries(cssKV).map(([key, value]) => `${key}: ${value}`);
1654
+ return rules.join(";");
1655
+ }
1656
+
1657
+ // src/spatial-react-components/CSSSpatialDiv/useHijackSpatialDivRef.ts
1658
+ function makeOriginalKey(key) {
1659
+ return `__original_${key}`;
1660
+ }
1661
+ function useHijackSpatialDivRef(refIn, cssParserRef) {
1662
+ const ref = cssParserRef;
1663
+ const spatialDivRef = useCallback3(
1664
+ (domElement) => {
1665
+ if (domElement && refIn) {
1666
+ const domStyle = domElement.style;
1667
+ const domStyleProxy = new Proxy(domStyle, {
1668
+ get(target, prop) {
1669
+ if (typeof target[prop] === "function") {
1670
+ return function(...args) {
1671
+ if (prop === "setProperty") {
1672
+ const [property, value] = args;
1673
+ if (property === SpatialCustomVars.backgroundMaterial) {
1674
+ ref.current?.style.setProperty(
1675
+ SpatialCustomVars.backgroundMaterial,
1676
+ value
1677
+ );
1678
+ } else if (property === SpatialCustomVars.back) {
1679
+ ref.current?.style.setProperty(
1680
+ SpatialCustomVars.back,
1681
+ value
1682
+ );
1683
+ } else if (property === "transform") {
1684
+ ref.current?.style.setProperty(property, value);
1685
+ return true;
1686
+ } else if (property === SpatialCustomVars.xrZIndex) {
1687
+ ref.current?.style.setProperty(
1688
+ SpatialCustomVars.xrZIndex,
1689
+ value
1690
+ );
1691
+ }
1692
+ } else if (prop === "removeProperty") {
1693
+ const [property] = args;
1694
+ if (property === SpatialCustomVars.backgroundMaterial || property === SpatialCustomVars.back || property === "transform" || property === SpatialCustomVars.xrZIndex) {
1695
+ ref.current?.style.removeProperty(property);
1696
+ }
1697
+ } else if (prop === "getPropertyValue") {
1698
+ const [property] = args;
1699
+ if (property === "transform") {
1700
+ return ref.current?.style[property];
1701
+ }
1702
+ }
1703
+ return target[prop](
1704
+ ...args
1705
+ );
1706
+ };
1707
+ }
1708
+ if (prop === "transform") {
1709
+ return ref.current?.style[prop];
1710
+ }
1711
+ if (prop === "visibility") {
1712
+ return ref.current?.style.visibility;
1713
+ }
1714
+ if (prop === "cssText") {
1715
+ }
1716
+ return Reflect.get(target, prop);
1717
+ },
1718
+ set(target, property, value) {
1719
+ if (property === SpatialCustomVars.backgroundMaterial) {
1720
+ ref.current?.style.setProperty(
1721
+ SpatialCustomVars.backgroundMaterial,
1722
+ value
1723
+ );
1724
+ } else if (property === SpatialCustomVars.back) {
1725
+ ref.current?.style.setProperty(
1726
+ SpatialCustomVars.back,
1727
+ value
1728
+ );
1729
+ } else if (property === SpatialCustomVars.xrZIndex) {
1730
+ ref.current?.style.setProperty(
1731
+ SpatialCustomVars.xrZIndex,
1732
+ value
1733
+ );
1734
+ } else if (property === "transform") {
1735
+ ref.current?.style.setProperty(property, value);
1736
+ return true;
1737
+ } else if (property === "visibility") {
1738
+ ref.current?.style.setProperty(property, value);
1739
+ return true;
1740
+ } else if (property === "cssText") {
1741
+ const toFilteredCSSProperties = [
1742
+ "transform",
1743
+ "visibility",
1744
+ SpatialCustomVars.back,
1745
+ SpatialCustomVars.backgroundMaterial,
1746
+ SpatialCustomVars.xrZIndex
1747
+ ];
1748
+ const { extractedValues, filteredCssText } = extractAndRemoveCustomProperties(
1749
+ value,
1750
+ toFilteredCSSProperties
1751
+ );
1752
+ toFilteredCSSProperties.forEach((key) => {
1753
+ if (extractedValues[key]) {
1754
+ ref.current?.style.setProperty(key, extractedValues[key]);
1755
+ } else {
1756
+ ref.current?.style.removeProperty(key);
1757
+ }
1758
+ });
1759
+ const appendedCSSText = joinToCSSText({
1760
+ transform: "none",
1761
+ visibility: "hidden"
1762
+ });
1763
+ return Reflect.set(
1764
+ target,
1765
+ property,
1766
+ [appendedCSSText, filteredCssText].join(";")
1767
+ );
1768
+ }
1769
+ return Reflect.set(target, property, value);
1770
+ }
1771
+ });
1772
+ const domClassList = domElement.classList;
1773
+ const domClassMethodKeys = ["add", "remove", "toggle", "replace"];
1774
+ domClassMethodKeys.forEach((key) => {
1775
+ const hiddenKey = makeOriginalKey(key);
1776
+ const hiddenKeyExist = domClassList[hiddenKey] !== void 0;
1777
+ const originalMethod = hiddenKeyExist ? domClassList[hiddenKey] : domClassList[key].bind(domClassList);
1778
+ domClassList[hiddenKey] = originalMethod;
1779
+ domClassList[key] = function(...args) {
1780
+ const result = originalMethod(...args);
1781
+ if (ref.current) {
1782
+ ref.current.className = domElement.className + " " + InjectClassName;
1783
+ }
1784
+ return result;
1785
+ };
1786
+ });
1787
+ const __getComputedStyle = (originalGetComputedStyle, pseudoElt) => {
1788
+ return originalGetComputedStyle(domElement, pseudoElt);
1789
+ };
1790
+ const proxyDomElement = new Proxy(domElement, {
1791
+ get(target, prop) {
1792
+ if (prop === "style") {
1793
+ return domStyleProxy;
1794
+ }
1795
+ if (prop === "__isSpatialDiv") {
1796
+ return true;
1797
+ }
1798
+ if (prop === "__getComputedStyle") {
1799
+ return __getComputedStyle;
1800
+ }
1801
+ if (typeof target[prop] === "function") {
1802
+ return function(...args) {
1803
+ if ("removeAttribute" === prop) {
1804
+ const [property] = args;
1805
+ if (property === "style") {
1806
+ domStyleProxy.cssText = "";
1807
+ return true;
1808
+ }
1809
+ if (property === "class") {
1810
+ proxyDomElement.className = "";
1811
+ return true;
1812
+ }
1813
+ }
1814
+ return target[prop](...args);
1815
+ };
1816
+ }
1817
+ return Reflect.get(target, prop, target);
1818
+ },
1819
+ set(target, prop, value) {
1820
+ if (ref.current) {
1821
+ if (prop === "className") {
1822
+ ref.current.className = value + " " + InjectClassName;
1823
+ }
1824
+ if (prop === "style") {
1825
+ domStyleProxy.cssText = joinToCSSText(value);
1826
+ return true;
1827
+ }
1828
+ }
1829
+ return Reflect.set(target, prop, value);
1830
+ }
1831
+ });
1832
+ if (typeof refIn === "function") {
1833
+ refIn(proxyDomElement);
1834
+ } else {
1835
+ refIn.current = proxyDomElement;
1836
+ }
1837
+ } else if (refIn) {
1838
+ if (typeof refIn === "function") {
1839
+ refIn(null);
1840
+ } else {
1841
+ refIn.current = null;
1842
+ }
1843
+ }
1844
+ },
1845
+ [refIn]
1846
+ );
1847
+ return spatialDivRef;
1848
+ }
1849
+
1850
+ // src/spatial-react-components/CSSSpatialDiv/CSSSpatialLayerContext.ts
1851
+ import { createContext as createContext7 } from "react";
1852
+ var CSSSpatialLayerContext = createContext7(0);
1853
+
1854
+ // src/spatial-react-components/CSSSpatialDiv/CSSSpatialRootContext.ts
1855
+ import { createContext as createContext8 } from "react";
1856
+ var CSSSpatialID = "CSSSpatialID";
1857
+ var CSSSpatialRootContextObject = class {
1858
+ prefix = "css";
1859
+ domSpatialId = null;
1860
+ fns = {};
1861
+ // cache dom for each spatialId
1862
+ spatialId2dom = {};
1863
+ // layer : [standardInstance sequence, portalInstance sequence]
1864
+ layerSequences = {};
1865
+ getSpatialID(layer, isInStandardInstance, debugName = "") {
1866
+ if (this.layerSequences[layer] === void 0) {
1867
+ this.layerSequences[layer] = [0, 0];
1868
+ }
1869
+ const idx = isInStandardInstance ? 0 : 1;
1870
+ const sequenceId = this.layerSequences[layer][idx];
1871
+ this.layerSequences[layer][idx] = sequenceId + 1;
1872
+ const spatialId = `${this.prefix}_${debugName}_${layer}_${sequenceId}`;
1873
+ return spatialId;
1874
+ }
1875
+ onDomChange(spatialId, fn) {
1876
+ this.fns[spatialId] = fn;
1877
+ }
1878
+ offDomChange(spatialId, fn) {
1879
+ delete this.fns[spatialId];
1880
+ }
1881
+ setCSSParserRef(cssSpatialID, domElement) {
1882
+ if (domElement) {
1883
+ this.spatialId2dom[cssSpatialID] = domElement;
1884
+ } else {
1885
+ delete this.spatialId2dom[cssSpatialID];
1886
+ }
1887
+ this.fns[cssSpatialID]?.(domElement);
1888
+ }
1889
+ };
1890
+ var CSSSpatialRootContext = createContext8(null);
1891
+
1892
+ // src/spatial-react-components/CSSSpatialDiv/CSSSpatialComponent.tsx
1893
+ import { Fragment, jsx as jsx7, jsxs as jsxs3 } from "react/jsx-runtime";
1894
+ function renderRootCSSSpatialComponent(inProps, refIn) {
1895
+ const cssSpatialRootContextObject = useMemo4(
1896
+ () => new CSSSpatialRootContextObject(),
1897
+ []
1898
+ );
1899
+ const {
1900
+ style = {},
1901
+ className = "",
1902
+ children,
1903
+ debugName,
1904
+ debugShowStandardInstance,
1905
+ ...props
1906
+ } = inProps;
1907
+ const { ref, spatialStyle, ready } = useSpatialStyle();
1908
+ const divRefStyle = {
1909
+ ...style,
1910
+ width: 0,
1911
+ height: 0,
1912
+ padding: 0,
1913
+ transition: "none",
1914
+ position: "absolute"
1915
+ };
1916
+ const spatialDivStyle = {
1917
+ ...style,
1918
+ transform: "none"
1919
+ };
1920
+ const El = inProps.component || "div";
1921
+ const spatialDivRef = useHijackSpatialDivRef(refIn, ref);
1922
+ const divRefClassName = className + " " + InjectClassName;
1923
+ return /* @__PURE__ */ jsxs3(CSSSpatialRootContext.Provider, { value: cssSpatialRootContextObject, children: [
1924
+ ready && /* @__PURE__ */ jsx7(
1925
+ SpatialReactComponent,
1926
+ {
1927
+ style: spatialDivStyle,
1928
+ className,
1929
+ children,
1930
+ ...props,
1931
+ spatialStyle,
1932
+ debugName,
1933
+ debugShowStandardInstance,
1934
+ ref: spatialDivRef
1935
+ }
1936
+ ),
1937
+ /* @__PURE__ */ jsx7(
1938
+ El,
1939
+ {
1940
+ style: divRefStyle,
1941
+ className: divRefClassName,
1942
+ ...props,
1943
+ ref,
1944
+ "data-cssparser": true
1945
+ }
1946
+ )
1947
+ ] });
1948
+ }
1949
+ function renderInWebEnv(props, ref) {
1950
+ return /* @__PURE__ */ jsx7(SpatialReactComponent, { ...props, ref });
1951
+ }
1952
+ function renderInStandardInstance(cssSpatialRootContextObject, cssSpatialID, inProps, refIn) {
1953
+ const { style: inStyle = {}, ...props } = inProps;
1954
+ const style = {
1955
+ ...inStyle,
1956
+ transform: "none",
1957
+ visibility: "hidden"
1958
+ };
1959
+ var cssParserRef = useRef6(null);
1960
+ const spatialDivRef = useHijackSpatialDivRef(refIn, cssParserRef);
1961
+ useEffect7(() => {
1962
+ const onDomChangeAction = (dom) => {
1963
+ cssParserRef.current = dom;
1964
+ };
1965
+ cssSpatialRootContextObject.onDomChange(cssSpatialID, onDomChangeAction);
1966
+ return () => {
1967
+ cssSpatialRootContextObject.offDomChange(cssSpatialID, onDomChangeAction);
1968
+ };
1969
+ }, []);
1970
+ return /* @__PURE__ */ jsx7(SpatialReactComponent, { style, ...props, ref: spatialDivRef });
1971
+ }
1972
+ function renderInPortalInstance(cssSpatialRootContextObject, cssSpatialID, inProps) {
1973
+ const {
1974
+ style = {},
1975
+ className = "",
1976
+ children,
1977
+ debugName,
1978
+ debugShowStandardInstance,
1979
+ ...props
1980
+ } = inProps;
1981
+ const { ref, spatialStyle, ready } = useSpatialStyle();
1982
+ const divRefStyle = {
1983
+ ...style,
1984
+ width: 0,
1985
+ height: 0,
1986
+ padding: 0,
1987
+ transition: "none",
1988
+ position: "absolute"
1989
+ };
1990
+ const spatialDivStyle = {
1991
+ ...style,
1992
+ transform: "none"
1993
+ };
1994
+ const El = inProps.component || "div";
1995
+ const divRefClassName = className + " " + InjectClassName;
1996
+ useEffect7(() => {
1997
+ cssSpatialRootContextObject.setCSSParserRef(cssSpatialID, ref.current);
1998
+ }, [ref.current]);
1999
+ return /* @__PURE__ */ jsxs3(Fragment, { children: [
2000
+ ready && /* @__PURE__ */ jsx7(
2001
+ SpatialReactComponent,
2002
+ {
2003
+ style: spatialDivStyle,
2004
+ className,
2005
+ children,
2006
+ ...props,
2007
+ spatialStyle,
2008
+ debugName,
2009
+ debugShowStandardInstance
2010
+ }
2011
+ ),
2012
+ /* @__PURE__ */ jsx7(
2013
+ El,
2014
+ {
2015
+ style: divRefStyle,
2016
+ className: divRefClassName,
2017
+ ...props,
2018
+ ref
2019
+ }
2020
+ )
2021
+ ] });
2022
+ }
2023
+ function CSSSpatialComponentBase(inProps, ref) {
2024
+ const { [CSSSpatialID]: cssSpatialID, ...props } = inProps;
2025
+ const isWebEnv = !getSession();
2026
+ if (isWebEnv) {
2027
+ return renderInWebEnv(props, ref);
2028
+ } else {
2029
+ const cssSpatialRootContextObject = useContext5(CSSSpatialRootContext);
2030
+ if (cssSpatialRootContextObject) {
2031
+ const isInStandardInstance = !!useContext5(
2032
+ SpatialIsStandardInstanceContext
2033
+ );
2034
+ if (isInStandardInstance) {
2035
+ return renderInStandardInstance(
2036
+ cssSpatialRootContextObject,
2037
+ cssSpatialID,
2038
+ props,
2039
+ ref
2040
+ );
2041
+ } else {
2042
+ return renderInPortalInstance(
2043
+ cssSpatialRootContextObject,
2044
+ cssSpatialID,
2045
+ props
2046
+ );
2047
+ }
2048
+ } else {
2049
+ return renderRootCSSSpatialComponent(props, ref);
2050
+ }
2051
+ }
2052
+ }
2053
+ var CSSSpatialComponentBaseWithRef = forwardRef6(CSSSpatialComponentBase);
2054
+ function CSSSpatialComponentWithRef(inProps, ref) {
2055
+ const layer = useContext5(CSSSpatialLayerContext) + 1;
2056
+ const cssSpatialRootContextObject = useContext5(CSSSpatialRootContext);
2057
+ const isRootInstance = !cssSpatialRootContextObject;
2058
+ const isInStandardInstance = !!useContext5(SpatialIsStandardInstanceContext);
2059
+ const cssSpatialID = useMemo4(() => {
2060
+ return isRootInstance ? layer.toString() : cssSpatialRootContextObject.getSpatialID(
2061
+ layer,
2062
+ isInStandardInstance,
2063
+ inProps.debugName
2064
+ );
2065
+ }, []);
2066
+ const props = { ...inProps, [CSSSpatialID]: cssSpatialID };
2067
+ return /* @__PURE__ */ jsx7(CSSSpatialDebugNameContext.Provider, { value: inProps.debugName || "", children: /* @__PURE__ */ jsx7(CSSSpatialLayerContext.Provider, { value: layer, children: /* @__PURE__ */ jsx7(CSSSpatialComponentBaseWithRef, { ...props, ref }) }) });
2068
+ }
2069
+ var CSSSpatialComponent = forwardRef6(CSSSpatialComponentWithRef);
2070
+
2071
+ // src/spatial-react-components/CSSSpatialDiv/CSSSpatialDiv.tsx
2072
+ import { jsx as jsx8 } from "react/jsx-runtime";
2073
+ var cachedWithCSSSpatialType = /* @__PURE__ */ new Map();
2074
+ function withCSSSpatial(Component) {
2075
+ if (cachedWithCSSSpatialType.has(Component)) {
2076
+ return cachedWithCSSSpatialType.get(Component);
2077
+ } else {
2078
+ const WithCSSSpatialComponent = forwardRef7(
2079
+ (givenProps, givenRef) => {
2080
+ const {
2081
+ component: ignoreComponent,
2082
+ className: origClassName = "",
2083
+ ...props
2084
+ } = givenProps;
2085
+ const className = "xr-spatial-default " + origClassName;
2086
+ return /* @__PURE__ */ jsx8(
2087
+ CSSSpatialComponent,
2088
+ {
2089
+ component: Component,
2090
+ className,
2091
+ ...props,
2092
+ ref: givenRef
2093
+ }
2094
+ );
2095
+ }
2096
+ );
2097
+ WithCSSSpatialComponent.displayName = `WithCSSSpatial(${typeof Component === "string" ? Component : Component.displayName || Component.name})`;
2098
+ cachedWithCSSSpatialType.set(Component, WithCSSSpatialComponent);
2099
+ cachedWithCSSSpatialType.set(
2100
+ WithCSSSpatialComponent,
2101
+ WithCSSSpatialComponent
2102
+ );
2103
+ return WithCSSSpatialComponent;
2104
+ }
2105
+ }
2106
+ var CSSSpatialPrimitive = {};
2107
+ (function createSpatialPrimitive2(CSSSpatialPrimitive2) {
2108
+ primitives.forEach((primitive) => {
2109
+ CSSSpatialPrimitive2[primitive] = withCSSSpatial(primitive);
2110
+ });
2111
+ })(CSSSpatialPrimitive);
2112
+ var CSSSpatialDiv = CSSSpatialPrimitive.div;
2113
+
2114
+ // src/spatial-react-components/SpatialView.tsx
2115
+ import { SpatialHelper } from "@webspatial/core-sdk";
2116
+ import {
2117
+ useRef as useRef7,
2118
+ useImperativeHandle,
2119
+ forwardRef as forwardRef8,
2120
+ useEffect as useEffect8
2121
+ } from "react";
2122
+ import { jsx as jsx9 } from "react/jsx-runtime";
2123
+ var runAsync = (fn) => {
2124
+ return fn();
2125
+ };
2126
+ var SpatialViewEl = forwardRef8(
2127
+ (props, ref) => {
2128
+ const divRef = useRef7(null);
2129
+ const spatialEntity = useRef7(null);
2130
+ const activePromise = useRef7(null);
2131
+ useImperativeHandle(ref, () => ({
2132
+ ...divRef.current,
2133
+ getViewEntity: async () => {
2134
+ if (activePromise.current) {
2135
+ await activePromise.current;
2136
+ }
2137
+ return spatialEntity.current;
2138
+ }
2139
+ }));
2140
+ if (getSession() == null) {
2141
+ return /* @__PURE__ */ jsx9("div", { ref: divRef, ...props, children: "WebSpatial is not supported in this browser" });
2142
+ }
2143
+ useEffect8(() => {
2144
+ activePromise.current = runAsync(async () => {
2145
+ if (activePromise.current) {
2146
+ await activePromise.current;
2147
+ }
2148
+ let sh = new SpatialHelper(getSession());
2149
+ let x = await sh.dom.attachSpatialView(divRef.current);
2150
+ spatialEntity.current = x.entity;
2151
+ if (props.onViewLoad) {
2152
+ props.onViewLoad(x.entity);
2153
+ }
2154
+ });
2155
+ return () => {
2156
+ runAsync(async () => {
2157
+ await activePromise.current;
2158
+ spatialEntity.current?.destroy();
2159
+ if (props.onViewUnload) {
2160
+ props.onViewUnload();
2161
+ }
2162
+ });
2163
+ };
2164
+ }, []);
2165
+ const { onViewLoad, onViewUnload, ...divProps } = props;
2166
+ return /* @__PURE__ */ jsx9("div", { ref: divRef, ...divProps });
2167
+ }
2168
+ );
2169
+
2170
+ // src/spatial-react-components/Model/index.tsx
2171
+ import {
2172
+ forwardRef as forwardRef11,
2173
+ Children,
2174
+ useMemo as useMemo9,
2175
+ useRef as useRef12,
2176
+ useEffect as useEffect16,
2177
+ useState as useState6
2178
+ } from "react";
2179
+
2180
+ // src/spatial-react-components/Model3D/Model3D.tsx
2181
+ import { forwardRef as forwardRef9, useContext as useContext8, useMemo as useMemo7 } from "react";
2182
+
2183
+ // src/spatial-react-components/Model3D/useDetectLayoutDomUpdated.ts
2184
+ import { useRef as useRef8, useEffect as useEffect9 } from "react";
2185
+ function useDetectLayoutDomUpdated(onDomUpdated) {
2186
+ const ref = useRef8(null);
2187
+ useEffect9(() => {
2188
+ const handleResize = () => {
2189
+ onDomUpdated();
2190
+ };
2191
+ window.addEventListener("resize", handleResize);
2192
+ return () => {
2193
+ window.removeEventListener("resize", handleResize);
2194
+ };
2195
+ }, []);
2196
+ useEffect9(() => {
2197
+ if (!ref.current) {
2198
+ console.warn("Ref is not attached to the DOM");
2199
+ return;
2200
+ }
2201
+ let ro = new ResizeObserver((elements) => {
2202
+ onDomUpdated();
2203
+ });
2204
+ ro.observe(ref.current);
2205
+ return () => {
2206
+ ro.disconnect();
2207
+ };
2208
+ }, []);
2209
+ useEffect9(() => {
2210
+ if (!ref.current) {
2211
+ console.warn("Ref is not attached to the DOM");
2212
+ return;
2213
+ }
2214
+ let ro = new MutationObserver((elements) => {
2215
+ onDomUpdated();
2216
+ });
2217
+ ro.observe(ref.current, {
2218
+ attributeFilter: ["class", "style"],
2219
+ subtree: true,
2220
+ attributeOldValue: false
2221
+ });
2222
+ return () => {
2223
+ ro.disconnect();
2224
+ };
2225
+ }, []);
2226
+ return ref;
2227
+ }
2228
+
2229
+ // src/spatial-react-components/Model3D/useModel3DNative.ts
2230
+ import { useRef as useRef9, useEffect as useEffect10, useState as useState4 } from "react";
2231
+
2232
+ // src/spatial-react-components/Model3D/utils.ts
2233
+ function PopulatePartialSpatialTransformType(spatialTransform = {}) {
2234
+ const {
2235
+ position = { x: 0, y: 0, z: 0 },
2236
+ rotation = { x: 0, y: 0, z: 0, w: 1 },
2237
+ scale = { x: 1, y: 1, z: 1 }
2238
+ } = spatialTransform;
2239
+ const { x: tx = 0, y: ty = 0, z: tz = 0 } = position;
2240
+ const { x: rx = 0, y: ry = 0, z: rz = 0, w = 1 } = rotation;
2241
+ const { x: sx = 1, y: sy = 1, z: sz = 1 } = scale;
2242
+ return {
2243
+ position: { x: tx, y: ty, z: tz },
2244
+ rotation: { x: rx, y: ry, z: rz, w },
2245
+ scale: { x: sx, y: sy, z: sz }
2246
+ };
2247
+ }
2248
+ function getAbsoluteURL(url) {
2249
+ if (!url) {
2250
+ return "";
2251
+ }
2252
+ if (url.startsWith("http")) {
2253
+ return url;
2254
+ }
2255
+ return `${location.origin}${url}`;
2256
+ }
2257
+
2258
+ // src/spatial-react-components/Model3D/Model3DNative.ts
2259
+ var Model3DNative = class {
2260
+ constructor(parentEntity) {
2261
+ this.parentEntity = parentEntity;
2262
+ }
2263
+ parentEntity;
2264
+ initPromise;
2265
+ entity;
2266
+ spatialModel3DComponent;
2267
+ // private modelUrl: string
2268
+ isDestroyed = false;
2269
+ _onDragStart;
2270
+ _onDrag;
2271
+ _onDragEnd;
2272
+ _onTap;
2273
+ _onDoubleTap;
2274
+ _onLongPress;
2275
+ async init(modelUrl, onSuccess, onFailure) {
2276
+ if (this.isDestroyed) {
2277
+ return;
2278
+ }
2279
+ this.initPromise = this.initInternal(modelUrl, onSuccess, onFailure);
2280
+ return this.initPromise;
2281
+ }
2282
+ async initInternal(modelUrl, onSuccess, onFailure) {
2283
+ var session = getSession();
2284
+ if (!session) {
2285
+ return;
2286
+ }
2287
+ const entity = await session.createEntity();
2288
+ await entity.setCoordinateSpace("Dom");
2289
+ const spatialModel3DComponent = await session.createModel3DComponent({
2290
+ url: getAbsoluteURL(modelUrl)
2291
+ });
2292
+ await entity.setComponent(spatialModel3DComponent);
2293
+ if (this.isDestroyed) {
2294
+ return;
2295
+ }
2296
+ if (this.parentEntity) {
2297
+ await entity.setParent(this.parentEntity);
2298
+ } else {
2299
+ var wc = session.getCurrentWindowComponent();
2300
+ var ent = await wc.getEntity();
2301
+ await entity.setParent(ent);
2302
+ }
2303
+ this.entity = entity;
2304
+ this.spatialModel3DComponent = spatialModel3DComponent;
2305
+ this.spatialModel3DComponent.onSuccess = onSuccess;
2306
+ this.spatialModel3DComponent.onFailure = onFailure;
2307
+ this.spatialModel3DComponent.onDragStart = this._onDragStart;
2308
+ this.spatialModel3DComponent.onDrag = this._onDrag;
2309
+ this.spatialModel3DComponent.onDragEnd = this._onDragEnd;
2310
+ this.spatialModel3DComponent.onTap = this._onTap;
2311
+ this.spatialModel3DComponent.onDoubleTap = this._onDoubleTap;
2312
+ this.spatialModel3DComponent.onLongPress = this._onLongPress;
2313
+ }
2314
+ async setVisible(visible) {
2315
+ if (this.entity) {
2316
+ await this.entity.setVisible(visible);
2317
+ }
2318
+ }
2319
+ async setContentMode(contentMode) {
2320
+ if (this.spatialModel3DComponent) {
2321
+ await this.spatialModel3DComponent.setContentMode(contentMode);
2322
+ }
2323
+ }
2324
+ async setResizable(resizable) {
2325
+ if (this.spatialModel3DComponent) {
2326
+ await this.spatialModel3DComponent.setResizable(resizable);
2327
+ }
2328
+ }
2329
+ async setAspectRatio(aspectRatio) {
2330
+ if (this.spatialModel3DComponent) {
2331
+ await this.spatialModel3DComponent.setAspectRatio(aspectRatio);
2332
+ }
2333
+ }
2334
+ async updateByDom(dom, options) {
2335
+ if (!this.entity || !this.spatialModel3DComponent) {
2336
+ return;
2337
+ }
2338
+ const computedStyle = getComputedStyle(dom);
2339
+ const isFixed = computedStyle.position === "fixed";
2340
+ const scrollY = isFixed ? 0 : window.scrollY;
2341
+ const rect = dom.getBoundingClientRect();
2342
+ const targetPosX = rect.left + (rect.right - rect.left) / 2;
2343
+ const targetPosY = rect.bottom + (rect.top - rect.bottom) / 2 + scrollY;
2344
+ const { spatialTransform } = options;
2345
+ const { position, rotation, scale } = spatialTransform;
2346
+ const entity = this.entity;
2347
+ entity.transform.position.x = targetPosX + position.x;
2348
+ entity.transform.position.y = targetPosY + position.y;
2349
+ entity.transform.position.z = position.z;
2350
+ entity.transform.orientation.x = rotation.x;
2351
+ entity.transform.orientation.y = rotation.y;
2352
+ entity.transform.orientation.z = rotation.z;
2353
+ entity.transform.orientation.w = rotation.w;
2354
+ entity.transform.scale.x = scale.x;
2355
+ entity.transform.scale.y = scale.y;
2356
+ entity.transform.scale.z = scale.z;
2357
+ await entity.updateTransform();
2358
+ const spatialModel3DComponent = this.spatialModel3DComponent;
2359
+ await spatialModel3DComponent.setResolution(rect.width, rect.height);
2360
+ const opacity = parseFloat(computedStyle.getPropertyValue("opacity"));
2361
+ await spatialModel3DComponent.setOpacity(opacity);
2362
+ const anchor = parseTransformOrigin(computedStyle);
2363
+ await spatialModel3DComponent.setRotationAnchor(anchor);
2364
+ await this.setScrollWithParent(!isFixed);
2365
+ }
2366
+ async updateRectAndTransform(rect, spatialTransform) {
2367
+ if (!this.entity || !this.spatialModel3DComponent) {
2368
+ return;
2369
+ }
2370
+ const targetPosX = rect.x + rect.width / 2;
2371
+ const targetPosY = rect.y + rect.height / 2;
2372
+ const { position, rotation, scale } = spatialTransform;
2373
+ const entity = this.entity;
2374
+ entity.transform.position.x = targetPosX + position.x;
2375
+ entity.transform.position.y = targetPosY + position.y;
2376
+ entity.transform.position.z = position.z;
2377
+ entity.transform.orientation.x = rotation.x;
2378
+ entity.transform.orientation.y = rotation.y;
2379
+ entity.transform.orientation.z = rotation.z;
2380
+ entity.transform.orientation.w = rotation.w;
2381
+ entity.transform.scale.x = scale.x;
2382
+ entity.transform.scale.y = scale.y;
2383
+ entity.transform.scale.z = scale.z;
2384
+ await entity.updateTransform();
2385
+ const spatialModel3DComponent = this.spatialModel3DComponent;
2386
+ await spatialModel3DComponent.setResolution(rect.width, rect.height);
2387
+ }
2388
+ async setRotationAnchor(anchor) {
2389
+ if (this.spatialModel3DComponent) {
2390
+ await this.spatialModel3DComponent.setRotationAnchor(anchor);
2391
+ }
2392
+ }
2393
+ async setOpacity(opacity) {
2394
+ if (this.spatialModel3DComponent) {
2395
+ this.spatialModel3DComponent.setOpacity(opacity);
2396
+ }
2397
+ }
2398
+ async setScrollWithParent(scrollWithParent) {
2399
+ if (this.spatialModel3DComponent) {
2400
+ this.spatialModel3DComponent.setScrollWithParent(scrollWithParent);
2401
+ }
2402
+ }
2403
+ async changeParentEntity(parentEntity) {
2404
+ if (this.parentEntity !== parentEntity) {
2405
+ if (this.entity) {
2406
+ await this.entity.setParent(parentEntity);
2407
+ }
2408
+ this.parentEntity = parentEntity;
2409
+ }
2410
+ }
2411
+ set onDragStart(callback) {
2412
+ if (this.spatialModel3DComponent) {
2413
+ this.spatialModel3DComponent.onDragStart = callback;
2414
+ }
2415
+ this._onDragStart = callback;
2416
+ }
2417
+ set onDrag(callback) {
2418
+ if (this.spatialModel3DComponent) {
2419
+ this.spatialModel3DComponent.onDrag = callback;
2420
+ }
2421
+ this._onDrag = callback;
2422
+ }
2423
+ set onDragEnd(callback) {
2424
+ if (this.spatialModel3DComponent) {
2425
+ this.spatialModel3DComponent.onDragEnd = callback;
2426
+ }
2427
+ this._onDragEnd = callback;
2428
+ }
2429
+ set onTap(callback) {
2430
+ if (this.spatialModel3DComponent) {
2431
+ this.spatialModel3DComponent.onTap = callback;
2432
+ }
2433
+ this._onTap = callback;
2434
+ }
2435
+ set onDoubleTap(callback) {
2436
+ if (this.spatialModel3DComponent) {
2437
+ this.spatialModel3DComponent.onDoubleTap = callback;
2438
+ }
2439
+ this._onDoubleTap = callback;
2440
+ }
2441
+ set onLongPress(callback) {
2442
+ if (this.spatialModel3DComponent) {
2443
+ this.spatialModel3DComponent.onLongPress = callback;
2444
+ }
2445
+ this._onLongPress = callback;
2446
+ }
2447
+ /**
2448
+ * Destroys the current 3D model instance
2449
+ * 1. Marks the instance as destroyed
2450
+ * 2. Waits for initialization to complete (if in progress)
2451
+ * 3. Destroys the spatial entity
2452
+ * 4. Cleans up all related references
2453
+ */
2454
+ async destroy() {
2455
+ this.isDestroyed = true;
2456
+ if (this.initPromise) {
2457
+ await this.initPromise;
2458
+ }
2459
+ this.entity?.destroy();
2460
+ this.entity = void 0;
2461
+ this.spatialModel3DComponent = void 0;
2462
+ this.initPromise = void 0;
2463
+ }
2464
+ };
2465
+
2466
+ // src/spatial-react-components/Model3D/useModel3DNative.ts
2467
+ function useModel3DNative(modelUrl, parentEntity, eventHandlers = {}, onModel3DNativeReadyCb) {
2468
+ let model3DNativeRef = useRef9(null);
2469
+ const [phase, setPhase] = useState4(
2470
+ "loading"
2471
+ );
2472
+ const [failureReason, setFailureReason] = useState4("");
2473
+ useEffect10(() => {
2474
+ let isDestroyed = false;
2475
+ const model3DContainer = new Model3DNative(parentEntity);
2476
+ model3DContainer.init(
2477
+ modelUrl,
2478
+ () => {
2479
+ setPhase("success");
2480
+ },
2481
+ (error) => {
2482
+ setPhase("failure");
2483
+ setFailureReason(error);
2484
+ }
2485
+ ).then(() => {
2486
+ if (!isDestroyed) {
2487
+ model3DNativeRef.current = model3DContainer;
2488
+ if (onModel3DNativeReadyCb) {
2489
+ onModel3DNativeReadyCb(model3DContainer);
2490
+ }
2491
+ }
2492
+ });
2493
+ return () => {
2494
+ isDestroyed = true;
2495
+ model3DContainer.destroy();
2496
+ model3DNativeRef.current = null;
2497
+ setPhase("loading");
2498
+ };
2499
+ }, [modelUrl]);
2500
+ useEffect10(() => {
2501
+ if (model3DNativeRef.current) {
2502
+ model3DNativeRef.current.onDragStart = eventHandlers.onDragStart;
2503
+ }
2504
+ }, [model3DNativeRef.current, eventHandlers.onDragStart]);
2505
+ useEffect10(() => {
2506
+ if (model3DNativeRef.current) {
2507
+ model3DNativeRef.current.onDrag = eventHandlers.onDrag;
2508
+ }
2509
+ }, [model3DNativeRef.current, eventHandlers.onDrag]);
2510
+ useEffect10(() => {
2511
+ if (model3DNativeRef.current) {
2512
+ model3DNativeRef.current.onDragEnd = eventHandlers.onDragEnd;
2513
+ }
2514
+ });
2515
+ useEffect10(() => {
2516
+ if (model3DNativeRef.current) {
2517
+ model3DNativeRef.current.onTap = eventHandlers.onTap;
2518
+ }
2519
+ }, [model3DNativeRef.current, eventHandlers.onTap]);
2520
+ useEffect10(() => {
2521
+ if (model3DNativeRef.current) {
2522
+ model3DNativeRef.current.onDoubleTap = eventHandlers.onDoubleTap;
2523
+ }
2524
+ }, [model3DNativeRef.current, eventHandlers.onDoubleTap]);
2525
+ useEffect10(() => {
2526
+ if (model3DNativeRef.current) {
2527
+ model3DNativeRef.current.onLongPress = eventHandlers.onLongPress;
2528
+ }
2529
+ }, [model3DNativeRef.current, eventHandlers.onLongPress]);
2530
+ return { model3DNativeRef, phase, failureReason };
2531
+ }
2532
+
2533
+ // src/spatial-react-components/Model3D/Model3DNotInSpatialDiv.tsx
2534
+ import { useCallback as useCallback4, useEffect as useEffect11, useMemo as useMemo5 } from "react";
2535
+ import { jsx as jsx10 } from "react/jsx-runtime";
2536
+ function renderModel3DNotInSpatialDiv(props, refIn) {
2537
+ const {
2538
+ className,
2539
+ style = {},
2540
+ modelUrl,
2541
+ visible,
2542
+ spatialTransform,
2543
+ contentMode = "fit",
2544
+ resizable = true,
2545
+ aspectRatio = 0,
2546
+ onLoad,
2547
+ children,
2548
+ onDragStart,
2549
+ onDrag,
2550
+ onDragEnd,
2551
+ onTap,
2552
+ onDoubleTap,
2553
+ onLongPress
2554
+ } = props;
2555
+ const theSpatialTransform = PopulatePartialSpatialTransformType(spatialTransform);
2556
+ const onDomUpdated = useCallback4(() => {
2557
+ if (model3DNativeRef.current && layoutInstanceRef.current) {
2558
+ const model3DNative = model3DNativeRef.current;
2559
+ model3DNative.updateByDom(layoutInstanceRef.current, {
2560
+ spatialTransform: theSpatialTransform
2561
+ });
2562
+ }
2563
+ }, [
2564
+ theSpatialTransform.position.x,
2565
+ theSpatialTransform.position.y,
2566
+ theSpatialTransform.position.z,
2567
+ theSpatialTransform.rotation.x,
2568
+ theSpatialTransform.rotation.y,
2569
+ theSpatialTransform.rotation.z,
2570
+ theSpatialTransform.rotation.w,
2571
+ theSpatialTransform.scale.x,
2572
+ theSpatialTransform.scale.y,
2573
+ theSpatialTransform.scale.z
2574
+ ]);
2575
+ const onModel3DContainerReadyCb = useCallback4(() => {
2576
+ if (model3DNativeRef.current && layoutInstanceRef.current) {
2577
+ model3DNativeRef.current.updateByDom(layoutInstanceRef.current, {
2578
+ spatialTransform: theSpatialTransform
2579
+ });
2580
+ }
2581
+ }, [
2582
+ theSpatialTransform.position.x,
2583
+ theSpatialTransform.position.y,
2584
+ theSpatialTransform.position.z,
2585
+ theSpatialTransform.rotation.x,
2586
+ theSpatialTransform.rotation.y,
2587
+ theSpatialTransform.rotation.z,
2588
+ theSpatialTransform.rotation.w,
2589
+ theSpatialTransform.scale.x,
2590
+ theSpatialTransform.scale.y,
2591
+ theSpatialTransform.scale.z
2592
+ ]);
2593
+ const onDragStartCb = useCallback4(
2594
+ (spatialDragEvent) => {
2595
+ if (onDragStart) {
2596
+ const dragEvent = {
2597
+ ...spatialDragEvent,
2598
+ target: layoutInstanceRef.current
2599
+ };
2600
+ onDragStart(dragEvent);
2601
+ }
2602
+ },
2603
+ [onDragStart]
2604
+ );
2605
+ const onDragCb = useCallback4(
2606
+ (spatialDragEvent) => {
2607
+ if (onDrag) {
2608
+ const dragEvent = {
2609
+ ...spatialDragEvent,
2610
+ target: layoutInstanceRef.current
2611
+ };
2612
+ onDrag(dragEvent);
2613
+ }
2614
+ },
2615
+ [onDrag]
2616
+ );
2617
+ const onDragEndCb = useCallback4(
2618
+ (spatialDragEvent) => {
2619
+ if (onDragEnd) {
2620
+ const dragEvent = {
2621
+ ...spatialDragEvent,
2622
+ target: layoutInstanceRef.current
2623
+ };
2624
+ onDragEnd(dragEvent);
2625
+ }
2626
+ },
2627
+ [onDragEnd]
2628
+ );
2629
+ const onTapCb = useCallback4(() => {
2630
+ if (onTap) {
2631
+ const event = {
2632
+ target: layoutInstanceRef.current
2633
+ };
2634
+ onTap(event);
2635
+ }
2636
+ }, [onTap]);
2637
+ const onDoubleTapCb = useCallback4(() => {
2638
+ if (onDoubleTap) {
2639
+ const event = {
2640
+ target: layoutInstanceRef.current
2641
+ };
2642
+ onDoubleTap(event);
2643
+ }
2644
+ }, [onDoubleTap]);
2645
+ const onLongPressCb = useCallback4(() => {
2646
+ if (onLongPress) {
2647
+ const event = {
2648
+ target: layoutInstanceRef.current
2649
+ };
2650
+ onLongPress(event);
2651
+ }
2652
+ }, [onLongPress]);
2653
+ const layoutInstanceRef = useDetectLayoutDomUpdated(onDomUpdated);
2654
+ const { model3DNativeRef, phase, failureReason } = useModel3DNative(
2655
+ modelUrl,
2656
+ void 0,
2657
+ {
2658
+ onDragStart: onDragStart ? onDragStartCb : void 0,
2659
+ onDrag: onDrag ? onDragCb : void 0,
2660
+ onDragEnd: onDragEnd ? onDragEndCb : void 0,
2661
+ onTap: onTap ? onTapCb : void 0,
2662
+ onDoubleTap: onDoubleTap ? onDoubleTapCb : void 0,
2663
+ onLongPress: onLongPress ? onLongPressCb : void 0
2664
+ },
2665
+ onModel3DContainerReadyCb
2666
+ );
2667
+ const onSuccess = useCallback4(() => {
2668
+ ;
2669
+ layoutInstanceRef.current.ready = true;
2670
+ if (onLoad) {
2671
+ onLoad({
2672
+ target: layoutInstanceRef.current
2673
+ });
2674
+ }
2675
+ }, [onLoad]);
2676
+ const onFailure = useCallback4(
2677
+ (_) => {
2678
+ const modelElement = layoutInstanceRef.current;
2679
+ modelElement.ready = false;
2680
+ if (onLoad) {
2681
+ onLoad({
2682
+ target: layoutInstanceRef.current
2683
+ });
2684
+ }
2685
+ },
2686
+ [onLoad]
2687
+ );
2688
+ useEffect11(() => {
2689
+ if (phase === "failure") {
2690
+ onFailure(failureReason);
2691
+ } else if (phase === "success") {
2692
+ onSuccess();
2693
+ }
2694
+ }, [phase]);
2695
+ useEffect11(() => {
2696
+ if (model3DNativeRef.current) {
2697
+ model3DNativeRef.current.setVisible(visible);
2698
+ }
2699
+ }, [model3DNativeRef.current, visible]);
2700
+ useEffect11(() => {
2701
+ if (model3DNativeRef.current && layoutInstanceRef.current) {
2702
+ model3DNativeRef.current.updateByDom(layoutInstanceRef.current, {
2703
+ spatialTransform: theSpatialTransform
2704
+ });
2705
+ }
2706
+ }, [
2707
+ theSpatialTransform.position.x,
2708
+ theSpatialTransform.position.y,
2709
+ theSpatialTransform.position.z,
2710
+ theSpatialTransform.rotation.x,
2711
+ theSpatialTransform.rotation.y,
2712
+ theSpatialTransform.rotation.z,
2713
+ theSpatialTransform.rotation.w,
2714
+ theSpatialTransform.scale.x,
2715
+ theSpatialTransform.scale.y,
2716
+ theSpatialTransform.scale.z
2717
+ ]);
2718
+ useEffect11(() => {
2719
+ if (model3DNativeRef.current) {
2720
+ model3DNativeRef.current.setContentMode(contentMode);
2721
+ }
2722
+ }, [model3DNativeRef.current, contentMode]);
2723
+ useEffect11(() => {
2724
+ if (model3DNativeRef.current) {
2725
+ model3DNativeRef.current.setResizable(resizable);
2726
+ }
2727
+ }, [model3DNativeRef.current, resizable]);
2728
+ useEffect11(() => {
2729
+ if (model3DNativeRef.current) {
2730
+ model3DNativeRef.current.setAspectRatio(aspectRatio);
2731
+ }
2732
+ }, [model3DNativeRef.current, aspectRatio]);
2733
+ const layoutDomStyle = {
2734
+ ...style,
2735
+ visibility: phase === "failure" ? "visible" : "hidden",
2736
+ transform: ""
2737
+ };
2738
+ const proxyRef = useMemo5(
2739
+ () => new Proxy(layoutInstanceRef, {
2740
+ get(target, prop, receiver) {
2741
+ return Reflect.get(target, prop, receiver);
2742
+ },
2743
+ set(target, prop, value, receiver) {
2744
+ if (prop === "current") {
2745
+ const domElement = value;
2746
+ if (domElement) {
2747
+ domElement.ready = false;
2748
+ domElement.currentSrc = modelUrl;
2749
+ }
2750
+ if (refIn) {
2751
+ if (typeof refIn === "function") {
2752
+ refIn(domElement);
2753
+ } else {
2754
+ refIn.current = domElement;
2755
+ }
2756
+ }
2757
+ }
2758
+ return Reflect.set(target, prop, value, receiver);
2759
+ }
2760
+ }),
2761
+ [layoutInstanceRef, refIn]
2762
+ );
2763
+ useEffect11(() => {
2764
+ return () => {
2765
+ if (layoutInstanceRef.current) {
2766
+ const modelElement = layoutInstanceRef.current;
2767
+ modelElement.ready = false;
2768
+ modelElement.currentSrc = modelUrl;
2769
+ }
2770
+ };
2771
+ }, [modelUrl]);
2772
+ return /* @__PURE__ */ jsx10("div", { className, style: layoutDomStyle, ref: proxyRef, children: phase === "failure" && children });
2773
+ }
2774
+
2775
+ // src/spatial-react-components/Model3D/Model3DStandardInstance.tsx
2776
+ import { jsx as jsx11 } from "react/jsx-runtime";
2777
+ function renderModel3DStandardInstance(spatialId, props, refIn) {
2778
+ const { className, style } = props;
2779
+ const extraProps = {
2780
+ [SpatialID]: spatialId
2781
+ };
2782
+ return /* @__PURE__ */ jsx11(
2783
+ "div",
2784
+ {
2785
+ "data-model3d-spatialid": spatialId,
2786
+ className,
2787
+ style,
2788
+ ref: refIn,
2789
+ ...extraProps
2790
+ }
2791
+ );
2792
+ }
2793
+
2794
+ // src/spatial-react-components/Model3D/Model3DPortalInstance.tsx
2795
+ import {
2796
+ useCallback as useCallback5,
2797
+ useContext as useContext7,
2798
+ useEffect as useEffect13,
2799
+ useMemo as useMemo6
2800
+ } from "react";
2801
+
2802
+ // src/spatial-react-components/Model3D/useSyncDomInfoFromStandardInstance.ts
2803
+ import { useRef as useRef10, useEffect as useEffect12, useState as useState5, useContext as useContext6 } from "react";
2804
+ function useSyncDomInfoFromStandardInstance(spatialId) {
2805
+ const [domRect, setDomRect] = useState5({
2806
+ x: 0,
2807
+ y: 0,
2808
+ width: 0,
2809
+ height: 0
2810
+ });
2811
+ const inheritedPortalStyleRef = useRef10({});
2812
+ const anchorRef = useRef10({
2813
+ x: 0.5,
2814
+ y: 0.5,
2815
+ z: 0.5
2816
+ });
2817
+ const opacityRef = useRef10(1);
2818
+ const spatialReactContextObject = useContext6(SpatialReactContext);
2819
+ const inheritedPortalClassNameRef = useRef10("");
2820
+ const modelRef = useRef10(null);
2821
+ useEffect12(() => {
2822
+ const syncDomRect = () => {
2823
+ const dom = spatialReactContextObject?.querySpatialDom(spatialId);
2824
+ if (!dom) {
2825
+ return;
2826
+ }
2827
+ modelRef.current = dom;
2828
+ const computedStyle = getComputedStyle(dom);
2829
+ inheritedPortalStyleRef.current = getInheritedStyleProps(computedStyle);
2830
+ const stylePosition = inheritedPortalStyleRef.current.position;
2831
+ const isFixedPosition = stylePosition === "fixed";
2832
+ let domRect2 = dom.getBoundingClientRect();
2833
+ let rectType = domRect2rectType(domRect2);
2834
+ if (!isFixedPosition) {
2835
+ const parentDom = spatialReactContextObject?.queryParentSpatialDom(spatialId);
2836
+ if (parentDom) {
2837
+ const parentDomRect = parentDom.getBoundingClientRect();
2838
+ const parentRectType = domRect2rectType(parentDomRect);
2839
+ rectType.x -= parentRectType.x;
2840
+ rectType.y -= parentRectType.y;
2841
+ }
2842
+ }
2843
+ const anchor = parseTransformOrigin(computedStyle);
2844
+ anchorRef.current = anchor;
2845
+ const opacity = parseFloat(computedStyle.getPropertyValue("opacity"));
2846
+ opacityRef.current = opacity;
2847
+ inheritedPortalClassNameRef.current = dom.className;
2848
+ setDomRect(rectType);
2849
+ };
2850
+ spatialReactContextObject?.onDomChange(spatialId, syncDomRect);
2851
+ return () => {
2852
+ spatialReactContextObject?.offDomChange(spatialId);
2853
+ };
2854
+ }, []);
2855
+ return {
2856
+ modelRef,
2857
+ domRect,
2858
+ inheritedPortalStyle: inheritedPortalStyleRef.current,
2859
+ anchor: anchorRef.current,
2860
+ opacity: opacityRef.current,
2861
+ className: inheritedPortalClassNameRef.current
2862
+ };
2863
+ }
2864
+
2865
+ // src/spatial-react-components/Model3D/Model3DPortalInstance.tsx
2866
+ import { Fragment as Fragment2, jsx as jsx12 } from "react/jsx-runtime";
2867
+ function useModelEvents(props, modelRef) {
2868
+ const {
2869
+ onDragStart,
2870
+ onDrag,
2871
+ onDragEnd,
2872
+ onTap,
2873
+ onDoubleTap,
2874
+ onLongPress
2875
+ } = props;
2876
+ const onDragStartCb = useCallback5(
2877
+ (spatialDragEvent) => {
2878
+ if (onDragStart) {
2879
+ const dragEvent = {
2880
+ ...spatialDragEvent,
2881
+ target: modelRef.current
2882
+ };
2883
+ onDragStart(dragEvent);
2884
+ }
2885
+ },
2886
+ [onDragStart]
2887
+ );
2888
+ const onDragCb = useCallback5(
2889
+ (spatialDragEvent) => {
2890
+ if (onDrag) {
2891
+ const dragEvent = {
2892
+ ...spatialDragEvent,
2893
+ target: modelRef.current
2894
+ };
2895
+ onDrag(dragEvent);
2896
+ }
2897
+ },
2898
+ [onDrag]
2899
+ );
2900
+ const onDragEndCb = useCallback5(
2901
+ (spatialDragEvent) => {
2902
+ if (onDragEnd) {
2903
+ const dragEvent = {
2904
+ ...spatialDragEvent,
2905
+ target: modelRef.current
2906
+ };
2907
+ onDragEnd(dragEvent);
2908
+ }
2909
+ },
2910
+ [onDragEnd]
2911
+ );
2912
+ const onTapCb = useCallback5(() => {
2913
+ if (onTap) {
2914
+ const event = {
2915
+ target: modelRef.current
2916
+ };
2917
+ onTap(event);
2918
+ }
2919
+ }, [onTap]);
2920
+ const onDoubleTapCb = useCallback5(() => {
2921
+ if (onDoubleTap) {
2922
+ const event = {
2923
+ target: modelRef.current
2924
+ };
2925
+ onDoubleTap(event);
2926
+ }
2927
+ }, [onDoubleTap]);
2928
+ const onLongPressCb = useCallback5(() => {
2929
+ if (onLongPress) {
2930
+ const event = {
2931
+ target: modelRef.current
2932
+ };
2933
+ onLongPress(event);
2934
+ }
2935
+ }, [onLongPress]);
2936
+ return {
2937
+ onDragStart: onDragStartCb,
2938
+ onDrag: onDragCb,
2939
+ onDragEnd: onDragEndCb,
2940
+ onTap: onTapCb,
2941
+ onDoubleTap: onDoubleTapCb,
2942
+ onLongPress: onLongPressCb
2943
+ };
2944
+ }
2945
+ function renderModel3DPortalInstance(spatialId, props) {
2946
+ const {
2947
+ style: _,
2948
+ modelUrl,
2949
+ visible,
2950
+ spatialTransform,
2951
+ contentMode = "fit",
2952
+ resizable = true,
2953
+ aspectRatio = 0,
2954
+ onLoad,
2955
+ children
2956
+ } = props;
2957
+ const theSpatialTransform = useMemo6(() => {
2958
+ return PopulatePartialSpatialTransformType(spatialTransform);
2959
+ }, [spatialTransform]);
2960
+ const {
2961
+ modelRef,
2962
+ domRect,
2963
+ inheritedPortalStyle,
2964
+ anchor,
2965
+ opacity,
2966
+ className
2967
+ } = useSyncDomInfoFromStandardInstance(spatialId);
2968
+ const parentSpatialWindowManager = useContext7(SpatialWindowManagerContext);
2969
+ const eventHandlers = useModelEvents(
2970
+ props,
2971
+ modelRef
2972
+ );
2973
+ const { model3DNativeRef, phase, failureReason } = useModel3DNative(
2974
+ modelUrl,
2975
+ parentSpatialWindowManager.entity,
2976
+ eventHandlers
2977
+ );
2978
+ useEffect13(() => {
2979
+ if (model3DNativeRef.current) {
2980
+ model3DNativeRef.current.updateRectAndTransform(
2981
+ domRect,
2982
+ theSpatialTransform
2983
+ );
2984
+ }
2985
+ }, [model3DNativeRef.current, domRect, theSpatialTransform]);
2986
+ useEffect13(() => {
2987
+ if (model3DNativeRef.current) {
2988
+ model3DNativeRef.current.setRotationAnchor(anchor);
2989
+ }
2990
+ }, [model3DNativeRef.current, anchor]);
2991
+ useEffect13(() => {
2992
+ if (model3DNativeRef.current) {
2993
+ model3DNativeRef.current.setVisible(visible);
2994
+ }
2995
+ }, [model3DNativeRef.current, visible]);
2996
+ useEffect13(() => {
2997
+ if (model3DNativeRef.current) {
2998
+ model3DNativeRef.current.setContentMode(contentMode);
2999
+ }
3000
+ }, [model3DNativeRef.current, contentMode]);
3001
+ useEffect13(() => {
3002
+ if (model3DNativeRef.current) {
3003
+ model3DNativeRef.current.setResizable(resizable);
3004
+ }
3005
+ }, [model3DNativeRef.current, resizable]);
3006
+ useEffect13(() => {
3007
+ if (model3DNativeRef.current) {
3008
+ model3DNativeRef.current.setAspectRatio(aspectRatio);
3009
+ }
3010
+ }, [model3DNativeRef.current, aspectRatio]);
3011
+ useEffect13(() => {
3012
+ if (model3DNativeRef.current) {
3013
+ model3DNativeRef.current.setOpacity(opacity);
3014
+ }
3015
+ }, [model3DNativeRef.current, opacity]);
3016
+ const isFixedPosition = inheritedPortalStyle.position == "fixed";
3017
+ useEffect13(() => {
3018
+ if (model3DNativeRef.current) {
3019
+ model3DNativeRef.current.setScrollWithParent(
3020
+ inheritedPortalStyle.position !== "fixed"
3021
+ );
3022
+ if (isFixedPosition) {
3023
+ ;
3024
+ (async function() {
3025
+ var wc = await getSession().getCurrentWindowComponent();
3026
+ var ent = await wc.getEntity();
3027
+ model3DNativeRef.current?.changeParentEntity(ent);
3028
+ })();
3029
+ }
3030
+ }
3031
+ }, [model3DNativeRef.current, isFixedPosition]);
3032
+ const onSuccess = useCallback5(() => {
3033
+ ;
3034
+ modelRef.current.ready = true;
3035
+ if (onLoad) {
3036
+ onLoad({
3037
+ target: modelRef.current
3038
+ });
3039
+ }
3040
+ }, [onLoad]);
3041
+ const onFailure = useCallback5(
3042
+ (_2) => {
3043
+ const modelElement = modelRef.current;
3044
+ modelElement.ready = false;
3045
+ if (onLoad) {
3046
+ onLoad({
3047
+ target: modelRef.current
3048
+ });
3049
+ }
3050
+ },
3051
+ [onLoad]
3052
+ );
3053
+ useEffect13(() => {
3054
+ if (phase === "failure") {
3055
+ onFailure(failureReason);
3056
+ } else if (phase === "success") {
3057
+ onSuccess();
3058
+ }
3059
+ }, [phase]);
3060
+ useEffect13(() => {
3061
+ return () => {
3062
+ const modelElement = modelRef.current;
3063
+ if (modelElement) {
3064
+ modelElement.ready = false;
3065
+ modelElement.currentSrc = modelUrl;
3066
+ }
3067
+ };
3068
+ }, [modelUrl]);
3069
+ const needRenderPlaceHolder = inheritedPortalStyle.position !== "absolute" && inheritedPortalStyle.position !== "fixed";
3070
+ if (!needRenderPlaceHolder && phase !== "failure") {
3071
+ return /* @__PURE__ */ jsx12(Fragment2, {});
3072
+ } else {
3073
+ const extraStyle = {
3074
+ visibility: "visible",
3075
+ top: "0px",
3076
+ left: "0px",
3077
+ margin: "0px",
3078
+ marginLeft: "0px",
3079
+ marginRight: "0px",
3080
+ marginTop: "0px",
3081
+ marginBottom: "0px",
3082
+ borderRadius: "0px",
3083
+ overflow: "",
3084
+ width: `${domRect.width}px`,
3085
+ height: `${domRect.height}px`
3086
+ };
3087
+ const style = {
3088
+ ...inheritedPortalStyle,
3089
+ ...extraStyle
3090
+ };
3091
+ return /* @__PURE__ */ jsx12(
3092
+ "div",
3093
+ {
3094
+ "data-model3d-spatialid": spatialId,
3095
+ className,
3096
+ style,
3097
+ children: phase === "failure" && children
3098
+ }
3099
+ );
3100
+ }
3101
+ }
3102
+
3103
+ // src/spatial-react-components/Model3D/Model3D.tsx
3104
+ function Model3DBase(props, refIn) {
3105
+ const parentSpatialReactContextObject = useContext8(SpatialReactContext);
3106
+ const isInSpatialDiv = !!parentSpatialReactContextObject;
3107
+ if (isInSpatialDiv) {
3108
+ const layer = useContext8(SpatialLayerContext) + 1;
3109
+ const isInStandardInstance = !!useContext8(SpatialIsStandardInstanceContext);
3110
+ const spatialId = useMemo7(() => {
3111
+ return parentSpatialReactContextObject.getSpatialID(
3112
+ layer,
3113
+ isInStandardInstance,
3114
+ "Model3D"
3115
+ );
3116
+ }, []);
3117
+ if (isInStandardInstance) {
3118
+ return renderModel3DStandardInstance(spatialId, props, refIn);
3119
+ } else {
3120
+ return renderModel3DPortalInstance(spatialId, props);
3121
+ }
3122
+ } else {
3123
+ return renderModel3DNotInSpatialDiv(props, refIn);
3124
+ }
3125
+ }
3126
+ var Model3D = forwardRef9(Model3DBase);
3127
+ Model3D.displayName = "Model3D";
3128
+
3129
+ // src/spatial-react-components/Model3D/CSSModel3D.tsx
3130
+ import { forwardRef as forwardRef10, useContext as useContext11, useMemo as useMemo8 } from "react";
3131
+
3132
+ // src/spatial-react-components/Model3D/CSSModel3DNotInSpatialDiv.tsx
3133
+ import { Fragment as Fragment3, jsx as jsx13, jsxs as jsxs4 } from "react/jsx-runtime";
3134
+ function renderCSSModel3DNotInSpatialDiv(inProps, refIn) {
3135
+ const { className, style = {}, ...props } = inProps;
3136
+ const cssParserDomStyle = {
3137
+ ...style,
3138
+ width: 0,
3139
+ height: 0,
3140
+ padding: 0,
3141
+ position: "absolute"
3142
+ };
3143
+ const { ref: cssParserDomRef, spatialStyle, ready } = useSpatialStyle();
3144
+ const ref = useHijackSpatialDivRef(
3145
+ refIn,
3146
+ cssParserDomRef
3147
+ );
3148
+ const spatialTransform = {
3149
+ position: spatialStyle.position,
3150
+ rotation: spatialStyle.rotation,
3151
+ scale: spatialStyle.scale
3152
+ };
3153
+ const visible = spatialStyle.visible;
3154
+ const model3DStyle = {
3155
+ ...style,
3156
+ transform: "none"
3157
+ };
3158
+ return /* @__PURE__ */ jsxs4(Fragment3, { children: [
3159
+ ready && /* @__PURE__ */ jsx13(
3160
+ Model3D,
3161
+ {
3162
+ className,
3163
+ style: model3DStyle,
3164
+ ref,
3165
+ spatialTransform,
3166
+ visible,
3167
+ ...props
3168
+ }
3169
+ ),
3170
+ /* @__PURE__ */ jsx13(
3171
+ "div",
3172
+ {
3173
+ className,
3174
+ style: cssParserDomStyle,
3175
+ ref: cssParserDomRef
3176
+ }
3177
+ )
3178
+ ] });
3179
+ }
3180
+
3181
+ // src/spatial-react-components/Model3D/CSSModel3DStandardInstance.tsx
3182
+ import { useContext as useContext9, useEffect as useEffect14, useRef as useRef11 } from "react";
3183
+ import { jsx as jsx14 } from "react/jsx-runtime";
3184
+ function renderCSSModel3DStandardInstance(spatialId, inProps, refIn) {
3185
+ const { style: inStyle = {}, ...props } = inProps;
3186
+ const style = {
3187
+ ...inStyle,
3188
+ transform: "none",
3189
+ visibility: "hidden"
3190
+ };
3191
+ var cssParserRef = useRef11(null);
3192
+ const ref = useHijackSpatialDivRef(
3193
+ refIn,
3194
+ cssParserRef
3195
+ );
3196
+ const rootSpatialReactContextObject = useContext9(SpatialReactContext);
3197
+ useEffect14(() => {
3198
+ const onSubEvent = (dom) => {
3199
+ cssParserRef.current = dom;
3200
+ };
3201
+ rootSpatialReactContextObject.onSubDivEvent(spatialId, onSubEvent);
3202
+ return () => {
3203
+ rootSpatialReactContextObject.offSubDivEvent(spatialId);
3204
+ };
3205
+ }, []);
3206
+ return /* @__PURE__ */ jsx14(Model3D, { style, ...props, ref, visible: true });
3207
+ }
3208
+
3209
+ // src/spatial-react-components/Model3D/CSSModel3DPortalInstance.tsx
3210
+ import { useContext as useContext10, useEffect as useEffect15 } from "react";
3211
+ import { Fragment as Fragment4, jsx as jsx15, jsxs as jsxs5 } from "react/jsx-runtime";
3212
+ function renderCSSModel3DPortalInstance(spatialId, inProps) {
3213
+ const { className, style = {}, ...props } = inProps;
3214
+ const rootSpatialReactContextObject = useContext10(SpatialReactContext);
3215
+ const { ref, spatialStyle, ready } = useSpatialStyle();
3216
+ const spatialTransform = {
3217
+ position: spatialStyle.position,
3218
+ rotation: spatialStyle.rotation,
3219
+ scale: spatialStyle.scale
3220
+ };
3221
+ const visible = spatialStyle.visible;
3222
+ useEffect15(() => {
3223
+ rootSpatialReactContextObject.notifySubDivEvent(spatialId, ref.current);
3224
+ }, [ref.current]);
3225
+ const cssParserDomStyle = {
3226
+ ...style,
3227
+ width: 0,
3228
+ height: 0,
3229
+ padding: 0,
3230
+ position: "absolute"
3231
+ };
3232
+ const model3DStyle = {
3233
+ ...style,
3234
+ transform: "none"
3235
+ };
3236
+ return /* @__PURE__ */ jsxs5(Fragment4, { children: [
3237
+ ready && /* @__PURE__ */ jsx15(
3238
+ Model3D,
3239
+ {
3240
+ className,
3241
+ style: model3DStyle,
3242
+ spatialTransform,
3243
+ visible,
3244
+ ...props
3245
+ }
3246
+ ),
3247
+ /* @__PURE__ */ jsx15("div", { className, style: cssParserDomStyle, ref })
3248
+ ] });
3249
+ }
3250
+
3251
+ // src/spatial-react-components/Model3D/CSSModel3D.tsx
3252
+ function CSSModel3DBase(props, refIn) {
3253
+ const rootSpatialReactContextObject = useContext11(SpatialReactContext);
3254
+ const isInSpatialDiv = !!rootSpatialReactContextObject;
3255
+ if (isInSpatialDiv) {
3256
+ const layer = useContext11(SpatialLayerContext) + 1;
3257
+ const isInStandardInstance = !!useContext11(SpatialIsStandardInstanceContext);
3258
+ const spatialId = useMemo8(() => {
3259
+ return rootSpatialReactContextObject.getSubDivSpatialID(
3260
+ layer,
3261
+ isInStandardInstance,
3262
+ "CSSModel3D"
3263
+ );
3264
+ }, []);
3265
+ if (isInStandardInstance) {
3266
+ return renderCSSModel3DStandardInstance(spatialId, props, refIn);
3267
+ } else {
3268
+ return renderCSSModel3DPortalInstance(spatialId, props);
3269
+ }
3270
+ } else {
3271
+ return renderCSSModel3DNotInSpatialDiv(props, refIn);
3272
+ }
3273
+ }
3274
+ var CSSModel3D = forwardRef10(CSSModel3DBase);
3275
+ CSSModel3D.displayName = "CSSModel3D";
3276
+
3277
+ // src/spatial-react-components/Model/index.tsx
3278
+ import { Fragment as Fragment5, jsx as jsx16, jsxs as jsxs6 } from "react/jsx-runtime";
3279
+ {
3280
+ const styleElement = document.createElement("style");
3281
+ styleElement.id = "__custom-class-model-webspatial";
3282
+ styleElement.innerHTML = `.__custom-class-model-webspatial { width: 300px; height: 300px; }`;
3283
+ if (document.getElementById("__custom-class-model-webspatial")) {
3284
+ console.warn("__custom-class-model-webspatial already exists");
3285
+ }
3286
+ document.head.prepend(styleElement);
3287
+ }
3288
+ function renderInModel3D(inProps, ref, modelUrl, placeHolder) {
3289
+ const { poster, ...props } = inProps;
3290
+ return /* @__PURE__ */ jsxs6(CSSModel3D, { modelUrl, ...props, ref, children: [
3291
+ " ",
3292
+ placeHolder,
3293
+ " "
3294
+ ] });
3295
+ }
3296
+ function parseChildren(child) {
3297
+ if (child === void 0) {
3298
+ throw new Error("children with <source> required ");
3299
+ }
3300
+ const children = Children.toArray(child);
3301
+ const sourceElements = children.filter(
3302
+ (node) => node.type === "source"
3303
+ );
3304
+ if (sourceElements.length === 0) {
3305
+ throw new Error("children with at least one <source> required ");
3306
+ }
3307
+ const gltfSources = sourceElements.filter((node) => {
3308
+ const type = node.props?.type.trim();
3309
+ const source = node.props?.src.trim().toLowerCase();
3310
+ const isGLFT = source.endsWith(".gltf");
3311
+ const isGLB = source.endsWith(".glb");
3312
+ return type.startsWith("model/gltf-binary") && isGLB || type.startsWith("model/gltf+json") && isGLFT;
3313
+ });
3314
+ const usdzSources = sourceElements.filter(
3315
+ (node) => node.props?.type.trim().startsWith("model/vnd.usdz+zip")
3316
+ );
3317
+ let lastChild = children[children.length - 1];
3318
+ const placeHolder = sourceElements.indexOf(lastChild) < 0 ? lastChild : void 0;
3319
+ const gltfSourceURL = gltfSources.length > 0 ? gltfSources[0].props?.src : "";
3320
+ const usdzSourceURL = usdzSources.length > 0 ? usdzSources[0].props?.src : "";
3321
+ return {
3322
+ placeHolder,
3323
+ gltfSourceURL: getAbsoluteURL(gltfSourceURL),
3324
+ usdzSourceURL: getAbsoluteURL(usdzSourceURL)
3325
+ };
3326
+ }
3327
+ function ModelBase(inProps, ref) {
3328
+ const { children, ...props } = inProps;
3329
+ props.className = "__custom-class-model-webspatial " + (props.className ? props.className : "");
3330
+ let className = props.className;
3331
+ const { placeHolder, gltfSourceURL, usdzSourceURL } = useMemo9(
3332
+ () => parseChildren(children),
3333
+ [children]
3334
+ );
3335
+ const isWebEnv = !getSession();
3336
+ if (isWebEnv) {
3337
+ const [loadFailed, setLoadFailed] = useState6(false);
3338
+ useEffect16(() => {
3339
+ if (gltfSourceURL == "") {
3340
+ console.warn("Unable to display model, no gltf/glb source provided");
3341
+ if (props2.onLoad) {
3342
+ props2.onLoad({
3343
+ target: { ready: false, currentSrc: gltfSourceURL }
3344
+ });
3345
+ }
3346
+ setLoadFailed(true);
3347
+ }
3348
+ }, []);
3349
+ const myModelViewer = useRef12(null);
3350
+ const { style = {}, ...props2 } = inProps;
3351
+ const isDragging = useRef12(false);
3352
+ let [modelViewerExists, setModelViewerExists] = useState6(false);
3353
+ useEffect16(() => {
3354
+ var modelViewerFound = false;
3355
+ customElements.whenDefined("model-viewer").then(function() {
3356
+ modelViewerFound = true;
3357
+ setModelViewerExists(modelViewerFound);
3358
+ });
3359
+ setTimeout(() => {
3360
+ if (!modelViewerFound) {
3361
+ console.warn(
3362
+ 'model-viewer element not loaded yet, if you want to fallback to webGL model loading, you must include the model-viewer library manually in your html file eg. \n\n <script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/4.0.0/model-viewer.min.js"></script>'
3363
+ );
3364
+ if (props2.onLoad) {
3365
+ props2.onLoad({
3366
+ target: { ready: false, currentSrc: gltfSourceURL }
3367
+ });
3368
+ }
3369
+ setLoadFailed(true);
3370
+ }
3371
+ }, 500);
3372
+ }, []);
3373
+ useEffect16(() => {
3374
+ if (!modelViewerExists) {
3375
+ return;
3376
+ }
3377
+ myModelViewer.current.addEventListener("error", (event) => {
3378
+ if (event.detail.type == "loadfailure") {
3379
+ if (props2.onLoad) {
3380
+ props2.onLoad({
3381
+ target: { ready: false, currentSrc: gltfSourceURL }
3382
+ });
3383
+ }
3384
+ setLoadFailed(true);
3385
+ }
3386
+ });
3387
+ myModelViewer.current.addEventListener("load", (event) => {
3388
+ if (props2.onLoad) {
3389
+ props2.onLoad({
3390
+ target: { ready: true, currentSrc: gltfSourceURL }
3391
+ });
3392
+ }
3393
+ setLoadFailed(false);
3394
+ });
3395
+ myModelViewer.current.addEventListener("pointerdown", (event) => {
3396
+ isDragging.current = true;
3397
+ if (props2.onDragStart) {
3398
+ props2.onDragStart({
3399
+ eventType: "dragstart",
3400
+ translation3D: { x: 0, y: 0, z: 0 },
3401
+ startLocation3D: { x: 0, y: 0, z: 0 },
3402
+ target: ref.current
3403
+ });
3404
+ }
3405
+ });
3406
+ myModelViewer.current.addEventListener("pointermove", (event) => {
3407
+ if (!isDragging.current) {
3408
+ return;
3409
+ }
3410
+ if (props2.onDrag) {
3411
+ props2.onDrag({
3412
+ eventType: "drag",
3413
+ translation3D: { x: 0, y: 0, z: 0 },
3414
+ startLocation3D: { x: 0, y: 0, z: 0 },
3415
+ target: ref.current
3416
+ });
3417
+ }
3418
+ });
3419
+ myModelViewer.current.addEventListener("pointerup", (event) => {
3420
+ if (!isDragging.current) {
3421
+ return;
3422
+ }
3423
+ isDragging.current = false;
3424
+ if (props2.onDragEnd) {
3425
+ props2.onDragEnd({
3426
+ eventType: "dragend",
3427
+ translation3D: { x: 0, y: 0, z: 0 },
3428
+ startLocation3D: { x: 0, y: 0, z: 0 },
3429
+ target: ref.current
3430
+ });
3431
+ }
3432
+ });
3433
+ }, [modelViewerExists]);
3434
+ useEffect16(() => {
3435
+ if (props2.contentMode !== void 0 && props2.contentMode !== "fit") {
3436
+ console.warn(
3437
+ "Model element contentMode != fit isn't supported on 2D screens"
3438
+ );
3439
+ }
3440
+ if (props2.resizable !== void 0 && props2.resizable !== false) {
3441
+ console.warn(
3442
+ "Model element resizable != false isn't supported on 2D screens"
3443
+ );
3444
+ }
3445
+ if (props2.aspectRatio !== void 0 && props2.aspectRatio !== 1) {
3446
+ console.warn(
3447
+ "Model element aspectRatio != 1 isn't supported on 2D screens"
3448
+ );
3449
+ }
3450
+ }, [props2.contentMode, props2.resizable, props2.aspectRatio]);
3451
+ return /* @__PURE__ */ jsx16("div", { ref, className, style, children: modelViewerExists ? /* @__PURE__ */ jsxs6(Fragment5, { children: [
3452
+ /* @__PURE__ */ jsx16(
3453
+ "model-viewer",
3454
+ {
3455
+ ref: myModelViewer,
3456
+ style: {
3457
+ display: loadFailed ? "none" : "",
3458
+ width: "100%",
3459
+ height: "100%"
3460
+ },
3461
+ src: gltfSourceURL,
3462
+ "camera-controls": true,
3463
+ "touch-action": "pan-y",
3464
+ poster: props2.poster
3465
+ }
3466
+ ),
3467
+ loadFailed ? /* @__PURE__ */ jsx16(Fragment5, { children: placeHolder }) : /* @__PURE__ */ jsx16(Fragment5, { children: " " })
3468
+ ] }) : /* @__PURE__ */ jsx16(Fragment5, { children: props2.poster ? /* @__PURE__ */ jsx16(
3469
+ "img",
3470
+ {
3471
+ className,
3472
+ style: Object.assign(structuredClone(style), {
3473
+ objectFit: "contain"
3474
+ }),
3475
+ src: props2.poster
3476
+ }
3477
+ ) : /* @__PURE__ */ jsx16(Fragment5, { children: placeHolder }) }) });
3478
+ } else {
3479
+ return renderInModel3D(props, ref, usdzSourceURL, placeHolder);
3480
+ }
3481
+ }
3482
+ var Model = forwardRef11(ModelBase);
3483
+ Model.displayName = "Model";
3484
+
3485
+ // src/polyfill/injectSceneHook.ts
6
3486
  async function injectSceneHook() {
7
3487
  if (!window.opener) return;
8
3488
  if (window._SceneHookOff) return;
@@ -33,15 +3513,15 @@ async function injectSceneHook() {
33
3513
  });
34
3514
  }
35
3515
 
36
- // src/jsx/spatialPolyfill.ts
37
- var isWebSpatialEnv = getSession2() !== null;
3516
+ // src/polyfill/spatialPolyfill.ts
3517
+ var isWebSpatialEnv = getSession() !== null;
38
3518
  var SpatialGlobalCustomVars = {
39
3519
  backgroundMaterial: "--xr-background-material"
40
3520
  };
41
3521
  var htmlBackgroundMaterial = "";
42
3522
  function setCurrentWindowStyle(backgroundMaterial) {
43
3523
  if (backgroundMaterial !== htmlBackgroundMaterial) {
44
- const session = getSession2();
3524
+ const session = getSession();
45
3525
  session.getCurrentWindowComponent().setStyle({
46
3526
  material: { type: backgroundMaterial }
47
3527
  });
@@ -68,7 +3548,7 @@ function checkCornerRadius() {
68
3548
  }
69
3549
  function setCornerRadius(cornerRadius) {
70
3550
  if (htmlCornerRadius.topLeading !== cornerRadius.topLeading || htmlCornerRadius.bottomLeading !== cornerRadius.bottomLeading || htmlCornerRadius.topTrailing !== cornerRadius.topTrailing || htmlCornerRadius.bottomTrailing !== cornerRadius.bottomTrailing) {
71
- const session = getSession2();
3551
+ const session = getSession();
72
3552
  if (!session) return;
73
3553
  session.getCurrentWindowComponent().setStyle({
74
3554
  cornerRadius
@@ -80,7 +3560,7 @@ function setCornerRadius(cornerRadius) {
80
3560
  }
81
3561
  }
82
3562
  function setOpacity(opacity) {
83
- const session = getSession2();
3563
+ const session = getSession();
84
3564
  if (!session) return;
85
3565
  session.getCurrentWindowComponent().setOpacity(opacity);
86
3566
  }
@@ -90,7 +3570,7 @@ function checkOpacity() {
90
3570
  setOpacity(opacity);
91
3571
  }
92
3572
  async function setHtmlVisible(visible) {
93
- const session = getSession2();
3573
+ const session = getSession();
94
3574
  if (!session) return;
95
3575
  const wc = session.getCurrentWindowComponent();
96
3576
  const ent = await wc.getEntity();
@@ -172,7 +3652,7 @@ function hijackGetComputedStyle() {
172
3652
  };
173
3653
  }
174
3654
  function hijackWindowOpen() {
175
- XRApp2.getInstance().init();
3655
+ XRApp.getInstance().init();
176
3656
  }
177
3657
  function spatialPolyfill() {
178
3658
  if (!isWebSpatialEnv) {
@@ -187,12 +3667,12 @@ function spatialPolyfill() {
187
3667
  }
188
3668
 
189
3669
  // src/jsx/jsx-dev-runtime.ts
190
- import { Fragment } from "react/jsx-runtime";
3670
+ import { Fragment as Fragment6 } from "react/jsx-runtime";
191
3671
 
192
3672
  // src/jsx/jsx-shared.ts
193
3673
  import { jsxDEV as _jsxDEV } from "react/jsx-dev-runtime";
194
3674
  import reactJSXRuntime from "react/jsx-runtime";
195
- import { withCSSSpatial, withSpatialMonitor } from "@webspatial/react-sdk";
3675
+ import { withCSSSpatial as withCSSSpatial2, withSpatialMonitor as withSpatialMonitor2 } from "@webspatial/react-sdk";
196
3676
  var attributeFlag = "enable-xr";
197
3677
  var styleFlag = "enableXr";
198
3678
  var classFlag = "__enableXr__";
@@ -201,15 +3681,15 @@ function replaceToSpatialPrimitiveType(type, props) {
201
3681
  const propsObject = props;
202
3682
  if (attributeFlag in propsObject) {
203
3683
  delete propsObject[attributeFlag];
204
- return withCSSSpatial(type);
3684
+ return withCSSSpatial2(type);
205
3685
  }
206
3686
  if (xrMonitorFlag in propsObject) {
207
3687
  delete propsObject[xrMonitorFlag];
208
- return withSpatialMonitor(type);
3688
+ return withSpatialMonitor2(type);
209
3689
  }
210
3690
  if (propsObject && propsObject.style && styleFlag in propsObject.style) {
211
3691
  delete propsObject.style[styleFlag];
212
- return withCSSSpatial(type);
3692
+ return withCSSSpatial2(type);
213
3693
  }
214
3694
  if (propsObject && propsObject.className) {
215
3695
  const originalClassNames = propsObject.className.split(" ");
@@ -217,12 +3697,12 @@ function replaceToSpatialPrimitiveType(type, props) {
217
3697
  if (idx !== -1) {
218
3698
  originalClassNames.splice(idx, 1);
219
3699
  propsObject.className = originalClassNames.join(" ");
220
- return withCSSSpatial(type);
3700
+ return withCSSSpatial2(type);
221
3701
  }
222
3702
  }
223
3703
  return type;
224
3704
  }
225
- function jsx(type, props, key) {
3705
+ function jsx17(type, props, key) {
226
3706
  type = replaceToSpatialPrimitiveType(type, props);
227
3707
  return reactJSXRuntime.jsx(type, props, key);
228
3708
  }
@@ -234,8 +3714,8 @@ function jsxDEV(type, props, key, isStatic, source, self) {
234
3714
  // src/jsx/jsx-dev-runtime.ts
235
3715
  spatialPolyfill();
236
3716
  export {
237
- Fragment,
238
- jsx,
3717
+ Fragment6 as Fragment,
3718
+ jsx17 as jsx,
239
3719
  jsxDEV
240
3720
  };
241
3721
  //# sourceMappingURL=jsx-dev-runtime.js.map