@plasmicapp/react-web 0.2.170 → 0.2.172

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 (41) hide show
  1. package/dist/all.d.ts +6 -2
  2. package/dist/react-web.cjs.development.js +288 -269
  3. package/dist/react-web.cjs.development.js.map +1 -1
  4. package/dist/react-web.cjs.production.min.js +1 -1
  5. package/dist/react-web.cjs.production.min.js.map +1 -1
  6. package/dist/react-web.esm.js +288 -271
  7. package/dist/react-web.esm.js.map +1 -1
  8. package/dist/states/graph.d.ts +2 -20
  9. package/dist/states/helpers.d.ts +5 -1
  10. package/dist/states/index.d.ts +1 -1
  11. package/dist/states/types.d.ts +26 -1
  12. package/dist/states/valtio.d.ts +4 -1
  13. package/dist/stories/UseDollarState.stories.d.ts +1 -0
  14. package/package.json +4 -4
  15. package/skinny/dist/{collection-utils-3496fd68.js → collection-utils-719bb030.js} +4 -4
  16. package/skinny/dist/{collection-utils-3496fd68.js.map → collection-utils-719bb030.js.map} +1 -1
  17. package/skinny/dist/{common-c2744215.js → common-dd6b46fb.js} +11 -2
  18. package/skinny/dist/{common-c2744215.js.map → common-dd6b46fb.js.map} +1 -1
  19. package/skinny/dist/index.js +279 -254
  20. package/skinny/dist/index.js.map +1 -1
  21. package/skinny/dist/plume/button/index.js +2 -2
  22. package/skinny/dist/plume/checkbox/index.js +4 -4
  23. package/skinny/dist/plume/menu/index.js +5 -5
  24. package/skinny/dist/plume/menu-button/index.js +4 -4
  25. package/skinny/dist/plume/select/index.js +5 -5
  26. package/skinny/dist/plume/switch/index.js +4 -4
  27. package/skinny/dist/plume/text-input/index.js +2 -2
  28. package/skinny/dist/plume/triggered-overlay/index.js +4 -4
  29. package/skinny/dist/{plume-utils-653f4b8a.js → plume-utils-43df92e6.js} +2 -2
  30. package/skinny/dist/{plume-utils-653f4b8a.js.map → plume-utils-43df92e6.js.map} +1 -1
  31. package/skinny/dist/{props-utils-70c2d02a.js → props-utils-eaa18f66.js} +2 -2
  32. package/skinny/dist/{props-utils-70c2d02a.js.map → props-utils-eaa18f66.js.map} +1 -1
  33. package/skinny/dist/{react-utils-76d05959.js → react-utils-628465ba.js} +2 -2
  34. package/skinny/dist/{react-utils-76d05959.js.map → react-utils-628465ba.js.map} +1 -1
  35. package/skinny/dist/render/PlasmicImg/index.js +2 -2
  36. package/skinny/dist/states/graph.d.ts +2 -20
  37. package/skinny/dist/states/helpers.d.ts +5 -1
  38. package/skinny/dist/states/index.d.ts +1 -1
  39. package/skinny/dist/states/types.d.ts +26 -1
  40. package/skinny/dist/states/valtio.d.ts +4 -1
  41. package/skinny/dist/stories/UseDollarState.stories.d.ts +1 -0
@@ -5,9 +5,9 @@ import * as React from 'react';
5
5
  import React__default, { useLayoutEffect, useEffect } from 'react';
6
6
  import get from 'dlv';
7
7
  export { default as get } from 'dlv';
8
- 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-76d05959.js';
9
- import { _ as __awaiter, a as __generator, b as __rest, c as __assign, d as __spreadArray, e as __read, o as omit, p as pick, f as __values, i as isSubset, g as chainSingleArgFuncs, n as notNil } from './common-c2744215.js';
10
- export { o as omit, p as pick } from './common-c2744215.js';
8
+ 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-628465ba.js';
9
+ import { _ as __awaiter, a as __generator, b as __rest, c as __assign, d as __spreadArray, e as __read, o as omit, p as pick, f as __values, i as isSubset, g as chainSingleArgFuncs, n as notNil, h as ensure } from './common-dd6b46fb.js';
10
+ export { o as omit, p as pick } from './common-dd6b46fb.js';
11
11
  export { PlasmicHead, plasmicHeadMeta } from './render/PlasmicHead/index.js';
12
12
  export { PlasmicImg } from './render/PlasmicImg/index.js';
13
13
  import { T as Trans } from './ssr-08aff522.js';
@@ -16,7 +16,7 @@ import ReactDOM__default from 'react-dom';
16
16
  import { useFocusRing } from '@react-aria/focus';
17
17
  import clone from 'clone';
18
18
  import deepEqual from 'fast-deep-equal';
19
- import { proxy, useSnapshot, ref } from 'valtio';
19
+ import { proxy, ref, useSnapshot } from 'valtio';
20
20
  import '@react-aria/ssr';
21
21
 
22
22
  // https://stackoverflow.com/a/2117523
@@ -746,14 +746,13 @@ function useTrigger(trigger, opts) {
746
746
  }
747
747
 
748
748
  var ARRAY_SYMBOL = Symbol("[]");
749
- var PLASMIC_STATE_PROXY_SYMBOL = Symbol("plasmic.state.proxy");
749
+ var PLASMIC_STATE_PROXY_SYMBOL = Symbol("plasmic.state.proxy");
750
+ var UNINITIALIZED = Symbol("plasmic.unitialized");
750
751
 
751
- var UNINITIALIZED = Symbol("plasmic.unitialized");
752
752
  var StateSpecNode = /** @class */ (function () {
753
753
  function StateSpecNode(specs) {
754
754
  this._specs = specs;
755
755
  this._edges = new Map();
756
- this._state = {};
757
756
  }
758
757
  StateSpecNode.prototype.setSpecs = function (specs) {
759
758
  this._specs = specs;
@@ -761,9 +760,6 @@ var StateSpecNode = /** @class */ (function () {
761
760
  StateSpecNode.prototype.edges = function () {
762
761
  return this._edges;
763
762
  };
764
- StateSpecNode.prototype.state = function () {
765
- return this._state;
766
- };
767
763
  StateSpecNode.prototype.hasEdge = function (key) {
768
764
  return this._edges.has(key);
769
765
  };
@@ -777,11 +773,11 @@ var StateSpecNode = /** @class */ (function () {
777
773
  return this._edges.values();
778
774
  };
779
775
  StateSpecNode.prototype.makeTransition = function (key) {
780
- key = isNum$1(key) ? ARRAY_SYMBOL : key;
776
+ key = isNum(key) ? ARRAY_SYMBOL : key;
781
777
  return this._edges.get(key);
782
778
  };
783
779
  StateSpecNode.prototype.isLeaf = function () {
784
- return this._edges.size === 0;
780
+ return this._edges.size === 0 && this.getAllSpecs().length > 0;
785
781
  };
786
782
  StateSpecNode.prototype.hasArrayTransition = function () {
787
783
  return this._edges.has(ARRAY_SYMBOL);
@@ -792,52 +788,6 @@ var StateSpecNode = /** @class */ (function () {
792
788
  StateSpecNode.prototype.getAllSpecs = function () {
793
789
  return this._specs;
794
790
  };
795
- StateSpecNode.prototype.getState = function (path) {
796
- return this._state[JSON.stringify(path)];
797
- };
798
- StateSpecNode.prototype.getInitFunc = function (stateCell) {
799
- var _a;
800
- return (_a = stateCell.registeredInitFunc) !== null && _a !== void 0 ? _a : this.getSpec().initFunc;
801
- };
802
- StateSpecNode.prototype.clearStates = function () {
803
- this._state = {};
804
- };
805
- StateSpecNode.prototype.states = function () {
806
- return Object.values(this._state);
807
- };
808
- StateSpecNode.prototype.hasState = function (path) {
809
- var key = JSON.stringify(path);
810
- return key in this._state;
811
- };
812
- StateSpecNode.prototype.createStateCell = function (path) {
813
- var key = JSON.stringify(path);
814
- this._state[key] = {
815
- listeners: [],
816
- initialValue: UNINITIALIZED,
817
- path: path
818
- };
819
- };
820
- StateSpecNode.prototype.deleteStateCell = function (prefixPath) {
821
- var _this = this;
822
- __spreadArray([], __read(Object.keys(this._state)), false).forEach(function (key) {
823
- var path = JSON.parse(key);
824
- if (shallowEqual(prefixPath, path.slice(0, prefixPath.length))) {
825
- delete _this._state[key];
826
- }
827
- });
828
- };
829
- StateSpecNode.prototype.setInitialValue = function (path, value) {
830
- var key = JSON.stringify(path);
831
- this._state[key].initialValue = value;
832
- };
833
- StateSpecNode.prototype.getInitialValue = function (path) {
834
- var key = JSON.stringify(path);
835
- return this._state[key].initialValue;
836
- };
837
- StateSpecNode.prototype.addListener = function (path, f) {
838
- var key = JSON.stringify(path);
839
- this._state[key].listeners.push(f);
840
- };
841
791
  return StateSpecNode;
842
792
  }());
843
793
  var transformPathStringToObj = function (str) {
@@ -891,7 +841,7 @@ function updateTree(root, specs) {
891
841
  };
892
842
  return rec(root, []);
893
843
  }
894
- function getStateCells(root) {
844
+ function getSpecTreeLeaves(root) {
895
845
  var leaves = [];
896
846
  var rec = function (node) {
897
847
  var e_1, _a;
@@ -954,169 +904,84 @@ function findStateCell(root, pathStr, repetitionIndex) {
954
904
  };
955
905
  }
956
906
 
957
- function generateStateOnChangeProp($state, path) {
958
- return function (val) { return set($state, path, val); };
959
- }
960
- function generateStateValueProp($state, path) {
961
- return get($state, path);
962
- }
963
- var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
964
- function isPlasmicStateProxy(obj) {
965
- return (obj != null && typeof obj === "object" && !!obj[PLASMIC_STATE_PROXY_SYMBOL]);
966
- }
967
- function getStateCellsInPlasmicProxy(obj) {
968
- if (!isPlasmicStateProxy(obj)) {
969
- return [];
970
- }
971
- var _a = obj[PLASMIC_STATE_PROXY_SYMBOL], rootNode = _a.node, rootPath = _a.path, isOutside = _a.isOutside;
972
- if (isOutside) {
973
- return [];
974
- }
975
- return getStateCells(rootNode).flatMap(function (node) {
976
- return node.states().map(function (stateCell) { return ({
977
- path: node.getSpec().path,
978
- realPath: stateCell.path.slice(rootPath.length)
979
- }); });
980
- });
981
- }
982
- function getStateSpecInPlasmicProxy(obj, path) {
983
- obj = get(obj, path.slice(0, path.length - 1));
984
- if (!isPlasmicStateProxy(obj)) {
985
- return undefined;
986
- }
987
- var _a = obj[PLASMIC_STATE_PROXY_SYMBOL], node = _a.node, isOutside = _a.isOutside;
988
- var nextNode = node.makeTransition(path[path.length - 1]);
989
- if (isOutside || node.isLeaf() || !nextNode) {
990
- return undefined;
991
- }
992
- return {
993
- spec: nextNode.getSpec(),
994
- isImplicitStateArray: nextNode.hasArrayTransition()
995
- };
996
- }
997
- function shallowEqual(a1, a2) {
998
- if (a1.length !== a2.length) {
999
- return false;
1000
- }
1001
- for (var i = 0; i < a1.length; i++) {
1002
- if (a1[i] !== a2[i]) {
1003
- return false;
1004
- }
1005
- }
1006
- return true;
1007
- }
1008
- /**
1009
- * Shallow comparison of arrays.
1010
- */
1011
- function arrayEq(xs, ys) {
1012
- return (xs.length === ys.length && xs.every(function (_, index) { return xs[index] === ys[index]; }));
1013
- }
1014
907
  function isNum$1(value) {
1015
908
  return typeof value === "symbol" ? false : !isNaN(+value);
1016
909
  }
1017
- function assert(cond, msg) {
1018
- if (msg === void 0) { msg = "Assertion failed"; }
1019
- if (!cond) {
1020
- // We always generate an non empty message so that it doesn't get swallowed
1021
- // by the async library.
1022
- msg = (typeof msg === "string" ? msg : msg()) || "Assertion failed";
1023
- debugger;
1024
- throw new Error(msg);
1025
- }
1026
- }
1027
- /**
1028
- * Forked from https://github.com/lukeed/dset
1029
- * Changes: fixed setting a deep value to a proxy object
1030
- */
1031
- function set(obj, keys, val) {
1032
- keys = keys.split ? keys.split(".") : keys;
1033
- var i = 0, l = keys.length, t = obj, x, k;
1034
- while (i < l) {
1035
- k = keys[i++];
1036
- if (k === "__proto__" || k === "constructor" || k === "prototype")
1037
- break;
1038
- var newValue = i === l
1039
- ? val
1040
- : typeof (x = t[k]) === typeof keys
1041
- ? x
1042
- : keys[i] * 0 !== 0 || !!~("" + keys[i]).indexOf(".")
1043
- ? {}
1044
- : [];
1045
- assignValue(t, k, newValue);
1046
- t = t[k];
1047
- }
910
+ function canProxy(value) {
911
+ return typeof value === "object" && value != null;
1048
912
  }
1049
- /**
1050
- * Forked from lodash
1051
- */
1052
- function baseAssignValue(object, key, value) {
1053
- if (key == "__proto__") {
1054
- Object.defineProperty(object, key, {
1055
- configurable: true,
1056
- enumerable: true,
1057
- value: value,
1058
- writable: true
1059
- });
913
+ var proxyObjToStateCell = new WeakMap();
914
+ function ensureStateCell(target, property, path, node) {
915
+ var _a;
916
+ if (!proxyObjToStateCell.has(target)) {
917
+ proxyObjToStateCell.set(target, {});
1060
918
  }
1061
- else {
1062
- object[key] = value;
919
+ var stateCell = proxyObjToStateCell.get(target);
920
+ if (!(property in stateCell)) {
921
+ stateCell[property] = {
922
+ listeners: [],
923
+ initialValue: UNINITIALIZED,
924
+ path: path,
925
+ node: node,
926
+ initFunc: node.getSpec().initFunc,
927
+ initFuncHash: (_a = node.getSpec().initFuncHash) !== null && _a !== void 0 ? _a : ""
928
+ };
1063
929
  }
930
+ return stateCell[property];
1064
931
  }
1065
- function eq(value, other) {
1066
- return value === other || (value !== value && other !== other);
932
+ function getStateCell(target, property) {
933
+ var _a;
934
+ return (_a = proxyObjToStateCell.get(target)) === null || _a === void 0 ? void 0 : _a[property];
1067
935
  }
1068
- function assignValue(object, key, value) {
1069
- var objValue = object[key];
1070
- if (!(Object.prototype.hasOwnProperty.call(object, key) && eq(objValue, value)) ||
1071
- (value === undefined && !(key in object))) {
1072
- baseAssignValue(object, key, value);
936
+ function tryGetStateCellFrom$StateRoot($state, path) {
937
+ if (path.length === 0) {
938
+ throw new Error("expected a path with length greater than 0");
1073
939
  }
1074
- }
1075
-
1076
- function isNum(value) {
1077
- return typeof value === "symbol" ? false : !isNaN(+value);
940
+ var target = get($state, path.slice(0, -1));
941
+ get(target, path.slice(-1)); // create state cell;
942
+ return getStateCell(target, path.slice(-1)[0]);
1078
943
  }
1079
- function canProxy(value) {
1080
- return typeof value === "object" && value != null;
944
+ function getStateCellFrom$StateRoot($state, path) {
945
+ return ensure(tryGetStateCellFrom$StateRoot($state, path));
1081
946
  }
1082
- function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyRoot) {
1083
- var _a, _b;
947
+ function initializeStateValue($$state, initialStateCell, proxyRoot) {
948
+ var _a, _b, _c;
1084
949
  var stateAccess = new Set();
1085
- var $state = create$StateProxy($$state, function (node, path) { return ({
950
+ var $state = create$StateProxy($$state, function (internalStateCell) { return ({
1086
951
  get: function () {
1087
- stateAccess.add({ path: path, node: node });
1088
- var spec = node.getSpec();
952
+ var stateCell = getStateCellFrom$StateRoot(proxyRoot, internalStateCell.path);
953
+ stateAccess.add({ stateCell: stateCell });
954
+ var spec = stateCell.node.getSpec();
1089
955
  if (spec.valueProp) {
1090
956
  return $$state.env.$props[spec.valueProp];
1091
957
  }
1092
- else if (!node.hasState(path) && spec.initFunc) {
1093
- node.createStateCell(path);
1094
- return initializeStateValue($$state, node, path, proxyRoot);
958
+ else if (spec.initFunc && stateCell.initialValue === UNINITIALIZED) {
959
+ return initializeStateValue($$state, stateCell, proxyRoot);
1095
960
  }
1096
- return get(proxyRoot, path);
961
+ return get(proxyRoot, stateCell.path);
1097
962
  },
1098
963
  set: function () {
1099
964
  throw new Error("Cannot update state values during initialization");
1100
965
  }
1101
966
  }); });
1102
967
  stateAccess.forEach(function (_a) {
1103
- var node = _a.node, path = _a.path;
1104
- node.addListener(path, function () {
1105
- var newValue = invokeInitFuncBackwardsCompatible(initialSpecNode.getSpec().initFunc, __assign({ $state: $state }, $$state.env));
1106
- set(proxyRoot, initialStatePath, newValue);
968
+ var stateCell = _a.stateCell;
969
+ stateCell.listeners.push(function () {
970
+ var _a;
971
+ var newValue = invokeInitFuncBackwardsCompatible(initialStateCell.node.getSpec().initFunc, __assign({ $state: $state }, ((_a = initialStateCell.overrideEnv) !== null && _a !== void 0 ? _a : $$state.env)));
972
+ set(proxyRoot, initialStateCell.path, newValue);
1107
973
  });
1108
974
  });
1109
- var initFunc = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath));
1110
- var initialValue = invokeInitFuncBackwardsCompatible(initFunc, __assign({ $state: $state }, $$state.env));
1111
- initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
1112
- var initialSpec = initialSpecNode.getSpec();
975
+ var initialValue = invokeInitFuncBackwardsCompatible(initialStateCell.initFunc, __assign({ $state: $state }, ((_a = initialStateCell.overrideEnv) !== null && _a !== void 0 ? _a : $$state.env)));
976
+ initialStateCell.initialValue = clone(initialValue);
977
+ var initialSpec = initialStateCell.node.getSpec();
1113
978
  var value = initialSpec.isImmutable
1114
979
  ? mkUntrackedValue(initialValue)
1115
980
  : clone(initialValue);
1116
- set(proxyRoot, initialStatePath, value);
981
+ set(proxyRoot, initialStateCell.path, value);
1117
982
  //immediately fire onChange
1118
983
  if (initialSpec.onChangeProp) {
1119
- (_b = (_a = $$state.env.$props)[initialSpec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, initialValue);
984
+ (_c = (_b = $$state.env.$props)[initialSpec.onChangeProp]) === null || _c === void 0 ? void 0 : _c.call(_b, initialValue);
1120
985
  }
1121
986
  return initialValue;
1122
987
  }
@@ -1124,7 +989,7 @@ function create$StateProxy($$state, leafHandlers) {
1124
989
  var proxyRoot;
1125
990
  var rec = function (currPath, currNode, isOutside, initialObject) {
1126
991
  var getNextPath = function (property) { return __spreadArray(__spreadArray([], __read(currPath), false), [
1127
- isNum(property) ? +property : property,
992
+ isNum$1(property) ? +property : property,
1128
993
  ], false); };
1129
994
  var spec = currNode.getSpec();
1130
995
  var handlers = {
@@ -1133,7 +998,7 @@ function create$StateProxy($$state, leafHandlers) {
1133
998
  if (!isOutside &&
1134
999
  !currNode.isLeaf() &&
1135
1000
  !currNode.hasArrayTransition() &&
1136
- !isNum(property)) {
1001
+ !isNum$1(property)) {
1137
1002
  throw new Error("Can't delete a property in the middle of the state spec");
1138
1003
  }
1139
1004
  delete get($$state.stateValues, currPath)[property];
@@ -1143,13 +1008,6 @@ function create$StateProxy($$state, leafHandlers) {
1143
1008
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
1144
1009
  (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, get(proxyRoot, currPath.slice(spec.pathObj.length)));
1145
1010
  }
1146
- var nextPath = getNextPath(property);
1147
- var nextNode = currNode.makeTransition(property);
1148
- if (nextNode) {
1149
- getStateCells(nextNode).forEach(function (node) {
1150
- node.deleteStateCell(nextPath);
1151
- });
1152
- }
1153
1011
  return Reflect.deleteProperty(target, property);
1154
1012
  },
1155
1013
  get: function (target, property, receiver) {
@@ -1167,7 +1025,7 @@ function create$StateProxy($$state, leafHandlers) {
1167
1025
  }
1168
1026
  var nextNode = currNode.makeTransition(property);
1169
1027
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1170
- return (_b = (_a = leafHandlers(nextNode, nextPath)).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
1028
+ return (_b = (_a = leafHandlers(ensureStateCell(receiver, property, nextPath, nextNode))).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
1171
1029
  }
1172
1030
  else if (nextNode && !(property in target)) {
1173
1031
  target[property] = rec(nextPath, nextNode, false, undefined);
@@ -1187,7 +1045,7 @@ function create$StateProxy($$state, leafHandlers) {
1187
1045
  return Reflect.set(target, property, value, receiver);
1188
1046
  }
1189
1047
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1190
- (_b = (_a = leafHandlers(nextNode, nextPath)).set) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, value, receiver);
1048
+ (_b = (_a = leafHandlers(ensureStateCell(receiver, property, nextPath, nextNode))).set) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, value, receiver);
1191
1049
  }
1192
1050
  if (!isOutside && !currNode.isLeaf() && !nextNode) {
1193
1051
  // can't set an unknown field in $state
@@ -1319,29 +1177,29 @@ function useDollarState(specs) {
1319
1177
  var rootSpecTree = buildTree(specs);
1320
1178
  return {
1321
1179
  rootSpecTree: rootSpecTree,
1322
- specTreeLeaves: getStateCells(rootSpecTree),
1180
+ specTreeLeaves: getSpecTreeLeaves(rootSpecTree),
1323
1181
  stateValues: proxy({}),
1324
1182
  env: envFieldsAreNonNill(env),
1325
1183
  specs: [],
1326
1184
  registrationsQueue: proxy([])
1327
1185
  };
1328
1186
  })()).current;
1329
- ($$state.env = envFieldsAreNonNill(env)), ($$state.specs = specs);
1330
- var create$State = function () {
1331
- var $state = Object.assign(create$StateProxy($$state, function (node, path) {
1332
- if (!node.hasState(path)) {
1333
- node.createStateCell(path);
1334
- var spec = node.getSpec();
1335
- if (spec.initFunc) {
1336
- initializeStateValue($$state, node, path, $state);
1337
- }
1338
- else if (!spec.valueProp) {
1339
- set($state, path, spec.initVal);
1340
- }
1187
+ $$state.env = envFieldsAreNonNill(env);
1188
+ $$state.specs = specs;
1189
+ var create$State = React__default.useCallback(function () {
1190
+ var $state = Object.assign(create$StateProxy($$state, function (stateCell) {
1191
+ var spec = stateCell.node.getSpec();
1192
+ if (stateCell.initialValue === UNINITIALIZED && spec.initFunc) {
1193
+ initializeStateValue($$state, stateCell, $state);
1194
+ }
1195
+ else if (stateCell.initialValue === UNINITIALIZED &&
1196
+ !spec.valueProp) {
1197
+ stateCell.initialValue = spec.initVal;
1198
+ set($state, stateCell.path, spec.initVal);
1341
1199
  }
1342
1200
  return {
1343
1201
  get: function (target, property, receiver) {
1344
- var spec = node.getSpec();
1202
+ var spec = stateCell.node.getSpec();
1345
1203
  if (spec.valueProp) {
1346
1204
  return $$state.env.$props[spec.valueProp];
1347
1205
  }
@@ -1351,18 +1209,26 @@ function useDollarState(specs) {
1351
1209
  }
1352
1210
  };
1353
1211
  }), {
1354
- registerInitFunc: function (pathStr, f, repetitionIndex) {
1212
+ registerInitFunc: function (pathStr, f, repetitionIndex, overrideEnv) {
1355
1213
  var _a = findStateCell($$state.rootSpecTree, pathStr, repetitionIndex), node = _a.node, realPath = _a.realPath;
1356
- if (!node.hasState(realPath)) {
1357
- node.createStateCell(realPath);
1358
- }
1359
- if (!deepEqual(node.getState(realPath).initialValue, f(__assign({ $state: $state }, $$state.env)))) {
1360
- $$state.registrationsQueue.push(mkUntrackedValue({ node: node, path: realPath, f: f }));
1214
+ var stateCell = getStateCellFrom$StateRoot($state, realPath);
1215
+ var env = overrideEnv
1216
+ ? envFieldsAreNonNill(overrideEnv)
1217
+ : $$state.env;
1218
+ if (!deepEqual(stateCell.initialValue, f(__assign({ $state: $state }, env)))) {
1219
+ $$state.registrationsQueue.push(mkUntrackedValue({
1220
+ node: node,
1221
+ path: realPath,
1222
+ f: f,
1223
+ overrideEnv: overrideEnv
1224
+ ? envFieldsAreNonNill(overrideEnv)
1225
+ : undefined
1226
+ }));
1361
1227
  }
1362
1228
  }
1363
1229
  });
1364
1230
  return $state;
1365
- };
1231
+ }, []);
1366
1232
  var ref = React__default.useRef(undefined);
1367
1233
  if (!ref.current) {
1368
1234
  ref.current = create$State();
@@ -1370,29 +1236,37 @@ function useDollarState(specs) {
1370
1236
  var $state = ref.current;
1371
1237
  if (opts === null || opts === void 0 ? void 0 : opts.inCanvas) {
1372
1238
  $$state.rootSpecTree = updateTree($$state.rootSpecTree, specs);
1373
- var newLeaves = getStateCells($$state.rootSpecTree);
1239
+ var newLeaves = getSpecTreeLeaves($$state.rootSpecTree);
1374
1240
  if (!arrayEq(newLeaves, $$state.specTreeLeaves)) {
1375
- var old$State_1 = $state;
1241
+ var old$state_1 = $state;
1376
1242
  $state = ref.current = create$State();
1377
1243
  $$state.specTreeLeaves = newLeaves;
1378
- $$state.specTreeLeaves
1379
- .flatMap(function (node) { return node.states(); })
1380
- .forEach(function (_a) {
1244
+ getStateCells(newLeaves, $$state.rootSpecTree).forEach(function (_a) {
1381
1245
  var path = _a.path;
1382
- set($state, path, get(old$State_1, path));
1246
+ var oldStateCell = tryGetStateCellFrom$StateRoot(old$state_1, path);
1247
+ if (oldStateCell) {
1248
+ set($state, path, get(old$state_1, path));
1249
+ }
1383
1250
  });
1384
1251
  }
1385
1252
  // we need to eager initialize all states in canvas to populate the data picker
1386
1253
  $$state.specTreeLeaves.forEach(function (node) {
1254
+ var _a;
1387
1255
  var spec = node.getSpec();
1388
- if (spec.isRepeated || node.hasState(spec.pathObj)) {
1256
+ if (spec.isRepeated) {
1257
+ return;
1258
+ }
1259
+ var stateCell = getStateCellFrom$StateRoot($state, spec.pathObj);
1260
+ var newSpec = specs.find(function (sp) { return sp.path === spec.path; });
1261
+ if (!newSpec || stateCell.initFuncHash === (newSpec === null || newSpec === void 0 ? void 0 : newSpec.initFuncHash)) {
1389
1262
  return;
1390
1263
  }
1391
- node.createStateCell(spec.pathObj);
1264
+ stateCell.initFunc = newSpec.initFunc;
1265
+ stateCell.initFuncHash = (_a = newSpec.initFuncHash) !== null && _a !== void 0 ? _a : "";
1392
1266
  var init = spec.valueProp
1393
1267
  ? $$state.env.$props[spec.valueProp]
1394
1268
  : spec.initFunc
1395
- ? initializeStateValue($$state, node, spec.pathObj, $state)
1269
+ ? initializeStateValue($$state, stateCell, $state)
1396
1270
  : spec.initVal;
1397
1271
  set($state, spec.pathObj, init);
1398
1272
  });
@@ -1400,38 +1274,36 @@ function useDollarState(specs) {
1400
1274
  // For each spec with an initFunc, evaluate it and see if
1401
1275
  // the init value has changed. If so, reset its state.
1402
1276
  var resetSpecs = [];
1403
- $$state.specTreeLeaves
1404
- .flatMap(function (node) { return node.states().map(function (stateCell) { return ({ stateCell: stateCell, node: node }); }); })
1405
- .forEach(function (_a) {
1406
- var node = _a.node, stateCell = _a.stateCell;
1407
- var initFunc = node.getInitFunc(stateCell);
1408
- if (initFunc) {
1409
- var newInit = invokeInitFuncBackwardsCompatible(initFunc, __assign({ $state: $state }, envFieldsAreNonNill(env)));
1277
+ getStateCells($state, $$state.rootSpecTree).forEach(function (stateCell) {
1278
+ var _a;
1279
+ if (stateCell.initFunc) {
1280
+ var newInit = invokeInitFuncBackwardsCompatible(stateCell.initFunc, __assign({ $state: $state }, ((_a = stateCell.overrideEnv) !== null && _a !== void 0 ? _a : envFieldsAreNonNill(env))));
1410
1281
  if (!deepEqual(newInit, stateCell.initialValue)) {
1411
- resetSpecs.push({ stateCell: stateCell, node: node });
1282
+ resetSpecs.push({ stateCell: stateCell });
1412
1283
  }
1413
1284
  }
1414
1285
  });
1415
- var reInitializeState = function (node, stateCell) {
1286
+ var reInitializeState = function (stateCell) {
1416
1287
  var _a, _b;
1417
- var newInit = initializeStateValue($$state, node, stateCell.path, $state);
1418
- var spec = node.getSpec();
1288
+ var newInit = initializeStateValue($$state, stateCell, $state);
1289
+ var spec = stateCell.node.getSpec();
1419
1290
  if (spec.onChangeProp) {
1420
1291
  (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, newInit);
1421
1292
  }
1422
1293
  };
1423
1294
  useIsomorphicLayoutEffect(function () {
1424
1295
  resetSpecs.forEach(function (_a) {
1425
- var stateCell = _a.stateCell, node = _a.node;
1426
- reInitializeState(node, stateCell);
1296
+ var stateCell = _a.stateCell;
1297
+ reInitializeState(stateCell);
1427
1298
  });
1428
1299
  }, [env.$props, resetSpecs]);
1429
1300
  useIsomorphicLayoutEffect(function () {
1430
1301
  while ($$state.registrationsQueue.length) {
1431
- var _a = $$state.registrationsQueue.shift(), node = _a.node, path = _a.path, f = _a.f;
1432
- var stateCell = node.getState(path);
1433
- stateCell.registeredInitFunc = f;
1434
- reInitializeState(node, stateCell);
1302
+ var _a = $$state.registrationsQueue.shift(), path = _a.path, f = _a.f, overrideEnv = _a.overrideEnv;
1303
+ var stateCell = getStateCellFrom$StateRoot($state, path);
1304
+ stateCell.initFunc = f;
1305
+ stateCell.overrideEnv = overrideEnv;
1306
+ reInitializeState(stateCell);
1435
1307
  }
1436
1308
  }, [$$state.registrationsQueue.length]);
1437
1309
  // immediately initialize exposed non-private states
@@ -1439,19 +1311,172 @@ function useDollarState(specs) {
1439
1311
  $$state.specTreeLeaves.forEach(function (node) {
1440
1312
  var spec = node.getSpec();
1441
1313
  if (!spec.isRepeated && spec.type !== "private" && spec.initFunc) {
1442
- node.createStateCell(spec.pathObj);
1443
- initializeStateValue($$state, node, spec.pathObj, $state);
1314
+ var stateCell = getStateCellFrom$StateRoot($state, spec.pathObj);
1315
+ initializeStateValue($$state, stateCell, $state);
1444
1316
  }
1445
1317
  });
1446
1318
  }, []);
1447
1319
  // Re-render if any value changed in one of these objects
1448
1320
  useSnapshot($$state.stateValues, { sync: true });
1449
- useSnapshot($$state.registrationsQueue);
1321
+ useSnapshot($$state.registrationsQueue, { sync: true });
1450
1322
  return $state;
1451
1323
  }
1452
1324
 
1325
+ function generateStateOnChangeProp($state, path) {
1326
+ return function (val) { return set($state, path, val); };
1327
+ }
1328
+ function generateStateValueProp($state, path) {
1329
+ return get($state, path);
1330
+ }
1331
+ var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
1332
+ function isPlasmicStateProxy(obj) {
1333
+ return (obj != null && typeof obj === "object" && !!obj[PLASMIC_STATE_PROXY_SYMBOL]);
1334
+ }
1335
+ function getStateCells($state, root) {
1336
+ var _a;
1337
+ if ($state == null || typeof $state !== "object") {
1338
+ return [];
1339
+ }
1340
+ var stateCells = Object.values((_a = proxyObjToStateCell.get($state)) !== null && _a !== void 0 ? _a : {});
1341
+ if (root.isLeaf()) {
1342
+ return stateCells;
1343
+ }
1344
+ if (root.hasArrayTransition()) {
1345
+ return __spreadArray(__spreadArray([], __read(stateCells), false), __read(Object.keys($state).flatMap(function (key) {
1346
+ return getStateCells($state[key], ensure(root.makeTransition(ARRAY_SYMBOL)));
1347
+ })), false);
1348
+ }
1349
+ else {
1350
+ return __spreadArray(__spreadArray([], __read(stateCells), false), __read(__spreadArray([], __read(root.edges().entries()), false).flatMap(function (_a) {
1351
+ var _b = __read(_a, 2), key = _b[0], child = _b[1];
1352
+ return typeof key === "string" && key in $state
1353
+ ? getStateCells($state[key], child)
1354
+ : [];
1355
+ })), false);
1356
+ }
1357
+ }
1358
+ function getStateCellsInPlasmicProxy(obj) {
1359
+ if (!isPlasmicStateProxy(obj)) {
1360
+ return [];
1361
+ }
1362
+ var _a = obj[PLASMIC_STATE_PROXY_SYMBOL], rootNode = _a.node, rootPath = _a.path, isOutside = _a.isOutside;
1363
+ if (isOutside) {
1364
+ return [];
1365
+ }
1366
+ return getStateCells(obj, rootNode).map(function (stateCell) { return ({
1367
+ path: stateCell.node.getSpec().path,
1368
+ realPath: stateCell.path.slice(rootPath.length)
1369
+ }); });
1370
+ }
1371
+ function getStateSpecInPlasmicProxy(obj, path) {
1372
+ obj = get(obj, path.slice(0, path.length - 1));
1373
+ if (!isPlasmicStateProxy(obj)) {
1374
+ return undefined;
1375
+ }
1376
+ var _a = obj[PLASMIC_STATE_PROXY_SYMBOL], node = _a.node, isOutside = _a.isOutside;
1377
+ var nextNode = node.makeTransition(path[path.length - 1]);
1378
+ if (isOutside || node.isLeaf() || !nextNode) {
1379
+ return undefined;
1380
+ }
1381
+ return {
1382
+ spec: nextNode.getSpec(),
1383
+ isImplicitStateArray: nextNode.hasArrayTransition()
1384
+ };
1385
+ }
1386
+ function getCurrentInitialValue(obj, path) {
1387
+ var _a;
1388
+ if (!isPlasmicStateProxy(obj)) {
1389
+ return undefined;
1390
+ }
1391
+ return (_a = tryGetStateCellFrom$StateRoot(obj, path)) === null || _a === void 0 ? void 0 : _a.initialValue;
1392
+ }
1393
+ function resetToInitialValue(obj, path) {
1394
+ var stateCell = tryGetStateCellFrom$StateRoot(obj, path);
1395
+ if (stateCell) {
1396
+ set(obj, path, stateCell.initialValue);
1397
+ }
1398
+ }
1399
+ function shallowEqual(a1, a2) {
1400
+ if (a1.length !== a2.length) {
1401
+ return false;
1402
+ }
1403
+ for (var i = 0; i < a1.length; i++) {
1404
+ if (a1[i] !== a2[i]) {
1405
+ return false;
1406
+ }
1407
+ }
1408
+ return true;
1409
+ }
1410
+ /**
1411
+ * Shallow comparison of arrays.
1412
+ */
1413
+ function arrayEq(xs, ys) {
1414
+ return (xs.length === ys.length && xs.every(function (_, index) { return xs[index] === ys[index]; }));
1415
+ }
1416
+ function isNum(value) {
1417
+ return typeof value === "symbol" ? false : !isNaN(+value);
1418
+ }
1419
+ function assert(cond, msg) {
1420
+ if (msg === void 0) { msg = "Assertion failed"; }
1421
+ if (!cond) {
1422
+ // We always generate an non empty message so that it doesn't get swallowed
1423
+ // by the async library.
1424
+ msg = (typeof msg === "string" ? msg : msg()) || "Assertion failed";
1425
+ debugger;
1426
+ throw new Error(msg);
1427
+ }
1428
+ }
1429
+ /**
1430
+ * Forked from https://github.com/lukeed/dset
1431
+ * Changes: fixed setting a deep value to a proxy object
1432
+ */
1433
+ function set(obj, keys, val) {
1434
+ keys = keys.split ? keys.split(".") : keys;
1435
+ var i = 0, l = keys.length, t = obj, x, k;
1436
+ while (i < l) {
1437
+ k = keys[i++];
1438
+ if (k === "__proto__" || k === "constructor" || k === "prototype")
1439
+ break;
1440
+ var newValue = i === l
1441
+ ? val
1442
+ : typeof (x = t[k]) === typeof keys
1443
+ ? x
1444
+ : keys[i] * 0 !== 0 || !!~("" + keys[i]).indexOf(".")
1445
+ ? {}
1446
+ : [];
1447
+ assignValue(t, k, newValue);
1448
+ t = t[k];
1449
+ }
1450
+ }
1451
+ /**
1452
+ * Forked from lodash
1453
+ */
1454
+ function baseAssignValue(object, key, value) {
1455
+ if (key == "__proto__") {
1456
+ Object.defineProperty(object, key, {
1457
+ configurable: true,
1458
+ enumerable: true,
1459
+ value: value,
1460
+ writable: true
1461
+ });
1462
+ }
1463
+ else {
1464
+ object[key] = value;
1465
+ }
1466
+ }
1467
+ function eq(value, other) {
1468
+ return value === other || (value !== value && other !== other);
1469
+ }
1470
+ function assignValue(object, key, value) {
1471
+ var objValue = object[key];
1472
+ if (!(Object.prototype.hasOwnProperty.call(object, key) && eq(objValue, value)) ||
1473
+ (value === undefined && !(key in object))) {
1474
+ baseAssignValue(object, key, value);
1475
+ }
1476
+ }
1477
+
1453
1478
  // Utilities used by generated code
1454
1479
  var classNames = classNames$1;
1455
1480
 
1456
- export { PlasmicIcon, PlasmicLink, PlasmicPageGuard, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateValueProp, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, hasVariant, isPlasmicStateProxy, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, set, useDollarState, useTrigger, wrapWithClassName };
1481
+ export { PlasmicIcon, PlasmicLink, PlasmicPageGuard, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateValueProp, getCurrentInitialValue, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, hasVariant, isPlasmicStateProxy, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, resetToInitialValue, set, useDollarState, useTrigger, wrapWithClassName };
1457
1482
  //# sourceMappingURL=index.js.map