@plasmicapp/react-web 0.2.140 → 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.
@@ -1512,19 +1512,12 @@ function useTrigger(trigger, opts) {
1512
1512
  var ARRAY_SYMBOL = /*#__PURE__*/Symbol("[]");
1513
1513
  var PLASMIC_STATE_PROXY_SYMBOL = /*#__PURE__*/Symbol("plasmic.state.proxy");
1514
1514
 
1515
- function generateStateOnChangeProp($state, stateName, dataReps) {
1516
- return function (val, path) {
1517
- return set($state, [stateName].concat(dataReps, path), val);
1515
+ function generateStateOnChangeProp($state, path) {
1516
+ return function (val) {
1517
+ return set($state, path, val);
1518
1518
  };
1519
1519
  }
1520
- /**
1521
- * This function generate the state value prop for repeated states
1522
- * Example:
1523
- * - parent[][].counter[].count
1524
- * We need to pass `parent[index1][index2].counter to the child component
1525
- */
1526
- function generateStateValueProp($state, path // ["parent", 0, 1, "counter"]
1527
- ) {
1520
+ function generateStateValueProp($state, path) {
1528
1521
  return _get($state, path);
1529
1522
  }
1530
1523
  var useIsomorphicLayoutEffect$1 = typeof window !== "undefined" ? useLayoutEffect : useEffect;
@@ -1542,6 +1535,14 @@ function shallowEqual(a1, a2) {
1542
1535
  }
1543
1536
  return true;
1544
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
+ }
1545
1546
  function isNum(value) {
1546
1547
  return typeof value === "symbol" ? false : !isNaN(+value);
1547
1548
  }
@@ -1604,69 +1605,84 @@ function assignValue(object, key, value) {
1604
1605
  var UNINITIALIZED = /*#__PURE__*/Symbol("plasmic.unitialized");
1605
1606
  var StateSpecNode = /*#__PURE__*/function () {
1606
1607
  function StateSpecNode(specs) {
1607
- this.specs = specs;
1608
- this.edges = new Map();
1609
- this.state = {};
1608
+ this._specs = specs;
1609
+ this._edges = new Map();
1610
+ this._state = {};
1610
1611
  }
1611
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
+ };
1612
1622
  _proto.hasEdge = function hasEdge(key) {
1613
- return this.edges.has(key);
1623
+ return this._edges.has(key);
1614
1624
  };
1615
1625
  _proto.addEdge = function addEdge(key, node) {
1616
- this.edges.set(key, node);
1626
+ this._edges.set(key, node);
1627
+ };
1628
+ _proto.clearEdges = function clearEdges() {
1629
+ this._edges = new Map();
1617
1630
  };
1618
1631
  _proto.children = function children() {
1619
- return this.edges.values();
1632
+ return this._edges.values();
1620
1633
  };
1621
1634
  _proto.makeTransition = function makeTransition(key) {
1622
1635
  key = isNum(key) ? ARRAY_SYMBOL : key;
1623
- return this.edges.get(key);
1636
+ return this._edges.get(key);
1624
1637
  };
1625
1638
  _proto.isLeaf = function isLeaf() {
1626
- return this.edges.size === 0;
1639
+ return this._edges.size === 0;
1627
1640
  };
1628
1641
  _proto.hasArrayTransition = function hasArrayTransition() {
1629
- return this.edges.has(ARRAY_SYMBOL);
1642
+ return this._edges.has(ARRAY_SYMBOL);
1630
1643
  };
1631
1644
  _proto.getSpec = function getSpec() {
1632
- return this.specs[0];
1645
+ return this._specs[0];
1633
1646
  };
1634
1647
  _proto.getAllSpecs = function getAllSpecs() {
1635
- return this.specs;
1648
+ return this._specs;
1636
1649
  };
1637
1650
  _proto.getState = function getState(path) {
1638
- 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;
1639
1656
  };
1640
1657
  _proto.clearStates = function clearStates() {
1641
- this.state = {};
1658
+ this._state = {};
1642
1659
  };
1643
1660
  _proto.states = function states() {
1644
- return Object.values(this.state);
1661
+ return Object.values(this._state);
1645
1662
  };
1646
1663
  _proto.hasState = function hasState(path) {
1647
1664
  var key = JSON.stringify(path);
1648
- return key in this.state;
1665
+ return key in this._state;
1649
1666
  };
1650
1667
  _proto.createStateCell = function createStateCell(path) {
1651
1668
  var key = JSON.stringify(path);
1652
- this.state[key] = {
1669
+ this._state[key] = {
1653
1670
  listeners: [],
1654
1671
  initialValue: UNINITIALIZED,
1655
- registeredInitFunc: this.getSpec().initFunc,
1656
1672
  path: path
1657
1673
  };
1658
1674
  };
1659
1675
  _proto.setInitialValue = function setInitialValue(path, value) {
1660
1676
  var key = JSON.stringify(path);
1661
- this.state[key].initialValue = value;
1677
+ this._state[key].initialValue = value;
1662
1678
  };
1663
1679
  _proto.getInitialValue = function getInitialValue(path) {
1664
1680
  var key = JSON.stringify(path);
1665
- return this.state[key].initialValue;
1681
+ return this._state[key].initialValue;
1666
1682
  };
1667
1683
  _proto.addListener = function addListener(path, f) {
1668
1684
  var key = JSON.stringify(path);
1669
- this.state[key].listeners.push(f);
1685
+ this._state[key].listeners.push(f);
1670
1686
  };
1671
1687
  return StateSpecNode;
1672
1688
  }();
@@ -1701,14 +1717,43 @@ function buildTree(specs) {
1701
1717
  };
1702
1718
  return rec([]);
1703
1719
  }
1704
- 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) {
1705
1750
  var leaves = [];
1706
1751
  var rec = function rec(node) {
1707
1752
  for (var _iterator = _createForOfIteratorHelperLoose(node.children()), _step; !(_step = _iterator()).done;) {
1708
1753
  var child = _step.value;
1709
1754
  rec(child);
1710
1755
  }
1711
- if (node.isLeaf()) {
1756
+ if (node.isLeaf() && node.getAllSpecs().length > 0) {
1712
1757
  leaves.push(node);
1713
1758
  }
1714
1759
  };
@@ -1723,7 +1768,6 @@ function findStateCell(root, pathStr, repetitionIndex) {
1723
1768
  var part = _step2.value;
1724
1769
  if (typeof part === "symbol") {
1725
1770
  if (!root.hasArrayTransition() || !repetitionIndex || currRepIndex > repetitionIndex.length) {
1726
- console.log(root);
1727
1771
  throw new Error("transition not found: pathStr " + pathStr + " part " + (typeof part === "symbol" ? "[]" : part));
1728
1772
  }
1729
1773
  realPath.push(repetitionIndex[currRepIndex++]);
@@ -1778,7 +1822,7 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1778
1822
  set(proxyRoot, initialStatePath, newValue);
1779
1823
  });
1780
1824
  });
1781
- var initialValue = initialSpecNode.getState(initialStatePath).registeredInitFunc($$state.props, $state, $$state.ctx);
1825
+ var initialValue = initialSpecNode.getInitFunc(initialSpecNode.getState(initialStatePath))($$state.props, $state, $$state.ctx);
1782
1826
  initialSpecNode.setInitialValue(initialStatePath, clone(initialValue));
1783
1827
  var initialSpec = initialSpecNode.getSpec();
1784
1828
  var value = initialSpec.isImmutable ? mkUntrackedValue(initialValue) : clone(initialValue);
@@ -1791,7 +1835,8 @@ function initializeStateValue($$state, initialSpecNode, initialStatePath, proxyR
1791
1835
  return initialValue;
1792
1836
  }
1793
1837
  function create$StateProxy($$state, leafHandlers) {
1794
- var rec = function rec(currPath, currNode, isOutside, proxyRoot, initialObject) {
1838
+ var proxyRoot;
1839
+ var rec = function rec(currPath, currNode, isOutside, initialObject) {
1795
1840
  var getNextPath = function getNextPath(property) {
1796
1841
  return [].concat(currPath, [isNum$1(property) ? +property : property]);
1797
1842
  };
@@ -1807,7 +1852,7 @@ function create$StateProxy($$state, leafHandlers) {
1807
1852
  //we are always in a leaf, since we only have two cases:
1808
1853
  // 1 - delete properties outside the state tree
1809
1854
  // 2 - delete indices in repeated implicit states, but these can't be exposed, so they don't have onChangeProp
1810
- (_$$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)));
1811
1856
  }
1812
1857
  return Reflect.deleteProperty(target, property);
1813
1858
  },
@@ -1815,7 +1860,6 @@ function create$StateProxy($$state, leafHandlers) {
1815
1860
  if (property === PLASMIC_STATE_PROXY_SYMBOL) {
1816
1861
  return true;
1817
1862
  }
1818
- proxyRoot = proxyRoot == null ? receiver : proxyRoot;
1819
1863
  var nextPath = getNextPath(property);
1820
1864
  if (isOutside || currNode.isLeaf()) {
1821
1865
  return Reflect.get(target, property, receiver);
@@ -1823,15 +1867,14 @@ function create$StateProxy($$state, leafHandlers) {
1823
1867
  var nextNode = currNode.makeTransition(property);
1824
1868
  if (nextNode != null && nextNode.isLeaf()) {
1825
1869
  var _leafHandlers$get, _leafHandlers;
1826
- 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);
1827
1871
  } else if (nextNode && !(property in target)) {
1828
- target[property] = rec(nextPath, nextNode, false, proxyRoot, undefined);
1872
+ target[property] = rec(nextPath, nextNode, false, undefined);
1829
1873
  }
1830
1874
  return Reflect.get(target, property, receiver);
1831
1875
  },
1832
1876
  set: function set$1(target, property, value, receiver) {
1833
1877
  var _nextNode, _nextNode2;
1834
- proxyRoot = proxyRoot == null ? receiver : proxyRoot;
1835
1878
  var nextPath = getNextPath(property);
1836
1879
  var nextNode = currNode.makeTransition(property);
1837
1880
  if (property === "registerInitFunc" && currPath.length === 0) {
@@ -1844,7 +1887,7 @@ function create$StateProxy($$state, leafHandlers) {
1844
1887
  }
1845
1888
  if ((_nextNode = nextNode) != null && _nextNode.isLeaf()) {
1846
1889
  var _leafHandlers$set, _leafHandlers2;
1847
- (_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);
1848
1891
  }
1849
1892
  if (!isOutside && !currNode.isLeaf() && !nextNode) {
1850
1893
  // can't set an unknown field in $state
@@ -1856,7 +1899,7 @@ function create$StateProxy($$state, leafHandlers) {
1856
1899
  nextNode = currNode;
1857
1900
  }
1858
1901
  if (canProxy(value)) {
1859
- target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), proxyRoot, value);
1902
+ target[property] = rec(nextPath, nextNode, isOutside || currNode.isLeaf(), value);
1860
1903
  } else if (!isOutside && !currNode.isLeaf() && !((_nextNode2 = nextNode) != null && _nextNode2.isLeaf())) {
1861
1904
  throw new Error("inserting a primitive value into a non-leaf");
1862
1905
  } else {
@@ -1875,6 +1918,9 @@ function create$StateProxy($$state, leafHandlers) {
1875
1918
  };
1876
1919
  var baseObject = !isOutside && !currNode.isLeaf() ? currNode.hasArrayTransition() ? [] : {} : Array.isArray(initialObject) ? [] : Object.create(Object.getPrototypeOf(initialObject != null ? initialObject : {}));
1877
1920
  var proxyObj = new Proxy(baseObject, handlers);
1921
+ if (currPath.length === 0) {
1922
+ proxyRoot = proxyObj;
1923
+ }
1878
1924
  if (initialObject) {
1879
1925
  Reflect.ownKeys(initialObject).forEach(function (key) {
1880
1926
  var desc = Object.getOwnPropertyDescriptor(initialObject, key);
@@ -1887,62 +1933,90 @@ function create$StateProxy($$state, leafHandlers) {
1887
1933
  }
1888
1934
  return proxyObj;
1889
1935
  };
1890
- return rec([], $$state.rootSpecTree, false, undefined, undefined);
1936
+ return rec([], $$state.rootSpecTree, false, undefined);
1891
1937
  }
1892
1938
  var mkUntrackedValue = function mkUntrackedValue(o) {
1893
1939
  return o != null && typeof o === "object" ? ref(o) : o;
1894
1940
  };
1895
- function useDollarState(specs, props, $ctx) {
1941
+ function useDollarState(specs, props, $ctx, opts) {
1896
1942
  var $$state = React__default.useRef(function () {
1897
1943
  var rootSpecTree = buildTree(specs);
1898
1944
  return {
1899
1945
  rootSpecTree: rootSpecTree,
1900
- specTreeLeaves: getLeaves(rootSpecTree),
1946
+ specTreeLeaves: getStateCells(rootSpecTree),
1901
1947
  stateValues: proxy({}),
1902
1948
  props: {},
1903
1949
  ctx: {},
1904
- registrationsQueue: []
1950
+ specs: [],
1951
+ registrationsQueue: proxy([])
1905
1952
  };
1906
1953
  }()).current;
1907
1954
  $$state.props = props;
1908
1955
  $$state.ctx = $ctx != null ? $ctx : {};
1909
- var $state = React__default.useRef(Object.assign(create$StateProxy($$state, function (node, path, proxyRoot) {
1910
- if (!node.hasState(path)) {
1911
- node.createStateCell(path);
1912
- var spec = node.getSpec();
1913
- if (spec.initFunc) {
1914
- initializeStateValue($$state, node, path, proxyRoot);
1915
- } else if (!spec.valueProp) {
1916
- set(proxyRoot, path, spec.initVal);
1917
- }
1918
- }
1919
- return {
1920
- get: function get(target, property, receiver) {
1956
+ $$state.specs = specs;
1957
+ var create$State = function create$State() {
1958
+ var $state = Object.assign(create$StateProxy($$state, function (node, path) {
1959
+ if (!node.hasState(path)) {
1960
+ node.createStateCell(path);
1921
1961
  var spec = node.getSpec();
1922
- if (spec.valueProp) {
1923
- return $$state.props[spec.valueProp];
1924
- } else {
1925
- return Reflect.get(target, property, receiver);
1962
+ if (spec.initFunc) {
1963
+ initializeStateValue($$state, node, path, $state);
1964
+ } else if (!spec.valueProp) {
1965
+ set($state, path, spec.initVal);
1926
1966
  }
1927
1967
  }
1928
- };
1929
- }), {
1930
- registerInitFunc: function registerInitFunc(pathStr, f, repetitionIndex) {
1931
- var _findStateCell = findStateCell($$state.rootSpecTree, pathStr, repetitionIndex),
1932
- node = _findStateCell.node,
1933
- realPath = _findStateCell.realPath;
1934
- if (!node.hasState(realPath)) {
1935
- node.createStateCell(realPath);
1968
+ return {
1969
+ get: function get(target, property, receiver) {
1970
+ var spec = node.getSpec();
1971
+ if (spec.valueProp) {
1972
+ return $$state.props[spec.valueProp];
1973
+ } else {
1974
+ return Reflect.get(target, property, receiver);
1975
+ }
1976
+ }
1977
+ };
1978
+ }), {
1979
+ registerInitFunc: function registerInitFunc(pathStr, f, repetitionIndex) {
1980
+ var _findStateCell = findStateCell($$state.rootSpecTree, pathStr, repetitionIndex),
1981
+ node = _findStateCell.node,
1982
+ realPath = _findStateCell.realPath;
1983
+ if (!node.hasState(realPath)) {
1984
+ node.createStateCell(realPath);
1985
+ }
1986
+ if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
1987
+ $$state.registrationsQueue.push(mkUntrackedValue({
1988
+ node: node,
1989
+ path: realPath,
1990
+ f: f
1991
+ }));
1992
+ }
1936
1993
  }
1937
- if (!deepEqual(node.getState(realPath).initialValue, f($$state.props, $state, $$state.ctx))) {
1938
- $$state.registrationsQueue.push({
1939
- node: node,
1940
- path: realPath,
1941
- f: f
1942
- });
1994
+ });
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;
1943
2014
  }
1944
- }
1945
- })).current;
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
+ }
1946
2020
  // For each spec with an initFunc, evaluate it and see if
1947
2021
  // the init value has changed. If so, reset its state.
1948
2022
  var resetSpecs = [];
@@ -1954,10 +2028,11 @@ function useDollarState(specs, props, $ctx) {
1954
2028
  };
1955
2029
  });
1956
2030
  }).forEach(function (_ref2) {
1957
- var stateCell = _ref2.stateCell,
1958
- node = _ref2.node;
1959
- if (stateCell.registeredInitFunc) {
1960
- 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 : {});
1961
2036
  if (!deepEqual(newInit, stateCell.initialValue)) {
1962
2037
  resetSpecs.push({
1963
2038
  stateCell: stateCell,
@@ -1982,16 +2057,16 @@ function useDollarState(specs, props, $ctx) {
1982
2057
  });
1983
2058
  }, [props, resetSpecs]);
1984
2059
  useIsomorphicLayoutEffect$1(function () {
1985
- $$state.registrationsQueue.forEach(function (_ref4) {
1986
- var node = _ref4.node,
1987
- path = _ref4.path,
1988
- 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;
1989
2065
  var stateCell = node.getState(path);
1990
2066
  stateCell.registeredInitFunc = f;
1991
2067
  reInitializeState(node, stateCell);
1992
- });
1993
- $$state.registrationsQueue = [];
1994
- }, [$$state.registrationsQueue]);
2068
+ }
2069
+ }, [$$state.registrationsQueue.length]);
1995
2070
  // immediately initialize exposed non-private states
1996
2071
  useIsomorphicLayoutEffect$1(function () {
1997
2072
  $$state.specTreeLeaves.forEach(function (node) {
@@ -2006,6 +2081,7 @@ function useDollarState(specs, props, $ctx) {
2006
2081
  useSnapshot($$state.stateValues, {
2007
2082
  sync: true
2008
2083
  });
2084
+ useSnapshot($$state.registrationsQueue);
2009
2085
  return $state;
2010
2086
  }
2011
2087