@webspatial/react-sdk 0.1.10 → 0.1.13

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,6 +1,6 @@
1
- import * as React from 'react';
1
+ import * as React$1 from 'react';
2
2
  import React__default, { ReactNode, CSSProperties, ElementType, ForwardedRef } from 'react';
3
- import { BackgroundMaterialType, CornerRadius, SpatialEntity, Vec3, SpatialSession } from '@webspatial/core-sdk';
3
+ import { BackgroundMaterialType, CornerRadius, SpatialEntity, Vec3, SpatialSession, WindowContainerOptions as WindowContainerOptions$1 } from '@webspatial/core-sdk';
4
4
  import { ModelViewerElement } from '@google/model-viewer';
5
5
  import { WindowContainerOptions } from '@webspatial/core-sdk/';
6
6
 
@@ -40,7 +40,7 @@ interface SpatialReactComponentProps {
40
40
  }
41
41
  type SpatialReactComponentRef = ForwardedRef<HTMLDivElement>;
42
42
 
43
- declare const SpatialReactComponent: React.ForwardRefExoticComponent<SpatialReactComponentProps & React.RefAttributes<HTMLDivElement>>;
43
+ declare const SpatialReactComponent: React$1.ForwardRefExoticComponent<SpatialReactComponentProps & React$1.RefAttributes<HTMLDivElement>>;
44
44
 
45
45
  declare function withSpatial(Component: React__default.ElementType): any;
46
46
  declare const SpatialPrimitive: Record<string, typeof SpatialReactComponent>;
@@ -51,7 +51,7 @@ declare function withSpatialMonitor(El: React__default.ElementType): any;
51
51
  type SpatialMonitorProps = {
52
52
  El?: ElementType;
53
53
  };
54
- declare const SpatialMonitor: React.ForwardRefExoticComponent<SpatialMonitorProps & React.RefAttributes<HTMLElement>>;
54
+ declare const SpatialMonitor: React$1.ForwardRefExoticComponent<SpatialMonitorProps & React$1.RefAttributes<HTMLElement>>;
55
55
 
56
56
  /**
57
57
  * External-developers can call this function to sync the standardInstance layout to PortalInstance.
@@ -68,7 +68,7 @@ declare function parseCornerRadius(computedStyle: CSSStyleDeclaration): {
68
68
  bottomTrailing: number;
69
69
  };
70
70
 
71
- declare const CSSSpatialComponent: React.ForwardRefExoticComponent<SpatialReactComponentProps & React.RefAttributes<HTMLDivElement>>;
71
+ declare const CSSSpatialComponent: React$1.ForwardRefExoticComponent<SpatialReactComponentProps & React$1.RefAttributes<HTMLDivElement>>;
72
72
 
73
73
  declare function withCSSSpatial(Component: React__default.ElementType): any;
74
74
  declare const CSSSpatialPrimitive: Record<string, typeof CSSSpatialComponent>;
@@ -86,6 +86,11 @@ interface SpatialViewRef extends HTMLDivElement {
86
86
  */
87
87
  declare const SpatialView: React__default.ForwardRefExoticComponent<SpatialViewProps & React__default.RefAttributes<SpatialViewRef>>;
88
88
 
89
+ type PartialSpatialTransformType = {
90
+ position?: Partial<vecType>;
91
+ rotation?: Partial<quatType>;
92
+ scale?: Partial<vecType>;
93
+ };
89
94
  interface ModelEvent {
90
95
  target: ModelElement;
91
96
  }
@@ -94,10 +99,34 @@ interface ModelDragEvent extends ModelEvent {
94
99
  translation3D: Vec3;
95
100
  startLocation3D: Vec3;
96
101
  }
102
+ interface Model3DProps {
103
+ spatialTransform?: PartialSpatialTransformType;
104
+ modelUrl: string;
105
+ visible: boolean;
106
+ contentMode?: 'fill' | 'fit';
107
+ resizable?: boolean;
108
+ aspectRatio?: number;
109
+ className?: string;
110
+ style?: CSSProperties | undefined;
111
+ children?: React.ReactNode;
112
+ onLoad?: (event: ModelEvent) => void;
113
+ onDragStart?: (dragEvent: ModelDragEvent) => void;
114
+ onDrag?: (dragEvent: ModelDragEvent) => void;
115
+ onDragEnd?: (dragEvent: ModelDragEvent) => void;
116
+ onTap?: (event: ModelEvent) => void;
117
+ onDoubleTap?: (event: ModelEvent) => void;
118
+ onLongPress?: (event: ModelEvent) => void;
119
+ }
97
120
  interface ModelElement extends HTMLDivElement {
98
121
  ready: boolean;
99
122
  currentSrc: string;
100
123
  }
124
+ type ModelElementRef = ForwardedRef<ModelElement>;
125
+ type CSSModel3DProps = Omit<Model3DProps, 'spatialTransform' | 'visible'>;
126
+
127
+ declare const Model3D: React$1.ForwardRefExoticComponent<Model3DProps & React$1.RefAttributes<ModelElement>>;
128
+
129
+ declare const CSSModel3D: React$1.ForwardRefExoticComponent<CSSModel3DProps & React$1.RefAttributes<ModelElement>>;
101
130
 
102
131
  declare global {
103
132
  namespace JSX {
@@ -159,6 +188,11 @@ declare class XRApp {
159
188
  open: (url?: string, target?: string, features?: string) => Window | null;
160
189
  initScene(name: string, callback: (pre: WindowContainerOptions) => WindowContainerOptions): void;
161
190
  }
162
- declare function initScene(name: string, callback: (pre: WindowContainerOptions) => WindowContainerOptions): void;
163
191
 
164
- export { CSSSpatialDiv, CSSSpatialPrimitive, Model, type ModelProps, SpatialDiv, type SpatialReactComponentProps as SpatialDivProps, type SpatialReactComponentRef as SpatialDivRef, SpatialMonitor, SpatialPrimitive, type SpatialReactComponentProps, type SpatialReactComponentRef, SpatialView, type SpatialViewRef, XRApp, defaultSceneConfig, enableDebugTool, getSession, initScene, notifyUpdateStandInstanceLayout, parseCornerRadius, withCSSSpatial, withSpatial, withSpatialMonitor };
192
+ declare function initScene(name: string, callback: (pre: WindowContainerOptions$1) => WindowContainerOptions$1): void;
193
+
194
+ declare function spatialPolyfill(): void;
195
+
196
+ declare const version: string;
197
+
198
+ export { CSSModel3D, CSSSpatialDiv, CSSSpatialPrimitive, Model, Model3D, type Model3DProps, type ModelDragEvent, type ModelElement, type ModelElementRef, type ModelEvent, type ModelProps, SpatialDiv, type SpatialReactComponentProps as SpatialDivProps, type SpatialReactComponentRef as SpatialDivRef, SpatialMonitor, SpatialPrimitive, type SpatialReactComponentProps, type SpatialReactComponentRef, SpatialView, type SpatialViewRef, XRApp, defaultSceneConfig, enableDebugTool, getSession, initScene, notifyUpdateStandInstanceLayout, parseCornerRadius, spatialPolyfill, version, withCSSSpatial, withSpatial, withSpatialMonitor };
@@ -1,3 +1,12 @@
1
+
2
+ (function(){
3
+ if(typeof window === 'undefined') return;
4
+ if(!window.__webspatialsdk__) window.__webspatialsdk__ = {}
5
+ window.__webspatialsdk__['react-sdk-version'] = "0.1.13"
6
+ window.__webspatialsdk__['XR_ENV'] = "avp"
7
+ })()
8
+
9
+
1
10
  // src/spatial-react-components/SpatialPrimitive.tsx
2
11
  import { forwardRef as forwardRef3 } from "react";
3
12
 
@@ -147,7 +156,6 @@ import { Spatial } from "@webspatial/core-sdk";
147
156
  var spatial = null;
148
157
  var _currentSession = null;
149
158
  function getSession() {
150
- if (false) return null;
151
159
  if (!spatial) {
152
160
  spatial = new Spatial();
153
161
  }
@@ -163,7 +171,6 @@ function getSession() {
163
171
 
164
172
  // src/utils/debugTool.ts
165
173
  async function getStat() {
166
- if (false) return;
167
174
  const statsInfo = await getSession()._getStats();
168
175
  return statsInfo;
169
176
  }
@@ -218,7 +225,6 @@ async function inspectRootWindowContainer(simple) {
218
225
  return rootWindowContainerInfo;
219
226
  }
220
227
  function enableDebugTool() {
221
- if (false) return;
222
228
  const session = getSession();
223
229
  Object.assign(window, {
224
230
  session,
@@ -477,7 +483,6 @@ var SpatialWindowManager = class {
477
483
  this.entity?._setName(debugName);
478
484
  }
479
485
  async initInternal(url) {
480
- if (false) return;
481
486
  this.entity = await getSession().createEntity();
482
487
  this.webview = await getSession().createWindowComponent();
483
488
  await this.webview.loadURL(url);
@@ -490,7 +495,6 @@ var SpatialWindowManager = class {
490
495
  await this.entity.setParent(ent);
491
496
  }
492
497
  async initInternalFromWindow() {
493
- if (false) return;
494
498
  var w = await getSession().createWindowContext();
495
499
  this.window = w;
496
500
  this.entity = await getSession().createEntity();
@@ -664,7 +668,8 @@ function getInheritedStyleProps(computedStyle) {
664
668
  // position also need to be synced
665
669
  "position",
666
670
  "width",
667
- "height"
671
+ "height",
672
+ "display"
668
673
  ];
669
674
  var props = {};
670
675
  for (var cssName of propNames) {
@@ -756,8 +761,6 @@ var XRApp = class _XRApp {
756
761
  if (target && target !== "_self") {
757
762
  event.preventDefault();
758
763
  window.open(url, target);
759
- } else {
760
- window.location.href = url;
761
764
  }
762
765
  }
763
766
  }
@@ -846,9 +849,6 @@ var XRApp = class _XRApp {
846
849
  this.configMap[name] = callback({ ...defaultSceneConfig });
847
850
  }
848
851
  };
849
- function initScene(name, callback) {
850
- return XRApp.getInstance().initScene(name, callback);
851
- }
852
852
 
853
853
  // src/spatial-react-components/SpatialReactComponent/PortalInstance.tsx
854
854
  import { jsx as jsx2, jsxs } from "react/jsx-runtime";
@@ -865,8 +865,7 @@ function renderJSXPortalInstance(inProps, inheritedPortalStyle, className) {
865
865
  marginTop: "0px",
866
866
  marginBottom: "0px",
867
867
  borderRadius: "0px",
868
- overflow: "",
869
- display: "block"
868
+ overflow: ""
870
869
  };
871
870
  const style = {
872
871
  ...inStyle,
@@ -1322,8 +1321,8 @@ function SpatialReactComponentRefactor(inProps, ref) {
1322
1321
  }
1323
1322
  function renderContentInLayer(inProps, ref) {
1324
1323
  const isInStandardInstance = useContext4(SpatialIsStandardInstanceContext);
1325
- const isWebSpatialEnv = getSession() !== null;
1326
- if (isInStandardInstance || !isWebSpatialEnv) {
1324
+ const isWebSpatialEnv2 = getSession() !== null;
1325
+ if (isInStandardInstance || !isWebSpatialEnv2) {
1327
1326
  return renderWebReactComponent(inProps, ref);
1328
1327
  } else {
1329
1328
  const parentSpatialReactContextObject = useContext4(SpatialReactContext);
@@ -2238,7 +2237,6 @@ var SpatialViewEl = forwardRef8(
2238
2237
  return /* @__PURE__ */ jsx9("div", { ref: divRef, ...props, children: "WebSpatial is not supported in this browser" });
2239
2238
  }
2240
2239
  useEffect8(() => {
2241
- if (false) return;
2242
2240
  activePromise.current = runAsync(async () => {
2243
2241
  if (activePromise.current) {
2244
2242
  await activePromise.current;
@@ -2251,7 +2249,6 @@ var SpatialViewEl = forwardRef8(
2251
2249
  }
2252
2250
  });
2253
2251
  return () => {
2254
- if (false) return;
2255
2252
  runAsync(async () => {
2256
2253
  await activePromise.current;
2257
2254
  spatialEntity.current?.destroy();
@@ -2380,7 +2377,6 @@ var Model3DNative = class {
2380
2377
  return this.initPromise;
2381
2378
  }
2382
2379
  async initInternal(modelUrl, onSuccess, onFailure) {
2383
- if (false) return;
2384
2380
  var session = getSession();
2385
2381
  if (!session) {
2386
2382
  return;
@@ -3582,6 +3578,198 @@ function ModelBase(inProps, ref) {
3582
3578
  }
3583
3579
  var Model = forwardRef11(ModelBase);
3584
3580
  Model.displayName = "Model";
3581
+
3582
+ // src/initScene.ts
3583
+ function initScene(name, callback) {
3584
+ return XRApp.getInstance().initScene(name, callback);
3585
+ }
3586
+
3587
+ // src/polyfill/injectSceneHook.ts
3588
+ async function injectSceneHook() {
3589
+ if (!window.opener) return;
3590
+ if (window._SceneHookOff) return;
3591
+ await getSession()?.setLoading("show");
3592
+ function onContentLoaded(callback) {
3593
+ if (document.readyState === "interactive" || document.readyState === "complete") {
3594
+ callback();
3595
+ } else {
3596
+ document.addEventListener("DOMContentLoaded", callback);
3597
+ }
3598
+ }
3599
+ onContentLoaded(async () => {
3600
+ let cfg = defaultSceneConfig;
3601
+ if (typeof window.xrCurrentSceneDefaults === "function") {
3602
+ try {
3603
+ cfg = await window.xrCurrentSceneDefaults?.();
3604
+ } catch (error) {
3605
+ console.error(error);
3606
+ }
3607
+ }
3608
+ await new Promise((resolve, reject) => {
3609
+ setTimeout(() => {
3610
+ resolve(null);
3611
+ }, 1e3);
3612
+ });
3613
+ await getSession()?.setLoading("hide");
3614
+ await XRApp.getInstance().show(window, cfg);
3615
+ });
3616
+ }
3617
+
3618
+ // src/polyfill/spatialPolyfill.ts
3619
+ var isWebSpatialEnv = getSession() !== null;
3620
+ var SpatialGlobalCustomVars = {
3621
+ backgroundMaterial: "--xr-background-material"
3622
+ };
3623
+ var htmlBackgroundMaterial = "";
3624
+ function setCurrentWindowStyle(backgroundMaterial) {
3625
+ if (backgroundMaterial !== htmlBackgroundMaterial) {
3626
+ const session = getSession();
3627
+ session.getCurrentWindowComponent().setStyle({
3628
+ material: { type: backgroundMaterial }
3629
+ });
3630
+ htmlBackgroundMaterial = backgroundMaterial;
3631
+ }
3632
+ }
3633
+ function checkHtmlBackgroundMaterial() {
3634
+ const computedStyle = getComputedStyle(document.documentElement);
3635
+ const backgroundMaterial = computedStyle.getPropertyValue(
3636
+ SpatialGlobalCustomVars.backgroundMaterial
3637
+ );
3638
+ setCurrentWindowStyle(backgroundMaterial || "none");
3639
+ }
3640
+ var htmlCornerRadius = {
3641
+ topLeading: 0,
3642
+ bottomLeading: 0,
3643
+ topTrailing: 0,
3644
+ bottomTrailing: 0
3645
+ };
3646
+ function checkCornerRadius() {
3647
+ const computedStyle = getComputedStyle(document.documentElement);
3648
+ const cornerRadius = parseCornerRadius(computedStyle);
3649
+ setCornerRadius(cornerRadius);
3650
+ }
3651
+ function setCornerRadius(cornerRadius) {
3652
+ if (htmlCornerRadius.topLeading !== cornerRadius.topLeading || htmlCornerRadius.bottomLeading !== cornerRadius.bottomLeading || htmlCornerRadius.topTrailing !== cornerRadius.topTrailing || htmlCornerRadius.bottomTrailing !== cornerRadius.bottomTrailing) {
3653
+ const session = getSession();
3654
+ if (!session) return;
3655
+ session.getCurrentWindowComponent().setStyle({
3656
+ cornerRadius
3657
+ });
3658
+ htmlCornerRadius.topLeading = cornerRadius.topLeading;
3659
+ htmlCornerRadius.bottomLeading = cornerRadius.bottomLeading;
3660
+ htmlCornerRadius.topTrailing = cornerRadius.topTrailing;
3661
+ htmlCornerRadius.bottomTrailing = cornerRadius.bottomTrailing;
3662
+ }
3663
+ }
3664
+ function setOpacity(opacity) {
3665
+ const session = getSession();
3666
+ if (!session) return;
3667
+ session.getCurrentWindowComponent().setOpacity(opacity);
3668
+ }
3669
+ function checkOpacity() {
3670
+ const computedStyle = getComputedStyle(document.documentElement);
3671
+ const opacity = parseFloat(computedStyle.getPropertyValue("opacity"));
3672
+ setOpacity(opacity);
3673
+ }
3674
+ async function setHtmlVisible(visible) {
3675
+ const session = getSession();
3676
+ if (!session) return;
3677
+ const wc = session.getCurrentWindowComponent();
3678
+ const ent = await wc.getEntity();
3679
+ ent?.setVisible(visible);
3680
+ }
3681
+ function checkHtmlVisible() {
3682
+ const computedStyle = getComputedStyle(document.documentElement);
3683
+ const visibility = computedStyle.getPropertyValue("visibility") !== "hidden";
3684
+ const widthGtZero = parseFloat(computedStyle.getPropertyValue("width")) > 0;
3685
+ setHtmlVisible(visibility && widthGtZero);
3686
+ }
3687
+ function hijackDocumentElementStyle() {
3688
+ const rawDocumentStyle = document.documentElement.style;
3689
+ const styleProxy = new Proxy(rawDocumentStyle, {
3690
+ set: function(target, key, value) {
3691
+ const ret = Reflect.set(target, key, value);
3692
+ if (key === SpatialGlobalCustomVars.backgroundMaterial) {
3693
+ setCurrentWindowStyle(value);
3694
+ }
3695
+ if (key === "border-radius" || key === "borderRadius" || key === "border-top-left-radius" || key === "borderTopLeftRadius" || key === "border-top-right-radius" || key === "borderTopRightRadius" || key === "border-bottom-left-radius" || key === "borderBottomLeftRadius" || key === "border-bottom-right-radius" || key === "borderBottomRightRadius") {
3696
+ checkCornerRadius();
3697
+ }
3698
+ if (key === "opacity") {
3699
+ checkOpacity();
3700
+ }
3701
+ if (key === "visibility" || key === "display") {
3702
+ checkHtmlVisible();
3703
+ }
3704
+ return ret;
3705
+ },
3706
+ get: function(target, prop) {
3707
+ if (typeof target[prop] === "function") {
3708
+ return function(...args) {
3709
+ if (prop === "setProperty") {
3710
+ const [property, value] = args;
3711
+ if (property === SpatialGlobalCustomVars.backgroundMaterial) {
3712
+ setCurrentWindowStyle(value);
3713
+ }
3714
+ } else if (prop === "removeProperty") {
3715
+ const [property] = args;
3716
+ if (property === SpatialGlobalCustomVars.backgroundMaterial) {
3717
+ setCurrentWindowStyle("none");
3718
+ }
3719
+ }
3720
+ return target[prop](
3721
+ ...args
3722
+ );
3723
+ };
3724
+ }
3725
+ return Reflect.get(target, prop);
3726
+ }
3727
+ });
3728
+ Object.defineProperty(document.documentElement, "style", {
3729
+ get: function() {
3730
+ return styleProxy;
3731
+ }
3732
+ });
3733
+ }
3734
+ function monitorExternalStyleChange() {
3735
+ const headObserver = new MutationObserver(function(mutationsList) {
3736
+ checkCSSProperties();
3737
+ });
3738
+ headObserver.observe(document.head, { childList: true, subtree: true });
3739
+ }
3740
+ function checkCSSProperties() {
3741
+ checkHtmlBackgroundMaterial();
3742
+ checkCornerRadius();
3743
+ checkOpacity();
3744
+ checkHtmlVisible();
3745
+ window.addEventListener("resize", checkHtmlVisible);
3746
+ }
3747
+ function hijackGetComputedStyle() {
3748
+ const rawFn = window.getComputedStyle.bind(window);
3749
+ window.getComputedStyle = (element, pseudoElt) => {
3750
+ if (element.__isSpatialDiv) {
3751
+ return element.__getComputedStyle(rawFn, pseudoElt);
3752
+ }
3753
+ return rawFn(element, pseudoElt);
3754
+ };
3755
+ }
3756
+ function hijackWindowOpen() {
3757
+ XRApp.getInstance().init();
3758
+ }
3759
+ function spatialPolyfill() {
3760
+ if (!isWebSpatialEnv) {
3761
+ return;
3762
+ }
3763
+ injectSceneHook();
3764
+ hijackWindowOpen();
3765
+ checkCSSProperties();
3766
+ hijackGetComputedStyle();
3767
+ hijackDocumentElementStyle();
3768
+ monitorExternalStyleChange();
3769
+ }
3770
+
3771
+ // src/index.ts
3772
+ var version = "0.1.13";
3585
3773
  export {
3586
3774
  CSSSpatialDiv,
3587
3775
  CSSSpatialPrimitive,
@@ -3597,6 +3785,8 @@ export {
3597
3785
  initScene,
3598
3786
  notifyUpdateStandInstanceLayout,
3599
3787
  parseCornerRadius,
3788
+ spatialPolyfill,
3789
+ version,
3600
3790
  withCSSSpatial,
3601
3791
  withSpatial,
3602
3792
  withSpatialMonitor