@plasmicapp/react-web 0.2.134 → 0.2.136

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,5 +1,5 @@
1
1
  import * as React from "react";
2
- import { $State } from "../states/valtio";
2
+ import { $State } from "../states";
3
3
  interface Variants {
4
4
  [vg: string]: any;
5
5
  }
@@ -0,0 +1,15 @@
1
+ import { $StateSpec, Internal$StateSpec } from ".";
2
+ export declare class StateSpecNode<T> {
3
+ private specs;
4
+ private edges;
5
+ constructor(specs: Internal$StateSpec<T>[]);
6
+ hasEdge(key: string | symbol): boolean;
7
+ addEdge(key: string | symbol, node: StateSpecNode<any>): void;
8
+ makeTransition(key: string | symbol | number): StateSpecNode<any> | undefined;
9
+ isLeaf(): boolean;
10
+ hasArrayTransition(): boolean;
11
+ getSpec(): Internal$StateSpec<T>;
12
+ getAllSpecs(): Internal$StateSpec<T>[];
13
+ }
14
+ export declare const transformPathStringToObj: (str: string) => (string | symbol)[];
15
+ export declare function buildGraph(specs: $StateSpec<any>[]): StateSpecNode<any>;
@@ -15,4 +15,6 @@ export declare function generateStateValueProp($state: $State, path: (string | n
15
15
  */
16
16
  export declare function set(obj: any, keys: any, val: any): void;
17
17
  export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
18
+ export declare function shallowEqual<T>(a1: T[], a2: T[]): boolean;
19
+ export declare function isNum(value: string | number | symbol): value is number;
18
20
  export { get };
@@ -1,8 +1,5 @@
1
- declare type InitFunc<T> = ($props: Record<string, any>, $state: $State) => T;
2
- export interface $State {
3
- [key: string]: any;
4
- registerInitFunc?: (path: string, f: InitFunc<any>) => any;
5
- }
1
+ export declare type InitFunc<T> = ($props: Record<string, any>, $state: $State, $ctx: Record<string, any>, indexes?: number[]) => T;
2
+ export declare type ObjectPath = (string | number)[];
6
3
  export interface $StateSpec<T> {
7
4
  path: string;
8
5
  initFunc?: InitFunc<T>;
@@ -10,6 +7,18 @@ export interface $StateSpec<T> {
10
7
  type: "private" | "readonly" | "writable";
11
8
  valueProp?: string;
12
9
  onChangeProp?: string;
10
+ isImmutable?: boolean;
11
+ }
12
+ export interface $State {
13
+ [key: string]: any;
14
+ registerInitFunc?: (path: string, f: InitFunc<any>) => any;
15
+ }
16
+ export declare const ARRAY_SYMBOL: unique symbol;
17
+ export interface Internal$StateSpec<T> extends $StateSpec<T> {
18
+ isRepeated: boolean;
19
+ pathObj: (string | symbol)[];
20
+ }
21
+ export interface Internal$StateInstance {
22
+ path: ObjectPath;
23
+ specKey: string;
13
24
  }
14
- declare function useVanillaDollarState(_specs: $StateSpec<any>[], props: Record<string, any>): $State;
15
- export default useVanillaDollarState;
@@ -1,18 +1,4 @@
1
- declare type InitFunc<T> = ($props: Record<string, any>, $state: $State, $ctx: Record<string, any>, indexes: number[]) => T;
2
- export interface $State {
3
- [key: string]: any;
4
- registerInitFunc?: (path: string, f: InitFunc<any>) => any;
5
- }
6
- export interface $StateSpec<T> {
7
- path: string;
8
- initFunc?: InitFunc<T>;
9
- initVal?: T;
10
- type: "private" | "readonly" | "writable";
11
- valueProp?: string;
12
- onChangeProp?: string;
13
- isArray?: boolean;
14
- isImmutable?: boolean;
15
- }
1
+ import { $StateSpec } from ".";
16
2
  export declare function useDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>): any;
17
3
  export default useDollarState;
18
- export declare function useCanvasDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>): any;
4
+ export declare function useCanvasDollarState(specs: $StateSpec<any>[], props: Record<string, any>, $ctx?: Record<string, any>): {};
@@ -0,0 +1,3 @@
1
+ import { $State, $StateSpec } from ".";
2
+ declare function useVanillaDollarState(_specs: $StateSpec<any>[], props: Record<string, any>): $State;
3
+ export default useVanillaDollarState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plasmicapp/react-web",
3
- "version": "0.2.134",
3
+ "version": "0.2.136",
4
4
  "description": "plasmic library for rendering in the presentational style",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -99,7 +99,7 @@
99
99
  "react": "^16.13.1",
100
100
  "react-dom": "^16.13.1",
101
101
  "rollup": "^2.47.0",
102
- "rollup-plugin-dts": "^3.0.1",
102
+ "rollup-plugin-dts": "^5.1.0",
103
103
  "rollup-plugin-typescript2": "^0.30.0",
104
104
  "size-limit": "^4.10.2",
105
105
  "tsdx": "^0.14.1",
@@ -110,5 +110,5 @@
110
110
  "react": ">=16.8.0",
111
111
  "react-dom": ">=16.8.0"
112
112
  },
113
- "gitHead": "79d47b209f2166dc80c5bb5f24491e0b8cbc02a4"
113
+ "gitHead": "fc4a6232afc0ab9b236d8e1f9988e3e598e86c27"
114
114
  }
@@ -13,6 +13,7 @@ export { PlasmicDataSourceContextProvider, PlasmicRootProvider, useCurrentUser,
13
13
  export { Stack } from "./render/Stack";
14
14
  export { genTranslatableString, Trans } from "./render/translation";
15
15
  export { useTrigger } from "./render/triggers";
16
+ export { $State } from "./states";
16
17
  export * from "./states/helpers";
17
- export { $State, default as useDollarState, useCanvasDollarState, } from "./states/valtio";
18
+ export { default as useDollarState, useCanvasDollarState, } from "./states/valtio";
18
19
  export declare const classNames: typeof _classNames;
@@ -681,18 +681,84 @@ function set(obj, keys, val) {
681
681
  }
682
682
  }
683
683
  }
684
- var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
684
+ var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
685
+ function shallowEqual$1(a1, a2) {
686
+ if (a1.length !== a2.length) {
687
+ return false;
688
+ }
689
+ for (var i = 0; i < a1.length; i++) {
690
+ if (a1[i] !== a2[i]) {
691
+ return false;
692
+ }
693
+ }
694
+ return true;
695
+ }
696
+ function isNum$1(value) {
697
+ return typeof value === "symbol" ? false : !isNaN(+value);
698
+ }
685
699
 
686
- var mkUntrackedValue = function (o) {
687
- return o != null && typeof o === "object" ? ref(o) : o;
688
- };
700
+ var ARRAY_SYMBOL = Symbol("[]");
701
+
702
+ var StateSpecNode = /** @class */ (function () {
703
+ function StateSpecNode(specs) {
704
+ this.specs = specs;
705
+ this.edges = new Map();
706
+ }
707
+ StateSpecNode.prototype.hasEdge = function (key) {
708
+ return this.edges.has(key);
709
+ };
710
+ StateSpecNode.prototype.addEdge = function (key, node) {
711
+ this.edges.set(key, node);
712
+ };
713
+ StateSpecNode.prototype.makeTransition = function (key) {
714
+ key = isNum$1(key) ? ARRAY_SYMBOL : key;
715
+ return this.edges.get(key);
716
+ };
717
+ StateSpecNode.prototype.isLeaf = function () {
718
+ return this.edges.size === 0;
719
+ };
720
+ StateSpecNode.prototype.hasArrayTransition = function () {
721
+ return this.edges.has(ARRAY_SYMBOL);
722
+ };
723
+ StateSpecNode.prototype.getSpec = function () {
724
+ return this.specs[0];
725
+ };
726
+ StateSpecNode.prototype.getAllSpecs = function () {
727
+ return this.specs;
728
+ };
729
+ return StateSpecNode;
730
+ }());
689
731
  var transformPathStringToObj = function (str) {
690
732
  var splitStatePathPart = function (state) {
691
733
  return state.endsWith("[]")
692
- ? __spreadArray(__spreadArray([], __read(splitStatePathPart(state.slice(0, -2))), false), ["[]"], false) : [state];
734
+ ? __spreadArray(__spreadArray([], __read(splitStatePathPart(state.slice(0, -2))), false), [ARRAY_SYMBOL], false) : [state];
693
735
  };
694
736
  return str.split(".").flatMap(splitStatePathPart);
695
737
  };
738
+ function buildGraph(specs) {
739
+ var internalSpec = specs.map(function (spec) {
740
+ return (__assign(__assign({}, spec), { pathObj: transformPathStringToObj(spec.path), isRepeated: spec.path.split(".").some(function (part) { return part.endsWith("[]"); }) }));
741
+ });
742
+ var rec = function (currentPath) {
743
+ var node = new StateSpecNode(internalSpec.filter(function (spec) {
744
+ return shallowEqual$1(currentPath, spec.pathObj.slice(0, currentPath.length));
745
+ }));
746
+ node.getAllSpecs().forEach(function (spec) {
747
+ if (spec.pathObj.length > currentPath.length) {
748
+ var nextKey = spec.pathObj[currentPath.length];
749
+ if (!node.hasEdge(nextKey)) {
750
+ node.addEdge(nextKey, rec(__spreadArray(__spreadArray([], __read(currentPath), false), [nextKey], false)));
751
+ }
752
+ }
753
+ });
754
+ return node;
755
+ };
756
+ return rec([]);
757
+ }
758
+
759
+ var mkUntrackedValue = function (o) {
760
+ return o != null && typeof o === "object" ? ref(o) : o;
761
+ };
696
762
  function shallowEqual(a1, a2) {
697
763
  if (a1.length !== a2.length) {
698
764
  return false;
@@ -718,34 +784,11 @@ function saveNewState($$state, path, spec) {
718
784
  .push({ path: path, specKey: spec.path });
719
785
  }
720
786
  function create$StateProxy($$state, handlers) {
721
- var getNextKeyToSpecMap = function (currPath) {
722
- return new Map(Object.entries(Object.values($$state.specsByKey)
723
- .filter(function (spec) {
724
- return shallowEqual(currPath.map(function (p) { return (isNum(p) ? "[]" : p); }), spec.pathObj.slice(0, currPath.length));
725
- })
726
- .reduce(function (agg, spec) {
727
- var nextKey = spec.pathObj[currPath.length];
728
- if (!(nextKey in agg)) {
729
- agg[nextKey] = [];
730
- }
731
- agg[nextKey].push(spec);
732
- return agg;
733
- }, {})));
734
- };
735
- var rec = function (currPath) {
736
- var nextKeyToSpecs = getNextKeyToSpecMap(currPath);
737
- var getSpecForProperty = function (property) {
738
- var _a, _b;
739
- return nextKeyToSpecs.has("[]") && isNum(property)
740
- ? (_a = nextKeyToSpecs.get("[]")) === null || _a === void 0 ? void 0 : _a[0]
741
- : typeof property === "string" && nextKeyToSpecs.has(property)
742
- ? (_b = nextKeyToSpecs.get(property)) === null || _b === void 0 ? void 0 : _b[0]
743
- : undefined;
744
- };
787
+ var rec = function (currPath, currNode) {
745
788
  var getNextPath = function (property) { return __spreadArray(__spreadArray([], __read(currPath), false), [
746
789
  isNum(property) ? +property : property,
747
790
  ], false); };
748
- return new Proxy(nextKeyToSpecs.has("[]") ? [] : {}, {
791
+ return new Proxy(currNode.hasArrayTransition() ? [] : {}, {
749
792
  deleteProperty: function (target, property) {
750
793
  var prefixPath = getNextPath(property);
751
794
  var specKeysToUpdate = new Set();
@@ -768,42 +811,41 @@ function create$StateProxy($$state, handlers) {
768
811
  },
769
812
  get: function (target, property, receiver) {
770
813
  var _a, _b;
771
- var spec = getSpecForProperty(property);
772
- if (spec && typeof property !== "symbol") {
773
- var nextPath = getNextPath(property);
774
- if (spec.pathObj.length === currPath.length + 1) {
775
- // reached the end of the spec
776
- target[property] = (_b = (_a = handlers(nextPath, spec)).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
814
+ var nextPath = getNextPath(property);
815
+ var nextNode = currNode.makeTransition(property);
816
+ if (nextNode) {
817
+ if (nextNode.isLeaf()) {
818
+ target[property] = (_b = (_a = handlers(nextPath, nextNode.getSpec())).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
777
819
  }
778
820
  else if (!(property in target)) {
779
- target[property] = rec(nextPath);
821
+ target[property] = rec(nextPath, nextNode);
780
822
  }
781
823
  }
782
824
  return Reflect.get(target, property, receiver);
783
825
  },
784
826
  set: function (target, property, value, receiver) {
785
827
  var e_1, _a;
786
- var _b, _c, _d;
787
- var spec = getSpecForProperty(property);
828
+ var _b, _c, _d, _e;
788
829
  var nextPath = getNextPath(property);
789
- if (spec && typeof property !== "symbol") {
790
- if (spec.pathObj.length === currPath.length + 1) {
830
+ var nextNode = currNode.makeTransition(property);
831
+ if (nextNode && typeof property !== "symbol") {
832
+ if (nextNode.isLeaf()) {
791
833
  // reached the end of the spec
792
- target[property] = (_c = (_b = handlers(nextPath, spec)).set) === null || _c === void 0 ? void 0 : _c.call(_b, target, property, value, receiver);
834
+ target[property] = (_c = (_b = handlers(nextPath, nextNode.getSpec())).set) === null || _c === void 0 ? void 0 : _c.call(_b, target, property, value, receiver);
793
835
  return Reflect.set(target, property, value, receiver);
794
836
  }
795
837
  else if (typeof value === "object") {
796
- target[property] = rec(nextPath);
838
+ target[property] = rec(nextPath, nextNode);
797
839
  try {
798
- for (var _e = __values(Object.keys(value)), _f = _e.next(); !_f.done; _f = _e.next()) {
799
- var key = _f.value;
840
+ for (var _f = __values(Object.keys(value)), _g = _f.next(); !_g.done; _g = _f.next()) {
841
+ var key = _g.value;
800
842
  target[property][key] = value[key];
801
843
  }
802
844
  }
803
845
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
804
846
  finally {
805
847
  try {
806
- if (_f && !_f.done && (_a = _e["return"])) _a.call(_e);
848
+ if (_g && !_g.done && (_a = _f["return"])) _a.call(_f);
807
849
  }
808
850
  finally { if (e_1) throw e_1.error; }
809
851
  }
@@ -813,11 +855,12 @@ function create$StateProxy($$state, handlers) {
813
855
  if (property === "registerInitFunc" && currPath.length === 0) {
814
856
  return Reflect.set(target, property, value, receiver);
815
857
  }
816
- if (nextKeyToSpecs.has("[]")) {
858
+ if (currNode.hasArrayTransition()) {
817
859
  set($$state.stateValues, nextPath, value);
818
- (_d = nextKeyToSpecs.get("[]")) === null || _d === void 0 ? void 0 : _d.forEach(function (spec) {
860
+ (_e = (_d = currNode
861
+ .makeTransition(ARRAY_SYMBOL)) === null || _d === void 0 ? void 0 : _d.getAllSpecs()) === null || _e === void 0 ? void 0 : _e.forEach(function (spec) {
819
862
  var _a, _b;
820
- if (spec === null || spec === void 0 ? void 0 : spec.onChangeProp) {
863
+ if (spec.onChangeProp) {
821
864
  (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value, nextPath);
822
865
  }
823
866
  });
@@ -828,7 +871,7 @@ function create$StateProxy($$state, handlers) {
828
871
  }
829
872
  });
830
873
  };
831
- return rec([]);
874
+ return rec([], $$state.rootStateSpec);
832
875
  }
833
876
  var deleteState = function ($$state, path) {
834
877
  var _a;
@@ -924,7 +967,8 @@ function useDollarState(specs, props, $ctx) {
924
967
  unsubscriptionsByState: {},
925
968
  props: {},
926
969
  ctx: {},
927
- registrationsQueue: []
970
+ registrationsQueue: [],
971
+ rootStateSpec: buildGraph(specs)
928
972
  })).current;
929
973
  $$state.props = mkUntrackedValue(props);
930
974
  $$state.ctx = mkUntrackedValue($ctx !== null && $ctx !== void 0 ? $ctx : {});
@@ -1021,25 +1065,12 @@ function useCanvasDollarState(specs, props, $ctx) {
1021
1065
  unsubscriptionsByState: {},
1022
1066
  props: {},
1023
1067
  ctx: {},
1024
- registrationsQueue: []
1068
+ registrationsQueue: [],
1069
+ rootStateSpec: buildGraph(specs)
1025
1070
  });
1026
1071
  $$state.props = mkUntrackedValue(props);
1027
1072
  $$state.ctx = mkUntrackedValue($ctx);
1028
- var $state = create$StateProxy($$state, function (path, spec) {
1029
- return {
1030
- get: function () {
1031
- return get($$state.stateValues, path);
1032
- },
1033
- set: function (_t, _p, value) {
1034
- var _a, _b;
1035
- saveValue($$state, path, spec, value);
1036
- if (spec.onChangeProp) {
1037
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value, path);
1038
- }
1039
- return true;
1040
- }
1041
- };
1042
- });
1073
+ var $state = {};
1043
1074
  try {
1044
1075
  for (var specs_1 = __values(specs), specs_1_1 = specs_1.next(); !specs_1_1.done; specs_1_1 = specs_1.next()) {
1045
1076
  var spec = specs_1_1.value;
@@ -1067,5 +1098,5 @@ function useCanvasDollarState(specs, props, $ctx) {
1067
1098
  // Utilities used by generated code
1068
1099
  var classNames = classNames$1;
1069
1100
 
1070
- export { PlasmicIcon, PlasmicLink, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateValueProp, hasVariant, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, set, useCanvasDollarState, useDollarState, useIsomorphicLayoutEffect, useTrigger, wrapWithClassName };
1101
+ export { PlasmicIcon, PlasmicLink, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateValueProp, hasVariant, isNum$1 as isNum, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, set, shallowEqual$1 as shallowEqual, useCanvasDollarState, useDollarState, useIsomorphicLayoutEffect, useTrigger, wrapWithClassName };
1071
1102
  //# sourceMappingURL=index.js.map