@plasmicapp/loader-react 1.0.93 → 1.0.96

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 @@ interface PlasmicRootContextValue {
4
4
  globalVariants?: GlobalVariantSpec[];
5
5
  globalContextsProps?: Record<string, any>;
6
6
  loader: InternalPlasmicComponentLoader;
7
+ variation?: Record<string, string>;
7
8
  }
8
9
  export interface GlobalVariantSpec {
9
10
  name: string;
@@ -55,6 +56,10 @@ export declare function PlasmicRootProvider(props: {
55
56
  * component.
56
57
  */
57
58
  globalContextsProps?: Record<string, any>;
59
+ /**
60
+ * Specifies a mapping of split id to slice id that should be activated
61
+ */
62
+ variation?: Record<string, string>;
58
63
  }): JSX.Element;
59
64
  export declare function usePlasmicRootContext(): PlasmicRootContextValue | undefined;
60
65
  export {};
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export { PlasmicCanvasContext, PlasmicCanvasHost, PrimitiveType, PropType, repea
2
2
  export { ComponentMeta, PageMeta, PageMetadata } from '@plasmicapp/loader-core';
3
3
  export { usePlasmicQueryData } from '@plasmicapp/query';
4
4
  export { convertBundlesToComponentRenderData } from './bundles';
5
- export { ComponentRenderData, InitOptions, initPlasmicLoader, PlasmicComponentLoader, } from './loader';
5
+ export { ComponentRenderData, InitOptions, initPlasmicLoader, InternalPlasmicComponentLoader, PlasmicComponentLoader, } from './loader';
6
6
  export { PlasmicComponent } from './PlasmicComponent';
7
7
  export { GlobalVariantSpec, PlasmicRootProvider } from './PlasmicRootProvider';
8
8
  export { extractPlasmicQueryData, plasmicPrepass } from './prepass';
@@ -7,6 +7,7 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
7
7
  var host = require('@plasmicapp/host');
8
8
  var PlasmicQuery = require('@plasmicapp/query');
9
9
  var loaderCore = require('@plasmicapp/loader-core');
10
+ var loaderSplits = require('@plasmicapp/loader-splits');
10
11
  var React = require('react');
11
12
  var React__default = _interopDefault(React);
12
13
  var ReactDOM = _interopDefault(require('react-dom'));
@@ -194,6 +195,8 @@ function prepComponentData(bundle) {
194
195
  };
195
196
  }
196
197
  function mergeBundles(target, from) {
198
+ var _from$activeSplits$fi;
199
+
197
200
  var existingCompIds = new Set(target.components.map(function (c) {
198
201
  return c.id;
199
202
  }));
@@ -270,6 +273,19 @@ function mergeBundles(target, from) {
270
273
  });
271
274
  }
272
275
 
276
+ var existingSplitIds = new Set(target.activeSplits.map(function (s) {
277
+ return s.id;
278
+ }));
279
+ var newSplits = (_from$activeSplits$fi = from.activeSplits.filter(function (s) {
280
+ return !existingSplitIds.has(s.id);
281
+ })) != null ? _from$activeSplits$fi : [];
282
+
283
+ if (newSplits.length > 0) {
284
+ target = _extends({}, target, {
285
+ activeSplits: [].concat(target.activeSplits, newSplits)
286
+ });
287
+ }
288
+
273
289
  return target;
274
290
  }
275
291
  var convertBundlesToComponentRenderData = function convertBundlesToComponentRenderData(bundles, compMetas) {
@@ -1223,6 +1239,69 @@ var ComponentLookup = /*#__PURE__*/function () {
1223
1239
  return ComponentLookup;
1224
1240
  }();
1225
1241
 
1242
+ function getPlasmicCookieValues() {
1243
+ return Object.fromEntries(document.cookie.split('; ').filter(function (cookie) {
1244
+ return cookie.includes('plasmic:');
1245
+ }).map(function (cookie) {
1246
+ return cookie.split('=');
1247
+ }).map(function (_ref) {
1248
+ var key = _ref[0],
1249
+ value = _ref[1];
1250
+ return [key.split(':')[1], value];
1251
+ }));
1252
+ }
1253
+ function updatePlasmicCookieValue(key, value) {
1254
+ document.cookie = "plasmic:" + key + "=" + value;
1255
+ }
1256
+ var getGlobalVariantsFromSplits = function getGlobalVariantsFromSplits(splits, variation) {
1257
+ var globalVariants = [];
1258
+ Object.keys(variation).map(function (variationKey) {
1259
+ var _variationKey$split = variationKey.split('.'),
1260
+ splitId = _variationKey$split[1];
1261
+
1262
+ var sliceId = variation[variationKey];
1263
+ var split = splits.find(function (s) {
1264
+ return s.id === splitId || s.externalId === splitId;
1265
+ });
1266
+
1267
+ if (split) {
1268
+ var slice = split.slices.find(function (s) {
1269
+ return s.id === sliceId || s.externalId === sliceId;
1270
+ });
1271
+
1272
+ if (slice) {
1273
+ slice.contents.map(function (x) {
1274
+ globalVariants.push({
1275
+ name: x.group,
1276
+ value: x.variant,
1277
+ projectId: x.projectId
1278
+ });
1279
+ });
1280
+ }
1281
+ }
1282
+ });
1283
+ return globalVariants;
1284
+ };
1285
+ var mergeGlobalVariantsSpec = function mergeGlobalVariantsSpec(target, from) {
1286
+ var result = [].concat(target);
1287
+ var existingGlobalVariants = new Set(target.map(function (t) {
1288
+ var _t$projectId;
1289
+
1290
+ return t.name + "-" + ((_t$projectId = t.projectId) != null ? _t$projectId : '');
1291
+ }));
1292
+ var newGlobals = from.filter(function (t) {
1293
+ var _t$projectId2;
1294
+
1295
+ return !existingGlobalVariants.has(t.name + "-" + ((_t$projectId2 = t.projectId) != null ? _t$projectId2 : ''));
1296
+ });
1297
+
1298
+ if (newGlobals.length > 0) {
1299
+ result = [].concat(result, newGlobals);
1300
+ }
1301
+
1302
+ return result;
1303
+ };
1304
+
1226
1305
  var PlasmicRootContext = /*#__PURE__*/React.createContext(undefined);
1227
1306
  /**
1228
1307
  * PlasmicRootProvider should be used at the root of your page
@@ -1237,20 +1316,41 @@ function PlasmicRootProvider(props) {
1237
1316
  skipFonts = props.skipFonts,
1238
1317
  prefetchedQueryData = props.prefetchedQueryData,
1239
1318
  suspenseForQueryData = props.suspenseForQueryData,
1240
- globalContextsProps = props.globalContextsProps;
1319
+ globalContextsProps = props.globalContextsProps,
1320
+ variation = props.variation;
1241
1321
  var loader = props.loader.__internal;
1242
1322
 
1243
1323
  if (prefetchedData) {
1244
1324
  loader.registerPrefetchedBundle(prefetchedData == null ? void 0 : prefetchedData.bundle);
1245
1325
  }
1246
1326
 
1327
+ var _React$useState = React.useState(loader.getActiveSplits()),
1328
+ splits = _React$useState[0],
1329
+ setSplits = _React$useState[1];
1330
+
1331
+ var forceUpdate = useForceUpdate();
1332
+ var watcher = React.useMemo(function () {
1333
+ return {
1334
+ onDataFetched: function onDataFetched() {
1335
+ setSplits(loader.getActiveSplits());
1336
+ forceUpdate();
1337
+ }
1338
+ };
1339
+ }, [loader, forceUpdate]);
1340
+ React.useEffect(function () {
1341
+ loader.subscribePlasmicRoot(watcher);
1342
+ return function () {
1343
+ return loader.unsubscribePlasmicRoot(watcher);
1344
+ };
1345
+ }, [watcher, loader]);
1247
1346
  var value = React.useMemo(function () {
1248
1347
  return {
1249
- globalVariants: globalVariants,
1348
+ globalVariants: mergeGlobalVariantsSpec(globalVariants != null ? globalVariants : [], getGlobalVariantsFromSplits(splits, variation != null ? variation : {})),
1250
1349
  globalContextsProps: globalContextsProps,
1251
- loader: loader
1350
+ loader: loader,
1351
+ variation: variation
1252
1352
  };
1253
- }, [globalVariants, globalContextsProps, loader]);
1353
+ }, [globalVariants, variation, globalContextsProps, loader, splits]);
1254
1354
  return React.createElement(PlasmicQuery.PlasmicQueryDataProvider, {
1255
1355
  prefetchedCache: prefetchedQueryData,
1256
1356
  suspense: suspenseForQueryData
@@ -1272,9 +1372,9 @@ var PlasmicCss = /*#__PURE__*/React.memo(function PlasmicCss(props) {
1272
1372
  prefetchedData = props.prefetchedData,
1273
1373
  skipFonts = props.skipFonts;
1274
1374
 
1275
- var _React$useState = React.useState(!!prefetchedData),
1276
- useScopedCss = _React$useState[0],
1277
- setUseScopedCss = _React$useState[1];
1375
+ var _React$useState2 = React.useState(!!prefetchedData),
1376
+ useScopedCss = _React$useState2[0],
1377
+ setUseScopedCss = _React$useState2[1];
1278
1378
 
1279
1379
  var builtCss = buildCss(loader, {
1280
1380
  scopedCompMetas: useScopedCss && prefetchedData ? prefetchedData.bundle.components : undefined,
@@ -1393,7 +1493,8 @@ var InternalPlasmicComponentLoader = /*#__PURE__*/function () {
1393
1493
  components: [],
1394
1494
  globalGroups: [],
1395
1495
  external: [],
1396
- projects: []
1496
+ projects: [],
1497
+ activeSplits: []
1397
1498
  };
1398
1499
  this.registry = loaderCore.Registry.getInstance();
1399
1500
  this.fetcher = new loaderCore.PlasmicModulesFetcher(opts);
@@ -1490,7 +1591,8 @@ var InternalPlasmicComponentLoader = /*#__PURE__*/function () {
1490
1591
  components: [],
1491
1592
  globalGroups: [],
1492
1593
  external: [],
1493
- projects: []
1594
+ projects: [],
1595
+ activeSplits: []
1494
1596
  };
1495
1597
  this.registry.clear();
1496
1598
  };
@@ -1718,6 +1820,10 @@ var InternalPlasmicComponentLoader = /*#__PURE__*/function () {
1718
1820
 
1719
1821
  _proto.getLookup = function getLookup() {
1720
1822
  return new ComponentLookup(this.bundle, this.registry);
1823
+ };
1824
+
1825
+ _proto.getActiveSplits = function getActiveSplits() {
1826
+ return this.bundle.activeSplits;
1721
1827
  } // @ts-ignore
1722
1828
  ;
1723
1829
 
@@ -1764,30 +1870,59 @@ var InternalPlasmicComponentLoader = /*#__PURE__*/function () {
1764
1870
  }
1765
1871
  };
1766
1872
 
1767
- _proto.fetchAllData = /*#__PURE__*/function () {
1768
- var _fetchAllData = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee7() {
1769
- var bundle;
1873
+ _proto.getActiveVariation = /*#__PURE__*/function () {
1874
+ var _getActiveVariation2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee7(opts) {
1770
1875
  return runtime_1.wrap(function _callee7$(_context7) {
1771
1876
  while (1) {
1772
1877
  switch (_context7.prev = _context7.next) {
1773
1878
  case 0:
1774
1879
  _context7.next = 2;
1880
+ return this.fetchAllData();
1881
+
1882
+ case 2:
1883
+ return _context7.abrupt("return", loaderSplits.getActiveVariation(_extends({}, opts, {
1884
+ splits: this.bundle.activeSplits
1885
+ })));
1886
+
1887
+ case 3:
1888
+ case "end":
1889
+ return _context7.stop();
1890
+ }
1891
+ }
1892
+ }, _callee7, this);
1893
+ }));
1894
+
1895
+ function getActiveVariation(_x3) {
1896
+ return _getActiveVariation2.apply(this, arguments);
1897
+ }
1898
+
1899
+ return getActiveVariation;
1900
+ }();
1901
+
1902
+ _proto.fetchAllData = /*#__PURE__*/function () {
1903
+ var _fetchAllData = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee8() {
1904
+ var bundle;
1905
+ return runtime_1.wrap(function _callee8$(_context8) {
1906
+ while (1) {
1907
+ switch (_context8.prev = _context8.next) {
1908
+ case 0:
1909
+ _context8.next = 2;
1775
1910
  return this.ensureFetcher().fetchAllData();
1776
1911
 
1777
1912
  case 2:
1778
- bundle = _context7.sent;
1913
+ bundle = _context8.sent;
1779
1914
  this.mergeBundle(bundle);
1780
1915
  this.roots.forEach(function (watcher) {
1781
1916
  return watcher.onDataFetched == null ? void 0 : watcher.onDataFetched();
1782
1917
  });
1783
- return _context7.abrupt("return", bundle);
1918
+ return _context8.abrupt("return", bundle);
1784
1919
 
1785
1920
  case 6:
1786
1921
  case "end":
1787
- return _context7.stop();
1922
+ return _context8.stop();
1788
1923
  }
1789
1924
  }
1790
- }, _callee7, this);
1925
+ }, _callee8, this);
1791
1926
  }));
1792
1927
 
1793
1928
  function fetchAllData() {
@@ -1947,22 +2082,22 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
1947
2082
  _proto2.fetchComponentData =
1948
2083
  /*#__PURE__*/
1949
2084
  function () {
1950
- var _fetchComponentData2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee8() {
2085
+ var _fetchComponentData2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee9() {
1951
2086
  var _this$__internal;
1952
2087
 
1953
- var _args8 = arguments;
1954
- return runtime_1.wrap(function _callee8$(_context8) {
2088
+ var _args9 = arguments;
2089
+ return runtime_1.wrap(function _callee9$(_context9) {
1955
2090
  while (1) {
1956
- switch (_context8.prev = _context8.next) {
2091
+ switch (_context9.prev = _context9.next) {
1957
2092
  case 0:
1958
- return _context8.abrupt("return", (_this$__internal = this.__internal).fetchComponentData.apply(_this$__internal, _args8));
2093
+ return _context9.abrupt("return", (_this$__internal = this.__internal).fetchComponentData.apply(_this$__internal, _args9));
1959
2094
 
1960
2095
  case 1:
1961
2096
  case "end":
1962
- return _context8.stop();
2097
+ return _context9.stop();
1963
2098
  }
1964
2099
  }
1965
- }, _callee8, this);
2100
+ }, _callee9, this);
1966
2101
  }));
1967
2102
 
1968
2103
  function fetchComponentData() {
@@ -1981,22 +2116,22 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
1981
2116
  _proto2.maybeFetchComponentData =
1982
2117
  /*#__PURE__*/
1983
2118
  function () {
1984
- var _maybeFetchComponentData2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee9() {
2119
+ var _maybeFetchComponentData2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee10() {
1985
2120
  var _this$__internal2;
1986
2121
 
1987
- var _args9 = arguments;
1988
- return runtime_1.wrap(function _callee9$(_context9) {
2122
+ var _args10 = arguments;
2123
+ return runtime_1.wrap(function _callee10$(_context10) {
1989
2124
  while (1) {
1990
- switch (_context9.prev = _context9.next) {
2125
+ switch (_context10.prev = _context10.next) {
1991
2126
  case 0:
1992
- return _context9.abrupt("return", (_this$__internal2 = this.__internal).maybeFetchComponentData.apply(_this$__internal2, _args9));
2127
+ return _context10.abrupt("return", (_this$__internal2 = this.__internal).maybeFetchComponentData.apply(_this$__internal2, _args10));
1993
2128
 
1994
2129
  case 1:
1995
2130
  case "end":
1996
- return _context9.stop();
2131
+ return _context10.stop();
1997
2132
  }
1998
2133
  }
1999
- }, _callee9, this);
2134
+ }, _callee10, this);
2000
2135
  }));
2001
2136
 
2002
2137
  function maybeFetchComponentData() {
@@ -2013,19 +2148,19 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
2013
2148
  _proto2.fetchPages =
2014
2149
  /*#__PURE__*/
2015
2150
  function () {
2016
- var _fetchPages2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee10() {
2017
- return runtime_1.wrap(function _callee10$(_context10) {
2151
+ var _fetchPages2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee11() {
2152
+ return runtime_1.wrap(function _callee11$(_context11) {
2018
2153
  while (1) {
2019
- switch (_context10.prev = _context10.next) {
2154
+ switch (_context11.prev = _context11.next) {
2020
2155
  case 0:
2021
- return _context10.abrupt("return", this.__internal.fetchPages());
2156
+ return _context11.abrupt("return", this.__internal.fetchPages());
2022
2157
 
2023
2158
  case 1:
2024
2159
  case "end":
2025
- return _context10.stop();
2160
+ return _context11.stop();
2026
2161
  }
2027
2162
  }
2028
- }, _callee10, this);
2163
+ }, _callee11, this);
2029
2164
  }));
2030
2165
 
2031
2166
  function fetchPages() {
@@ -2042,19 +2177,19 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
2042
2177
  _proto2.fetchComponents =
2043
2178
  /*#__PURE__*/
2044
2179
  function () {
2045
- var _fetchComponents2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee11() {
2046
- return runtime_1.wrap(function _callee11$(_context11) {
2180
+ var _fetchComponents2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee12() {
2181
+ return runtime_1.wrap(function _callee12$(_context12) {
2047
2182
  while (1) {
2048
- switch (_context11.prev = _context11.next) {
2183
+ switch (_context12.prev = _context12.next) {
2049
2184
  case 0:
2050
- return _context11.abrupt("return", this.__internal.fetchComponents());
2185
+ return _context12.abrupt("return", this.__internal.fetchComponents());
2051
2186
 
2052
2187
  case 1:
2053
2188
  case "end":
2054
- return _context11.stop();
2189
+ return _context12.stop();
2055
2190
  }
2056
2191
  }
2057
- }, _callee11, this);
2192
+ }, _callee12, this);
2058
2193
  }));
2059
2194
 
2060
2195
  function fetchComponents() {
@@ -2064,6 +2199,71 @@ var PlasmicComponentLoader = /*#__PURE__*/function () {
2064
2199
  return fetchComponents;
2065
2200
  }();
2066
2201
 
2202
+ _proto2._getActiveVariation = /*#__PURE__*/function () {
2203
+ var _getActiveVariation3 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee13(opts) {
2204
+ return runtime_1.wrap(function _callee13$(_context13) {
2205
+ while (1) {
2206
+ switch (_context13.prev = _context13.next) {
2207
+ case 0:
2208
+ return _context13.abrupt("return", this.__internal.getActiveVariation(opts));
2209
+
2210
+ case 1:
2211
+ case "end":
2212
+ return _context13.stop();
2213
+ }
2214
+ }
2215
+ }, _callee13, this);
2216
+ }));
2217
+
2218
+ function _getActiveVariation(_x4) {
2219
+ return _getActiveVariation3.apply(this, arguments);
2220
+ }
2221
+
2222
+ return _getActiveVariation;
2223
+ }();
2224
+
2225
+ _proto2.getActiveVariation = /*#__PURE__*/function () {
2226
+ var _getActiveVariation4 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/runtime_1.mark(function _callee14(opts) {
2227
+ return runtime_1.wrap(function _callee14$(_context14) {
2228
+ while (1) {
2229
+ switch (_context14.prev = _context14.next) {
2230
+ case 0:
2231
+ return _context14.abrupt("return", this._getActiveVariation({
2232
+ traits: opts.traits,
2233
+ getKnownValue: function getKnownValue(key) {
2234
+ if (opts.known) {
2235
+ return opts.known[key];
2236
+ } else {
2237
+ var cookies = getPlasmicCookieValues();
2238
+ return cookies[key];
2239
+ }
2240
+ },
2241
+ updateKnownValue: function updateKnownValue(key, value) {
2242
+ if (!opts.known) {
2243
+ updatePlasmicCookieValue(key, value);
2244
+ }
2245
+ }
2246
+ }));
2247
+
2248
+ case 1:
2249
+ case "end":
2250
+ return _context14.stop();
2251
+ }
2252
+ }
2253
+ }, _callee14, this);
2254
+ }));
2255
+
2256
+ function getActiveVariation(_x5) {
2257
+ return _getActiveVariation4.apply(this, arguments);
2258
+ }
2259
+
2260
+ return getActiveVariation;
2261
+ }();
2262
+
2263
+ _proto2.getActiveSplits = function getActiveSplits() {
2264
+ return this.__internal.getActiveSplits();
2265
+ };
2266
+
2067
2267
  _proto2.clearCache = function clearCache() {
2068
2268
  return this.__internal.clearCache();
2069
2269
  };
@@ -2492,6 +2692,7 @@ Object.defineProperty(exports, 'usePlasmicQueryData', {
2492
2692
  return PlasmicQuery.usePlasmicQueryData;
2493
2693
  }
2494
2694
  });
2695
+ exports.InternalPlasmicComponentLoader = InternalPlasmicComponentLoader;
2495
2696
  exports.PlasmicComponent = PlasmicComponent;
2496
2697
  exports.PlasmicComponentLoader = PlasmicComponentLoader;
2497
2698
  exports.PlasmicRootProvider = PlasmicRootProvider;