@webspatial/react-sdk 0.0.6 → 0.0.8

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