@webspatial/react-sdk 0.0.10 → 0.0.12

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