@plasmicapp/react-web 0.2.182 → 0.2.184

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.
@@ -15,6 +15,7 @@ export declare function generateStateOnChangePropForCodeComponents($state: $Stat
15
15
  export declare function generateStateValueProp($state: $State, path: ObjectPath): any;
16
16
  export declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
17
17
  export declare function isPlasmicStateProxy(obj: any): boolean;
18
+ export declare function is$StateProxy(obj: any): number | boolean | undefined;
18
19
  export declare function getStateCells($state: $State, root: StateSpecNode<any>): StateCell<any>[];
19
20
  export declare function getStateCellsInPlasmicProxy(obj: any): {
20
21
  realPath: ObjectPath;
@@ -1,4 +1,4 @@
1
1
  export { default as get } from "dlv";
2
- export { generateStateOnChangeProp, generateStateOnChangePropForCodeComponents, generateStateValueProp, getCurrentInitialValue, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, initializeCodeComponentStates, initializePlasmicStates, isPlasmicStateProxy, resetToInitialValue, set, } from "./helpers";
2
+ export { generateStateOnChangeProp, generateStateOnChangePropForCodeComponents, generateStateValueProp, getCurrentInitialValue, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, initializeCodeComponentStates, initializePlasmicStates, is$StateProxy, isPlasmicStateProxy, resetToInitialValue, set, } from "./helpers";
3
3
  export { $State, $StateSpec } from "./types";
4
4
  export { useDollarState } from "./valtio";
@@ -1,6 +1,10 @@
1
- import { $State, $StateSpec, ObjectPath, StateCell } from "./types";
1
+ import { StateSpecNode } from "./graph";
2
+ import { $State, $StateSpec, Internal$State, ObjectPath, StateCell } from "./types";
2
3
  export declare const proxyObjToStateCell: WeakMap<object, Record<string | number | symbol, StateCell<any>>>;
4
+ export declare const valtioSubscriptions: WeakMap<object, (() => boolean)[]>;
3
5
  export declare function tryGetStateCellFrom$StateRoot($state: $State, path: ObjectPath): StateCell<any> | undefined;
4
6
  export declare function getStateCellFrom$StateRoot($state: $State, path: ObjectPath): StateCell<any>;
7
+ export declare function unsubscribeToValtio($$state: Internal$State, statePath: ObjectPath): void;
8
+ export declare function subscribeToValtio($$state: Internal$State, statePath: ObjectPath, node: StateSpecNode<any>): void;
5
9
  export declare function useDollarState(specs: $StateSpec<any>[], ...rest: any[]): $State;
6
10
  export default useDollarState;
@@ -72,5 +72,6 @@ export declare const ImmutableStateCells: import("@storybook/csf").AnnotatedStor
72
72
  people: Person[];
73
73
  }>;
74
74
  export declare const InCanvasDollarState: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {}>;
75
+ export declare const AddDeleteSpecsInCanvas: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {}>;
75
76
  export declare const TodoApp: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {}>;
76
77
  export declare const CycleInStateInitialization: import("@storybook/csf").AnnotatedStoryFn<import("@storybook/react").ReactFramework, {}>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plasmicapp/react-web",
3
- "version": "0.2.182",
3
+ "version": "0.2.184",
4
4
  "description": "plasmic library for rendering in the presentational style",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -102,7 +102,7 @@
102
102
  },
103
103
  "prettier": {},
104
104
  "dependencies": {
105
- "@plasmicapp/data-sources": "0.1.54",
105
+ "@plasmicapp/data-sources": "0.1.55",
106
106
  "@plasmicapp/data-sources-context": "0.1.7",
107
107
  "@plasmicapp/host": "1.0.120",
108
108
  "@plasmicapp/query": "0.1.61",
@@ -171,5 +171,5 @@
171
171
  "react": ">=16.8.0",
172
172
  "react-dom": ">=16.8.0"
173
173
  },
174
- "gitHead": "dff00ecdf8a21a7e4744bcfd80efc4d5478fd00d"
174
+ "gitHead": "9eeaff2cc90823316469cbf70122b0c5c650a594"
175
175
  }
@@ -14,9 +14,9 @@ import { T as Trans } from './ssr-08aff522.js';
14
14
  export { P as PlasmicRootProvider, T as Trans, g as genTranslatableString, u as useIsSSR } from './ssr-08aff522.js';
15
15
  import ReactDOM__default from 'react-dom';
16
16
  import { useFocusRing } from '@react-aria/focus';
17
+ import { proxy, ref, useSnapshot, getVersion, subscribe } from 'valtio';
17
18
  import clone from 'clone';
18
19
  import deepEqual from 'fast-deep-equal';
19
- import { proxy, ref, useSnapshot } from 'valtio';
20
20
  import '@react-aria/ssr';
21
21
 
22
22
  // https://stackoverflow.com/a/2117523
@@ -945,6 +945,7 @@ function canProxy(value) {
945
945
  return typeof value === "object" && value != null;
946
946
  }
947
947
  var proxyObjToStateCell = new WeakMap();
948
+ var valtioSubscriptions = new WeakMap();
948
949
  function ensureStateCell(target, property, path, node) {
949
950
  var _a;
950
951
  if (!proxyObjToStateCell.has(target)) {
@@ -978,6 +979,32 @@ function tryGetStateCellFrom$StateRoot($state, path) {
978
979
  function getStateCellFrom$StateRoot($state, path) {
979
980
  return ensure(tryGetStateCellFrom$StateRoot($state, path));
980
981
  }
982
+ function unsubscribeToValtio($$state, statePath) {
983
+ var _a;
984
+ var oldValue = get($$state.stateValues, statePath);
985
+ if (getVersion(oldValue)) {
986
+ (_a = valtioSubscriptions.get(oldValue)) === null || _a === void 0 ? void 0 : _a.forEach(function (f) { return f(); });
987
+ valtioSubscriptions["delete"](oldValue);
988
+ }
989
+ }
990
+ function subscribeToValtio($$state, statePath, node) {
991
+ var spec = node.getSpec();
992
+ var maybeValtioProxy = spec.valueProp
993
+ ? $$state.env.$props[spec.valueProp]
994
+ : get($$state.stateValues, statePath);
995
+ if (getVersion(maybeValtioProxy) && spec.onChangeProp) {
996
+ var unsub = subscribe(maybeValtioProxy, function () {
997
+ var _a, _b;
998
+ (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, spec.valueProp
999
+ ? $$state.env.$props[spec.valueProp]
1000
+ : get($$state.stateValues, statePath));
1001
+ });
1002
+ if (!valtioSubscriptions.has(maybeValtioProxy)) {
1003
+ valtioSubscriptions.set(maybeValtioProxy, []);
1004
+ }
1005
+ ensure(valtioSubscriptions.get(maybeValtioProxy)).push(unsub);
1006
+ }
1007
+ }
981
1008
  function initializeStateValue($$state, initialStateCell, proxyRoot) {
982
1009
  var _a, _b, _c;
983
1010
  var initialStateName = initialStateCell.node.getSpec().path;
@@ -1041,7 +1068,7 @@ function initializeStateValue($$state, initialStateCell, proxyRoot) {
1041
1068
  }
1042
1069
  function create$StateProxy($$state, leafHandlers) {
1043
1070
  var proxyRoot;
1044
- var rec = function (currPath, currNode, isOutside, initialObject) {
1071
+ var rec = function (currPath, currNode) {
1045
1072
  var getNextPath = function (property) { return __spreadArray(__spreadArray([], __read(currPath), false), [
1046
1073
  isNum$1(property) ? +property : property,
1047
1074
  ], false); };
@@ -1049,8 +1076,7 @@ function create$StateProxy($$state, leafHandlers) {
1049
1076
  var handlers = {
1050
1077
  deleteProperty: function (target, property) {
1051
1078
  var _a, _b;
1052
- if (!isOutside &&
1053
- !currNode.isLeaf() &&
1079
+ if (!currNode.isLeaf() &&
1054
1080
  !currNode.hasArrayTransition() &&
1055
1081
  !isNum$1(property)) {
1056
1082
  throw new InvalidOperation("Can't delete a property in the middle of the state spec");
@@ -1069,20 +1095,16 @@ function create$StateProxy($$state, leafHandlers) {
1069
1095
  if (property === PLASMIC_STATE_PROXY_SYMBOL) {
1070
1096
  return {
1071
1097
  node: currNode,
1072
- path: currPath,
1073
- isOutside: isOutside
1098
+ path: currPath
1074
1099
  };
1075
1100
  }
1076
1101
  var nextPath = getNextPath(property);
1077
- if (isOutside || currNode.isLeaf()) {
1078
- return Reflect.get(target, property, receiver);
1079
- }
1080
1102
  var nextNode = currNode.makeTransition(property);
1081
1103
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1082
1104
  return (_b = (_a = leafHandlers(ensureStateCell(receiver, property, nextPath, nextNode))).get) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, receiver);
1083
1105
  }
1084
1106
  else if (nextNode && !(property in target)) {
1085
- target[property] = rec(nextPath, nextNode, false, undefined);
1107
+ target[property] = rec(nextPath, nextNode);
1086
1108
  }
1087
1109
  return Reflect.get(target, property, receiver);
1088
1110
  },
@@ -1090,6 +1112,7 @@ function create$StateProxy($$state, leafHandlers) {
1090
1112
  var _a, _b, _c, _d;
1091
1113
  var nextPath = getNextPath(property);
1092
1114
  var nextNode = currNode.makeTransition(property);
1115
+ var nextSpec = nextNode === null || nextNode === void 0 ? void 0 : nextNode.getSpec();
1093
1116
  if (property === "registerInitFunc" && currPath.length === 0) {
1094
1117
  return Reflect.set(target, property, value, receiver);
1095
1118
  }
@@ -1100,70 +1123,41 @@ function create$StateProxy($$state, leafHandlers) {
1100
1123
  }
1101
1124
  if (nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf()) {
1102
1125
  (_b = (_a = leafHandlers(ensureStateCell(receiver, property, nextPath, nextNode))).set) === null || _b === void 0 ? void 0 : _b.call(_a, target, property, value, receiver);
1126
+ Reflect.set(target, property, value, receiver);
1127
+ if (nextSpec === null || nextSpec === void 0 ? void 0 : nextSpec.onChangeProp) {
1128
+ (_d = (_c = $$state.env.$props)[nextSpec.onChangeProp]) === null || _d === void 0 ? void 0 : _d.call(_c, value);
1129
+ }
1103
1130
  }
1104
- if (!isOutside && !currNode.isLeaf() && !nextNode) {
1131
+ if (!nextNode) {
1105
1132
  // can't set an unknown field in $state
1106
1133
  return false;
1107
1134
  }
1108
- // we keep pointing to the leaf
1109
- if (!nextNode) {
1110
- assert(isOutside || currNode.isLeaf, "unexpected update in nextNode");
1111
- nextNode = currNode;
1112
- }
1113
- if (canProxy(value)) {
1114
- target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), value);
1135
+ if (canProxy(value) && !nextNode.isLeaf()) {
1136
+ target[property] = rec(nextPath, nextNode);
1137
+ Reflect.ownKeys(value).forEach(function (key) {
1138
+ target[property][key] = value[key];
1139
+ });
1115
1140
  }
1116
- else if (!isOutside && !currNode.isLeaf() && !(nextNode === null || nextNode === void 0 ? void 0 : nextNode.isLeaf())) {
1141
+ else if (!nextNode.isLeaf()) {
1117
1142
  throw new InvalidOperation("inserting a primitive value into a non-leaf");
1118
1143
  }
1119
- else {
1120
- Reflect.set(target, property, value, receiver);
1121
- }
1122
- if (currNode.isLeaf()) {
1123
- if (spec.onChangeProp) {
1124
- (_d = (_c = $$state.env.$props)[spec.onChangeProp]) === null || _d === void 0 ? void 0 : _d.call(_c, target);
1125
- }
1126
- }
1127
- else {
1128
- nextNode.getAllSpecs().forEach(function (spec) {
1129
- var _a, _b;
1130
- if (spec.onChangeProp) {
1131
- (_b = (_a = $$state.env.$props)[spec.onChangeProp]) === null || _b === void 0 ? void 0 : _b.call(_a, value);
1132
- }
1133
- });
1134
- }
1135
- var newValue = (isOutside || currNode.isLeaf()) && currNode.getSpec().isImmutable
1144
+ var newValue = nextNode.isLeaf() && (nextSpec === null || nextSpec === void 0 ? void 0 : nextSpec.isImmutable)
1136
1145
  ? mkUntrackedValue(value)
1137
1146
  : value;
1147
+ unsubscribeToValtio($$state, nextPath);
1138
1148
  set($$state.stateValues, nextPath, newValue);
1149
+ subscribeToValtio($$state, nextPath, nextNode);
1139
1150
  return true;
1140
1151
  }
1141
1152
  };
1142
- var baseObject = !isOutside && !currNode.isLeaf()
1143
- ? currNode.hasArrayTransition()
1144
- ? []
1145
- : {}
1146
- : Array.isArray(initialObject)
1147
- ? []
1148
- : Object.create(Object.getPrototypeOf(initialObject !== null && initialObject !== void 0 ? initialObject : {}));
1153
+ var baseObject = currNode.hasArrayTransition() ? [] : {};
1149
1154
  var proxyObj = new Proxy(baseObject, handlers);
1150
1155
  if (currPath.length === 0) {
1151
1156
  proxyRoot = proxyObj;
1152
1157
  }
1153
- if (initialObject) {
1154
- Reflect.ownKeys(initialObject).forEach(function (key) {
1155
- var desc = Object.getOwnPropertyDescriptor(initialObject, key);
1156
- if (desc.get || desc.set) {
1157
- Object.defineProperty(baseObject, key, desc);
1158
- }
1159
- else {
1160
- proxyObj[key] = initialObject[key];
1161
- }
1162
- });
1163
- }
1164
1158
  return proxyObj;
1165
1159
  };
1166
- return rec([], $$state.rootSpecTree, false, undefined);
1160
+ return rec([], $$state.rootSpecTree);
1167
1161
  }
1168
1162
  var mkUntrackedValue = function (o) {
1169
1163
  return o != null && typeof o === "object" ? ref(o) : o;
@@ -1253,13 +1247,15 @@ function useDollarState(specs) {
1253
1247
  set($state, stateCell.path, spec.initVal);
1254
1248
  }
1255
1249
  return {
1256
- get: function (target, property, receiver) {
1250
+ get: function () {
1257
1251
  var spec = stateCell.node.getSpec();
1258
1252
  if (spec.valueProp) {
1259
- return $$state.env.$props[spec.valueProp];
1253
+ var valueProp = $$state.env.$props[spec.valueProp];
1254
+ subscribeToValtio($$state, stateCell.path, stateCell.node);
1255
+ return valueProp;
1260
1256
  }
1261
1257
  else {
1262
- return Reflect.get(target, property, receiver);
1258
+ return get($$state.stateValues, stateCell.path);
1263
1259
  }
1264
1260
  }
1265
1261
  };
@@ -1296,11 +1292,13 @@ function useDollarState(specs) {
1296
1292
  var old$state_1 = $state;
1297
1293
  $state = ref.current = create$State();
1298
1294
  $$state.specTreeLeaves = newLeaves;
1299
- getStateCells(newLeaves, $$state.rootSpecTree).forEach(function (_a) {
1295
+ getStateCells(old$state_1, $$state.rootSpecTree).forEach(function (_a) {
1300
1296
  var path = _a.path;
1301
1297
  var oldStateCell = tryGetStateCellFrom$StateRoot(old$state_1, path);
1302
1298
  if (oldStateCell) {
1303
1299
  set($state, path, get(old$state_1, path));
1300
+ var newStateCell = getStateCellFrom$StateRoot($state, path);
1301
+ newStateCell.initialValue = oldStateCell.initialValue;
1304
1302
  }
1305
1303
  });
1306
1304
  }
@@ -1313,7 +1311,9 @@ function useDollarState(specs) {
1313
1311
  }
1314
1312
  var stateCell = getStateCellFrom$StateRoot($state, spec.pathObj);
1315
1313
  var newSpec = specs.find(function (sp) { return sp.path === spec.path; });
1316
- if (!newSpec || stateCell.initFuncHash === (newSpec === null || newSpec === void 0 ? void 0 : newSpec.initFuncHash)) {
1314
+ if (!newSpec ||
1315
+ stateCell.initFuncHash === (newSpec === null || newSpec === void 0 ? void 0 : newSpec.initFuncHash) ||
1316
+ stateCell.initialValue !== UNINITIALIZED) {
1317
1317
  return;
1318
1318
  }
1319
1319
  stateCell.initFunc = newSpec.initFunc;
@@ -1445,28 +1445,33 @@ var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect
1445
1445
  function isPlasmicStateProxy(obj) {
1446
1446
  return (obj != null && typeof obj === "object" && !!obj[PLASMIC_STATE_PROXY_SYMBOL]);
1447
1447
  }
1448
+ function is$StateProxy(obj) {
1449
+ return (obj != null &&
1450
+ typeof obj === "object" &&
1451
+ (!!obj[PLASMIC_STATE_PROXY_SYMBOL] || getVersion(obj)));
1452
+ }
1448
1453
  function getStateCells($state, root) {
1449
1454
  var e_3, _a;
1450
1455
  var _b;
1451
1456
  if ($state == null || typeof $state !== "object") {
1452
1457
  return [];
1453
1458
  }
1454
- var stateCells = Object.values((_b = proxyObjToStateCell.get($state)) !== null && _b !== void 0 ? _b : {});
1455
- if (root.isLeaf()) {
1456
- return stateCells;
1457
- }
1458
1459
  if (root.hasArrayTransition()) {
1459
- return __spreadArray(__spreadArray([], __read(stateCells), false), __read(Object.keys($state).flatMap(function (key) {
1460
+ return Object.keys($state).flatMap(function (key) {
1460
1461
  return getStateCells($state[key], ensure(root.makeTransition(ARRAY_SYMBOL)));
1461
- })), false);
1462
+ });
1462
1463
  }
1463
1464
  else {
1464
- var childrenStateCells = [];
1465
+ var stateCell = (_b = proxyObjToStateCell.get($state)) !== null && _b !== void 0 ? _b : {};
1466
+ var stateCells = [];
1465
1467
  try {
1466
1468
  for (var _c = __values(root.edges().entries()), _d = _c.next(); !_d.done; _d = _c.next()) {
1467
1469
  var _e = __read(_d.value, 2), key = _e[0], child = _e[1];
1468
1470
  if (typeof key === "string" && key in $state) {
1469
- childrenStateCells.push.apply(childrenStateCells, __spreadArray([], __read(getStateCells($state[key], child)), false));
1471
+ stateCells.push.apply(stateCells, __spreadArray([], __read(getStateCells($state[key], child)), false));
1472
+ if (key in stateCell) {
1473
+ stateCells.push(stateCell[key]);
1474
+ }
1470
1475
  }
1471
1476
  }
1472
1477
  }
@@ -1477,7 +1482,7 @@ function getStateCells($state, root) {
1477
1482
  }
1478
1483
  finally { if (e_3) throw e_3.error; }
1479
1484
  }
1480
- return __spreadArray(__spreadArray([], __read(stateCells), false), __read(childrenStateCells), false);
1485
+ return stateCells;
1481
1486
  }
1482
1487
  }
1483
1488
  function getStateCellsInPlasmicProxy(obj) {
@@ -1498,9 +1503,9 @@ function getStateSpecInPlasmicProxy(obj, path) {
1498
1503
  if (!isPlasmicStateProxy(obj)) {
1499
1504
  return undefined;
1500
1505
  }
1501
- var _a = obj[PLASMIC_STATE_PROXY_SYMBOL], node = _a.node, isOutside = _a.isOutside;
1506
+ var node = obj[PLASMIC_STATE_PROXY_SYMBOL].node;
1502
1507
  var nextNode = node.makeTransition(path[path.length - 1]);
1503
- if (isOutside || node.isLeaf() || !nextNode) {
1508
+ if (node.isLeaf() || !nextNode) {
1504
1509
  return undefined;
1505
1510
  }
1506
1511
  return {
@@ -1541,16 +1546,6 @@ function arrayEq(xs, ys) {
1541
1546
  function isNum(value) {
1542
1547
  return typeof value === "symbol" ? false : !isNaN(+value);
1543
1548
  }
1544
- function assert(cond, msg) {
1545
- if (msg === void 0) { msg = "Assertion failed"; }
1546
- if (!cond) {
1547
- // We always generate an non empty message so that it doesn't get swallowed
1548
- // by the async library.
1549
- msg = (typeof msg === "string" ? msg : msg()) || "Assertion failed";
1550
- debugger;
1551
- throw new Error(msg);
1552
- }
1553
- }
1554
1549
  /**
1555
1550
  * Forked from https://github.com/lukeed/dset
1556
1551
  * Changes: fixed setting a deep value to a proxy object
@@ -1603,5 +1598,5 @@ function assignValue(object, key, value) {
1603
1598
  // Utilities used by generated code
1604
1599
  var classNames = classNames$1;
1605
1600
 
1606
- export { PlasmicIcon, PlasmicLink, PlasmicPageGuard, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateOnChangePropForCodeComponents, generateStateValueProp, getCurrentInitialValue, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, hasVariant, initializeCodeComponentStates, initializePlasmicStates, isPlasmicStateProxy, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, resetToInitialValue, set, useDollarState, useTrigger, wrapWithClassName };
1601
+ export { PlasmicIcon, PlasmicLink, PlasmicPageGuard, PlasmicSlot, Stack, classNames, createPlasmicElementProxy, createUseScreenVariants, deriveRenderOpts, ensureGlobalVariants, generateStateOnChangeProp, generateStateOnChangePropForCodeComponents, generateStateValueProp, getCurrentInitialValue, getStateCellsInPlasmicProxy, getStateSpecInPlasmicProxy, hasVariant, initializeCodeComponentStates, initializePlasmicStates, is$StateProxy, isPlasmicStateProxy, makeFragment, mergeVariantsWithStates, renderPlasmicSlot, resetToInitialValue, set, useDollarState, useTrigger, wrapWithClassName };
1607
1602
  //# sourceMappingURL=index.js.map