@plasmicapp/loader-react 1.0.93 → 1.0.94

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.
@@ -4,6 +4,7 @@ import * as PlasmicQuery from '@plasmicapp/query';
4
4
  import { PlasmicQueryDataProvider, PlasmicPrepassContext } from '@plasmicapp/query';
5
5
  export { usePlasmicQueryData } from '@plasmicapp/query';
6
6
  import { getBundleSubset, Registry, PlasmicModulesFetcher } from '@plasmicapp/loader-core';
7
+ import { getActiveVariation } from '@plasmicapp/loader-splits';
7
8
  import React__default, { useState, useCallback, useEffect, useRef, useMemo, createElement, memo, useContext, createContext } from 'react';
8
9
  import ReactDOM from 'react-dom';
9
10
  import * as jsxDevRuntime from 'react/jsx-dev-runtime';
@@ -190,6 +191,8 @@ function prepComponentData(bundle) {
190
191
  };
191
192
  }
192
193
  function mergeBundles(target, from) {
194
+ var _from$activeSplits$fi;
195
+
193
196
  var existingCompIds = new Set(target.components.map(function (c) {
194
197
  return c.id;
195
198
  }));
@@ -266,6 +269,19 @@ function mergeBundles(target, from) {
266
269
  });
267
270
  }
268
271
 
272
+ var existingSplitIds = new Set(target.activeSplits.map(function (s) {
273
+ return s.id;
274
+ }));
275
+ var newSplits = (_from$activeSplits$fi = from.activeSplits.filter(function (s) {
276
+ return !existingSplitIds.has(s.id);
277
+ })) != null ? _from$activeSplits$fi : [];
278
+
279
+ if (newSplits.length > 0) {
280
+ target = _extends({}, target, {
281
+ activeSplits: [].concat(target.activeSplits, newSplits)
282
+ });
283
+ }
284
+
269
285
  return target;
270
286
  }
271
287
  var convertBundlesToComponentRenderData = function convertBundlesToComponentRenderData(bundles, compMetas) {
@@ -1219,6 +1235,69 @@ var ComponentLookup = /*#__PURE__*/function () {
1219
1235
  return ComponentLookup;
1220
1236
  }();
1221
1237
 
1238
+ function getPlasmicCookieValues() {
1239
+ return Object.fromEntries(document.cookie.split('; ').filter(function (cookie) {
1240
+ return cookie.includes('plasmic:');
1241
+ }).map(function (cookie) {
1242
+ return cookie.split('=');
1243
+ }).map(function (_ref) {
1244
+ var key = _ref[0],
1245
+ value = _ref[1];
1246
+ return [key.split(':')[1], value];
1247
+ }));
1248
+ }
1249
+ function updatePlasmicCookieValue(key, value) {
1250
+ document.cookie = "plasmic:" + key + "=" + value;
1251
+ }
1252
+ var getGlobalVariantsFromSplits = function getGlobalVariantsFromSplits(splits, variation) {
1253
+ var globalVariants = [];
1254
+ Object.keys(variation).map(function (variationKey) {
1255
+ var _variationKey$split = variationKey.split('.'),
1256
+ splitId = _variationKey$split[1];
1257
+
1258
+ var sliceId = variation[variationKey];
1259
+ var split = splits.find(function (s) {
1260
+ return s.id === splitId || s.externalId === splitId;
1261
+ });
1262
+
1263
+ if (split) {
1264
+ var slice = split.slices.find(function (s) {
1265
+ return s.id === sliceId || s.externalId === sliceId;
1266
+ });
1267
+
1268
+ if (slice) {
1269
+ slice.contents.map(function (x) {
1270
+ globalVariants.push({
1271
+ name: x.group,
1272
+ value: x.variant,
1273
+ projectId: x.projectId
1274
+ });
1275
+ });
1276
+ }
1277
+ }
1278
+ });
1279
+ return globalVariants;
1280
+ };
1281
+ var mergeGlobalVariantsSpec = function mergeGlobalVariantsSpec(target, from) {
1282
+ var result = [].concat(target);
1283
+ var existingGlobalVariants = new Set(target.map(function (t) {
1284
+ var _t$projectId;
1285
+
1286
+ return t.name + "-" + ((_t$projectId = t.projectId) != null ? _t$projectId : '');
1287
+ }));
1288
+ var newGlobals = from.filter(function (t) {
1289
+ var _t$projectId2;
1290
+
1291
+ return !existingGlobalVariants.has(t.name + "-" + ((_t$projectId2 = t.projectId) != null ? _t$projectId2 : ''));
1292
+ });
1293
+
1294
+ if (newGlobals.length > 0) {
1295
+ result = [].concat(result, newGlobals);
1296
+ }
1297
+
1298
+ return result;
1299
+ };
1300
+
1222
1301
  var PlasmicRootContext = /*#__PURE__*/createContext(undefined);
1223
1302
  /**
1224
1303
  * PlasmicRootProvider should be used at the root of your page
@@ -1233,20 +1312,41 @@ function PlasmicRootProvider(props) {
1233
1312
  skipFonts = props.skipFonts,
1234
1313
  prefetchedQueryData = props.prefetchedQueryData,
1235
1314
  suspenseForQueryData = props.suspenseForQueryData,
1236
- globalContextsProps = props.globalContextsProps;
1315
+ globalContextsProps = props.globalContextsProps,
1316
+ variation = props.variation;
1237
1317
  var loader = props.loader.__internal;
1238
1318
 
1239
1319
  if (prefetchedData) {
1240
1320
  loader.registerPrefetchedBundle(prefetchedData == null ? void 0 : prefetchedData.bundle);
1241
1321
  }
1242
1322
 
1323
+ var _React$useState = useState(loader.getActiveSplits()),
1324
+ splits = _React$useState[0],
1325
+ setSplits = _React$useState[1];
1326
+
1327
+ var forceUpdate = useForceUpdate();
1328
+ var watcher = useMemo(function () {
1329
+ return {
1330
+ onDataFetched: function onDataFetched() {
1331
+ setSplits(loader.getActiveSplits());
1332
+ forceUpdate();
1333
+ }
1334
+ };
1335
+ }, [loader, forceUpdate]);
1336
+ useEffect(function () {
1337
+ loader.subscribePlasmicRoot(watcher);
1338
+ return function () {
1339
+ return loader.unsubscribePlasmicRoot(watcher);
1340
+ };
1341
+ }, [watcher, loader]);
1243
1342
  var value = useMemo(function () {
1244
1343
  return {
1245
- globalVariants: globalVariants,
1344
+ globalVariants: mergeGlobalVariantsSpec(globalVariants != null ? globalVariants : [], getGlobalVariantsFromSplits(splits, variation != null ? variation : {})),
1246
1345
  globalContextsProps: globalContextsProps,
1247
- loader: loader
1346
+ loader: loader,
1347
+ variation: variation
1248
1348
  };
1249
- }, [globalVariants, globalContextsProps, loader]);
1349
+ }, [globalVariants, variation, globalContextsProps, loader, splits]);
1250
1350
  return createElement(PlasmicQueryDataProvider, {
1251
1351
  prefetchedCache: prefetchedQueryData,
1252
1352
  suspense: suspenseForQueryData
@@ -1268,9 +1368,9 @@ var PlasmicCss = /*#__PURE__*/memo(function PlasmicCss(props) {
1268
1368
  prefetchedData = props.prefetchedData,
1269
1369
  skipFonts = props.skipFonts;
1270
1370
 
1271
- var _React$useState = useState(!!prefetchedData),
1272
- useScopedCss = _React$useState[0],
1273
- setUseScopedCss = _React$useState[1];
1371
+ var _React$useState2 = useState(!!prefetchedData),
1372
+ useScopedCss = _React$useState2[0],
1373
+ setUseScopedCss = _React$useState2[1];
1274
1374
 
1275
1375
  var builtCss = buildCss(loader, {
1276
1376
  scopedCompMetas: useScopedCss && prefetchedData ? prefetchedData.bundle.components : undefined,
@@ -1389,7 +1489,8 @@ var InternalPlasmicComponentLoader = /*#__PURE__*/function () {
1389
1489
  components: [],
1390
1490
  globalGroups: [],
1391
1491
  external: [],
1392
- projects: []
1492
+ projects: [],
1493
+ activeSplits: []
1393
1494
  };
1394
1495
  this.registry = Registry.getInstance();
1395
1496
  this.fetcher = new PlasmicModulesFetcher(opts);
@@ -1486,7 +1587,8 @@ var InternalPlasmicComponentLoader = /*#__PURE__*/function () {
1486
1587
  components: [],
1487
1588
  globalGroups: [],
1488
1589
  external: [],
1489
- projects: []
1590
+ projects: [],
1591
+ activeSplits: []
1490
1592
  };
1491
1593
  this.registry.clear();
1492
1594
  };
@@ -1714,6 +1816,10 @@ var InternalPlasmicComponentLoader = /*#__PURE__*/function () {
1714
1816
 
1715
1817
  _proto.getLookup = function getLookup() {
1716
1818
  return new ComponentLookup(this.bundle, this.registry);
1819
+ };
1820
+
1821
+ _proto.getActiveSplits = function getActiveSplits() {
1822
+ return this.bundle.activeSplits;
1717
1823
  } // @ts-ignore
1718
1824
  ;
1719
1825
 
@@ -1760,30 +1866,59 @@ var InternalPlasmicComponentLoader = /*#__PURE__*/function () {
1760
1866
  }
1761
1867
  };
1762
1868
 
1763
- _proto.fetchAllData = /*#__PURE__*/function () {
1764
- var _fetchAllData = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee7() {
1765
- var bundle;
1869
+ _proto.getActiveVariation = /*#__PURE__*/function () {
1870
+ var _getActiveVariation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee7(opts) {
1766
1871
  return runtime_1.wrap(function _callee7$(_context7) {
1767
1872
  while (1) {
1768
1873
  switch (_context7.prev = _context7.next) {
1769
1874
  case 0:
1770
1875
  _context7.next = 2;
1876
+ return this.fetchAllData();
1877
+
1878
+ case 2:
1879
+ return _context7.abrupt("return", getActiveVariation(_extends({}, opts, {
1880
+ splits: this.bundle.activeSplits
1881
+ })));
1882
+
1883
+ case 3:
1884
+ case "end":
1885
+ return _context7.stop();
1886
+ }
1887
+ }
1888
+ }, _callee7, this);
1889
+ }));
1890
+
1891
+ function getActiveVariation$1(_x3) {
1892
+ return _getActiveVariation2.apply(this, arguments);
1893
+ }
1894
+
1895
+ return getActiveVariation$1;
1896
+ }();
1897
+
1898
+ _proto.fetchAllData = /*#__PURE__*/function () {
1899
+ var _fetchAllData = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee8() {
1900
+ var bundle;
1901
+ return runtime_1.wrap(function _callee8$(_context8) {
1902
+ while (1) {
1903
+ switch (_context8.prev = _context8.next) {
1904
+ case 0:
1905
+ _context8.next = 2;
1771
1906
  return this.ensureFetcher().fetchAllData();
1772
1907
 
1773
1908
  case 2:
1774
- bundle = _context7.sent;
1909
+ bundle = _context8.sent;
1775
1910
  this.mergeBundle(bundle);
1776
1911
  this.roots.forEach(function (watcher) {
1777
1912
  return watcher.onDataFetched == null ? void 0 : watcher.onDataFetched();
1778
1913
  });
1779
- return _context7.abrupt("return", bundle);
1914
+ return _context8.abrupt("return", bundle);
1780
1915
 
1781
1916
  case 6:
1782
1917
  case "end":
1783
- return _context7.stop();
1918
+ return _context8.stop();
1784
1919
  }
1785
1920
  }
1786
- }, _callee7, this);
1921
+ }, _callee8, this);
1787
1922
  }));
1788
1923
 
1789
1924
  function fetchAllData() {
@@ -1943,22 +2078,22 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
1943
2078
  _proto2.fetchComponentData =
1944
2079
  /*#__PURE__*/
1945
2080
  function () {
1946
- var _fetchComponentData2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee8() {
2081
+ var _fetchComponentData2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee9() {
1947
2082
  var _this$__internal;
1948
2083
 
1949
- var _args8 = arguments;
1950
- return runtime_1.wrap(function _callee8$(_context8) {
2084
+ var _args9 = arguments;
2085
+ return runtime_1.wrap(function _callee9$(_context9) {
1951
2086
  while (1) {
1952
- switch (_context8.prev = _context8.next) {
2087
+ switch (_context9.prev = _context9.next) {
1953
2088
  case 0:
1954
- return _context8.abrupt("return", (_this$__internal = this.__internal).fetchComponentData.apply(_this$__internal, _args8));
2089
+ return _context9.abrupt("return", (_this$__internal = this.__internal).fetchComponentData.apply(_this$__internal, _args9));
1955
2090
 
1956
2091
  case 1:
1957
2092
  case "end":
1958
- return _context8.stop();
2093
+ return _context9.stop();
1959
2094
  }
1960
2095
  }
1961
- }, _callee8, this);
2096
+ }, _callee9, this);
1962
2097
  }));
1963
2098
 
1964
2099
  function fetchComponentData() {
@@ -1977,22 +2112,22 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
1977
2112
  _proto2.maybeFetchComponentData =
1978
2113
  /*#__PURE__*/
1979
2114
  function () {
1980
- var _maybeFetchComponentData2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee9() {
2115
+ var _maybeFetchComponentData2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee10() {
1981
2116
  var _this$__internal2;
1982
2117
 
1983
- var _args9 = arguments;
1984
- return runtime_1.wrap(function _callee9$(_context9) {
2118
+ var _args10 = arguments;
2119
+ return runtime_1.wrap(function _callee10$(_context10) {
1985
2120
  while (1) {
1986
- switch (_context9.prev = _context9.next) {
2121
+ switch (_context10.prev = _context10.next) {
1987
2122
  case 0:
1988
- return _context9.abrupt("return", (_this$__internal2 = this.__internal).maybeFetchComponentData.apply(_this$__internal2, _args9));
2123
+ return _context10.abrupt("return", (_this$__internal2 = this.__internal).maybeFetchComponentData.apply(_this$__internal2, _args10));
1989
2124
 
1990
2125
  case 1:
1991
2126
  case "end":
1992
- return _context9.stop();
2127
+ return _context10.stop();
1993
2128
  }
1994
2129
  }
1995
- }, _callee9, this);
2130
+ }, _callee10, this);
1996
2131
  }));
1997
2132
 
1998
2133
  function maybeFetchComponentData() {
@@ -2009,19 +2144,19 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
2009
2144
  _proto2.fetchPages =
2010
2145
  /*#__PURE__*/
2011
2146
  function () {
2012
- var _fetchPages2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee10() {
2013
- return runtime_1.wrap(function _callee10$(_context10) {
2147
+ var _fetchPages2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee11() {
2148
+ return runtime_1.wrap(function _callee11$(_context11) {
2014
2149
  while (1) {
2015
- switch (_context10.prev = _context10.next) {
2150
+ switch (_context11.prev = _context11.next) {
2016
2151
  case 0:
2017
- return _context10.abrupt("return", this.__internal.fetchPages());
2152
+ return _context11.abrupt("return", this.__internal.fetchPages());
2018
2153
 
2019
2154
  case 1:
2020
2155
  case "end":
2021
- return _context10.stop();
2156
+ return _context11.stop();
2022
2157
  }
2023
2158
  }
2024
- }, _callee10, this);
2159
+ }, _callee11, this);
2025
2160
  }));
2026
2161
 
2027
2162
  function fetchPages() {
@@ -2038,19 +2173,19 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
2038
2173
  _proto2.fetchComponents =
2039
2174
  /*#__PURE__*/
2040
2175
  function () {
2041
- var _fetchComponents2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee11() {
2042
- return runtime_1.wrap(function _callee11$(_context11) {
2176
+ var _fetchComponents2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee12() {
2177
+ return runtime_1.wrap(function _callee12$(_context12) {
2043
2178
  while (1) {
2044
- switch (_context11.prev = _context11.next) {
2179
+ switch (_context12.prev = _context12.next) {
2045
2180
  case 0:
2046
- return _context11.abrupt("return", this.__internal.fetchComponents());
2181
+ return _context12.abrupt("return", this.__internal.fetchComponents());
2047
2182
 
2048
2183
  case 1:
2049
2184
  case "end":
2050
- return _context11.stop();
2185
+ return _context12.stop();
2051
2186
  }
2052
2187
  }
2053
- }, _callee11, this);
2188
+ }, _callee12, this);
2054
2189
  }));
2055
2190
 
2056
2191
  function fetchComponents() {
@@ -2060,6 +2195,71 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
2060
2195
  return fetchComponents;
2061
2196
  }();
2062
2197
 
2198
+ _proto2._getActiveVariation = /*#__PURE__*/function () {
2199
+ var _getActiveVariation3 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee13(opts) {
2200
+ return runtime_1.wrap(function _callee13$(_context13) {
2201
+ while (1) {
2202
+ switch (_context13.prev = _context13.next) {
2203
+ case 0:
2204
+ return _context13.abrupt("return", this.__internal.getActiveVariation(opts));
2205
+
2206
+ case 1:
2207
+ case "end":
2208
+ return _context13.stop();
2209
+ }
2210
+ }
2211
+ }, _callee13, this);
2212
+ }));
2213
+
2214
+ function _getActiveVariation(_x4) {
2215
+ return _getActiveVariation3.apply(this, arguments);
2216
+ }
2217
+
2218
+ return _getActiveVariation;
2219
+ }();
2220
+
2221
+ _proto2.getActiveVariation = /*#__PURE__*/function () {
2222
+ var _getActiveVariation4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee14(opts) {
2223
+ return runtime_1.wrap(function _callee14$(_context14) {
2224
+ while (1) {
2225
+ switch (_context14.prev = _context14.next) {
2226
+ case 0:
2227
+ return _context14.abrupt("return", this._getActiveVariation({
2228
+ traits: opts.traits,
2229
+ getKnownValue: function getKnownValue(key) {
2230
+ if (opts.known) {
2231
+ return opts.known[key];
2232
+ } else {
2233
+ var cookies = getPlasmicCookieValues();
2234
+ return cookies[key];
2235
+ }
2236
+ },
2237
+ updateKnownValue: function updateKnownValue(key, value) {
2238
+ if (!opts.known) {
2239
+ updatePlasmicCookieValue(key, value);
2240
+ }
2241
+ }
2242
+ }));
2243
+
2244
+ case 1:
2245
+ case "end":
2246
+ return _context14.stop();
2247
+ }
2248
+ }
2249
+ }, _callee14, this);
2250
+ }));
2251
+
2252
+ function getActiveVariation(_x5) {
2253
+ return _getActiveVariation4.apply(this, arguments);
2254
+ }
2255
+
2256
+ return getActiveVariation;
2257
+ }();
2258
+
2259
+ _proto2.getActiveSplits = function getActiveSplits() {
2260
+ return this.__internal.getActiveSplits();
2261
+ };
2262
+
2063
2263
  _proto2.clearCache = function clearCache() {
2064
2264
  return this.__internal.clearCache();
2065
2265
  };
@@ -2458,5 +2658,5 @@ function makeElement(loader, lookup, opts) {
2458
2658
  }));
2459
2659
  }
2460
2660
 
2461
- export { PlasmicComponent, PlasmicComponentLoader, PlasmicRootProvider, convertBundlesToComponentRenderData, extractPlasmicQueryData, hydrateFromElement, initPlasmicLoader, plasmicPrepass, renderToElement, renderToString, usePlasmicComponent };
2661
+ export { InternalPlasmicComponentLoader, PlasmicComponent, PlasmicComponentLoader, PlasmicRootProvider, convertBundlesToComponentRenderData, extractPlasmicQueryData, hydrateFromElement, initPlasmicLoader, plasmicPrepass, renderToElement, renderToString, usePlasmicComponent };
2462
2662
  //# sourceMappingURL=loader-react.esm.js.map