@plasmicapp/react-web 0.2.139 → 0.2.141

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.
@@ -1280,10 +1280,16 @@ function renderPlasmicSlot(opts) {
1280
1280
  }
1281
1281
  function maybeAsString(node) {
1282
1282
  // Unwrap fragments
1283
- if (isValidElement(node) && (
1284
- // Fragment and Trans don't render DOM elements
1285
- node.type === Fragment || node.type === Trans)) {
1286
- return maybeAsString(node.props.children);
1283
+ if (isValidElement(node)) {
1284
+ // Fragment doesn't render DOM elements
1285
+ if (node.type === Fragment) {
1286
+ return maybeAsString(node.props.children);
1287
+ } else if (node.type === Trans) {
1288
+ // Trans also doesn't render DOM elements. But we don't want to just render
1289
+ // its content string, because we want to keep the <Trans/> for the localization.
1290
+ // So we render the same node, to be wrapped into __wab_slot-string-wrapper.
1291
+ return node;
1292
+ }
1287
1293
  }
1288
1294
  if (typeof node === "string") {
1289
1295
  return node;
@@ -1506,19 +1512,12 @@ function useTrigger(trigger, opts) {
1506
1512
  var ARRAY_SYMBOL = /*#__PURE__*/Symbol("[]");
1507
1513
  var PLASMIC_STATE_PROXY_SYMBOL = /*#__PURE__*/Symbol("plasmic.state.proxy");
1508
1514
 
1509
- function generateStateOnChangeProp($state, stateName, dataReps) {
1510
- return function (val, path) {
1511
- return set($state, [stateName].concat(dataReps, path), val);
1515
+ function generateStateOnChangeProp($state, path) {
1516
+ return function (val) {
1517
+ return set($state, path, val);
1512
1518
  };
1513
1519
  }
1514
- /**
1515
- * This function generate the state value prop for repeated states
1516
- * Example:
1517
- * - parent[][].counter[].count
1518
- * We need to pass `parent[index1][index2].counter to the child component
1519
- */
1520
- function generateStateValueProp($state, path // ["parent", 0, 1, "counter"]
1521
- ) {
1520
+ function generateStateValueProp($state, path) {
1522
1521
  return _get($state, path);
1523
1522
  }
1524
1523
  var useIsomorphicLayoutEffect$1 = typeof window !== "undefined" ? useLayoutEffect : useEffect;
@@ -1536,6 +1535,14 @@ function shallowEqual(a1, a2) {
1536
1535
  }
1537
1536
  return true;
1538
1537
  }
1538
+ /**
1539
+ * Shallow comparison of arrays.
1540
+ */
1541
+ function arrayEq(xs, ys) {
1542
+ return xs.length === ys.length && xs.every(function (_, index) {
1543
+ return xs[index] === ys[index];
1544
+ });
1545
+ }
1539
1546
  function isNum(value) {
1540
1547
  return typeof value === "symbol" ? false : !isNaN(+value);
1541
1548
  }
@@ -1598,69 +1605,84 @@ function assignValue(object, key, value) {
1598
1605
  var UNINITIALIZED = /*#__PURE__*/Symbol("plasmic.unitialized");
1599
1606
  var StateSpecNode = /*#__PURE__*/function () {
1600
1607
  function StateSpecNode(specs) {
1601
- this.specs = specs;
1602
- this.edges = new Map();
1603
- this.state = {};
1608
+ this._specs = specs;
1609
+ this._edges = new Map();
1610
+ this._state = {};
1604
1611
  }
1605
1612
  var _proto = StateSpecNode.prototype;
1613
+ _proto.setSpecs = function setSpecs(specs) {
1614
+ this._specs = specs;
1615
+ };
1616
+ _proto.edges = function edges() {
1617
+ return this._edges;
1618
+ };
1619
+ _proto.state = function state() {
1620
+ return this._state;
1621
+ };
1606
1622
  _proto.hasEdge = function hasEdge(key) {
1607
- return this.edges.has(key);
1623
+ return this._edges.has(key);
1608
1624
  };
1609
1625
  _proto.addEdge = function addEdge(key, node) {
1610
- this.edges.set(key, node);
1626
+ this._edges.set(key, node);
1627
+ };
1628
+ _proto.clearEdges = function clearEdges() {
1629
+ this._edges = new Map();
1611
1630
  };
1612
1631
  _proto.children = function children() {
1613
- return this.edges.values();
1632
+ return this._edges.values();
1614
1633
  };
1615
1634
  _proto.makeTransition = function makeTransition(key) {
1616
1635
  key = isNum(key) ? ARRAY_SYMBOL : key;
1617
- return this.edges.get(key);
1636
+ return this._edges.get(key);
1618
1637
  };
1619
1638
  _proto.isLeaf = function isLeaf() {
1620
- return this.edges.size === 0;
1639
+ return this._edges.size === 0;
1621
1640
  };
1622
1641
  _proto.hasArrayTransition = function hasArrayTransition() {
1623
- return this.edges.has(ARRAY_SYMBOL);
1642
+ return this._edges.has(ARRAY_SYMBOL);
1624
1643
  };
1625
1644
  _proto.getSpec = function getSpec() {
1626
- return this.specs[0];
1645
+ return this._specs[0];
1627
1646
  };
1628
1647
  _proto.getAllSpecs = function getAllSpecs() {
1629
- return this.specs;
1648
+ return this._specs;
1630
1649
  };
1631
1650
  _proto.getState = function getState(path) {
1632
- return this.state[JSON.stringify(path)];
1651
+ return this._state[JSON.stringify(path)];
1652
+ };
1653
+ _proto.getInitFunc = function getInitFunc(stateCell) {
1654
+ var _stateCell$registered;
1655
+ return (_stateCell$registered = stateCell.registeredInitFunc) != null ? _stateCell$registered : this.getSpec().initFunc;
1633
1656
  };
1634
1657
  _proto.clearStates = function clearStates() {
1635
- this.state = {};
1658
+ this._state = {};
1636
1659
  };
1637
1660
  _proto.states = function states() {
1638
- return Object.values(this.state);
1661
+ return Object.values(this._state);
1639
1662
  };
1640
1663
  _proto.hasState = function hasState(path) {
1641
1664
  var key = JSON.stringify(path);
1642
- return key in this.state;
1665
+ return key in this._state;
1643
1666
  };
1644
1667
  _proto.createStateCell = function createStateCell(path) {
1645
1668
  var key = JSON.stringify(path);
1646
- this.state[key] = {
1669
+ this._state[key] = {
1647
1670
  listeners: [],
1648
1671
  initialValue: UNINITIALIZED,
1649
- registeredInitFunc: this.getSpec().initFunc,
1650
1672
  path: path
1651
1673
  };
1652
1674
  };
1653
1675
  _proto.setInitialValue = function setInitialValue(path, value) {
1654
1676
  var key = JSON.stringify(path);
1655
- this.state[key].initialValue = value;
1677
+ this._state[key].initialValue = value;
1656
1678
  };
1657
1679
  _proto.getInitialValue = function getInitialValue(path) {
1658
1680
  var key = JSON.stringify(path);
1659
- return this.state[key].initialValue;
1681
+ return this._state[key].initialValue;
1660
1682
  };
1661
1683
  _proto.addListener = function addListener(path, f) {
1662
1684
  var key = JSON.stringify(path);
1663
- this.state[key].listeners.push(f);
1685
+ this._state[key].listeners.push(f);
1664
1686
  };
1665
1687
  return StateSpecNode;
1666
1688
  }();
@@ -1695,14 +1717,43 @@ function buildTree(specs) {
1695
1717
  };
1696
1718
  return rec([]);
1697
1719
  }
1698
- function getLeaves(root) {
1720
+ function updateTree(root, specs) {
1721
+ var internalSpec = specs.map(function (spec) {
1722
+ return _extends({}, spec, {
1723
+ pathObj: transformPathStringToObj(spec.path),
1724
+ isRepeated: spec.path.split(".").some(function (part) {
1725
+ return part.endsWith("[]");
1726
+ })
1727
+ });
1728
+ });
1729
+ var rec = function rec(oldNode, currentPath) {
1730
+ var nodeSpecs = internalSpec.filter(function (spec) {
1731
+ return shallowEqual(currentPath, spec.pathObj.slice(0, currentPath.length));
1732
+ });
1733
+ var node = oldNode != null ? oldNode : new StateSpecNode(nodeSpecs);
1734
+ node.setSpecs(nodeSpecs);
1735
+ var oldEdges = oldNode == null ? void 0 : oldNode.edges();
1736
+ node.clearEdges();
1737
+ node.getAllSpecs().forEach(function (spec) {
1738
+ if (spec.pathObj.length > currentPath.length) {
1739
+ var nextKey = spec.pathObj[currentPath.length];
1740
+ if (!node.hasEdge(nextKey)) {
1741
+ node.addEdge(nextKey, rec(oldEdges == null ? void 0 : oldEdges.get(nextKey), [].concat(currentPath, [nextKey])));
1742
+ }
1743
+ }
1744
+ });
1745
+ return node;
1746
+ };
1747
+ return rec(root, []);
1748
+ }
1749
+ function getStateCells(root) {
1699
1750
  var leaves = [];
1700
1751
  var rec = function rec(node) {
1701
1752
  for (var _iterator = _createForOfIteratorHelperLoose(node.children()), _step; !(_step = _iterator()).done;) {
1702
1753
  var child = _step.value;
1703
1754
  rec(child);
1704
1755
  }
1705
- if (node.isLeaf()) {
1756
+ if (node.isLeaf() && node.getAllSpecs().length > 0) {
1706
1757
  leaves.push(node);
1707
1758
  }
1708
1759
  };
@@ -1717,7 +1768,6 @@ function findStateCell(root, pathStr, repetitionIndex) {
1717
1768
  var part = _step2.value;
1718
1769
  if (typeof part === "symbol") {
1719
1770
  if (!root.hasArrayTransition() || !repetitionIndex || currRepIndex > repetitionIndex.length) {
1720
- console.log(root);
1721
1771
  throw new Error("transition not found: pathStr " + pathStr + " part " + (typeof part === "symbol" ? "[]" : part));
1722
1772
  }
1723
1773
  realPath.push(repetitionIndex[currRepIndex++]);
@@ -1772,7 +1822,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1772
1822
  set(proxyRoot, initialStatePath, newValue);
1773
1823
  });
1774
1824
  });
1775
- var initialValue = initialSpecNode.getState(initialStatePath).registeredInitFunc($$state.props, $state, $$state.ctx);
1825
+ var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))($$state.props, $state, $$state.ctx);
1776
1826
  initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
1777
1827
  var initialSpec = initialSpecNode.getSpec();
1778
1828
  var value = initialSpec.isImmutable ? mkUntrackedValue(initialValue) : clone(initialValue);
@@ -1785,7 +1835,8 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1785
1835
  return initialValue;
1786
1836
  }
1787
1837
  function create$StateProxy($$state, leafHandlers) {
1788
- var rec = function rec(currPath, currNode, isOutside, proxyRoot, initialObject) {
1838
+ var proxyRoot;
1839
+ var rec = function rec(currPath, currNode, isOutside, initialObject) {
1789
1840
  var getNextPath = function getNextPath(property) {
1790
1841
  return [].concat(currPath, [isNum$1(property) ? +property : property]);
1791
1842
  };
@@ -1801,7 +1852,7 @@ function create$StateProxy($$state, leafHandlers) {
1801
1852
  //we are always in a leaf, since we only have two cases:
1802
1853
  // 1 - delete properties outside the state tree
1803
1854
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
1804
- (_$$state$props$spec$o = (_$$state$props2 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o.call(_$$state$props2, _get($$state.stateValues, currPath.slice(spec.pathObj.length)));
1855
+ (_$$state$props$spec$o = (_$$state$props2 = $$state.props)[spec.onChangeProp]) == null ? void 0 : _$$state$props$spec$o.call(_$$state$props2, _get(proxyRoot, currPath.slice(spec.pathObj.length)));
1805
1856
  }
1806
1857
  return Reflect.deleteProperty(target, property);
1807
1858
  },
@@ -1809,7 +1860,6 @@ function create$StateProxy($$state, leafHandlers) {
1809
1860
  if (property === PLASMIC_STATE_PROXY_SYMBOL) {
1810
1861
  return true;
1811
1862
  }
1812
- proxyRoot = proxyRoot == null ? receiver : proxyRoot;
1813
1863
  var nextPath = getNextPath(property);
1814
1864
  if (isOutside || currNode.isLeaf()) {
1815
1865
  return Reflect.get(target, property, receiver);
@@ -1817,15 +1867,14 @@ function create$StateProxy($$state, leafHandlers) {
1817
1867
  var nextNode = currNode.makeTransition(property);
1818
1868
  if (nextNode != null && nextNode.isLeaf()) {
1819
1869
  var _leafHandlers$get, _leafHandlers;
1820
- return (_leafHandlers$get = (_leafHandlers = leafHandlers(nextNode, nextPath, proxyRoot)).get) == null ? void 0 : _leafHandlers$get.call(_leafHandlers, target, property, receiver);
1870
+ return (_leafHandlers$get = (_leafHandlers = leafHandlers(nextNode, nextPath)).get) == null ? void 0 : _leafHandlers$get.call(_leafHandlers, target, property, receiver);
1821
1871
  } else if (nextNode && !(property in target)) {
1822
- target[property] = rec(nextPath, nextNode, false, proxyRoot, undefined);
1872
+ target[property] = rec(nextPath, nextNode, false, undefined);
1823
1873
  }
1824
1874
  return Reflect.get(target, property, receiver);
1825
1875
  },
1826
1876
  set: function set$1(target, property, value, receiver) {
1827
1877
  var _nextNode, _nextNode2;
1828
- proxyRoot = proxyRoot == null ? receiver : proxyRoot;
1829
1878
  var nextPath = getNextPath(property);
1830
1879
  var nextNode = currNode.makeTransition(property);
1831
1880
  if (property === "registerInitFunc" && currPath.length === 0) {
@@ -1838,7 +1887,7 @@ function create$StateProxy($$state, leafHandlers) {
1838
1887
  }
1839
1888
  if ((_nextNode = nextNode) != null && _nextNode.isLeaf()) {
1840
1889
  var _leafHandlers$set, _leafHandlers2;
1841
- (_leafHandlers$set = (_leafHandlers2 = leafHandlers(nextNode, nextPath, proxyRoot)).set) == null ? void 0 : _leafHandlers$set.call(_leafHandlers2, target, property, value, receiver);
1890
+ (_leafHandlers$set = (_leafHandlers2 = leafHandlers(nextNode, nextPath)).set) == null ? void 0 : _leafHandlers$set.call(_leafHandlers2, target, property, value, receiver);
1842
1891
  }
1843
1892
  if (!isOutside && !currNode.isLeaf() && !nextNode) {
1844
1893
  // can't set an unknown field in $state
@@ -1850,7 +1899,7 @@ function create$StateProxy($$state, leafHandlers) {
1850
1899
  nextNode = currNode;
1851
1900
  }
1852
1901
  if (canProxy(value)) {
1853
- target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), proxyRoot, value);
1902
+ target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), value);
1854
1903
  } else if (!isOutside && !currNode.isLeaf() && !((_nextNode2 = nextNode) != null && _nextNode2.isLeaf())) {
1855
1904
  throw new Error("inserting a primitive value into a non-leaf");
1856
1905
  } else {
@@ -1867,8 +1916,11 @@ function create$StateProxy($$state, leafHandlers) {
1867
1916
  return true;
1868
1917
  }
1869
1918
  };
1870
- var baseObject = !isOutside && !currNode.isLeaf() ? currNode.hasArrayTransition() ? [] : {} : Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject));
1919
+ var baseObject = !isOutside && !currNode.isLeaf() ? currNode.hasArrayTransition() ? [] : {} : Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject != null ? initialObject : {}));
1871
1920
  var proxyObj = new Proxy(baseObject, handlers);
1921
+ if (currPath.length === 0) {
1922
+ proxyRoot = proxyObj;
1923
+ }
1872
1924
  if (initialObject) {
1873
1925
  Reflect.ownKeys(initialObject).forEach(function (key) {
1874
1926
  var desc = Object.getOwnPropertyDescriptor(initialObject, key);
@@ -1881,34 +1933,36 @@ function create$StateProxy($$state, leafHandlers) {
1881
1933
  }
1882
1934
  return proxyObj;
1883
1935
  };
1884
- return rec([], $$state.rootSpecTree, false, undefined, undefined);
1936
+ return rec([], $$state.rootSpecTree, false, undefined);
1885
1937
  }
1886
1938
  var mkUntrackedValue = function mkUntrackedValue(o) {
1887
1939
  return o != null && typeof o === "object" ? ref(o) : o;
1888
1940
  };
1889
- function useDollarState(specs, props, $ctx) {
1941
+ function useDollarState(specs, props, $ctx, opts) {
1890
1942
  var $$state = React__default.useRef(function () {
1891
1943
  var rootSpecTree = buildTree(specs);
1892
1944
  return {
1893
1945
  rootSpecTree: rootSpecTree,
1894
- specTreeLeaves: getLeaves(rootSpecTree),
1946
+ specTreeLeaves: getStateCells(rootSpecTree),
1895
1947
  stateValues: proxy({}),
1896
1948
  props: {},
1897
1949
  ctx: {},
1898
- registrationsQueue: []
1950
+ specs: [],
1951
+ registrationsQueue: proxy([])
1899
1952
  };
1900
1953
  }()).current;
1901
1954
  $$state.props = props;
1902
1955
  $$state.ctx = $ctx != null ? $ctx : {};
1903
- var $state = React__default.useRef(function () {
1904
- var useRef$state = Object.assign(create$StateProxy($$state, function (node, path, proxyRoot) {
1956
+ $$state.specs = specs;
1957
+ var create$State = function create$State() {
1958
+ var $state = Object.assign(create$StateProxy($$state, function (node, path) {
1905
1959
  if (!node.hasState(path)) {
1906
1960
  node.createStateCell(path);
1907
1961
  var spec = node.getSpec();
1908
1962
  if (spec.initFunc) {
1909
- initializeStateValue($$state, node, path, proxyRoot);
1963
+ initializeStateValue($$state, node, path, $state);
1910
1964
  } else if (!spec.valueProp) {
1911
- set(proxyRoot, path, spec.initVal);
1965
+ set($state, path, spec.initVal);
1912
1966
  }
1913
1967
  }
1914
1968
  return {
@@ -1929,17 +1983,40 @@ function useDollarState(specs, props, $ctx) {
1929
1983
  if (!node.hasState(realPath)) {
1930
1984
  node.createStateCell(realPath);
1931
1985
  }
1932
- if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, useRef$state, $$state.ctx))) {
1933
- $$state.registrationsQueue.push({
1986
+ if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
1987
+ $$state.registrationsQueue.push(mkUntrackedValue({
1934
1988
  node: node,
1935
1989
  path: realPath,
1936
1990
  f: f
1937
- });
1991
+ }));
1938
1992
  }
1939
1993
  }
1940
1994
  });
1941
- return useRef$state;
1942
- }()).current;
1995
+ return $state;
1996
+ };
1997
+ var ref = React__default.useRef(undefined);
1998
+ if (!ref.current) {
1999
+ ref.current = create$State();
2000
+ }
2001
+ var $state = ref.current;
2002
+ if (opts != null && opts.inCanvas) {
2003
+ $$state.rootSpecTree = updateTree($$state.rootSpecTree, specs);
2004
+ var newLeaves = getStateCells($$state.rootSpecTree);
2005
+ if (!arrayEq(newLeaves, $$state.specTreeLeaves)) {
2006
+ $state = ref.current = create$State();
2007
+ $$state.specTreeLeaves = newLeaves;
2008
+ }
2009
+ // we need to eager initialize all states in canvas to populate the data picker
2010
+ $$state.specTreeLeaves.forEach(function (node) {
2011
+ var spec = node.getSpec();
2012
+ if (spec.isRepeated || node.hasState(spec.pathObj)) {
2013
+ return;
2014
+ }
2015
+ node.createStateCell(spec.pathObj);
2016
+ var init = spec.valueProp ? $$state.props[spec.valueProp] : spec.initFunc ? initializeStateValue($$state, node, spec.pathObj, $state) : spec.initVal;
2017
+ set($state, spec.pathObj, init);
2018
+ });
2019
+ }
1943
2020
  // For each spec with an initFunc, evaluate it and see if
1944
2021
  // the init value has changed. If so, reset its state.
1945
2022
  var resetSpecs = [];
@@ -1951,10 +2028,11 @@ function useDollarState(specs, props, $ctx) {
1951
2028
  };
1952
2029
  });
1953
2030
  }).forEach(function (_ref2) {
1954
- var stateCell = _ref2.stateCell,
1955
- node = _ref2.node;
1956
- if (stateCell.registeredInitFunc) {
1957
- var newInit = stateCell.registeredInitFunc(props, $state, $ctx != null ? $ctx : {});
2031
+ var node = _ref2.node,
2032
+ stateCell = _ref2.stateCell;
2033
+ var initFunc = node.getInitFunc(stateCell);
2034
+ if (initFunc) {
2035
+ var newInit = initFunc(props, $state, $ctx != null ? $ctx : {});
1958
2036
  if (!deepEqual(newInit, stateCell.initialValue)) {
1959
2037
  resetSpecs.push({
1960
2038
  stateCell: stateCell,
@@ -1979,16 +2057,16 @@ function useDollarState(specs, props, $ctx) {
1979
2057
  });
1980
2058
  }, [props, resetSpecs]);
1981
2059
  useIsomorphicLayoutEffect$1(function () {
1982
- $$state.registrationsQueue.forEach(function (_ref4) {
1983
- var node = _ref4.node,
1984
- path = _ref4.path,
1985
- f = _ref4.f;
2060
+ while ($$state.registrationsQueue.length) {
2061
+ var _$$state$registration = $$state.registrationsQueue.shift(),
2062
+ node = _$$state$registration.node,
2063
+ path = _$$state$registration.path,
2064
+ f = _$$state$registration.f;
1986
2065
  var stateCell = node.getState(path);
1987
2066
  stateCell.registeredInitFunc = f;
1988
2067
  reInitializeState(node, stateCell);
1989
- });
1990
- $$state.registrationsQueue = [];
1991
- }, [$$state.registrationsQueue]);
2068
+ }
2069
+ }, [$$state.registrationsQueue.length]);
1992
2070
  // immediately initialize exposed non-private states
1993
2071
  useIsomorphicLayoutEffect$1(function () {
1994
2072
  $$state.specTreeLeaves.forEach(function (node) {
@@ -2003,6 +2081,7 @@ function useDollarState(specs, props, $ctx) {
2003
2081
  useSnapshot($$state.stateValues, {
2004
2082
  sync: true
2005
2083
  });
2084
+ useSnapshot($$state.registrationsQueue);
2006
2085
  return $state;
2007
2086
  }
2008
2087