@plasmicapp/react-web 0.2.136 → 0.2.138

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.
Files changed (36) hide show
  1. package/dist/all.d.ts +49 -46
  2. package/dist/auth/PlasmicPageGuard.d.ts +7 -0
  3. package/dist/index-common.d.ts +2 -3
  4. package/dist/react-web.cjs.development.js +708 -1087
  5. package/dist/react-web.cjs.development.js.map +1 -1
  6. package/dist/react-web.cjs.production.min.js +1 -1
  7. package/dist/react-web.cjs.production.min.js.map +1 -1
  8. package/dist/react-web.esm.js +710 -1086
  9. package/dist/react-web.esm.js.map +1 -1
  10. package/dist/render/translation.d.ts +2 -1
  11. package/dist/states/graph.d.ts +23 -2
  12. package/dist/states/helpers.d.ts +7 -6
  13. package/dist/states/index.d.ts +4 -24
  14. package/dist/states/types.d.ts +24 -0
  15. package/dist/states/valtio.d.ts +2 -3
  16. package/dist/stories/UseDollarState.stories.d.ts +1 -0
  17. package/package.json +5 -6
  18. package/skinny/dist/auth/PlasmicPageGuard.d.ts +7 -0
  19. package/skinny/dist/index-common.d.ts +2 -3
  20. package/skinny/dist/index.js +393 -319
  21. package/skinny/dist/index.js.map +1 -1
  22. package/skinny/dist/plume/checkbox/index.js +1 -1
  23. package/skinny/dist/plume/menu/index.js +1 -1
  24. package/skinny/dist/plume/menu-button/index.js +1 -1
  25. package/skinny/dist/plume/select/index.js +1 -1
  26. package/skinny/dist/plume/switch/index.js +1 -1
  27. package/skinny/dist/render/translation.d.ts +2 -1
  28. package/skinny/dist/{ssr-b16a1854.js → ssr-f6caec06.js} +7 -6
  29. package/skinny/dist/ssr-f6caec06.js.map +1 -0
  30. package/skinny/dist/states/graph.d.ts +23 -2
  31. package/skinny/dist/states/helpers.d.ts +7 -6
  32. package/skinny/dist/states/index.d.ts +4 -24
  33. package/skinny/dist/states/types.d.ts +24 -0
  34. package/skinny/dist/states/valtio.d.ts +2 -3
  35. package/skinny/dist/stories/UseDollarState.stories.d.ts +1 -0
  36. package/skinny/dist/ssr-b16a1854.js.map +0 -1
@@ -1,24 +1,46 @@
1
1
  import classNames$1 from 'classnames';
2
- import get from 'dlv';
3
- export { default as get } from 'dlv';
4
2
  import * as React from 'react';
5
3
  import React__default, { useLayoutEffect, useEffect } from 'react';
4
+ import get from 'dlv';
5
+ export { default as get } from 'dlv';
6
6
  import { c as createElementWithChildren, m as mergeProps, e as ensureNotArray, N as NONE, a as mergePropVals, i as isReactNode, b as isBrowser, u as useIsomorphicLayoutEffect$1 } from './react-utils-7bc53870.js';
7
7
  import { _ as __rest, a as __assign, b as __spreadArray, c as __read, o as omit, p as pick, d as __values, i as isSubset, e as chainSingleArgFuncs, n as notNil } from './common-9ebe374e.js';
8
8
  export { o as omit, p as pick } from './common-9ebe374e.js';
9
9
  export { PlasmicHead, plasmicHeadMeta } from './render/PlasmicHead/index.js';
10
10
  export { PlasmicImg } from './render/PlasmicImg/index.js';
11
- import { T as Trans } from './ssr-b16a1854.js';
12
- export { P as PlasmicRootProvider, T as Trans, g as genTranslatableString, u as useIsSSR } from './ssr-b16a1854.js';
11
+ import { T as Trans } from './ssr-f6caec06.js';
12
+ export { P as PlasmicRootProvider, T as Trans, g as genTranslatableString, u as useIsSSR } from './ssr-f6caec06.js';
13
13
  import ReactDOM__default from 'react-dom';
14
14
  import { useFocusRing } from '@react-aria/focus';
15
15
  import clone from 'clone';
16
16
  import deepEqual from 'fast-deep-equal';
17
- import { proxy, ref, useSnapshot } from 'valtio';
18
- import { subscribeKey } from 'valtio/utils';
17
+ import { proxy, useSnapshot, ref } from 'valtio';
19
18
  export { PlasmicDataSourceContextProvider, useCurrentUser } from '@plasmicapp/data-sources-context';
20
19
  import '@react-aria/ssr';
21
20
 
21
+ function PlasmicPageGuard(props) {
22
+ var children = props.children, dataSourceCtxValue = props.dataSourceCtxValue, validRoles = props.validRoles;
23
+ if (!dataSourceCtxValue || dataSourceCtxValue.isUserLoading) {
24
+ return null;
25
+ }
26
+ function canUserViewPage() {
27
+ if (!dataSourceCtxValue) {
28
+ return false;
29
+ }
30
+ if (!dataSourceCtxValue.user) {
31
+ return false;
32
+ }
33
+ if (!("roleId" in dataSourceCtxValue.user)) {
34
+ return false;
35
+ }
36
+ return validRoles.includes(dataSourceCtxValue.user.roleId);
37
+ }
38
+ if (!canUserViewPage()) {
39
+ return React__default.createElement("div", null, "You don't have access to this page");
40
+ }
41
+ return children;
42
+ }
43
+
22
44
  function renderStack(as, props, hasGap, ref) {
23
45
  var children = props.children, rest = __rest(props, ["children"]);
24
46
  var wrappedChildren = wrapFlexContainerChildren(children, hasGap !== null && hasGap !== void 0 ? hasGap : false);
@@ -651,6 +673,31 @@ function generateStateValueProp($state, path // ["parent", 0, 1, "counter"]
651
673
  ) {
652
674
  return get($state, path);
653
675
  }
676
+ var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
677
+ function shallowEqual(a1, a2) {
678
+ if (a1.length !== a2.length) {
679
+ return false;
680
+ }
681
+ for (var i = 0; i < a1.length; i++) {
682
+ if (a1[i] !== a2[i]) {
683
+ return false;
684
+ }
685
+ }
686
+ return true;
687
+ }
688
+ function isNum$1(value) {
689
+ return typeof value === "symbol" ? false : !isNaN(+value);
690
+ }
691
+ function assert(cond, msg) {
692
+ if (msg === void 0) { msg = "Assertion failed"; }
693
+ if (!cond) {
694
+ // We always generate an non empty message so that it doesn't get swallowed
695
+ // by the async library.
696
+ msg = (typeof msg === "string" ? msg : msg()) || "Assertion failed";
697
+ debugger;
698
+ throw new Error(msg);
699
+ }
700
+ }
654
701
  /**
655
702
  * Forked from https://github.com/lukeed/dset
656
703
  * Changes: fixed setting a deep value to a proxy object
@@ -662,47 +709,52 @@ function set(obj, keys, val) {
662
709
  k = keys[i++];
663
710
  if (k === "__proto__" || k === "constructor" || k === "prototype")
664
711
  break;
665
- if (i === l) {
666
- t[k] = val;
667
- t = t[k];
668
- }
669
- else {
670
- if (typeof (x = t[k]) === typeof keys) {
671
- t = t[k] = x;
672
- }
673
- else if (keys[i] * 0 !== 0 || !!~("" + keys[i]).indexOf(".")) {
674
- t[k] = {};
675
- t = t[k];
676
- }
677
- else {
678
- t[k] = [];
679
- t = t[k];
680
- }
681
- }
712
+ var newValue = i === l
713
+ ? val
714
+ : typeof (x = t[k]) === typeof keys
715
+ ? x
716
+ : keys[i] * 0 !== 0 || !!~("" + keys[i]).indexOf(".")
717
+ ? {}
718
+ : [];
719
+ assignValue(t, k, newValue);
720
+ t = t[k];
682
721
  }
683
722
  }
684
- var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
685
- function shallowEqual$1(a1, a2) {
686
- if (a1.length !== a2.length) {
687
- return false;
723
+ /**
724
+ * Forked from lodash
725
+ */
726
+ function baseAssignValue(object, key, value) {
727
+ if (key == "__proto__") {
728
+ Object.defineProperty(object, key, {
729
+ configurable: true,
730
+ enumerable: true,
731
+ value: value,
732
+ writable: true
733
+ });
688
734
  }
689
- for (var i = 0; i < a1.length; i++) {
690
- if (a1[i] !== a2[i]) {
691
- return false;
692
- }
735
+ else {
736
+ object[key] = value;
693
737
  }
694
- return true;
695
738
  }
696
- function isNum$1(value) {
697
- return typeof value === "symbol" ? false : !isNaN(+value);
739
+ function eq(value, other) {
740
+ return value === other || (value !== value && other !== other);
741
+ }
742
+ function assignValue(object, key, value) {
743
+ var objValue = object[key];
744
+ if (!(Object.prototype.hasOwnProperty.call(object, key) && eq(objValue, value)) ||
745
+ (value === undefined && !(key in object))) {
746
+ baseAssignValue(object, key, value);
747
+ }
698
748
  }
699
749
 
700
750
  var ARRAY_SYMBOL = Symbol("[]");
701
751
 
752
+ var UNINITIALIZED = Symbol("plasmic.unitialized");
702
753
  var StateSpecNode = /** @class */ (function () {
703
754
  function StateSpecNode(specs) {
704
755
  this.specs = specs;
705
756
  this.edges = new Map();
757
+ this.state = {};
706
758
  }
707
759
  StateSpecNode.prototype.hasEdge = function (key) {
708
760
  return this.edges.has(key);
@@ -710,6 +762,9 @@ var StateSpecNode = /** @class */ (function () {
710
762
  StateSpecNode.prototype.addEdge = function (key, node) {
711
763
  this.edges.set(key, node);
712
764
  };
765
+ StateSpecNode.prototype.children = function () {
766
+ return this.edges.values();
767
+ };
713
768
  StateSpecNode.prototype.makeTransition = function (key) {
714
769
  key = isNum$1(key) ? ARRAY_SYMBOL : key;
715
770
  return this.edges.get(key);
@@ -726,6 +781,40 @@ var StateSpecNode = /** @class */ (function () {
726
781
  StateSpecNode.prototype.getAllSpecs = function () {
727
782
  return this.specs;
728
783
  };
784
+ StateSpecNode.prototype.getState = function (path) {
785
+ return this.state[JSON.stringify(path)];
786
+ };
787
+ StateSpecNode.prototype.clearStates = function () {
788
+ this.state = {};
789
+ };
790
+ StateSpecNode.prototype.states = function () {
791
+ return Object.values(this.state);
792
+ };
793
+ StateSpecNode.prototype.hasState = function (path) {
794
+ var key = JSON.stringify(path);
795
+ return key in this.state;
796
+ };
797
+ StateSpecNode.prototype.createStateCell = function (path) {
798
+ var key = JSON.stringify(path);
799
+ this.state[key] = {
800
+ listeners: [],
801
+ initialValue: UNINITIALIZED,
802
+ registeredInitFunc: this.getSpec().initFunc,
803
+ path: path
804
+ };
805
+ };
806
+ StateSpecNode.prototype.setInitialValue = function (path, value) {
807
+ var key = JSON.stringify(path);
808
+ this.state[key].initialValue = value;
809
+ };
810
+ StateSpecNode.prototype.getInitialValue = function (path) {
811
+ var key = JSON.stringify(path);
812
+ return this.state[key].initialValue;
813
+ };
814
+ StateSpecNode.prototype.addListener = function (path, f) {
815
+ var key = JSON.stringify(path);
816
+ this.state[key].listeners.push(f);
817
+ };
729
818
  return StateSpecNode;
730
819
  }());
731
820
  var transformPathStringToObj = function (str) {
@@ -735,13 +824,13 @@ var transformPathStringToObj = function (str) {
735
824
  };
736
825
  return str.split(".").flatMap(splitStatePathPart);
737
826
  };
738
- function buildGraph(specs) {
827
+ function buildTree(specs) {
739
828
  var internalSpec = specs.map(function (spec) {
740
829
  return (__assign(__assign({}, spec), { pathObj: transformPathStringToObj(spec.path), isRepeated: spec.path.split(".").some(function (part) { return part.endsWith("[]"); }) }));
741
830
  });
742
831
  var rec = function (currentPath) {
743
832
  var node = new StateSpecNode(internalSpec.filter(function (spec) {
744
- return shallowEqual$1(currentPath, spec.pathObj.slice(0, currentPath.length));
833
+ return shallowEqual(currentPath, spec.pathObj.slice(0, currentPath.length));
745
834
  }));
746
835
  node.getAllSpecs().forEach(function (spec) {
747
836
  if (spec.pathObj.length > currentPath.length) {
@@ -754,349 +843,334 @@ function buildGraph(specs) {
754
843
  return node;
755
844
  };
756
845
  return rec([]);
757
- }
758
-
759
- var mkUntrackedValue = function (o) {
760
- return o != null && typeof o === "object" ? ref(o) : o;
761
- };
762
- function shallowEqual(a1, a2) {
763
- if (a1.length !== a2.length) {
764
- return false;
846
+ }
847
+ function getLeaves(root) {
848
+ var leaves = [];
849
+ var rec = function (node) {
850
+ var e_1, _a;
851
+ try {
852
+ for (var _b = __values(node.children()), _c = _b.next(); !_c.done; _c = _b.next()) {
853
+ var child = _c.value;
854
+ rec(child);
855
+ }
856
+ }
857
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
858
+ finally {
859
+ try {
860
+ if (_c && !_c.done && (_a = _b["return"])) _a.call(_b);
861
+ }
862
+ finally { if (e_1) throw e_1.error; }
863
+ }
864
+ if (node.isLeaf()) {
865
+ leaves.push(node);
866
+ }
867
+ };
868
+ rec(root);
869
+ return leaves;
870
+ }
871
+ function findStateCell(root, pathStr, repetitionIndex) {
872
+ var e_2, _a;
873
+ var realPath = [];
874
+ var pathObj = transformPathStringToObj(pathStr);
875
+ var currRepIndex = 0;
876
+ try {
877
+ for (var pathObj_1 = __values(pathObj), pathObj_1_1 = pathObj_1.next(); !pathObj_1_1.done; pathObj_1_1 = pathObj_1.next()) {
878
+ var part = pathObj_1_1.value;
879
+ if (typeof part === "symbol") {
880
+ if (!root.hasArrayTransition() ||
881
+ !repetitionIndex ||
882
+ currRepIndex > repetitionIndex.length) {
883
+ console.log(root);
884
+ throw new Error("transition not found: pathStr ".concat(pathStr, " part ").concat(typeof part === "symbol" ? "[]" : part));
885
+ }
886
+ realPath.push(repetitionIndex[currRepIndex++]);
887
+ root = root.makeTransition(ARRAY_SYMBOL);
888
+ }
889
+ else {
890
+ if (!root.hasEdge(part)) {
891
+ throw new Error("transition not found: pathStr ".concat(pathStr, " part ").concat(typeof part === "symbol" ? "[]" : part));
892
+ }
893
+ realPath.push(part);
894
+ root = root.makeTransition(part);
895
+ }
896
+ }
765
897
  }
766
- for (var i = 0; i < a1.length; i++) {
767
- if (a1[i] !== a2[i]) {
768
- return false;
898
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
899
+ finally {
900
+ try {
901
+ if (pathObj_1_1 && !pathObj_1_1.done && (_a = pathObj_1["return"])) _a.call(pathObj_1);
769
902
  }
903
+ finally { if (e_2) throw e_2.error; }
770
904
  }
771
- return true;
772
- }
905
+ return {
906
+ node: root,
907
+ realPath: realPath
908
+ };
909
+ }
910
+
773
911
  function isNum(value) {
774
912
  return typeof value === "symbol" ? false : !isNaN(+value);
775
913
  }
776
- function saveNewState($$state, path, spec) {
777
- var key = JSON.stringify(path);
778
- $$state.existingStates.set(key, { path: path, specKey: spec.path });
779
- if (!$$state.statesInstanceBySpec.has(spec.path)) {
780
- $$state.statesInstanceBySpec.set(spec.path, []);
781
- }
782
- $$state.statesInstanceBySpec
783
- .get(spec.path)
784
- .push({ path: path, specKey: spec.path });
914
+ function canProxy(value) {
915
+ return typeof value === "object" && value != null;
916
+ }
917
+ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyRoot) {
918
+ var _a, _b;
919
+ var stateAccess = new Set();
920
+ var $state = create$StateProxy($$state, function (node, path) { return ({
921
+ get: function () {
922
+ stateAccess.add({ path: path, node: node });
923
+ var spec = node.getSpec();
924
+ if (spec.valueProp) {
925
+ return $$state.props[spec.valueProp];
926
+ }
927
+ else if (!node.hasState(path) && spec.initFunc) {
928
+ return initializeStateValue($$state, node, path, proxyRoot);
929
+ }
930
+ return get(proxyRoot, path);
931
+ },
932
+ set: function () {
933
+ throw new Error("Cannot update state values during initialization");
934
+ }
935
+ }); });
936
+ stateAccess.forEach(function (_a) {
937
+ var node = _a.node, path = _a.path;
938
+ node.addListener(path, function () {
939
+ var newValue = initialSpecNode.getSpec().initFunc($$state.props, $state, $$state.ctx);
940
+ set(proxyRoot, initialStatePath, newValue);
941
+ });
942
+ });
943
+ var initialValue = initialSpecNode.getState(initialStatePath)
944
+ .registeredInitFunc($$state.props, $state, $$state.ctx);
945
+ initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
946
+ var initialSpec = initialSpecNode.getSpec();
947
+ var value = initialSpec.isImmutable
948
+ ? mkUntrackedValue(initialValue)
949
+ : clone(initialValue);
950
+ set(proxyRoot, initialStatePath, value);
951
+ //immediately fire onChange
952
+ if (initialSpec.onChangeProp) {
953
+ (_b = (_a = $$state.props)[initialSpec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, initialValue);
954
+ }
955
+ return initialValue;
785
956
  }
786
- function create$StateProxy($$state, handlers) {
787
- var rec = function (currPath, currNode) {
957
+ function create$StateProxy($$state, leafHandlers) {
958
+ var rec = function (currPath, currNode, isOutside, proxyRoot, initialObject) {
788
959
  var getNextPath = function (property) { return __spreadArray(__spreadArray([], __read(currPath), false), [
789
960
  isNum(property) ? +property : property,
790
961
  ], false); };
791
- return new Proxy(currNode.hasArrayTransition() ? [] : {}, {
962
+ var spec = currNode.getSpec();
963
+ var handlers = {
792
964
  deleteProperty: function (target, property) {
793
- var prefixPath = getNextPath(property);
794
- var specKeysToUpdate = new Set();
795
- $$state.existingStates.forEach(function (_a) {
796
- var path = _a.path, specKey = _a.specKey;
797
- if (path.length >= prefixPath.length &&
798
- shallowEqual(path.slice(0, prefixPath.length), prefixPath)) {
799
- deleteState($$state, path);
800
- specKeysToUpdate.add(specKey);
801
- }
802
- });
803
- specKeysToUpdate.forEach(function (specKey) {
804
- var _a, _b;
805
- var spec = $$state.specsByKey[specKey];
806
- if (spec.onChangeProp) {
807
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get($$state.stateValues, currPath), currPath);
808
- }
809
- });
965
+ var _a, _b;
966
+ if (!isOutside &&
967
+ !currNode.isLeaf() &&
968
+ !currNode.hasArrayTransition() &&
969
+ !isNum(property)) {
970
+ throw new Error("Can't delete a property in the middle of the state spec");
971
+ }
972
+ delete get($$state.stateValues, currPath)[property];
973
+ if (spec.onChangeProp) {
974
+ //we are always in a leaf, since we only have two cases:
975
+ // 1 - delete properties outside the state tree
976
+ // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
977
+ (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get($$state.stateValues, currPath.slice(spec.pathObj.length)));
978
+ }
810
979
  return Reflect.deleteProperty(target, property);
811
980
  },
812
981
  get: function (target, property, receiver) {
813
982
  var _a, _b;
983
+ proxyRoot = proxyRoot == null ? receiver : proxyRoot;
814
984
  var nextPath = getNextPath(property);
985
+ if (isOutside || currNode.isLeaf()) {
986
+ return Reflect.get(target, property, receiver);
987
+ }
815
988
  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);
819
- }
820
- else if (!(property in target)) {
821
- target[property] = rec(nextPath, nextNode);
822
- }
989
+ if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
990
+ return (_b = (_a = leafHandlers(nextNode, nextPath, proxyRoot)).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
991
+ }
992
+ else if (nextNode && !(property in target)) {
993
+ target[property] = rec(nextPath, nextNode, false, proxyRoot, undefined);
823
994
  }
824
995
  return Reflect.get(target, property, receiver);
825
996
  },
826
997
  set: function (target, property, value, receiver) {
827
- var e_1, _a;
828
- var _b, _c, _d, _e;
998
+ var _a, _b;
999
+ proxyRoot = proxyRoot == null ? receiver : proxyRoot;
829
1000
  var nextPath = getNextPath(property);
830
1001
  var nextNode = currNode.makeTransition(property);
831
- if (nextNode && typeof property !== "symbol") {
832
- if (nextNode.isLeaf()) {
833
- // reached the end of the spec
834
- target[property] = (_c = (_b = handlers(nextPath, nextNode.getSpec())).set) === null || _c === void 0 ? void 0 : _c.call(_b, target, property, value, receiver);
835
- return Reflect.set(target, property, value, receiver);
836
- }
837
- else if (typeof value === "object") {
838
- target[property] = rec(nextPath, nextNode);
839
- try {
840
- for (var _f = __values(Object.keys(value)), _g = _f.next(); !_g.done; _g = _f.next()) {
841
- var key = _g.value;
842
- target[property][key] = value[key];
843
- }
844
- }
845
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
846
- finally {
847
- try {
848
- if (_g && !_g.done && (_a = _f["return"])) _a.call(_f);
849
- }
850
- finally { if (e_1) throw e_1.error; }
851
- }
852
- return true;
853
- }
854
- }
855
1002
  if (property === "registerInitFunc" && currPath.length === 0) {
856
1003
  return Reflect.set(target, property, value, receiver);
857
1004
  }
858
- if (currNode.hasArrayTransition()) {
1005
+ if (!nextNode && currNode.hasArrayTransition()) {
859
1006
  set($$state.stateValues, nextPath, value);
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) {
862
- var _a, _b;
863
- if (spec.onChangeProp) {
864
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value, nextPath);
865
- }
866
- });
1007
+ //array can set his own properties such as length, map, ...
867
1008
  return Reflect.set(target, property, value, receiver);
868
1009
  }
869
- // invalid setting a value that doesn't make part of the spec
870
- return false;
1010
+ if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1011
+ (_b = (_a = leafHandlers(nextNode, nextPath, proxyRoot)).set) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, value, receiver);
1012
+ }
1013
+ if (!isOutside && !currNode.isLeaf() && !nextNode) {
1014
+ // can't set an unknown field in $state
1015
+ return false;
1016
+ }
1017
+ // we keep pointing to the leaf
1018
+ if (!nextNode) {
1019
+ assert(isOutside || currNode.isLeaf, "unexpected update in nextNode");
1020
+ nextNode = currNode;
1021
+ }
1022
+ if (canProxy(value)) {
1023
+ target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), proxyRoot, value);
1024
+ }
1025
+ else if (!isOutside && !currNode.isLeaf() && !(nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf())) {
1026
+ throw new Error("inserting a primitive value into a non-leaf");
1027
+ }
1028
+ else {
1029
+ Reflect.set(target, property, value, receiver);
1030
+ }
1031
+ nextNode.getAllSpecs().forEach(function (spec) {
1032
+ var _a, _b;
1033
+ if (spec.onChangeProp) {
1034
+ (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value);
1035
+ }
1036
+ });
1037
+ var newValue = (isOutside || currNode.isLeaf()) && currNode.getSpec().isImmutable
1038
+ ? mkUntrackedValue(value)
1039
+ : value;
1040
+ set($$state.stateValues, nextPath, newValue);
1041
+ return true;
871
1042
  }
872
- });
1043
+ };
1044
+ var baseObject = !isOutside && !currNode.isLeaf()
1045
+ ? currNode.hasArrayTransition()
1046
+ ? []
1047
+ : {}
1048
+ : Array.isArray(initialObject)
1049
+ ? []
1050
+ : Object.create(Object.getPrototypeOf(initialObject));
1051
+ var proxyObj = new Proxy(baseObject, handlers);
1052
+ if (initialObject) {
1053
+ Reflect.ownKeys(initialObject).forEach(function (key) {
1054
+ var desc = Object.getOwnPropertyDescriptor(initialObject, key);
1055
+ if (desc.get || desc.set) {
1056
+ Object.defineProperty(baseObject, key, desc);
1057
+ }
1058
+ else {
1059
+ proxyObj[key] = initialObject[key];
1060
+ }
1061
+ });
1062
+ }
1063
+ return proxyObj;
873
1064
  };
874
- return rec([], $$state.rootStateSpec);
1065
+ return rec([], $$state.rootSpecTree, false, undefined, undefined);
875
1066
  }
876
- var deleteState = function ($$state, path) {
877
- var _a;
878
- var key = JSON.stringify(path);
879
- (_a = $$state.unsubscriptionsByState[key]) === null || _a === void 0 ? void 0 : _a.forEach(function (f) { return f(); });
880
- delete $$state.unsubscriptionsByState[key];
881
- $$state.existingStates["delete"](key);
882
- // delete get($$state.stateValues, path.slice(-1))[path.slice(-1)[0]];
883
- // delete get($$state.initStateValues, path.slice(-1))[path.slice(-1)[0]];
884
- };
885
- var getIndexes = function (path, spec) {
886
- var indexes = [];
887
- if (path.length !== spec.pathObj.length) {
888
- throw new Error("Unexpected error: state path and spec path have different lengths");
889
- }
890
- for (var i = 0; i < spec.pathObj.length; i++) {
891
- if (spec.pathObj[i] === "[]") {
892
- indexes.push(path[i]);
893
- }
894
- }
895
- return indexes;
1067
+ var mkUntrackedValue = function (o) {
1068
+ return o != null && typeof o === "object" ? ref(o) : o;
896
1069
  };
897
- function initializeStateValue($$state, initialStatePath, initialSpec) {
898
- var _a;
899
- var initialStateKey = JSON.stringify(initialStatePath);
900
- var stateAccess = new Set();
901
- var $state = create$StateProxy($$state, function (path, spec) { return ({
902
- get: function () {
903
- var key = JSON.stringify(path);
904
- stateAccess.add({ path: path, spec: spec });
905
- if (spec.valueProp) {
906
- return !spec.isRepeated
907
- ? $$state.props[spec.valueProp]
908
- : get($$state.props[spec.valueProp], path.slice(1));
909
- }
910
- if ($$state.existingStates.has(key)) {
911
- // is already initialized
912
- return get($$state.stateValues, path);
913
- }
914
- else if (spec.initFunc) {
915
- initializeStateValue($$state, path, spec);
916
- }
917
- return get($$state.stateValues, path);
918
- },
919
- set: function () {
920
- throw new Error("Cannot update state values during initialization");
921
- }
922
- }); });
923
- (_a = $$state.unsubscriptionsByState[initialStateKey]) === null || _a === void 0 ? void 0 : _a.forEach(function (f) { return f(); });
924
- $$state.unsubscriptionsByState[initialStateKey] = [];
925
- stateAccess.forEach(function (_a) {
926
- var path = _a.path, spec = _a.spec;
927
- var unsubscribe = subscribeKey(get($$state.stateValues, path.slice(-1)), path.slice(-1)[0], function () {
928
- return saveValue($$state, initialStatePath, initialSpec, initialSpec.initFunc($$state.props, $state, $$state.ctx, getIndexes(path, spec)));
929
- });
930
- $$state.unsubscriptionsByState[initialStateKey].push(unsubscribe);
931
- });
932
- var initialValue = initialSpec.initFunc($$state.props, $state, $$state.ctx, getIndexes(initialStatePath, initialSpec));
933
- saveStateInitialValue($$state, initialStatePath, initialSpec, initialValue);
934
- return initialValue;
935
- }
936
- function saveValue($$state, path, spec, value) {
937
- if (spec.isImmutable) {
938
- set($$state.stateValues, path, mkUntrackedValue(value));
939
- }
940
- else {
941
- set($$state.stateValues, path, value);
942
- }
943
- }
944
- function saveStateInitialValue($$state, path, spec, initialValue) {
945
- if (spec.isImmutable) {
946
- var untrackedValue = mkUntrackedValue(initialValue);
947
- set($$state.stateValues, path, untrackedValue);
948
- set($$state.initStateValues, path, clone(untrackedValue));
949
- }
950
- else {
951
- set($$state.stateValues, path, clone(initialValue));
952
- set($$state.initStateValues, path, clone(initialValue));
953
- }
954
- }
955
1070
  function useDollarState(specs, props, $ctx) {
956
- var $$state = React__default.useRef(proxy({
957
- stateValues: {},
958
- initStateValues: {},
959
- specsByKey: Object.fromEntries(specs.map(function (spec) { return [
960
- spec.path,
961
- __assign(__assign({}, spec), { pathObj: transformPathStringToObj(spec.path), isRepeated: spec.path
962
- .split(".")
963
- .some(function (part) { return part.endsWith("[]"); }) }),
964
- ]; })),
965
- statesInstanceBySpec: new Map(),
966
- existingStates: new Map(),
967
- unsubscriptionsByState: {},
968
- props: {},
969
- ctx: {},
970
- registrationsQueue: [],
971
- rootStateSpec: buildGraph(specs)
972
- })).current;
973
- $$state.props = mkUntrackedValue(props);
974
- $$state.ctx = mkUntrackedValue($ctx !== null && $ctx !== void 0 ? $ctx : {});
975
- var $state = React__default.useRef(Object.assign(create$StateProxy($$state, function (path, spec) {
976
- var _a;
977
- var key = JSON.stringify(path);
978
- if (!$$state.existingStates.has(key)) {
979
- saveNewState($$state, path, spec);
980
- var initialValue = !spec.initFunc
981
- ? (_a = spec.initVal) !== null && _a !== void 0 ? _a : undefined
982
- : initializeStateValue($$state, path, spec);
983
- saveStateInitialValue($$state, path, spec, initialValue);
984
- }
1071
+ var $$state = React__default.useRef((function () {
1072
+ var rootSpecTree = buildTree(specs);
985
1073
  return {
986
- get: function () {
987
- if (spec.valueProp) {
988
- var value = !spec.isRepeated
989
- ? $$state.props[spec.valueProp]
990
- : get($$state.props[spec.valueProp], path.slice(1));
991
- return value;
992
- }
993
- else {
994
- return get($$state.stateValues, path);
1074
+ rootSpecTree: rootSpecTree,
1075
+ specTreeLeaves: getLeaves(rootSpecTree),
1076
+ stateValues: proxy({}),
1077
+ props: {},
1078
+ ctx: {},
1079
+ registrationsQueue: []
1080
+ };
1081
+ })()).current;
1082
+ $$state.props = props;
1083
+ $$state.ctx = $ctx !== null && $ctx !== void 0 ? $ctx : {};
1084
+ var $state = React__default.useRef((function () {
1085
+ var useRef$state = Object.assign(create$StateProxy($$state, function (node, path, proxyRoot) {
1086
+ if (!node.hasState(path)) {
1087
+ node.createStateCell(path);
1088
+ var spec = node.getSpec();
1089
+ if (spec.initFunc) {
1090
+ initializeStateValue($$state, node, path, proxyRoot);
995
1091
  }
996
- },
997
- set: function (_t, _p, value) {
998
- var _a, _b;
999
- saveValue($$state, path, spec, value);
1000
- if (spec.onChangeProp) {
1001
- (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value, path);
1092
+ else if (!spec.valueProp) {
1093
+ set(proxyRoot, path, spec.initVal);
1002
1094
  }
1003
- return true;
1004
1095
  }
1005
- };
1006
- }), {
1007
- registerInitFunc: function (pathStr, f) {
1008
- var _a;
1009
- if ((_a = $$state.statesInstanceBySpec
1010
- .get(pathStr)) === null || _a === void 0 ? void 0 : _a.some(function (_a) {
1011
- var path = _a.path, specKey = _a.specKey;
1012
- return !deepEqual(get($$state.initStateValues, path), f(props, $state, $ctx !== null && $ctx !== void 0 ? $ctx : {}, getIndexes(path, $$state.specsByKey[specKey])));
1013
- })) {
1014
- $$state.registrationsQueue.push({ pathStr: pathStr, f: f });
1096
+ return {
1097
+ get: function (target, property, receiver) {
1098
+ var spec = node.getSpec();
1099
+ if (spec.valueProp) {
1100
+ return $$state.props[spec.valueProp];
1101
+ }
1102
+ else {
1103
+ return Reflect.get(target, property, receiver);
1104
+ }
1105
+ }
1106
+ };
1107
+ }), {
1108
+ registerInitFunc: function (pathStr, f, repetitionIndex) {
1109
+ var _a = findStateCell($$state.rootSpecTree, pathStr, repetitionIndex), node = _a.node, realPath = _a.realPath;
1110
+ if (!node.hasState(realPath)) {
1111
+ node.createStateCell(realPath);
1112
+ }
1113
+ if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, useRef$state, $$state.ctx))) {
1114
+ $$state.registrationsQueue.push({ node: node, path: realPath, f: f });
1115
+ }
1015
1116
  }
1016
- }
1017
- })).current;
1117
+ });
1118
+ return useRef$state;
1119
+ })()).current;
1018
1120
  // For each spec with an initFunc, evaluate it and see if
1019
1121
  // the init value has changed. If so, reset its state.
1020
1122
  var resetSpecs = [];
1021
- $$state.existingStates.forEach(function (_a) {
1022
- var path = _a.path, specKey = _a.specKey;
1023
- var spec = $$state.specsByKey[specKey];
1024
- if (spec.initFunc) {
1025
- var newInit = spec.initFunc(props, $state, $ctx !== null && $ctx !== void 0 ? $ctx : {}, getIndexes(path, spec));
1026
- if (!deepEqual(newInit, get($$state.initStateValues, path))) {
1027
- resetSpecs.push({ path: path, spec: spec });
1123
+ $$state.specTreeLeaves
1124
+ .flatMap(function (node) { return node.states().map(function (stateCell) { return ({ stateCell: stateCell, node: node }); }); })
1125
+ .forEach(function (_a) {
1126
+ var stateCell = _a.stateCell, node = _a.node;
1127
+ if (stateCell.registeredInitFunc) {
1128
+ var newInit = stateCell.registeredInitFunc(props, $state, $ctx !== null && $ctx !== void 0 ? $ctx : {});
1129
+ if (!deepEqual(newInit, stateCell.initialValue)) {
1130
+ resetSpecs.push({ stateCell: stateCell, node: node });
1028
1131
  }
1029
1132
  }
1030
1133
  });
1134
+ var reInitializeState = function (node, stateCell) {
1135
+ var _a, _b;
1136
+ var newInit = initializeStateValue($$state, node, stateCell.path, $state);
1137
+ var spec = node.getSpec();
1138
+ if (spec.onChangeProp) {
1139
+ (_b = (_a = $$state.props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, newInit);
1140
+ }
1141
+ };
1031
1142
  useIsomorphicLayoutEffect(function () {
1032
1143
  resetSpecs.forEach(function (_a) {
1033
- var _b, _c;
1034
- var path = _a.path, spec = _a.spec;
1035
- var newInit = initializeStateValue($$state, path, spec);
1036
- if (spec.onChangeProp) {
1037
- (_c = (_b = $$state.props)[spec.onChangeProp]) === null || _c === void 0 ? void 0 : _c.call(_b, newInit, path);
1038
- }
1144
+ var stateCell = _a.stateCell, node = _a.node;
1145
+ reInitializeState(node, stateCell);
1039
1146
  });
1040
1147
  }, [props, resetSpecs]);
1041
1148
  useIsomorphicLayoutEffect(function () {
1042
1149
  $$state.registrationsQueue.forEach(function (_a) {
1043
- var f = _a.f, pathStr = _a.pathStr;
1044
- $$state.specsByKey[pathStr].initFunc = f;
1150
+ var node = _a.node, path = _a.path, f = _a.f;
1151
+ var stateCell = node.getState(path);
1152
+ stateCell.registeredInitFunc = f;
1153
+ reInitializeState(node, stateCell);
1045
1154
  });
1046
1155
  $$state.registrationsQueue = [];
1047
1156
  }, [$$state.registrationsQueue]);
1157
+ // immediately initialize exposed non-private states
1158
+ useIsomorphicLayoutEffect(function () {
1159
+ $$state.specTreeLeaves.forEach(function (node) {
1160
+ var spec = node.getSpec();
1161
+ if (!spec.isRepeated && spec.type !== "private" && spec.initFunc) {
1162
+ node.createStateCell(spec.pathObj);
1163
+ initializeStateValue($$state, node, spec.pathObj, $state);
1164
+ }
1165
+ });
1166
+ }, []);
1048
1167
  // Re-render if any value changed in one of these objects
1049
1168
  useSnapshot($$state.stateValues, { sync: true });
1050
- useSnapshot($$state.specsByKey, { sync: true });
1051
- return $state;
1052
- }
1053
- // Simple version of $state useDollarState for read-only
1054
- function useCanvasDollarState(specs, props, $ctx) {
1055
- var e_2, _a;
1056
- var $$state = proxy({
1057
- stateValues: {},
1058
- initStateValues: {},
1059
- specsByKey: Object.fromEntries(specs.map(function (spec) { return [
1060
- spec.path,
1061
- __assign(__assign({}, spec), { pathObj: transformPathStringToObj(spec.path), isRepeated: spec.path.split(".").some(function (part) { return part.endsWith("[]"); }) }),
1062
- ]; })),
1063
- statesInstanceBySpec: new Map(),
1064
- existingStates: new Map(),
1065
- unsubscriptionsByState: {},
1066
- props: {},
1067
- ctx: {},
1068
- registrationsQueue: [],
1069
- rootStateSpec: buildGraph(specs)
1070
- });
1071
- $$state.props = mkUntrackedValue(props);
1072
- $$state.ctx = mkUntrackedValue($ctx);
1073
- var $state = {};
1074
- try {
1075
- for (var specs_1 = __values(specs), specs_1_1 = specs_1.next(); !specs_1_1.done; specs_1_1 = specs_1.next()) {
1076
- var spec = specs_1_1.value;
1077
- var path = transformPathStringToObj(spec.path);
1078
- var init = spec.valueProp
1079
- ? $$state.props[spec.valueProp]
1080
- : spec.initVal
1081
- ? spec.initVal
1082
- : spec.initFunc
1083
- ? initializeStateValue($$state, path, $$state.specsByKey[spec.path])
1084
- : undefined;
1085
- set($state, path, init);
1086
- }
1087
- }
1088
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
1089
- finally {
1090
- try {
1091
- if (specs_1_1 && !specs_1_1.done && (_a = specs_1["return"])) _a.call(specs_1);
1092
- }
1093
- finally { if (e_2) throw e_2.error; }
1094
- }
1095
1169
  return $state;
1096
1170
  }
1097
1171
 
1098
1172
  // Utilities used by generated code
1099
1173
  var classNames = classNames$1;
1100
1174
 
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 };
1175
+ export { PlasmicIcon, PlasmicLink, PlasmicPageGuard, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateValueProp, hasVariant, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, set, useDollarState, useTrigger, wrapWithClassName };
1102
1176
  //# sourceMappingURL=index.js.map