@plasmicapp/react-web 0.2.155 → 0.2.157

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.
package/dist/all.d.ts CHANGED
@@ -36,7 +36,17 @@ declare type HTMLElementRefOf<T extends keyof JSX.IntrinsicElements> = Exclude<R
36
36
 
37
37
  declare function dlv(object: object, key: string | Array<string | number>, defaultValue?: any): any;
38
38
 
39
- declare type InitFunc<T> = ($props: Record<string, any>, $state: $State, $ctx: Record<string, any>, indexes?: number[]) => T;
39
+ declare type InitFuncEnv = {
40
+ $props: Record<string, any>;
41
+ $state: Record<string, any>;
42
+ $queries?: Record<string, any>;
43
+ $ctx?: Record<string, any>;
44
+ };
45
+ declare type DollarStateEnv = Omit<InitFuncEnv, "$state">;
46
+ declare type NoUndefinedField<T> = {
47
+ [P in keyof T]-?: T[P];
48
+ };
49
+ declare type InitFunc<T> = (env: NoUndefinedField<InitFuncEnv>) => T;
40
50
  declare type ObjectPath = (string | number)[];
41
51
  interface $StateSpec<T> {
42
52
  path: string;
@@ -74,7 +84,7 @@ declare function getStateSpecInPlasmicProxy(obj: any, path: ObjectPath): {
74
84
  */
75
85
  declare function set(obj: any, keys: any, val: any): void;
76
86
 
77
- declare function useDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>, opts?: {
87
+ declare function useDollarState(specs: $StateSpec<any>[], env: DollarStateEnv, opts?: {
78
88
  inCanvas: boolean;
79
89
  }): $State;
80
90
 
@@ -2355,6 +2355,15 @@ var StateSpecNode = /*#__PURE__*/function () {
2355
2355
  path: path
2356
2356
  };
2357
2357
  };
2358
+ _proto.deleteStateCell = function deleteStateCell(prefixPath) {
2359
+ var _this = this;
2360
+ [].concat(Object.keys(this._state)).forEach(function (key) {
2361
+ var path = JSON.parse(key);
2362
+ if (shallowEqual(prefixPath, path.slice(0, prefixPath.length))) {
2363
+ delete _this._state[key];
2364
+ }
2365
+ });
2366
+ };
2358
2367
  _proto.setInitialValue = function setInitialValue(path, value) {
2359
2368
  var key = JSON.stringify(path);
2360
2369
  this._state[key].initialValue = value;
@@ -2613,7 +2622,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
2613
2622
  });
2614
2623
  var spec = node.getSpec();
2615
2624
  if (spec.valueProp) {
2616
- return $$state.props[spec.valueProp];
2625
+ return $$state.env.$props[spec.valueProp];
2617
2626
  } else if (!node.hasState(path) && spec.initFunc) {
2618
2627
  node.createStateCell(path);
2619
2628
  return initializeStateValue($$state, node, path, proxyRoot);
@@ -2629,19 +2638,23 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
2629
2638
  var node = _ref.node,
2630
2639
  path = _ref.path;
2631
2640
  node.addListener(path, function () {
2632
- var newValue = initialSpecNode.getSpec().initFunc($$state.props, $state, $$state.ctx);
2641
+ var newValue = initialSpecNode.getSpec().initFunc(_extends({
2642
+ $state: $state
2643
+ }, $$state.env));
2633
2644
  set(proxyRoot, initialStatePath, newValue);
2634
2645
  });
2635
2646
  });
2636
- var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))($$state.props, $state, $$state.ctx);
2647
+ var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))(_extends({
2648
+ $state: $state
2649
+ }, $$state.env));
2637
2650
  initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
2638
2651
  var initialSpec = initialSpecNode.getSpec();
2639
2652
  var value = initialSpec.isImmutable ? mkUntrackedValue(initialValue) : clone(initialValue);
2640
2653
  set(proxyRoot, initialStatePath, value);
2641
2654
  //immediately fire onChange
2642
2655
  if (initialSpec.onChangeProp) {
2643
- var _$$state$props$initia, _$$state$props;
2644
- (_$$state$props$initia = (_$$state$props = $$state.props)[initialSpec.onChangeProp]) == null ? void 0 : _$$state$props$initia.call(_$$state$props, initialValue);
2656
+ var _$$state$env$$props$i, _$$state$env$$props;
2657
+ (_$$state$env$$props$i = (_$$state$env$$props = $$state.env.$props)[initialSpec.onChangeProp]) == null ? void 0 : _$$state$env$$props$i.call(_$$state$env$$props, initialValue);
2645
2658
  }
2646
2659
  return initialValue;
2647
2660
  }
@@ -2659,11 +2672,18 @@ function create$StateProxy($$state, leafHandlers) {
2659
2672
  }
2660
2673
  delete _get($$state.stateValues, currPath)[property];
2661
2674
  if (spec.onChangeProp) {
2662
- var _$$state$props$spec$o, _$$state$props2;
2675
+ var _$$state$env$$props$s, _$$state$env$$props2;
2663
2676
  //we are always in a leaf, since we only have two cases:
2664
2677
  // 1 - delete properties outside the state tree
2665
2678
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
2666
- (_$$state$props$spec$o = (_$$state$props2 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o.call(_$$state$props2, _get(proxyRoot, currPath.slice(spec.pathObj.length)));
2679
+ (_$$state$env$$props$s = (_$$state$env$$props2 = $$state.env.$props)[spec.onChangeProp]) == null ? void 0 : _$$state$env$$props$s.call(_$$state$env$$props2, _get(proxyRoot, currPath.slice(spec.pathObj.length)));
2680
+ }
2681
+ var nextPath = getNextPath(property);
2682
+ var nextNode = currNode.makeTransition(property);
2683
+ if (nextNode) {
2684
+ getStateCells(nextNode).forEach(function (node) {
2685
+ node.deleteStateCell(nextPath);
2686
+ });
2667
2687
  }
2668
2688
  return Reflect.deleteProperty(target, property);
2669
2689
  },
@@ -2720,12 +2740,19 @@ function create$StateProxy($$state, leafHandlers) {
2720
2740
  } else {
2721
2741
  Reflect.set(target, property, value, receiver);
2722
2742
  }
2723
- nextNode.getAllSpecs().forEach(function (spec) {
2743
+ if (currNode.isLeaf()) {
2724
2744
  if (spec.onChangeProp) {
2725
- var _$$state$props$spec$o2, _$$state$props3;
2726
- (_$$state$props$spec$o2 = (_$$state$props3 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o2.call(_$$state$props3, value);
2745
+ var _$$state$env$$props$s2, _$$state$env$$props3;
2746
+ (_$$state$env$$props$s2 = (_$$state$env$$props3 = $$state.env.$props)[spec.onChangeProp]) == null ? void 0 : _$$state$env$$props$s2.call(_$$state$env$$props3, target);
2727
2747
  }
2728
- });
2748
+ } else {
2749
+ nextNode.getAllSpecs().forEach(function (spec) {
2750
+ if (spec.onChangeProp) {
2751
+ var _$$state$env$$props$s3, _$$state$env$$props4;
2752
+ (_$$state$env$$props$s3 = (_$$state$env$$props4 = $$state.env.$props)[spec.onChangeProp]) == null ? void 0 : _$$state$env$$props$s3.call(_$$state$env$$props4, value);
2753
+ }
2754
+ });
2755
+ }
2729
2756
  var newValue = (isOutside || currNode.isLeaf()) && currNode.getSpec().isImmutable ? mkUntrackedValue(value) : value;
2730
2757
  set($$state.stateValues, nextPath, newValue);
2731
2758
  return true;
@@ -2753,22 +2780,27 @@ function create$StateProxy($$state, leafHandlers) {
2753
2780
  var mkUntrackedValue = function mkUntrackedValue(o) {
2754
2781
  return o != null && typeof o === "object" ? valtio.ref(o) : o;
2755
2782
  };
2756
- function useDollarState(specs, props, $ctx, opts) {
2783
+ var envFieldsAreNonNill = function envFieldsAreNonNill(env) {
2784
+ var _env$$ctx, _env$$queries;
2785
+ return {
2786
+ $props: env.$props,
2787
+ $ctx: (_env$$ctx = env.$ctx) != null ? _env$$ctx : {},
2788
+ $queries: (_env$$queries = env.$queries) != null ? _env$$queries : {}
2789
+ };
2790
+ };
2791
+ function useDollarState(specs, env, opts) {
2757
2792
  var $$state = React__default.useRef(function () {
2758
2793
  var rootSpecTree = buildTree(specs);
2759
2794
  return {
2760
2795
  rootSpecTree: rootSpecTree,
2761
2796
  specTreeLeaves: getStateCells(rootSpecTree),
2762
2797
  stateValues: valtio.proxy({}),
2763
- props: {},
2764
- ctx: {},
2798
+ env: envFieldsAreNonNill(env),
2765
2799
  specs: [],
2766
2800
  registrationsQueue: valtio.proxy([])
2767
2801
  };
2768
2802
  }()).current;
2769
- $$state.props = props;
2770
- $$state.ctx = $ctx != null ? $ctx : {};
2771
- $$state.specs = specs;
2803
+ $$state.env = envFieldsAreNonNill(env), $$state.specs = specs;
2772
2804
  var create$State = function create$State() {
2773
2805
  var $state = Object.assign(create$StateProxy($$state, function (node, path) {
2774
2806
  if (!node.hasState(path)) {
@@ -2784,7 +2816,7 @@ function useDollarState(specs, props, $ctx, opts) {
2784
2816
  get: function get(target, property, receiver) {
2785
2817
  var spec = node.getSpec();
2786
2818
  if (spec.valueProp) {
2787
- return $$state.props[spec.valueProp];
2819
+ return $$state.env.$props[spec.valueProp];
2788
2820
  } else {
2789
2821
  return Reflect.get(target, property, receiver);
2790
2822
  }
@@ -2798,7 +2830,9 @@ function useDollarState(specs, props, $ctx, opts) {
2798
2830
  if (!node.hasState(realPath)) {
2799
2831
  node.createStateCell(realPath);
2800
2832
  }
2801
- if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
2833
+ if (!deepEqual(node.getState(realPath).initialValue, f(_extends({
2834
+ $state: $state
2835
+ }, $$state.env)))) {
2802
2836
  $$state.registrationsQueue.push(mkUntrackedValue({
2803
2837
  node: node,
2804
2838
  path: realPath,
@@ -2835,7 +2869,7 @@ function useDollarState(specs, props, $ctx, opts) {
2835
2869
  return;
2836
2870
  }
2837
2871
  node.createStateCell(spec.pathObj);
2838
- var init = spec.valueProp ? $$state.props[spec.valueProp] : spec.initFunc ? initializeStateValue($$state, node, spec.pathObj, $state) : spec.initVal;
2872
+ var init = spec.valueProp ? $$state.env.$props[spec.valueProp] : spec.initFunc ? initializeStateValue($$state, node, spec.pathObj, $state) : spec.initVal;
2839
2873
  set($state, spec.pathObj, init);
2840
2874
  });
2841
2875
  }
@@ -2854,7 +2888,9 @@ function useDollarState(specs, props, $ctx, opts) {
2854
2888
  stateCell = _ref3.stateCell;
2855
2889
  var initFunc = node.getInitFunc(stateCell);
2856
2890
  if (initFunc) {
2857
- var newInit = initFunc(props, $state, $ctx != null ? $ctx : {});
2891
+ var newInit = initFunc(_extends({
2892
+ $state: $state
2893
+ }, envFieldsAreNonNill(env)));
2858
2894
  if (!deepEqual(newInit, stateCell.initialValue)) {
2859
2895
  resetSpecs.push({
2860
2896
  stateCell: stateCell,
@@ -2867,8 +2903,8 @@ function useDollarState(specs, props, $ctx, opts) {
2867
2903
  var newInit = initializeStateValue($$state, node, stateCell.path, $state);
2868
2904
  var spec = node.getSpec();
2869
2905
  if (spec.onChangeProp) {
2870
- var _$$state$props$spec$o3, _$$state$props4;
2871
- (_$$state$props$spec$o3 = (_$$state$props4 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o3.call(_$$state$props4, newInit);
2906
+ var _$$state$env$$props$s4, _$$state$env$$props5;
2907
+ (_$$state$env$$props$s4 = (_$$state$env$$props5 = $$state.env.$props)[spec.onChangeProp]) == null ? void 0 : _$$state$env$$props$s4.call(_$$state$env$$props5, newInit);
2872
2908
  }
2873
2909
  };
2874
2910
  useIsomorphicLayoutEffect$1(function () {
@@ -2877,7 +2913,7 @@ function useDollarState(specs, props, $ctx, opts) {
2877
2913
  node = _ref4.node;
2878
2914
  reInitializeState(node, stateCell);
2879
2915
  });
2880
- }, [props, resetSpecs]);
2916
+ }, [env.$props, resetSpecs]);
2881
2917
  useIsomorphicLayoutEffect$1(function () {
2882
2918
  while ($$state.registrationsQueue.length) {
2883
2919
  var _$$state$registration = $$state.registrationsQueue.shift(),