@webspatial/react-sdk 0.1.9 → 0.1.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  import * as React 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
 
@@ -159,6 +159,9 @@ declare class XRApp {
159
159
  open: (url?: string, target?: string, features?: string) => Window | null;
160
160
  initScene(name: string, callback: (pre: WindowContainerOptions) => WindowContainerOptions): void;
161
161
  }
162
- declare function initScene(name: string, callback: (pre: WindowContainerOptions) => WindowContainerOptions): void;
163
162
 
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 };
163
+ declare function initScene(name: string, callback: (pre: WindowContainerOptions$1) => WindowContainerOptions$1): void;
164
+
165
+ declare function spatialPolyfill(): void;
166
+
167
+ 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, spatialPolyfill, withCSSSpatial, withSpatial, withSpatialMonitor };
package/dist/web/index.js CHANGED
@@ -142,12 +142,43 @@ var primitives = [
142
142
  // src/spatial-react-components/SpatialReactComponent/SpatialReactComponent.tsx
143
143
  import { forwardRef as forwardRef2, useMemo as useMemo2, useContext as useContext4 } from "react";
144
144
 
145
+ // src/noRuntime.ts
146
+ var SpatialHelper = {};
147
+ var Spatial = class {
148
+ /**
149
+ * Requests a session object from the browser
150
+ * @returns The session or null if not availible in the current browser
151
+ * [TODO] discuss implications of this not being async
152
+ */
153
+ requestSession() {
154
+ return null;
155
+ }
156
+ /**
157
+ * @returns true if web spatial is supported by this webpage
158
+ */
159
+ isSupported() {
160
+ return false;
161
+ }
162
+ /**
163
+ * Gets the native version, format is "x.x.x"
164
+ * @returns native version string
165
+ */
166
+ getNativeVersion() {
167
+ return null;
168
+ }
169
+ /**
170
+ * Gets the client version, format is "x.x.x"
171
+ * @returns client version string
172
+ */
173
+ getClientVersion() {
174
+ return null;
175
+ }
176
+ };
177
+
145
178
  // src/utils/getSession.ts
146
- import { Spatial } from "@webspatial/core-sdk";
147
179
  var spatial = null;
148
180
  var _currentSession = null;
149
181
  function getSession() {
150
- if (true) return null;
151
182
  if (!spatial) {
152
183
  spatial = new Spatial();
153
184
  }
@@ -163,7 +194,6 @@ function getSession() {
163
194
 
164
195
  // src/utils/debugTool.ts
165
196
  async function getStat() {
166
- if (true) return;
167
197
  const statsInfo = await getSession()._getStats();
168
198
  return statsInfo;
169
199
  }
@@ -218,7 +248,6 @@ async function inspectRootWindowContainer(simple) {
218
248
  return rootWindowContainerInfo;
219
249
  }
220
250
  function enableDebugTool() {
221
- if (true) return;
222
251
  const session = getSession();
223
252
  Object.assign(window, {
224
253
  session,
@@ -477,7 +506,6 @@ var SpatialWindowManager = class {
477
506
  this.entity?._setName(debugName);
478
507
  }
479
508
  async initInternal(url) {
480
- if (true) return;
481
509
  this.entity = await getSession().createEntity();
482
510
  this.webview = await getSession().createWindowComponent();
483
511
  await this.webview.loadURL(url);
@@ -490,7 +518,6 @@ var SpatialWindowManager = class {
490
518
  await this.entity.setParent(ent);
491
519
  }
492
520
  async initInternalFromWindow() {
493
- if (true) return;
494
521
  var w = await getSession().createWindowContext();
495
522
  this.window = w;
496
523
  this.entity = await getSession().createEntity();
@@ -756,8 +783,6 @@ var XRApp = class _XRApp {
756
783
  if (target && target !== "_self") {
757
784
  event.preventDefault();
758
785
  window.open(url, target);
759
- } else {
760
- window.location.href = url;
761
786
  }
762
787
  }
763
788
  }
@@ -846,9 +871,6 @@ var XRApp = class _XRApp {
846
871
  this.configMap[name] = callback({ ...defaultSceneConfig });
847
872
  }
848
873
  };
849
- function initScene(name, callback) {
850
- return XRApp.getInstance().initScene(name, callback);
851
- }
852
874
 
853
875
  // src/spatial-react-components/SpatialReactComponent/PortalInstance.tsx
854
876
  import { jsx as jsx2, jsxs } from "react/jsx-runtime";
@@ -1322,8 +1344,8 @@ function SpatialReactComponentRefactor(inProps, ref) {
1322
1344
  }
1323
1345
  function renderContentInLayer(inProps, ref) {
1324
1346
  const isInStandardInstance = useContext4(SpatialIsStandardInstanceContext);
1325
- const isWebSpatialEnv = getSession() !== null;
1326
- if (isInStandardInstance || !isWebSpatialEnv) {
1347
+ const isWebSpatialEnv2 = getSession() !== null;
1348
+ if (isInStandardInstance || !isWebSpatialEnv2) {
1327
1349
  return renderWebReactComponent(inProps, ref);
1328
1350
  } else {
1329
1351
  const parentSpatialReactContextObject = useContext4(SpatialReactContext);
@@ -2119,7 +2141,7 @@ function renderInPortalInstance(cssSpatialRootContextObject, cssSpatialID, inPro
2119
2141
  }
2120
2142
  function CSSSpatialComponentBase(inProps, ref) {
2121
2143
  const { [CSSSpatialID]: cssSpatialID, ...props } = inProps;
2122
- const isWebEnv = true;
2144
+ const isWebEnv = !getSession();
2123
2145
  if (isWebEnv) {
2124
2146
  return renderInWebEnv(props, ref);
2125
2147
  } else {
@@ -2209,7 +2231,6 @@ var CSSSpatialPrimitive = {};
2209
2231
  var CSSSpatialDiv = CSSSpatialPrimitive.div;
2210
2232
 
2211
2233
  // src/spatial-react-components/SpatialView.tsx
2212
- import { SpatialHelper } from "@webspatial/core-sdk";
2213
2234
  import {
2214
2235
  useRef as useRef7,
2215
2236
  useImperativeHandle,
@@ -2238,7 +2259,6 @@ var SpatialViewEl = forwardRef8(
2238
2259
  return /* @__PURE__ */ jsx9("div", { ref: divRef, ...props, children: "WebSpatial is not supported in this browser" });
2239
2260
  }
2240
2261
  useEffect8(() => {
2241
- if (true) return;
2242
2262
  activePromise.current = runAsync(async () => {
2243
2263
  if (activePromise.current) {
2244
2264
  await activePromise.current;
@@ -2251,7 +2271,6 @@ var SpatialViewEl = forwardRef8(
2251
2271
  }
2252
2272
  });
2253
2273
  return () => {
2254
- if (true) return;
2255
2274
  runAsync(async () => {
2256
2275
  await activePromise.current;
2257
2276
  spatialEntity.current?.destroy();
@@ -2380,7 +2399,6 @@ var Model3DNative = class {
2380
2399
  return this.initPromise;
2381
2400
  }
2382
2401
  async initInternal(modelUrl, onSuccess, onFailure) {
2383
- if (true) return;
2384
2402
  var session = getSession();
2385
2403
  if (!session) {
2386
2404
  return;
@@ -3582,6 +3600,195 @@ function ModelBase(inProps, ref) {
3582
3600
  }
3583
3601
  var Model = forwardRef11(ModelBase);
3584
3602
  Model.displayName = "Model";
3603
+
3604
+ // src/initScene.web.ts
3605
+ function initScene(name, callback) {
3606
+ return;
3607
+ }
3608
+
3609
+ // src/polyfill/injectSceneHook.ts
3610
+ async function injectSceneHook() {
3611
+ if (!window.opener) return;
3612
+ if (window._SceneHookOff) return;
3613
+ await getSession()?.setLoading("show");
3614
+ function onContentLoaded(callback) {
3615
+ if (document.readyState === "interactive" || document.readyState === "complete") {
3616
+ callback();
3617
+ } else {
3618
+ document.addEventListener("DOMContentLoaded", callback);
3619
+ }
3620
+ }
3621
+ onContentLoaded(async () => {
3622
+ let cfg = defaultSceneConfig;
3623
+ if (typeof window.xrCurrentSceneDefaults === "function") {
3624
+ try {
3625
+ cfg = await window.xrCurrentSceneDefaults?.();
3626
+ } catch (error) {
3627
+ console.error(error);
3628
+ }
3629
+ }
3630
+ await new Promise((resolve, reject) => {
3631
+ setTimeout(() => {
3632
+ resolve(null);
3633
+ }, 1e3);
3634
+ });
3635
+ await getSession()?.setLoading("hide");
3636
+ await XRApp.getInstance().show(window, cfg);
3637
+ });
3638
+ }
3639
+
3640
+ // src/polyfill/spatialPolyfill.ts
3641
+ var isWebSpatialEnv = getSession() !== null;
3642
+ var SpatialGlobalCustomVars = {
3643
+ backgroundMaterial: "--xr-background-material"
3644
+ };
3645
+ var htmlBackgroundMaterial = "";
3646
+ function setCurrentWindowStyle(backgroundMaterial) {
3647
+ if (backgroundMaterial !== htmlBackgroundMaterial) {
3648
+ const session = getSession();
3649
+ session.getCurrentWindowComponent().setStyle({
3650
+ material: { type: backgroundMaterial }
3651
+ });
3652
+ htmlBackgroundMaterial = backgroundMaterial;
3653
+ }
3654
+ }
3655
+ function checkHtmlBackgroundMaterial() {
3656
+ const computedStyle = getComputedStyle(document.documentElement);
3657
+ const backgroundMaterial = computedStyle.getPropertyValue(
3658
+ SpatialGlobalCustomVars.backgroundMaterial
3659
+ );
3660
+ setCurrentWindowStyle(backgroundMaterial || "none");
3661
+ }
3662
+ var htmlCornerRadius = {
3663
+ topLeading: 0,
3664
+ bottomLeading: 0,
3665
+ topTrailing: 0,
3666
+ bottomTrailing: 0
3667
+ };
3668
+ function checkCornerRadius() {
3669
+ const computedStyle = getComputedStyle(document.documentElement);
3670
+ const cornerRadius = parseCornerRadius(computedStyle);
3671
+ setCornerRadius(cornerRadius);
3672
+ }
3673
+ function setCornerRadius(cornerRadius) {
3674
+ if (htmlCornerRadius.topLeading !== cornerRadius.topLeading || htmlCornerRadius.bottomLeading !== cornerRadius.bottomLeading || htmlCornerRadius.topTrailing !== cornerRadius.topTrailing || htmlCornerRadius.bottomTrailing !== cornerRadius.bottomTrailing) {
3675
+ const session = getSession();
3676
+ if (!session) return;
3677
+ session.getCurrentWindowComponent().setStyle({
3678
+ cornerRadius
3679
+ });
3680
+ htmlCornerRadius.topLeading = cornerRadius.topLeading;
3681
+ htmlCornerRadius.bottomLeading = cornerRadius.bottomLeading;
3682
+ htmlCornerRadius.topTrailing = cornerRadius.topTrailing;
3683
+ htmlCornerRadius.bottomTrailing = cornerRadius.bottomTrailing;
3684
+ }
3685
+ }
3686
+ function setOpacity(opacity) {
3687
+ const session = getSession();
3688
+ if (!session) return;
3689
+ session.getCurrentWindowComponent().setOpacity(opacity);
3690
+ }
3691
+ function checkOpacity() {
3692
+ const computedStyle = getComputedStyle(document.documentElement);
3693
+ const opacity = parseFloat(computedStyle.getPropertyValue("opacity"));
3694
+ setOpacity(opacity);
3695
+ }
3696
+ async function setHtmlVisible(visible) {
3697
+ const session = getSession();
3698
+ if (!session) return;
3699
+ const wc = session.getCurrentWindowComponent();
3700
+ const ent = await wc.getEntity();
3701
+ ent?.setVisible(visible);
3702
+ }
3703
+ function checkHtmlVisible() {
3704
+ const computedStyle = getComputedStyle(document.documentElement);
3705
+ const visibility = computedStyle.getPropertyValue("visibility") !== "hidden";
3706
+ const widthGtZero = parseFloat(computedStyle.getPropertyValue("width")) > 0;
3707
+ setHtmlVisible(visibility && widthGtZero);
3708
+ }
3709
+ function hijackDocumentElementStyle() {
3710
+ const rawDocumentStyle = document.documentElement.style;
3711
+ const styleProxy = new Proxy(rawDocumentStyle, {
3712
+ set: function(target, key, value) {
3713
+ const ret = Reflect.set(target, key, value);
3714
+ if (key === SpatialGlobalCustomVars.backgroundMaterial) {
3715
+ setCurrentWindowStyle(value);
3716
+ }
3717
+ 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") {
3718
+ checkCornerRadius();
3719
+ }
3720
+ if (key === "opacity") {
3721
+ checkOpacity();
3722
+ }
3723
+ if (key === "visibility" || key === "display") {
3724
+ checkHtmlVisible();
3725
+ }
3726
+ return ret;
3727
+ },
3728
+ get: function(target, prop) {
3729
+ if (typeof target[prop] === "function") {
3730
+ return function(...args) {
3731
+ if (prop === "setProperty") {
3732
+ const [property, value] = args;
3733
+ if (property === SpatialGlobalCustomVars.backgroundMaterial) {
3734
+ setCurrentWindowStyle(value);
3735
+ }
3736
+ } else if (prop === "removeProperty") {
3737
+ const [property] = args;
3738
+ if (property === SpatialGlobalCustomVars.backgroundMaterial) {
3739
+ setCurrentWindowStyle("none");
3740
+ }
3741
+ }
3742
+ return target[prop](
3743
+ ...args
3744
+ );
3745
+ };
3746
+ }
3747
+ return Reflect.get(target, prop);
3748
+ }
3749
+ });
3750
+ Object.defineProperty(document.documentElement, "style", {
3751
+ get: function() {
3752
+ return styleProxy;
3753
+ }
3754
+ });
3755
+ }
3756
+ function monitorExternalStyleChange() {
3757
+ const headObserver = new MutationObserver(function(mutationsList) {
3758
+ checkCSSProperties();
3759
+ });
3760
+ headObserver.observe(document.head, { childList: true, subtree: true });
3761
+ }
3762
+ function checkCSSProperties() {
3763
+ checkHtmlBackgroundMaterial();
3764
+ checkCornerRadius();
3765
+ checkOpacity();
3766
+ checkHtmlVisible();
3767
+ window.addEventListener("resize", checkHtmlVisible);
3768
+ }
3769
+ function hijackGetComputedStyle() {
3770
+ const rawFn = window.getComputedStyle.bind(window);
3771
+ window.getComputedStyle = (element, pseudoElt) => {
3772
+ if (element.__isSpatialDiv) {
3773
+ return element.__getComputedStyle(rawFn, pseudoElt);
3774
+ }
3775
+ return rawFn(element, pseudoElt);
3776
+ };
3777
+ }
3778
+ function hijackWindowOpen() {
3779
+ XRApp.getInstance().init();
3780
+ }
3781
+ function spatialPolyfill() {
3782
+ if (!isWebSpatialEnv) {
3783
+ return;
3784
+ }
3785
+ injectSceneHook();
3786
+ hijackWindowOpen();
3787
+ checkCSSProperties();
3788
+ hijackGetComputedStyle();
3789
+ hijackDocumentElementStyle();
3790
+ monitorExternalStyleChange();
3791
+ }
3585
3792
  export {
3586
3793
  CSSSpatialDiv,
3587
3794
  CSSSpatialPrimitive,
@@ -3597,6 +3804,7 @@ export {
3597
3804
  initScene,
3598
3805
  notifyUpdateStandInstanceLayout,
3599
3806
  parseCornerRadius,
3807
+ spatialPolyfill,
3600
3808
  withCSSSpatial,
3601
3809
  withSpatial,
3602
3810
  withSpatialMonitor