@plasmicapp/react-web 0.2.158 → 0.2.160

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,16 @@ 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 NoUndefinedField<T> = {
46
+ [P in keyof T]-?: T[P];
47
+ };
48
+ declare type InitFunc<T> = (env: NoUndefinedField<InitFuncEnv>) => T;
40
49
  declare type ObjectPath = (string | number)[];
41
50
  interface $StateSpec<T> {
42
51
  path: string;
@@ -74,9 +83,7 @@ declare function getStateSpecInPlasmicProxy(obj: any, path: ObjectPath): {
74
83
  */
75
84
  declare function set(obj: any, keys: any, val: any): void;
76
85
 
77
- declare function useDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>, opts?: {
78
- inCanvas: boolean;
79
- }): $State;
86
+ declare function useDollarState(specs: $StateSpec<any>[], ...rest: any[]): $State;
80
87
 
81
88
  interface Variants {
82
89
  [vg: string]: any;
@@ -2625,7 +2625,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
2625
2625
  });
2626
2626
  var spec = node.getSpec();
2627
2627
  if (spec.valueProp) {
2628
- return $$state.props[spec.valueProp];
2628
+ return $$state.env.$props[spec.valueProp];
2629
2629
  } else if (!node.hasState(path) && spec.initFunc) {
2630
2630
  node.createStateCell(path);
2631
2631
  return initializeStateValue($$state, node, path, proxyRoot);
@@ -2641,19 +2641,23 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
2641
2641
  var node = _ref.node,
2642
2642
  path = _ref.path;
2643
2643
  node.addListener(path, function () {
2644
- var newValue = initialSpecNode.getSpec().initFunc($$state.props, $state, $$state.ctx);
2644
+ var newValue = initialSpecNode.getSpec().initFunc(_extends({
2645
+ $state: $state
2646
+ }, $$state.env));
2645
2647
  set(proxyRoot, initialStatePath, newValue);
2646
2648
  });
2647
2649
  });
2648
- var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))($$state.props, $state, $$state.ctx);
2650
+ var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))(_extends({
2651
+ $state: $state
2652
+ }, $$state.env));
2649
2653
  initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
2650
2654
  var initialSpec = initialSpecNode.getSpec();
2651
2655
  var value = initialSpec.isImmutable ? mkUntrackedValue(initialValue) : clone(initialValue);
2652
2656
  set(proxyRoot, initialStatePath, value);
2653
2657
  //immediately fire onChange
2654
2658
  if (initialSpec.onChangeProp) {
2655
- var _$$state$props$initia, _$$state$props;
2656
- (_$$state$props$initia = (_$$state$props = $$state.props)[initialSpec.onChangeProp]) == null ? void 0 : _$$state$props$initia.call(_$$state$props, initialValue);
2659
+ var _$$state$env$$props$i, _$$state$env$$props;
2660
+ (_$$state$env$$props$i = (_$$state$env$$props = $$state.env.$props)[initialSpec.onChangeProp]) == null ? void 0 : _$$state$env$$props$i.call(_$$state$env$$props, initialValue);
2657
2661
  }
2658
2662
  return initialValue;
2659
2663
  }
@@ -2671,11 +2675,11 @@ function create$StateProxy($$state, leafHandlers) {
2671
2675
  }
2672
2676
  delete _get($$state.stateValues, currPath)[property];
2673
2677
  if (spec.onChangeProp) {
2674
- var _$$state$props$spec$o, _$$state$props2;
2678
+ var _$$state$env$$props$s, _$$state$env$$props2;
2675
2679
  //we are always in a leaf, since we only have two cases:
2676
2680
  // 1 - delete properties outside the state tree
2677
2681
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
2678
- (_$$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)));
2682
+ (_$$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)));
2679
2683
  }
2680
2684
  var nextPath = getNextPath(property);
2681
2685
  var nextNode = currNode.makeTransition(property);
@@ -2739,12 +2743,19 @@ function create$StateProxy($$state, leafHandlers) {
2739
2743
  } else {
2740
2744
  Reflect.set(target, property, value, receiver);
2741
2745
  }
2742
- nextNode.getAllSpecs().forEach(function (spec) {
2746
+ if (currNode.isLeaf()) {
2743
2747
  if (spec.onChangeProp) {
2744
- var _$$state$props$spec$o2, _$$state$props3;
2745
- (_$$state$props$spec$o2 = (_$$state$props3 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o2.call(_$$state$props3, value);
2748
+ var _$$state$env$$props$s2, _$$state$env$$props3;
2749
+ (_$$state$env$$props$s2 = (_$$state$env$$props3 = $$state.env.$props)[spec.onChangeProp]) == null ? void 0 : _$$state$env$$props$s2.call(_$$state$env$$props3, target);
2746
2750
  }
2747
- });
2751
+ } else {
2752
+ nextNode.getAllSpecs().forEach(function (spec) {
2753
+ if (spec.onChangeProp) {
2754
+ var _$$state$env$$props$s3, _$$state$env$$props4;
2755
+ (_$$state$env$$props$s3 = (_$$state$env$$props4 = $$state.env.$props)[spec.onChangeProp]) == null ? void 0 : _$$state$env$$props$s3.call(_$$state$env$$props4, value);
2756
+ }
2757
+ });
2758
+ }
2748
2759
  var newValue = (isOutside || currNode.isLeaf()) && currNode.getSpec().isImmutable ? mkUntrackedValue(value) : value;
2749
2760
  set($$state.stateValues, nextPath, newValue);
2750
2761
  return true;
@@ -2772,22 +2783,65 @@ function create$StateProxy($$state, leafHandlers) {
2772
2783
  var mkUntrackedValue = function mkUntrackedValue(o) {
2773
2784
  return o != null && typeof o === "object" ? valtio.ref(o) : o;
2774
2785
  };
2775
- function useDollarState(specs, props, $ctx, opts) {
2786
+ var envFieldsAreNonNill = function envFieldsAreNonNill(env) {
2787
+ var _env$$ctx, _env$$queries;
2788
+ return {
2789
+ $props: env.$props,
2790
+ $ctx: (_env$$ctx = env.$ctx) != null ? _env$$ctx : {},
2791
+ $queries: (_env$$queries = env.$queries) != null ? _env$$queries : {}
2792
+ };
2793
+ };
2794
+ /**
2795
+ * We need to support two versions with different parameters to be backward compatible
2796
+ * {
2797
+ * specs: $StateSpec<any>[],
2798
+ * props: Record<string, any>,
2799
+ * $ctx?: Record<string, any>,
2800
+ * opts?: { inCanvas: boolean; }
2801
+ * }
2802
+ * {
2803
+ * specs: $StateSpec<any>[],
2804
+ * env: { $props; $queries; $ctx },
2805
+ * opts?: { inCanvas: boolean }
2806
+ * }
2807
+ */
2808
+ function extractDollarStateParametersBackwardCompatible() {
2809
+ if ("$props" in (arguments.length <= 0 ? undefined : arguments[0])) {
2810
+ // latest version
2811
+ return {
2812
+ env: arguments.length <= 0 ? undefined : arguments[0],
2813
+ opts: arguments.length <= 1 ? undefined : arguments[1]
2814
+ };
2815
+ } else {
2816
+ return {
2817
+ env: {
2818
+ $props: arguments.length <= 0 ? undefined : arguments[0],
2819
+ $ctx: arguments.length <= 1 ? undefined : arguments[1],
2820
+ $queries: {}
2821
+ },
2822
+ opts: arguments.length <= 2 ? undefined : arguments[2]
2823
+ };
2824
+ }
2825
+ }
2826
+ function useDollarState(specs) {
2827
+ for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
2828
+ rest[_key - 1] = arguments[_key];
2829
+ }
2830
+ var _extractDollarStatePa = extractDollarStateParametersBackwardCompatible.apply(void 0, rest),
2831
+ env = _extractDollarStatePa.env,
2832
+ opts = _extractDollarStatePa.opts;
2776
2833
  var $$state = React__default.useRef(function () {
2777
2834
  var rootSpecTree = buildTree(specs);
2778
2835
  return {
2779
2836
  rootSpecTree: rootSpecTree,
2780
2837
  specTreeLeaves: getStateCells(rootSpecTree),
2781
2838
  stateValues: valtio.proxy({}),
2782
- props: {},
2783
- ctx: {},
2839
+ env: envFieldsAreNonNill(env),
2784
2840
  specs: [],
2785
2841
  registrationsQueue: valtio.proxy([])
2786
2842
  };
2787
2843
  }()).current;
2788
- $$state.props = props;
2789
- $$state.ctx = $ctx != null ? $ctx : {};
2790
- $$state.specs = specs;
2844
+ $$state.env = envFieldsAreNonNill(env), $$state.specs = specs;
2791
2845
  var create$State = function create$State() {
2792
2846
  var $state = Object.assign(create$StateProxy($$state, function (node, path) {
2793
2847
  if (!node.hasState(path)) {
@@ -2803,7 +2857,7 @@ function useDollarState(specs, props, $ctx, opts) {
2803
2857
  get: function get(target, property, receiver) {
2804
2858
  var spec = node.getSpec();
2805
2859
  if (spec.valueProp) {
2806
- return $$state.props[spec.valueProp];
2860
+ return $$state.env.$props[spec.valueProp];
2807
2861
  } else {
2808
2862
  return Reflect.get(target, property, receiver);
2809
2863
  }
@@ -2817,7 +2871,9 @@ function useDollarState(specs, props, $ctx, opts) {
2817
2871
  if (!node.hasState(realPath)) {
2818
2872
  node.createStateCell(realPath);
2819
2873
  }
2820
- if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
2874
+ if (!deepEqual(node.getState(realPath).initialValue, f(_extends({
2875
+ $state: $state
2876
+ }, $$state.env)))) {
2821
2877
  $$state.registrationsQueue.push(mkUntrackedValue({
2822
2878
  node: node,
2823
2879
  path: realPath,
@@ -2854,7 +2910,7 @@ function useDollarState(specs, props, $ctx, opts) {
2854
2910
  return;
2855
2911
  }
2856
2912
  node.createStateCell(spec.pathObj);
2857
- var init = spec.valueProp ? $$state.props[spec.valueProp] : spec.initFunc ? initializeStateValue($$state, node, spec.pathObj, $state) : spec.initVal;
2913
+ var init = spec.valueProp ? $$state.env.$props[spec.valueProp] : spec.initFunc ? initializeStateValue($$state, node, spec.pathObj, $state) : spec.initVal;
2858
2914
  set($state, spec.pathObj, init);
2859
2915
  });
2860
2916
  }
@@ -2873,7 +2929,9 @@ function useDollarState(specs, props, $ctx, opts) {
2873
2929
  stateCell = _ref3.stateCell;
2874
2930
  var initFunc = node.getInitFunc(stateCell);
2875
2931
  if (initFunc) {
2876
- var newInit = initFunc(props, $state, $ctx != null ? $ctx : {});
2932
+ var newInit = initFunc(_extends({
2933
+ $state: $state
2934
+ }, envFieldsAreNonNill(env)));
2877
2935
  if (!deepEqual(newInit, stateCell.initialValue)) {
2878
2936
  resetSpecs.push({
2879
2937
  stateCell: stateCell,
@@ -2886,8 +2944,8 @@ function useDollarState(specs, props, $ctx, opts) {
2886
2944
  var newInit = initializeStateValue($$state, node, stateCell.path, $state);
2887
2945
  var spec = node.getSpec();
2888
2946
  if (spec.onChangeProp) {
2889
- var _$$state$props$spec$o3, _$$state$props4;
2890
- (_$$state$props$spec$o3 = (_$$state$props4 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o3.call(_$$state$props4, newInit);
2947
+ var _$$state$env$$props$s4, _$$state$env$$props5;
2948
+ (_$$state$env$$props$s4 = (_$$state$env$$props5 = $$state.env.$props)[spec.onChangeProp]) == null ? void 0 : _$$state$env$$props$s4.call(_$$state$env$$props5, newInit);
2891
2949
  }
2892
2950
  };
2893
2951
  useIsomorphicLayoutEffect$1(function () {
@@ -2896,7 +2954,7 @@ function useDollarState(specs, props, $ctx, opts) {
2896
2954
  node = _ref4.node;
2897
2955
  reInitializeState(node, stateCell);
2898
2956
  });
2899
- }, [props, resetSpecs]);
2957
+ }, [env.$props, resetSpecs]);
2900
2958
  useIsomorphicLayoutEffect$1(function () {
2901
2959
  while ($$state.registrationsQueue.length) {
2902
2960
  var _$$state$registration = $$state.registrationsQueue.shift(),