@makeswift/runtime 0.6.4 → 0.6.6

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.
Files changed (62) hide show
  1. package/dist/Box.es.js +1 -1
  2. package/dist/Button.es.js +1 -1
  3. package/dist/Carousel.es.js +1 -1
  4. package/dist/Countdown.es.js +1 -1
  5. package/dist/Divider.es.js +1 -1
  6. package/dist/EditableText.es.js +1 -1
  7. package/dist/Embed.es.js +1 -1
  8. package/dist/Form.es.js +1 -1
  9. package/dist/Image.es.js +1 -1
  10. package/dist/LiveProvider.es.js +1 -1
  11. package/dist/Navigation.es.js +1 -1
  12. package/dist/PreviewProvider.cjs.js +15 -0
  13. package/dist/PreviewProvider.cjs.js.map +1 -1
  14. package/dist/PreviewProvider.es.js +16 -2
  15. package/dist/PreviewProvider.es.js.map +1 -1
  16. package/dist/ReadOnlyText.cjs.js +32 -3
  17. package/dist/ReadOnlyText.cjs.js.map +1 -1
  18. package/dist/ReadOnlyText.es.js +33 -4
  19. package/dist/ReadOnlyText.es.js.map +1 -1
  20. package/dist/Root.es.js +1 -1
  21. package/dist/SocialLinks.es.js +1 -1
  22. package/dist/Text.es.js +1 -1
  23. package/dist/Video.es.js +1 -1
  24. package/dist/actions.cjs.js +23 -1
  25. package/dist/actions.cjs.js.map +1 -1
  26. package/dist/actions.es.js +21 -2
  27. package/dist/actions.es.js.map +1 -1
  28. package/dist/components.cjs.js +1 -0
  29. package/dist/components.cjs.js.map +1 -1
  30. package/dist/components.es.js +1 -1
  31. package/dist/index.cjs.js +479 -17
  32. package/dist/index.cjs.js.map +1 -1
  33. package/dist/index.es.js +479 -19
  34. package/dist/index.es.js.map +1 -1
  35. package/dist/index.es2.js +1 -1
  36. package/dist/index.es3.js +1 -1
  37. package/dist/index.es4.js +1 -1
  38. package/dist/index.es6.js +1 -1
  39. package/dist/main.cjs.js +1 -0
  40. package/dist/main.cjs.js.map +1 -1
  41. package/dist/main.es.js +1 -1
  42. package/dist/next.cjs.js +1 -0
  43. package/dist/next.cjs.js.map +1 -1
  44. package/dist/next.es.js +1 -1
  45. package/dist/types/src/api/introspection.d.ts.map +1 -1
  46. package/dist/types/src/components/page/Page.d.ts +5 -1
  47. package/dist/types/src/components/page/Page.d.ts.map +1 -1
  48. package/dist/types/src/index.d.ts +1 -1
  49. package/dist/types/src/index.d.ts.map +1 -1
  50. package/dist/types/src/next/api-handler.d.ts +14 -2
  51. package/dist/types/src/next/api-handler.d.ts.map +1 -1
  52. package/dist/types/src/next/client.d.ts +69 -32
  53. package/dist/types/src/next/client.d.ts.map +1 -1
  54. package/dist/types/src/next/index.d.ts +5 -1
  55. package/dist/types/src/next/index.d.ts.map +1 -1
  56. package/dist/types/src/runtimes/react/index.d.ts.map +1 -1
  57. package/dist/types/src/state/actions.d.ts +19 -1
  58. package/dist/types/src/state/actions.d.ts.map +1 -1
  59. package/dist/types/src/state/modules/api-resources.d.ts +6 -2
  60. package/dist/types/src/state/modules/api-resources.d.ts.map +1 -1
  61. package/dist/types/src/state/react-builder-preview.d.ts.map +1 -1
  62. package/package.json +1 -1
package/dist/index.es.js CHANGED
@@ -40,7 +40,7 @@ import { useEffect, createContext, useContext, useState, useMemo, useRef, Childr
40
40
  import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector";
41
41
  import dynamic from "next/dynamic";
42
42
  import { g as getPropControllerDescriptors, i as isElementReference, a as getElementSwatchIds, b as getFileIds, d as getTypographyIds, e as getTableIds, f as getPageIds, h as getElementChildren, j as createDocumentReference, M as MakeswiftComponentType, k as getBorderSwatchIds, l as isNonNullable, m as getBoxShadowsSwatchIds, n as getResponsiveColorSwatchIds, o as isPropControllersHandle, p as getComponentPropControllerDescriptors, q as getPropControllers, r as configureStore$1, s as getDocument, t as getElementId, u as getIsPreview, v as getIsInBuilder, w as copyElementTree, x as getReactComponent, y as getBuilderEditMode } from "./constants.es.js";
43
- import { A as ActionTypes, g as introspectedResourcesFulfilled, h as apiResourceFulfilled, t as typographiesFulfilled, j as registerComponentHandleEffect, k as mountComponentEffect, l as registerComponentEffect, n as registerReactComponentEffect } from "./actions.es.js";
43
+ import { A as ActionTypes, h as introspectedResourcesFulfilled, j as apiResourceFulfilled, t as typographiesFulfilled, k as registerComponentHandleEffect, l as mountComponentEffect, n as registerComponentEffect, o as registerReactComponentEffect } from "./actions.es.js";
44
44
  import { cache, cx } from "@emotion/css";
45
45
  import { serializeStyles } from "@emotion/serialize";
46
46
  import { registerStyles, insertStyles } from "@emotion/utils";
@@ -119,6 +119,7 @@ async function introspect(element, client, store) {
119
119
  const tableIds = /* @__PURE__ */ new Set();
120
120
  const pageIds = /* @__PURE__ */ new Set();
121
121
  const remaining = [element];
122
+ const seen = /* @__PURE__ */ new Set();
122
123
  let current;
123
124
  while (current = remaining.pop()) {
124
125
  let getResourcesFromElementDescriptors = function(elementDescriptors2, props) {
@@ -130,7 +131,12 @@ async function introspect(element, client, store) {
130
131
  getTypographyIds(descriptor, props[propName]).forEach((typographyId) => typographyIds.add(typographyId));
131
132
  getTableIds(descriptor, props[propName]).forEach((tableId) => tableIds.add(tableId));
132
133
  getPageIds(descriptor, props[propName]).forEach((pageId) => pageIds.add(pageId));
133
- getElementChildren(descriptor, props[propName]).forEach((child) => remaining.push(child));
134
+ getElementChildren(descriptor, props[propName]).forEach((child) => {
135
+ if (!seen.has(child.key)) {
136
+ seen.add(child.key);
137
+ remaining.push(child);
138
+ }
139
+ });
134
140
  if (descriptor.type === ShapeControlType) {
135
141
  const prop = props[propName];
136
142
  if (prop == null)
@@ -230,14 +236,44 @@ const deepEqual = (a, b) => {
230
236
  }
231
237
  return true;
232
238
  };
233
- function getInitialState(serializedState = []) {
234
- return new Map(serializedState.map(([resourceType, resources]) => [resourceType, new Map(resources)]));
239
+ function getInitialState(serializedState = {
240
+ Swatch: [],
241
+ File: [],
242
+ Typography: [],
243
+ PagePathnameSlice: [],
244
+ GlobalElement: [],
245
+ Table: [],
246
+ Snippet: [],
247
+ Page: [],
248
+ Site: []
249
+ }) {
250
+ return new Map(Object.entries(serializedState).map(([apiResourceType, resources]) => [
251
+ apiResourceType,
252
+ new Map(resources.map(({ id, value }) => [id, value]))
253
+ ]));
235
254
  }
236
255
  function getSerializedState$1(state) {
237
- return Array.from(state.entries()).map(([resourceType, resources]) => [
238
- resourceType,
239
- Array.from(resources.entries())
240
- ]);
256
+ const resourceMap = {
257
+ Swatch: [],
258
+ File: [],
259
+ Typography: [],
260
+ PagePathnameSlice: [],
261
+ GlobalElement: [],
262
+ Table: [],
263
+ Snippet: [],
264
+ Page: [],
265
+ Site: []
266
+ };
267
+ Array.from(state.entries()).forEach(([resourceType, resources]) => {
268
+ const particularResourceMap = [];
269
+ Array.from(resources.entries()).forEach(([id, value]) => {
270
+ if (value != null) {
271
+ particularResourceMap.push({ id, value });
272
+ }
273
+ });
274
+ resourceMap[resourceType] = particularResourceMap;
275
+ });
276
+ return resourceMap;
241
277
  }
242
278
  function getHasAPIResource$1(state, resourceType, resourceId) {
243
279
  var _a, _b;
@@ -886,6 +922,159 @@ function Page$1({
886
922
  }, snippet.id))]
887
923
  });
888
924
  }
925
+ function unstable_Page$1({
926
+ pageData
927
+ }) {
928
+ var _a;
929
+ const isInBuilder = useIsInBuilder();
930
+ const [snippets, setSnippets] = useState(pageData.snapshot.resources.snippets);
931
+ const cachedPage = useCachedPage(isInBuilder ? pageData.pageId : null);
932
+ useEffect(() => {
933
+ if (cachedPage == null)
934
+ return;
935
+ const oldSnippets = snippets.map(filterUsedSnippetProperties);
936
+ const newSnippets = cachedPage.snippets.map(filterUsedSnippetProperties);
937
+ if (deepEqual(newSnippets, oldSnippets))
938
+ return;
939
+ setSnippets(cachedPage.snippets);
940
+ }, [cachedPage]);
941
+ const site = useCachedSite(isInBuilder ? pageData.siteId : null);
942
+ const favicon = (_a = pageData.snapshot.resources.meta.favicon) != null ? _a : defaultFavicon;
943
+ const {
944
+ title,
945
+ description,
946
+ keywords,
947
+ socialImage
948
+ } = pageData.snapshot.resources.meta;
949
+ const {
950
+ canonicalUrl,
951
+ isIndexingBlocked
952
+ } = pageData.snapshot.resources.seo;
953
+ const fontFamilyParamValue = useMemo(() => {
954
+ if (site == null) {
955
+ return pageData.snapshot.resources.fonts.map(({
956
+ family,
957
+ variants
958
+ }) => {
959
+ return `${family.replace(/ /g, "+")}:${variants.join()}`;
960
+ }).join("|");
961
+ }
962
+ return site.googleFonts.edges.filter((edge) => edge != null).map(({
963
+ activeVariants,
964
+ node: {
965
+ family,
966
+ variants
967
+ }
968
+ }) => {
969
+ const activeVariantSpecifiers = variants.filter((variant) => activeVariants.some((activeVariant) => activeVariant.specifier === variant.specifier)).map((variant) => variant.specifier).join();
970
+ return `${family.replace(/ /g, "+")}:${activeVariantSpecifiers}`;
971
+ }).join("|");
972
+ }, [site, pageData.snapshot.resources.fonts]);
973
+ const filteredSnippets = useMemo(() => snippets.filter((snippet) => isInBuilder ? snippet.builderEnabled : snippet.liveEnabled), [snippets, isInBuilder]);
974
+ const headSnippets = useMemo(() => filteredSnippets.filter((snippet) => snippet.location === SnippetLocation.Head), [filteredSnippets]);
975
+ const previousHeadSnippets = useRef(null);
976
+ useEffect(() => {
977
+ var _a2;
978
+ const headSnippetsToCleanUp = ((_a2 = previousHeadSnippets.current) != null ? _a2 : []).filter((previousSnippet) => previousSnippet.cleanup != null).filter((previousSnippet) => !headSnippets.some((snippet) => previousSnippet.id === snippet.id));
979
+ headSnippetsToCleanUp.forEach((snippetToCleanUp) => {
980
+ if (snippetToCleanUp.cleanup == null)
981
+ return;
982
+ const cleanUp = new Function(snippetToCleanUp.cleanup);
983
+ try {
984
+ cleanUp();
985
+ } catch {
986
+ }
987
+ });
988
+ previousHeadSnippets.current = headSnippets;
989
+ }, [headSnippets]);
990
+ return /* @__PURE__ */ jsxs(Fragment, {
991
+ children: [/* @__PURE__ */ jsxs(Head, {
992
+ children: [/* @__PURE__ */ jsx("style", {
993
+ children: `
994
+ html {
995
+ font-family: sans-serif;
996
+ }
997
+ div#__next {
998
+ overflow: hidden;
999
+ }
1000
+ `
1001
+ }), /* @__PURE__ */ jsx("link", {
1002
+ rel: "icon",
1003
+ type: favicon.mimetype,
1004
+ href: favicon.publicUrl
1005
+ }), canonicalUrl && /* @__PURE__ */ jsx("link", {
1006
+ rel: "canonical",
1007
+ href: canonicalUrl
1008
+ }), isIndexingBlocked && /* @__PURE__ */ jsx("meta", {
1009
+ name: "robots",
1010
+ content: "noindex"
1011
+ }), title && /* @__PURE__ */ jsxs(Fragment, {
1012
+ children: [/* @__PURE__ */ jsx("title", {
1013
+ children: title
1014
+ }), /* @__PURE__ */ jsx("meta", {
1015
+ property: "og:title",
1016
+ content: title
1017
+ }), /* @__PURE__ */ jsx("meta", {
1018
+ name: "twitter:title",
1019
+ content: title
1020
+ }), /* @__PURE__ */ jsx("meta", {
1021
+ itemProp: "name",
1022
+ content: title
1023
+ })]
1024
+ }), description && /* @__PURE__ */ jsxs(Fragment, {
1025
+ children: [/* @__PURE__ */ jsx("meta", {
1026
+ name: "description",
1027
+ content: description
1028
+ }), /* @__PURE__ */ jsx("meta", {
1029
+ property: "og:description",
1030
+ content: description
1031
+ }), /* @__PURE__ */ jsx("meta", {
1032
+ name: "twitter:description",
1033
+ content: description
1034
+ }), /* @__PURE__ */ jsx("meta", {
1035
+ itemProp: "description",
1036
+ content: description
1037
+ })]
1038
+ }), keywords && /* @__PURE__ */ jsx("meta", {
1039
+ name: "keywords",
1040
+ content: keywords
1041
+ }), socialImage && /* @__PURE__ */ jsxs(Fragment, {
1042
+ children: [/* @__PURE__ */ jsx("meta", {
1043
+ property: "og:image",
1044
+ content: socialImage.publicUrl
1045
+ }), /* @__PURE__ */ jsx("meta", {
1046
+ property: "og:image:type",
1047
+ content: socialImage.mimetype
1048
+ }), /* @__PURE__ */ jsx("meta", {
1049
+ name: "twitter:image",
1050
+ content: socialImage.publicUrl
1051
+ }), /* @__PURE__ */ jsx("meta", {
1052
+ name: "twitter:card",
1053
+ content: "summary_large_image"
1054
+ }), /* @__PURE__ */ jsx("meta", {
1055
+ itemProp: "image",
1056
+ content: socialImage.publicUrl
1057
+ })]
1058
+ }), fontFamilyParamValue !== "" && /* @__PURE__ */ jsx(Fragment, {
1059
+ children: /* @__PURE__ */ jsx("link", {
1060
+ rel: "stylesheet",
1061
+ href: `https://fonts.googleapis.com/css?family=${fontFamilyParamValue}&display=swap`
1062
+ })
1063
+ }), headSnippets.map(snippetToElement).map((children) => Children.map(children, (child) => {
1064
+ if (typeof child === "string")
1065
+ return child;
1066
+ if (VALID_HEAD_ELEMENT_TYPES.includes(child.type))
1067
+ return child;
1068
+ return null;
1069
+ }))]
1070
+ }), /* @__PURE__ */ jsx(DocumentReference, {
1071
+ documentReference: createDocumentReference(pageData.pageId)
1072
+ }), filteredSnippets.filter((snippet) => snippet.location === SnippetLocation.Body).map((snippet) => /* @__PURE__ */ jsx(BodySnippet, {
1073
+ code: snippet.code,
1074
+ cleanup: snippet.cleanup
1075
+ }, snippet.id))]
1076
+ });
1077
+ }
889
1078
  function useCachedPage(pageId) {
890
1079
  const client = useMakeswiftClient();
891
1080
  const getSnapshot = () => pageId == null ? null : client.readPage(pageId);
@@ -898,6 +1087,7 @@ function useCachedSite(siteId) {
898
1087
  const site = useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
899
1088
  return site;
900
1089
  }
1090
+ const version = "0.6.6";
901
1091
  class Makeswift {
902
1092
  constructor(apiKey, { apiOrigin = "https://api.makeswift.com" } = {}) {
903
1093
  __publicField(this, "apiKey");
@@ -951,6 +1141,199 @@ Received "${apiKey}" instead.`);
951
1141
  const snapshot = this.getPageSnapshotByPageId(page.id, { preview });
952
1142
  return snapshot;
953
1143
  }
1144
+ async unstable_getPageData(path, { preview } = {}) {
1145
+ const [page] = await this.getPages({ path });
1146
+ if (page == null)
1147
+ return null;
1148
+ const document2 = await this.getDocumentForPage(page.id);
1149
+ const snapshot = await this.unstable_createSnapshotForPage({ document: document2, pageId: page.id });
1150
+ return {
1151
+ pageId: page.id,
1152
+ siteId: document2.site.id,
1153
+ snapshot,
1154
+ options: { preview: preview || false, apiOrigin: this.apiOrigin.href }
1155
+ };
1156
+ }
1157
+ async getDocumentForPage(pageId) {
1158
+ const response = await this.fetch(`/v1/pages/${pageId}/document?preview=false`);
1159
+ if (!response.ok) {
1160
+ if (response.status === 404)
1161
+ throw Error("bad");
1162
+ throw new Error(`Failed to create snapshot with error: "${response.statusText}"`);
1163
+ }
1164
+ const document2 = await response.json();
1165
+ return document2;
1166
+ }
1167
+ async unstable_createSnapshotForPage({
1168
+ document: document2,
1169
+ pageId
1170
+ }) {
1171
+ let fetchedDocument = document2;
1172
+ if (fetchedDocument == null) {
1173
+ const response = await this.fetch(`/v1/pages/${pageId}/document?preview=false`);
1174
+ if (!response.ok) {
1175
+ if (response.status === 404) {
1176
+ throw new Error("Not found");
1177
+ }
1178
+ throw new Error(`Failed to create snapshot with error: "${response.statusText}"`);
1179
+ }
1180
+ fetchedDocument = await response.json();
1181
+ }
1182
+ if (fetchedDocument == null) {
1183
+ throw Error("fetchedDocument should never be null");
1184
+ }
1185
+ const client = new MakeswiftClient({ uri: new URL("graphql", this.apiOrigin).href });
1186
+ const prefetchedResources = await client.prefetch(fetchedDocument.data);
1187
+ const resources = __spreadProps(__spreadValues({}, prefetchedResources), {
1188
+ snippets: fetchedDocument.snippets,
1189
+ meta: fetchedDocument.meta,
1190
+ seo: fetchedDocument.seo,
1191
+ fonts: fetchedDocument.fonts
1192
+ });
1193
+ return { resources, elementTree: fetchedDocument.data, runtimeVersion: version };
1194
+ }
1195
+ async unstable_createSnapshot({
1196
+ publishedResources,
1197
+ deletedResources,
1198
+ publishedElementTree,
1199
+ currentSnapshot
1200
+ }) {
1201
+ const client = new MakeswiftClient({ uri: new URL("graphql", this.apiOrigin).href });
1202
+ function normalizeToMakeswiftResources(partialResources) {
1203
+ const resources2 = {
1204
+ Swatch: (partialResources == null ? void 0 : partialResources.Swatch) || [],
1205
+ File: (partialResources == null ? void 0 : partialResources.File) || [],
1206
+ Typography: (partialResources == null ? void 0 : partialResources.Typography) || [],
1207
+ PagePathnameSlice: (partialResources == null ? void 0 : partialResources.PagePathnameSlice) || [],
1208
+ GlobalElement: (partialResources == null ? void 0 : partialResources.GlobalElement) || [],
1209
+ Table: (partialResources == null ? void 0 : partialResources.Table) || [],
1210
+ Snippet: (partialResources == null ? void 0 : partialResources.Snippet) || [],
1211
+ Page: (partialResources == null ? void 0 : partialResources.Page) || [],
1212
+ Site: (partialResources == null ? void 0 : partialResources.Site) || [],
1213
+ snippets: (partialResources == null ? void 0 : partialResources.snippets) || [],
1214
+ fonts: (partialResources == null ? void 0 : partialResources.fonts) || [],
1215
+ meta: (partialResources == null ? void 0 : partialResources.meta) || {},
1216
+ seo: (partialResources == null ? void 0 : partialResources.seo) || {}
1217
+ };
1218
+ return resources2;
1219
+ }
1220
+ function mergeResources({
1221
+ resourcesFromPublishedElementTree: resourcesFromPublishedElementTree2,
1222
+ resourcesFromCurrentSnapshot: resourcesFromCurrentSnapshot2,
1223
+ publishedResources: publishedResources2,
1224
+ deletedResources: deletedResources2
1225
+ }) {
1226
+ function mergeElementTreeResource(resourceSet, deletedResources3) {
1227
+ const map = new Map(resourceSet.map(({ id, value }) => [id, value]));
1228
+ deletedResources3.forEach(({ id }) => map.delete(id));
1229
+ const finalResourceSet = [];
1230
+ Array.from(map.entries()).forEach(([id, value]) => {
1231
+ if (value != null) {
1232
+ finalResourceSet.push({ id, value });
1233
+ }
1234
+ });
1235
+ return finalResourceSet;
1236
+ }
1237
+ function mergeSnippets(snippets, deletedSnippet) {
1238
+ const map = new Map(snippets.map((value) => [value.id, value]));
1239
+ deletedSnippet.forEach(({ id }) => map.delete(id));
1240
+ const uniqueSnippets = [];
1241
+ Array.from(map.entries()).forEach(([_, value]) => {
1242
+ uniqueSnippets.push(value);
1243
+ });
1244
+ return uniqueSnippets;
1245
+ }
1246
+ function mergeFonts(fonts, deletedFonts) {
1247
+ const map = new Map(fonts.map((value) => [value.family, value]));
1248
+ deletedFonts.forEach(({ family }) => map.delete(family));
1249
+ const uniqueFonts = [];
1250
+ Array.from(map.entries()).forEach(([_, value]) => {
1251
+ uniqueFonts.push(value);
1252
+ });
1253
+ return uniqueFonts;
1254
+ }
1255
+ const resources2 = {
1256
+ Swatch: mergeElementTreeResource([
1257
+ ...resourcesFromPublishedElementTree2.Swatch,
1258
+ ...resourcesFromCurrentSnapshot2.Swatch,
1259
+ ...publishedResources2.Swatch
1260
+ ], deletedResources2.Swatch),
1261
+ File: mergeElementTreeResource([
1262
+ ...resourcesFromPublishedElementTree2.File,
1263
+ ...resourcesFromCurrentSnapshot2.File,
1264
+ ...publishedResources2.File
1265
+ ], deletedResources2.File),
1266
+ Typography: mergeElementTreeResource([
1267
+ ...resourcesFromPublishedElementTree2.Typography,
1268
+ ...resourcesFromCurrentSnapshot2.Typography,
1269
+ ...publishedResources2.Typography
1270
+ ], deletedResources2.Typography),
1271
+ PagePathnameSlice: mergeElementTreeResource([
1272
+ ...resourcesFromPublishedElementTree2.PagePathnameSlice,
1273
+ ...resourcesFromCurrentSnapshot2.PagePathnameSlice,
1274
+ ...publishedResources2.PagePathnameSlice
1275
+ ], deletedResources2.PagePathnameSlice),
1276
+ GlobalElement: mergeElementTreeResource([
1277
+ ...resourcesFromPublishedElementTree2.GlobalElement,
1278
+ ...resourcesFromCurrentSnapshot2.GlobalElement,
1279
+ ...publishedResources2.GlobalElement
1280
+ ], deletedResources2.GlobalElement),
1281
+ Table: mergeElementTreeResource([
1282
+ ...resourcesFromPublishedElementTree2.Table,
1283
+ ...resourcesFromCurrentSnapshot2.Table,
1284
+ ...publishedResources2.Table
1285
+ ], deletedResources2.Table),
1286
+ snippets: mergeSnippets([
1287
+ ...resourcesFromCurrentSnapshot2.snippets,
1288
+ ...resourcesFromPublishedElementTree2.snippets,
1289
+ ...publishedResources2.snippets
1290
+ ], deletedResources2.snippets),
1291
+ fonts: mergeFonts([
1292
+ ...resourcesFromCurrentSnapshot2.fonts,
1293
+ ...resourcesFromPublishedElementTree2.fonts,
1294
+ ...publishedResources2.fonts
1295
+ ], deletedResources2.fonts),
1296
+ meta: __spreadValues(__spreadValues({}, resourcesFromCurrentSnapshot2.meta), publishedResources2.meta),
1297
+ seo: __spreadValues(__spreadValues({}, resourcesFromCurrentSnapshot2.seo), publishedResources2.seo),
1298
+ Snippet: [],
1299
+ Page: [],
1300
+ Site: []
1301
+ };
1302
+ return resources2;
1303
+ }
1304
+ const resourcesFromPublishedElementTree = publishedElementTree != null ? normalizeToMakeswiftResources(await client.prefetch(publishedElementTree)) : normalizeToMakeswiftResources({});
1305
+ const resourcesFromCurrentSnapshot = normalizeToMakeswiftResources((currentSnapshot == null ? void 0 : currentSnapshot.resources) || {});
1306
+ const resources = mergeResources({
1307
+ resourcesFromPublishedElementTree,
1308
+ resourcesFromCurrentSnapshot,
1309
+ publishedResources: normalizeToMakeswiftResources(publishedResources),
1310
+ deletedResources: normalizeToMakeswiftResources(deletedResources)
1311
+ });
1312
+ const elementTree = publishedElementTree || (currentSnapshot == null ? void 0 : currentSnapshot.elementTree);
1313
+ if (elementTree == null) {
1314
+ throw Error("elementTree should not be null; something went wrong.");
1315
+ }
1316
+ return {
1317
+ resources,
1318
+ elementTree,
1319
+ runtimeVersion: version
1320
+ };
1321
+ }
1322
+ unstable_getSnapshotResourceMapping(snapshot) {
1323
+ const resources = snapshot.resources;
1324
+ function parseResourceIds({ id }) {
1325
+ return id;
1326
+ }
1327
+ return [
1328
+ ...resources.Swatch.map(parseResourceIds),
1329
+ ...resources.File.map(parseResourceIds),
1330
+ ...resources.Typography.map(parseResourceIds),
1331
+ ...resources.PagePathnameSlice.map(parseResourceIds),
1332
+ ...resources.GlobalElement.map(parseResourceIds),
1333
+ ...resources.Table.map(parseResourceIds),
1334
+ ...resources.snippets.map(parseResourceIds)
1335
+ ];
1336
+ }
954
1337
  }
955
1338
  function PreviewModeScript({
956
1339
  isPreview = false,
@@ -1079,11 +1462,14 @@ class Document$1 extends NextDocument {
1079
1462
  });
1080
1463
  }
1081
1464
  }
1082
- const version = "0.6.4";
1083
1465
  function isErrorWithMessage(error) {
1084
1466
  return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
1085
1467
  }
1086
- function MakeswiftApiHandler(apiKey, { appOrigin = "https://app.makeswift.com", getFonts } = {}) {
1468
+ function MakeswiftApiHandler(apiKey, {
1469
+ appOrigin = "https://app.makeswift.com",
1470
+ apiOrigin = "https://api.makeswift.com",
1471
+ getFonts
1472
+ } = {}) {
1087
1473
  const cors = Cors({ origin: appOrigin });
1088
1474
  const previewModeProxy = createProxyServer();
1089
1475
  previewModeProxy.on("proxyReq", (proxyReq) => {
@@ -1120,7 +1506,8 @@ Read more about dynamic catch-all routes here: https://nextjs.org/docs/routing/d
1120
1506
  return res.json({
1121
1507
  version,
1122
1508
  previewMode: true,
1123
- interactionMode: true
1509
+ interactionMode: true,
1510
+ clientSideNavigation: true
1124
1511
  });
1125
1512
  }
1126
1513
  case "revalidate": {
@@ -1193,6 +1580,35 @@ Read more here: https://nextjs.org/blog/next-12-2#on-demand-incremental-static-r
1193
1580
  const response = { elementTree: generatedElementTree };
1194
1581
  return res.json(response);
1195
1582
  }
1583
+ case "snapshot": {
1584
+ let validateBody = function(body2) {
1585
+ if (body2.pageId == null) {
1586
+ return res.status(400).json({ message: "Must define pageId." });
1587
+ }
1588
+ if (body2.currentSnapshot == null && body2.publishedElementTree == null) {
1589
+ return res.status(400).json({ message: "Either currentSnapshot or publishedElementTree must be defined." });
1590
+ }
1591
+ };
1592
+ const body = req.body;
1593
+ validateBody(body);
1594
+ const client = new Makeswift(apiKey, {
1595
+ apiOrigin
1596
+ });
1597
+ const snapshot = await client.unstable_createSnapshot({
1598
+ publishedResources: body.publishedResources,
1599
+ deletedResources: body.deletedResources,
1600
+ publishedElementTree: body.publishedElementTree,
1601
+ currentSnapshot: body.currentSnapshot
1602
+ });
1603
+ const usedResources = client.unstable_getSnapshotResourceMapping(snapshot);
1604
+ const response = {
1605
+ pageId: body.pageId,
1606
+ snapshot,
1607
+ livePageChanges: body.livePageChanges,
1608
+ usedResources
1609
+ };
1610
+ return res.json(response);
1611
+ }
1196
1612
  default:
1197
1613
  return res.status(404).json({ message: "Not Found" });
1198
1614
  }
@@ -1327,6 +1743,37 @@ const Page = memo(({
1327
1743
  }, snapshot.document.data.key)
1328
1744
  });
1329
1745
  });
1746
+ const unstable_Page = memo(({
1747
+ pageData
1748
+ }) => {
1749
+ function resourcesToCacheData(resources) {
1750
+ if (resources == null)
1751
+ return void 0;
1752
+ return {
1753
+ Swatch: resources.Swatch,
1754
+ File: resources.File,
1755
+ Typography: resources.Typography,
1756
+ PagePathnameSlice: resources.PagePathnameSlice,
1757
+ GlobalElement: resources.GlobalElement,
1758
+ Table: resources.Table,
1759
+ Snippet: resources.Snippet,
1760
+ Page: resources.Page,
1761
+ Site: resources.Site
1762
+ };
1763
+ }
1764
+ const client = useMemo(() => new MakeswiftClient({
1765
+ uri: new URL("graphql", pageData.options.apiOrigin).href,
1766
+ cacheData: resourcesToCacheData(pageData.snapshot.resources)
1767
+ }), [pageData]);
1768
+ return /* @__PURE__ */ jsx(RuntimeProvider, {
1769
+ client,
1770
+ rootElements: /* @__PURE__ */ new Map([[pageData.pageId, pageData.snapshot.elementTree]]),
1771
+ preview: pageData.options.preview,
1772
+ children: /* @__PURE__ */ jsx(unstable_Page$1, {
1773
+ pageData
1774
+ })
1775
+ });
1776
+ });
1330
1777
  const keys = (o) => Object.keys(o);
1331
1778
  const coalesce = (...args) => {
1332
1779
  let i;
@@ -3584,6 +4031,7 @@ const DocumentContext = createContext(null);
3584
4031
  function useDocumentKey() {
3585
4032
  return useContext(DocumentContext);
3586
4033
  }
4034
+ const DocumentCyclesContext = createContext([]);
3587
4035
  function useStore() {
3588
4036
  return useContext(StoreContext);
3589
4037
  }
@@ -3655,20 +4103,32 @@ const ElementReference = memo(forwardRef(function ElementReference2({
3655
4103
  const globalElement = useGlobalElement(elementReference.value);
3656
4104
  const globalElementData = globalElement == null ? void 0 : globalElement.data;
3657
4105
  const elementReferenceDocument = useDocument(elementReference.key);
4106
+ const documentKey = elementReference.key;
4107
+ const documentKeys = useContext(DocumentCyclesContext);
4108
+ const providedDocumentKeys = useMemo(() => [...documentKeys, documentKey], [documentKeys, documentKey]);
3658
4109
  if (globalElementData == null) {
3659
4110
  return /* @__PURE__ */ jsx(FallbackComponent, {
3660
4111
  ref,
3661
4112
  text: "This global component doesn't exist"
3662
4113
  });
3663
4114
  }
3664
- return elementReferenceDocument != null ? /* @__PURE__ */ jsx(Document, {
3665
- document: elementReferenceDocument,
3666
- ref
3667
- }) : /* @__PURE__ */ jsx(DisableRegisterElement.Provider, {
3668
- value: true,
3669
- children: /* @__PURE__ */ jsx(ElementData, {
3670
- elementData: globalElementData,
4115
+ if (documentKeys.includes(documentKey)) {
4116
+ return /* @__PURE__ */ jsx(FallbackComponent, {
4117
+ ref,
4118
+ text: "This global component contains itself!"
4119
+ });
4120
+ }
4121
+ return /* @__PURE__ */ jsx(DocumentCyclesContext.Provider, {
4122
+ value: providedDocumentKeys,
4123
+ children: elementReferenceDocument != null ? /* @__PURE__ */ jsx(Document, {
4124
+ document: elementReferenceDocument,
3671
4125
  ref
4126
+ }) : /* @__PURE__ */ jsx(DisableRegisterElement.Provider, {
4127
+ value: true,
4128
+ children: /* @__PURE__ */ jsx(ElementData, {
4129
+ elementData: globalElementData,
4130
+ ref
4131
+ })
3672
4132
  })
3673
4133
  });
3674
4134
  }));
@@ -3741,5 +4201,5 @@ const DocumentReference = memo(forwardRef(function DocumentReference2({
3741
4201
  document: document2
3742
4202
  });
3743
4203
  }));
3744
- export { getServerSideProps as $, Alignments as A, useFile as B, Contrasts as C, DocumentReference as D, Element as E, DEVICES as F, findDeviceOverride as G, forwardNextDynamicRef as H, useIsPreview as I, useFiles as J, useSwatches as K, usePagePathnameSlice as L, useElementId as M, deepEqual as N, storeContextDefaultValue as O, PageProvider as P, StoreContext as Q, RuntimeProvider as R, Shapes as S, MakeswiftProvider as T, useBuilderEditMode as U, pollBoxModel as V, useTypography as W, shallowMergeFallbacks as X, MakeswiftClient as Y, getStaticPaths as Z, getStaticProps as _, ReactRuntime as a, Page as a0, Makeswift as a1, PreviewModeScript as a2, Document$1 as a3, MakeswiftApiHandler as a4, usePageId as b, DEFAULT_BOX_ANIMATE_TYPE as c, DEFAULT_BOX_ANIMATE_DELAY as d, DEFAULT_BOX_ANIMATE_DURATION as e, DEFAULT_ITEM_ANIMATE_TYPE as f, DEFAULT_ITEM_ANIMATE_DELAY as g, DEFAULT_ITEM_ANIMATE_DURATION as h, DEFAULT_ITEM_STAGGER_DURATION as i, useBorder as j, useBoxShadow as k, useResponsiveColor as l, useMediaQuery as m, Page$1 as n, useStyle as o, responsiveWidth as p, responsiveTextStyle as q, responsiveStyle as r, colorToString as s, Sizes as t, useIsInBuilder as u, useFormContext as v, responsiveGridItem as w, useTable as x, useMakeswiftClient as y, Provider as z };
4204
+ export { getStaticProps as $, Provider as A, Alignments as B, Contrasts as C, DocumentReference as D, Element as E, useFile as F, DEVICES as G, findDeviceOverride as H, forwardNextDynamicRef as I, useIsPreview as J, useFiles as K, useSwatches as L, usePagePathnameSlice as M, useElementId as N, deepEqual as O, PageProvider as P, storeContextDefaultValue as Q, RuntimeProvider as R, Shapes as S, StoreContext as T, MakeswiftProvider as U, useBuilderEditMode as V, pollBoxModel as W, useTypography as X, shallowMergeFallbacks as Y, MakeswiftClient as Z, getStaticPaths as _, ReactRuntime as a, getServerSideProps as a0, Page as a1, unstable_Page as a2, Makeswift as a3, PreviewModeScript as a4, Document$1 as a5, MakeswiftApiHandler as a6, usePageId as b, DEFAULT_BOX_ANIMATE_TYPE as c, DEFAULT_BOX_ANIMATE_DELAY as d, DEFAULT_BOX_ANIMATE_DURATION as e, DEFAULT_ITEM_ANIMATE_TYPE as f, DEFAULT_ITEM_ANIMATE_DELAY as g, DEFAULT_ITEM_ANIMATE_DURATION as h, DEFAULT_ITEM_STAGGER_DURATION as i, useBorder as j, useBoxShadow as k, useResponsiveColor as l, useMediaQuery as m, Page$1 as n, unstable_Page$1 as o, useStyle as p, responsiveWidth as q, responsiveStyle as r, responsiveTextStyle as s, colorToString as t, useIsInBuilder as u, Sizes as v, useFormContext as w, responsiveGridItem as x, useTable as y, useMakeswiftClient as z };
3745
4205
  //# sourceMappingURL=index.es.js.map