@makeswift/runtime 0.6.3 → 0.6.5

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 (100) 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.cjs.js +12 -3
  7. package/dist/EditableText.cjs.js.map +1 -1
  8. package/dist/EditableText.es.js +13 -4
  9. package/dist/EditableText.es.js.map +1 -1
  10. package/dist/Embed.es.js +1 -1
  11. package/dist/Form.es.js +1 -1
  12. package/dist/Image.es.js +1 -1
  13. package/dist/LiveProvider.es.js +1 -1
  14. package/dist/Navigation.es.js +1 -1
  15. package/dist/PreviewProvider.cjs.js +53 -16
  16. package/dist/PreviewProvider.cjs.js.map +1 -1
  17. package/dist/PreviewProvider.es.js +56 -20
  18. package/dist/PreviewProvider.es.js.map +1 -1
  19. package/dist/ReadOnlyText.cjs.js +5 -2
  20. package/dist/ReadOnlyText.cjs.js.map +1 -1
  21. package/dist/ReadOnlyText.es.js +6 -3
  22. package/dist/ReadOnlyText.es.js.map +1 -1
  23. package/dist/Root.es.js +1 -1
  24. package/dist/SocialLinks.es.js +1 -1
  25. package/dist/Text.es.js +1 -1
  26. package/dist/Video.es.js +1 -1
  27. package/dist/actions.cjs.js +11 -1
  28. package/dist/actions.cjs.js.map +1 -1
  29. package/dist/actions.es.js +11 -2
  30. package/dist/actions.es.js.map +1 -1
  31. package/dist/builder.cjs.js +1 -0
  32. package/dist/builder.cjs.js.map +1 -1
  33. package/dist/builder.es.js +1 -0
  34. package/dist/builder.es.js.map +1 -1
  35. package/dist/components.cjs.js +1 -0
  36. package/dist/components.cjs.js.map +1 -1
  37. package/dist/components.es.js +1 -1
  38. package/dist/constants.cjs.js +6 -1
  39. package/dist/constants.cjs.js.map +1 -1
  40. package/dist/constants.es.js +7 -3
  41. package/dist/constants.es.js.map +1 -1
  42. package/dist/controls.cjs.js +1 -0
  43. package/dist/controls.cjs.js.map +1 -1
  44. package/dist/controls.es.js +2 -1
  45. package/dist/controls.es.js.map +1 -1
  46. package/dist/index.cjs.js +494 -15
  47. package/dist/index.cjs.js.map +1 -1
  48. package/dist/index.es.js +495 -19
  49. package/dist/index.es.js.map +1 -1
  50. package/dist/index.es2.js +2 -2
  51. package/dist/index.es3.js +1 -1
  52. package/dist/index.es4.js +1 -1
  53. package/dist/index.es6.js +2 -2
  54. package/dist/main.cjs.js +1 -0
  55. package/dist/main.cjs.js.map +1 -1
  56. package/dist/main.es.js +1 -1
  57. package/dist/next.cjs.js +1 -0
  58. package/dist/next.cjs.js.map +1 -1
  59. package/dist/next.es.js +1 -1
  60. package/dist/prop-controllers.cjs.js +1 -0
  61. package/dist/prop-controllers.cjs.js.map +1 -1
  62. package/dist/prop-controllers.es.js +1 -0
  63. package/dist/prop-controllers.es.js.map +1 -1
  64. package/dist/rich-text.cjs.js +20 -4
  65. package/dist/rich-text.cjs.js.map +1 -1
  66. package/dist/rich-text.es.js +19 -5
  67. package/dist/rich-text.es.js.map +1 -1
  68. package/dist/types/src/components/builtin/Text/EditableText.d.ts.map +1 -1
  69. package/dist/types/src/components/builtin/Text/ReadOnlyText.d.ts.map +1 -1
  70. package/dist/types/src/components/builtin/Text/register.d.ts.map +1 -1
  71. package/dist/types/src/components/page/Page.d.ts +5 -1
  72. package/dist/types/src/components/page/Page.d.ts.map +1 -1
  73. package/dist/types/src/controls/rich-text.d.ts +1 -1
  74. package/dist/types/src/controls/rich-text.d.ts.map +1 -1
  75. package/dist/types/src/index.d.ts +1 -1
  76. package/dist/types/src/index.d.ts.map +1 -1
  77. package/dist/types/src/next/api-handler.d.ts +14 -2
  78. package/dist/types/src/next/api-handler.d.ts.map +1 -1
  79. package/dist/types/src/next/client.d.ts +69 -32
  80. package/dist/types/src/next/client.d.ts.map +1 -1
  81. package/dist/types/src/next/index.d.ts +5 -1
  82. package/dist/types/src/next/index.d.ts.map +1 -1
  83. package/dist/types/src/next/preview-mode.d.ts.map +1 -1
  84. package/dist/types/src/prop-controllers/instances.d.ts +1 -1
  85. package/dist/types/src/prop-controllers/instances.d.ts.map +1 -1
  86. package/dist/types/src/runtimes/react/index.d.ts +2 -0
  87. package/dist/types/src/runtimes/react/index.d.ts.map +1 -1
  88. package/dist/types/src/state/actions.d.ts +12 -1
  89. package/dist/types/src/state/actions.d.ts.map +1 -1
  90. package/dist/types/src/state/modules/api-resources.d.ts +6 -2
  91. package/dist/types/src/state/modules/api-resources.d.ts.map +1 -1
  92. package/dist/types/src/state/modules/builder-edit-mode.d.ts +11 -0
  93. package/dist/types/src/state/modules/builder-edit-mode.d.ts.map +1 -0
  94. package/dist/types/src/state/react-builder-preview.d.ts +2 -0
  95. package/dist/types/src/state/react-builder-preview.d.ts.map +1 -1
  96. package/dist/types/src/state/react-page.d.ts +3 -0
  97. package/dist/types/src/state/react-page.d.ts.map +1 -1
  98. package/package.json +1 -1
  99. package/dist/types/src/utils/constants.d.ts +0 -6
  100. package/dist/types/src/utils/constants.d.ts.map +0 -1
package/dist/index.es.js CHANGED
@@ -39,13 +39,13 @@ import * as React from "react";
39
39
  import { useEffect, createContext, useContext, useState, useMemo, useRef, Children, createElement, forwardRef, memo, useCallback, useImperativeHandle, Component, Suspense } from "react";
40
40
  import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector";
41
41
  import dynamic from "next/dynamic";
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 } from "./constants.es.js";
43
- import { A as ActionTypes, f as introspectedResourcesFulfilled, g as apiResourceFulfilled, t as typographiesFulfilled, h as registerComponentHandleEffect, j as mountComponentEffect, k as registerComponentEffect, l as registerReactComponentEffect } from "./actions.es.js";
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";
44
44
  import { cache, cx } from "@emotion/css";
45
45
  import { serializeStyles } from "@emotion/serialize";
46
46
  import { registerStyles, insertStyles } from "@emotion/utils";
47
47
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
48
- import { T as Types, E as ElementID, B as Backgrounds, W as Width, h as ResponsiveIconRadioGroup, M as Margin, P as Padding, i as Border, j as BorderRadius, k as Shadows, G as GapY, l as GapX, m as ResponsiveSelect, n as ResponsiveNumber, o as Checkbox, p as Grid, q as TextInput, L as Link, r as ResponsiveColor, s as TextStyle, t as Images, N as Number$1, D as Date$1, F as Font, u as ResponsiveLength, v as TextArea, w as Table, x as TableFormFields, y as Image, z as ResponsiveOpacity, A as NavigationLinks, H as SocialLinks, J as RichText, V as Video, K as StyleControlProperty, O as ImageControlValueFormat, I as ImageControlType, C as ColorControlType, Q as BorderPropControllerFormat, U as ShadowsPropControllerFormat, X as BorderRadiusPropControllerFormat, Y as MarginPropControllerFormat, Z as PaddingPropControllerFormat, _ as WidthPropControllerFormat, S as SlotControlType, $ as RichTextControlType, a as StyleControlType } from "./rich-text.es.js";
48
+ import { T as Types, E as ElementID, B as Backgrounds, W as Width, h as ResponsiveIconRadioGroup, M as Margin, P as Padding, i as Border, j as BorderRadius, k as Shadows, G as GapY, l as GapX, m as ResponsiveSelect, n as ResponsiveNumber, o as Checkbox, p as Grid, q as TextInput, L as Link, s as ResponsiveColor, t as TextStyle, u as Images, N as Number$1, D as Date$1, F as Font, v as ResponsiveLength, w as TextArea, x as Table, y as TableFormFields, z as Image, A as ResponsiveOpacity, H as NavigationLinks, J as SocialLinks, K as RichText, V as Video, O as StyleControlProperty, Q as ImageControlValueFormat, I as ImageControlType, C as ColorControlType, U as BorderPropControllerFormat, X as ShadowsPropControllerFormat, Y as BorderRadiusPropControllerFormat, Z as MarginPropControllerFormat, _ as PaddingPropControllerFormat, $ as WidthPropControllerFormat, S as SlotControlType, a0 as RichTextControlType, a as StyleControlType } from "./rich-text.es.js";
49
49
  import { S as SelectControlType, T as TextAreaControlType, a as TextInputControlType, N as NumberControlType, C as CheckboxControlType } from "./text-input.es.js";
50
50
  import { C as ComboboxControlType } from "./combobox.es.js";
51
51
  import { S as ShapeControlType, L as ListControlType, b as LinkControlType } from "./control.es.js";
@@ -230,14 +230,44 @@ const deepEqual = (a, b) => {
230
230
  }
231
231
  return true;
232
232
  };
233
- function getInitialState(serializedState = []) {
234
- return new Map(serializedState.map(([resourceType, resources]) => [resourceType, new Map(resources)]));
233
+ function getInitialState(serializedState = {
234
+ Swatch: [],
235
+ File: [],
236
+ Typography: [],
237
+ PagePathnameSlice: [],
238
+ GlobalElement: [],
239
+ Table: [],
240
+ Snippet: [],
241
+ Page: [],
242
+ Site: []
243
+ }) {
244
+ return new Map(Object.entries(serializedState).map(([apiResourceType, resources]) => [
245
+ apiResourceType,
246
+ new Map(resources.map(({ id, value }) => [id, value]))
247
+ ]));
235
248
  }
236
249
  function getSerializedState$1(state) {
237
- return Array.from(state.entries()).map(([resourceType, resources]) => [
238
- resourceType,
239
- Array.from(resources.entries())
240
- ]);
250
+ const resourceMap = {
251
+ Swatch: [],
252
+ File: [],
253
+ Typography: [],
254
+ PagePathnameSlice: [],
255
+ GlobalElement: [],
256
+ Table: [],
257
+ Snippet: [],
258
+ Page: [],
259
+ Site: []
260
+ };
261
+ Array.from(state.entries()).forEach(([resourceType, resources]) => {
262
+ const particularResourceMap = [];
263
+ Array.from(resources.entries()).forEach(([id, value]) => {
264
+ if (value != null) {
265
+ particularResourceMap.push({ id, value });
266
+ }
267
+ });
268
+ resourceMap[resourceType] = particularResourceMap;
269
+ });
270
+ return resourceMap;
241
271
  }
242
272
  function getHasAPIResource$1(state, resourceType, resourceId) {
243
273
  var _a, _b;
@@ -886,6 +916,159 @@ function Page$1({
886
916
  }, snippet.id))]
887
917
  });
888
918
  }
919
+ function unstable_Page$1({
920
+ pageData
921
+ }) {
922
+ var _a;
923
+ const isInBuilder = useIsInBuilder();
924
+ const [snippets, setSnippets] = useState(pageData.snapshot.resources.snippets);
925
+ const cachedPage = useCachedPage(isInBuilder ? pageData.pageId : null);
926
+ useEffect(() => {
927
+ if (cachedPage == null)
928
+ return;
929
+ const oldSnippets = snippets.map(filterUsedSnippetProperties);
930
+ const newSnippets = cachedPage.snippets.map(filterUsedSnippetProperties);
931
+ if (deepEqual(newSnippets, oldSnippets))
932
+ return;
933
+ setSnippets(cachedPage.snippets);
934
+ }, [cachedPage]);
935
+ const site = useCachedSite(isInBuilder ? pageData.siteId : null);
936
+ const favicon = (_a = pageData.snapshot.resources.meta.favicon) != null ? _a : defaultFavicon;
937
+ const {
938
+ title,
939
+ description,
940
+ keywords,
941
+ socialImage
942
+ } = pageData.snapshot.resources.meta;
943
+ const {
944
+ canonicalUrl,
945
+ isIndexingBlocked
946
+ } = pageData.snapshot.resources.seo;
947
+ const fontFamilyParamValue = useMemo(() => {
948
+ if (site == null) {
949
+ return pageData.snapshot.resources.fonts.map(({
950
+ family,
951
+ variants
952
+ }) => {
953
+ return `${family.replace(/ /g, "+")}:${variants.join()}`;
954
+ }).join("|");
955
+ }
956
+ return site.googleFonts.edges.filter((edge) => edge != null).map(({
957
+ activeVariants,
958
+ node: {
959
+ family,
960
+ variants
961
+ }
962
+ }) => {
963
+ const activeVariantSpecifiers = variants.filter((variant) => activeVariants.some((activeVariant) => activeVariant.specifier === variant.specifier)).map((variant) => variant.specifier).join();
964
+ return `${family.replace(/ /g, "+")}:${activeVariantSpecifiers}`;
965
+ }).join("|");
966
+ }, [site, pageData.snapshot.resources.fonts]);
967
+ const filteredSnippets = useMemo(() => snippets.filter((snippet) => isInBuilder ? snippet.builderEnabled : snippet.liveEnabled), [snippets, isInBuilder]);
968
+ const headSnippets = useMemo(() => filteredSnippets.filter((snippet) => snippet.location === SnippetLocation.Head), [filteredSnippets]);
969
+ const previousHeadSnippets = useRef(null);
970
+ useEffect(() => {
971
+ var _a2;
972
+ const headSnippetsToCleanUp = ((_a2 = previousHeadSnippets.current) != null ? _a2 : []).filter((previousSnippet) => previousSnippet.cleanup != null).filter((previousSnippet) => !headSnippets.some((snippet) => previousSnippet.id === snippet.id));
973
+ headSnippetsToCleanUp.forEach((snippetToCleanUp) => {
974
+ if (snippetToCleanUp.cleanup == null)
975
+ return;
976
+ const cleanUp = new Function(snippetToCleanUp.cleanup);
977
+ try {
978
+ cleanUp();
979
+ } catch {
980
+ }
981
+ });
982
+ previousHeadSnippets.current = headSnippets;
983
+ }, [headSnippets]);
984
+ return /* @__PURE__ */ jsxs(Fragment, {
985
+ children: [/* @__PURE__ */ jsxs(Head, {
986
+ children: [/* @__PURE__ */ jsx("style", {
987
+ children: `
988
+ html {
989
+ font-family: sans-serif;
990
+ }
991
+ div#__next {
992
+ overflow: hidden;
993
+ }
994
+ `
995
+ }), /* @__PURE__ */ jsx("link", {
996
+ rel: "icon",
997
+ type: favicon.mimetype,
998
+ href: favicon.publicUrl
999
+ }), canonicalUrl && /* @__PURE__ */ jsx("link", {
1000
+ rel: "canonical",
1001
+ href: canonicalUrl
1002
+ }), isIndexingBlocked && /* @__PURE__ */ jsx("meta", {
1003
+ name: "robots",
1004
+ content: "noindex"
1005
+ }), title && /* @__PURE__ */ jsxs(Fragment, {
1006
+ children: [/* @__PURE__ */ jsx("title", {
1007
+ children: title
1008
+ }), /* @__PURE__ */ jsx("meta", {
1009
+ property: "og:title",
1010
+ content: title
1011
+ }), /* @__PURE__ */ jsx("meta", {
1012
+ name: "twitter:title",
1013
+ content: title
1014
+ }), /* @__PURE__ */ jsx("meta", {
1015
+ itemProp: "name",
1016
+ content: title
1017
+ })]
1018
+ }), description && /* @__PURE__ */ jsxs(Fragment, {
1019
+ children: [/* @__PURE__ */ jsx("meta", {
1020
+ name: "description",
1021
+ content: description
1022
+ }), /* @__PURE__ */ jsx("meta", {
1023
+ property: "og:description",
1024
+ content: description
1025
+ }), /* @__PURE__ */ jsx("meta", {
1026
+ name: "twitter:description",
1027
+ content: description
1028
+ }), /* @__PURE__ */ jsx("meta", {
1029
+ itemProp: "description",
1030
+ content: description
1031
+ })]
1032
+ }), keywords && /* @__PURE__ */ jsx("meta", {
1033
+ name: "keywords",
1034
+ content: keywords
1035
+ }), socialImage && /* @__PURE__ */ jsxs(Fragment, {
1036
+ children: [/* @__PURE__ */ jsx("meta", {
1037
+ property: "og:image",
1038
+ content: socialImage.publicUrl
1039
+ }), /* @__PURE__ */ jsx("meta", {
1040
+ property: "og:image:type",
1041
+ content: socialImage.mimetype
1042
+ }), /* @__PURE__ */ jsx("meta", {
1043
+ name: "twitter:image",
1044
+ content: socialImage.publicUrl
1045
+ }), /* @__PURE__ */ jsx("meta", {
1046
+ name: "twitter:card",
1047
+ content: "summary_large_image"
1048
+ }), /* @__PURE__ */ jsx("meta", {
1049
+ itemProp: "image",
1050
+ content: socialImage.publicUrl
1051
+ })]
1052
+ }), fontFamilyParamValue !== "" && /* @__PURE__ */ jsx(Fragment, {
1053
+ children: /* @__PURE__ */ jsx("link", {
1054
+ rel: "stylesheet",
1055
+ href: `https://fonts.googleapis.com/css?family=${fontFamilyParamValue}&display=swap`
1056
+ })
1057
+ }), headSnippets.map(snippetToElement).map((children) => Children.map(children, (child) => {
1058
+ if (typeof child === "string")
1059
+ return child;
1060
+ if (VALID_HEAD_ELEMENT_TYPES.includes(child.type))
1061
+ return child;
1062
+ return null;
1063
+ }))]
1064
+ }), /* @__PURE__ */ jsx(DocumentReference, {
1065
+ documentReference: createDocumentReference(pageData.pageId)
1066
+ }), filteredSnippets.filter((snippet) => snippet.location === SnippetLocation.Body).map((snippet) => /* @__PURE__ */ jsx(BodySnippet, {
1067
+ code: snippet.code,
1068
+ cleanup: snippet.cleanup
1069
+ }, snippet.id))]
1070
+ });
1071
+ }
889
1072
  function useCachedPage(pageId) {
890
1073
  const client = useMakeswiftClient();
891
1074
  const getSnapshot = () => pageId == null ? null : client.readPage(pageId);
@@ -898,6 +1081,7 @@ function useCachedSite(siteId) {
898
1081
  const site = useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
899
1082
  return site;
900
1083
  }
1084
+ const version = "0.6.5";
901
1085
  class Makeswift {
902
1086
  constructor(apiKey, { apiOrigin = "https://api.makeswift.com" } = {}) {
903
1087
  __publicField(this, "apiKey");
@@ -951,12 +1135,205 @@ Received "${apiKey}" instead.`);
951
1135
  const snapshot = this.getPageSnapshotByPageId(page.id, { preview });
952
1136
  return snapshot;
953
1137
  }
1138
+ async unstable_getPageData(path, { preview } = {}) {
1139
+ const [page] = await this.getPages({ path });
1140
+ if (page == null)
1141
+ return null;
1142
+ const document2 = await this.getDocumentForPage(page.id);
1143
+ const snapshot = await this.unstable_createSnapshotForPage({ document: document2, pageId: page.id });
1144
+ return {
1145
+ pageId: page.id,
1146
+ siteId: document2.site.id,
1147
+ snapshot,
1148
+ options: { preview: preview || false, apiOrigin: this.apiOrigin.href }
1149
+ };
1150
+ }
1151
+ async getDocumentForPage(pageId) {
1152
+ const response = await this.fetch(`/v1/pages/${pageId}/document?preview=false`);
1153
+ if (!response.ok) {
1154
+ if (response.status === 404)
1155
+ throw Error("bad");
1156
+ throw new Error(`Failed to create snapshot with error: "${response.statusText}"`);
1157
+ }
1158
+ const document2 = await response.json();
1159
+ return document2;
1160
+ }
1161
+ async unstable_createSnapshotForPage({
1162
+ document: document2,
1163
+ pageId
1164
+ }) {
1165
+ let fetchedDocument = document2;
1166
+ if (fetchedDocument == null) {
1167
+ const response = await this.fetch(`/v1/pages/${pageId}/document?preview=false`);
1168
+ if (!response.ok) {
1169
+ if (response.status === 404) {
1170
+ throw new Error("Not found");
1171
+ }
1172
+ throw new Error(`Failed to create snapshot with error: "${response.statusText}"`);
1173
+ }
1174
+ fetchedDocument = await response.json();
1175
+ }
1176
+ if (fetchedDocument == null) {
1177
+ throw Error("fetchedDocument should never be null");
1178
+ }
1179
+ const client = new MakeswiftClient({ uri: new URL("graphql", this.apiOrigin).href });
1180
+ const prefetchedResources = await client.prefetch(fetchedDocument.data);
1181
+ const resources = __spreadProps(__spreadValues({}, prefetchedResources), {
1182
+ snippets: fetchedDocument.snippets,
1183
+ meta: fetchedDocument.meta,
1184
+ seo: fetchedDocument.seo,
1185
+ fonts: fetchedDocument.fonts
1186
+ });
1187
+ return { resources, elementTree: fetchedDocument.data, runtimeVersion: version };
1188
+ }
1189
+ async unstable_createSnapshot({
1190
+ publishedResources,
1191
+ deletedResources,
1192
+ publishedElementTree,
1193
+ currentSnapshot
1194
+ }) {
1195
+ const client = new MakeswiftClient({ uri: new URL("graphql", this.apiOrigin).href });
1196
+ function normalizeToMakeswiftResources(partialResources) {
1197
+ const resources2 = {
1198
+ Swatch: (partialResources == null ? void 0 : partialResources.Swatch) || [],
1199
+ File: (partialResources == null ? void 0 : partialResources.File) || [],
1200
+ Typography: (partialResources == null ? void 0 : partialResources.Typography) || [],
1201
+ PagePathnameSlice: (partialResources == null ? void 0 : partialResources.PagePathnameSlice) || [],
1202
+ GlobalElement: (partialResources == null ? void 0 : partialResources.GlobalElement) || [],
1203
+ Table: (partialResources == null ? void 0 : partialResources.Table) || [],
1204
+ Snippet: (partialResources == null ? void 0 : partialResources.Snippet) || [],
1205
+ Page: (partialResources == null ? void 0 : partialResources.Page) || [],
1206
+ Site: (partialResources == null ? void 0 : partialResources.Site) || [],
1207
+ snippets: (partialResources == null ? void 0 : partialResources.snippets) || [],
1208
+ fonts: (partialResources == null ? void 0 : partialResources.fonts) || [],
1209
+ meta: (partialResources == null ? void 0 : partialResources.meta) || {},
1210
+ seo: (partialResources == null ? void 0 : partialResources.seo) || {}
1211
+ };
1212
+ return resources2;
1213
+ }
1214
+ function mergeResources({
1215
+ resourcesFromPublishedElementTree: resourcesFromPublishedElementTree2,
1216
+ resourcesFromCurrentSnapshot: resourcesFromCurrentSnapshot2,
1217
+ publishedResources: publishedResources2,
1218
+ deletedResources: deletedResources2
1219
+ }) {
1220
+ function mergeElementTreeResource(resourceSet, deletedResources3) {
1221
+ const map = new Map(resourceSet.map(({ id, value }) => [id, value]));
1222
+ deletedResources3.forEach(({ id }) => map.delete(id));
1223
+ const finalResourceSet = [];
1224
+ Array.from(map.entries()).forEach(([id, value]) => {
1225
+ if (value != null) {
1226
+ finalResourceSet.push({ id, value });
1227
+ }
1228
+ });
1229
+ return finalResourceSet;
1230
+ }
1231
+ function mergeSnippets(snippets, deletedSnippet) {
1232
+ const map = new Map(snippets.map((value) => [value.id, value]));
1233
+ deletedSnippet.forEach(({ id }) => map.delete(id));
1234
+ const uniqueSnippets = [];
1235
+ Array.from(map.entries()).forEach(([_, value]) => {
1236
+ uniqueSnippets.push(value);
1237
+ });
1238
+ return uniqueSnippets;
1239
+ }
1240
+ function mergeFonts(fonts, deletedFonts) {
1241
+ const map = new Map(fonts.map((value) => [value.family, value]));
1242
+ deletedFonts.forEach(({ family }) => map.delete(family));
1243
+ const uniqueFonts = [];
1244
+ Array.from(map.entries()).forEach(([_, value]) => {
1245
+ uniqueFonts.push(value);
1246
+ });
1247
+ return uniqueFonts;
1248
+ }
1249
+ const resources2 = {
1250
+ Swatch: mergeElementTreeResource([
1251
+ ...resourcesFromPublishedElementTree2.Swatch,
1252
+ ...resourcesFromCurrentSnapshot2.Swatch,
1253
+ ...publishedResources2.Swatch
1254
+ ], deletedResources2.Swatch),
1255
+ File: mergeElementTreeResource([
1256
+ ...resourcesFromPublishedElementTree2.File,
1257
+ ...resourcesFromCurrentSnapshot2.File,
1258
+ ...publishedResources2.File
1259
+ ], deletedResources2.File),
1260
+ Typography: mergeElementTreeResource([
1261
+ ...resourcesFromPublishedElementTree2.Typography,
1262
+ ...resourcesFromCurrentSnapshot2.Typography,
1263
+ ...publishedResources2.Typography
1264
+ ], deletedResources2.Typography),
1265
+ PagePathnameSlice: mergeElementTreeResource([
1266
+ ...resourcesFromPublishedElementTree2.PagePathnameSlice,
1267
+ ...resourcesFromCurrentSnapshot2.PagePathnameSlice,
1268
+ ...publishedResources2.PagePathnameSlice
1269
+ ], deletedResources2.PagePathnameSlice),
1270
+ GlobalElement: mergeElementTreeResource([
1271
+ ...resourcesFromPublishedElementTree2.GlobalElement,
1272
+ ...resourcesFromCurrentSnapshot2.GlobalElement,
1273
+ ...publishedResources2.GlobalElement
1274
+ ], deletedResources2.GlobalElement),
1275
+ Table: mergeElementTreeResource([
1276
+ ...resourcesFromPublishedElementTree2.Table,
1277
+ ...resourcesFromCurrentSnapshot2.Table,
1278
+ ...publishedResources2.Table
1279
+ ], deletedResources2.Table),
1280
+ snippets: mergeSnippets([
1281
+ ...resourcesFromCurrentSnapshot2.snippets,
1282
+ ...resourcesFromPublishedElementTree2.snippets,
1283
+ ...publishedResources2.snippets
1284
+ ], deletedResources2.snippets),
1285
+ fonts: mergeFonts([
1286
+ ...resourcesFromCurrentSnapshot2.fonts,
1287
+ ...resourcesFromPublishedElementTree2.fonts,
1288
+ ...publishedResources2.fonts
1289
+ ], deletedResources2.fonts),
1290
+ meta: __spreadValues(__spreadValues({}, resourcesFromCurrentSnapshot2.meta), publishedResources2.meta),
1291
+ seo: __spreadValues(__spreadValues({}, resourcesFromCurrentSnapshot2.seo), publishedResources2.seo),
1292
+ Snippet: [],
1293
+ Page: [],
1294
+ Site: []
1295
+ };
1296
+ return resources2;
1297
+ }
1298
+ const resourcesFromPublishedElementTree = publishedElementTree != null ? normalizeToMakeswiftResources(await client.prefetch(publishedElementTree)) : normalizeToMakeswiftResources({});
1299
+ const resourcesFromCurrentSnapshot = normalizeToMakeswiftResources((currentSnapshot == null ? void 0 : currentSnapshot.resources) || {});
1300
+ const resources = mergeResources({
1301
+ resourcesFromPublishedElementTree,
1302
+ resourcesFromCurrentSnapshot,
1303
+ publishedResources: normalizeToMakeswiftResources(publishedResources),
1304
+ deletedResources: normalizeToMakeswiftResources(deletedResources)
1305
+ });
1306
+ const elementTree = publishedElementTree || (currentSnapshot == null ? void 0 : currentSnapshot.elementTree);
1307
+ if (elementTree == null) {
1308
+ throw Error("elementTree should not be null; something went wrong.");
1309
+ }
1310
+ return {
1311
+ resources,
1312
+ elementTree,
1313
+ runtimeVersion: version
1314
+ };
1315
+ }
1316
+ unstable_getSnapshotResourceMapping(snapshot) {
1317
+ const resources = snapshot.resources;
1318
+ function parseResourceIds({ id }) {
1319
+ return id;
1320
+ }
1321
+ return [
1322
+ ...resources.Swatch.map(parseResourceIds),
1323
+ ...resources.File.map(parseResourceIds),
1324
+ ...resources.Typography.map(parseResourceIds),
1325
+ ...resources.PagePathnameSlice.map(parseResourceIds),
1326
+ ...resources.GlobalElement.map(parseResourceIds),
1327
+ ...resources.Table.map(parseResourceIds),
1328
+ ...resources.snippets.map(parseResourceIds)
1329
+ ];
1330
+ }
954
1331
  }
955
1332
  function PreviewModeScript({
956
1333
  isPreview = false,
957
1334
  appOrigin = "https://app.makeswift.com"
958
1335
  }) {
959
- const __html = `
1336
+ const previewModeScript = `
960
1337
  const isPreview = ${isPreview}
961
1338
  const appOrigin = '${appOrigin.replace("'", "\\'")}'
962
1339
  const searchParamName = 'x-makeswift-preview-mode'
@@ -1004,12 +1381,43 @@ if (originalUrl.searchParams.has(searchParamName)) {
1004
1381
  window.history.replaceState(null, '', url)
1005
1382
  }
1006
1383
  `;
1007
- return /* @__PURE__ */ jsx("script", {
1008
- id: "makeswift-preview-mode",
1009
- type: "module",
1010
- dangerouslySetInnerHTML: {
1011
- __html
1384
+ const makeswiftConnectionCheckScript = `
1385
+ const appOrigin = '${appOrigin.replace("'", "\\'")}'
1386
+
1387
+ if (window.parent !== window) {
1388
+ window.addEventListener('message', event => {
1389
+ if (
1390
+ event.origin === appOrigin &&
1391
+ event.data.type === '${ActionTypes.MAKESWIFT_CONNECTION_INIT}'
1392
+ ) {
1393
+ setInterval(() => {
1394
+ window.parent.postMessage({
1395
+ type: '${ActionTypes.MAKESWIFT_CONNECTION_CHECK}',
1396
+ payload: {
1397
+ currentUrl: window.location.href
1398
+ }
1399
+ }, appOrigin)
1400
+ }, 20)
1012
1401
  }
1402
+ })
1403
+
1404
+ window.parent.postMessage({ type: '${ActionTypes.MAKESWIFT_CONNECTION_INIT}' }, appOrigin)
1405
+ }
1406
+ `;
1407
+ return /* @__PURE__ */ jsxs(Fragment, {
1408
+ children: [/* @__PURE__ */ jsx("script", {
1409
+ id: "makeswift-preview-mode",
1410
+ type: "module",
1411
+ dangerouslySetInnerHTML: {
1412
+ __html: previewModeScript
1413
+ }
1414
+ }), /* @__PURE__ */ jsx("script", {
1415
+ id: "makeswift-connection-check",
1416
+ type: "module",
1417
+ dangerouslySetInnerHTML: {
1418
+ __html: makeswiftConnectionCheckScript
1419
+ }
1420
+ })]
1013
1421
  });
1014
1422
  }
1015
1423
  class Document$1 extends NextDocument {
@@ -1048,11 +1456,14 @@ class Document$1 extends NextDocument {
1048
1456
  });
1049
1457
  }
1050
1458
  }
1051
- const version = "0.6.3";
1052
1459
  function isErrorWithMessage(error) {
1053
1460
  return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
1054
1461
  }
1055
- function MakeswiftApiHandler(apiKey, { appOrigin = "https://app.makeswift.com", getFonts } = {}) {
1462
+ function MakeswiftApiHandler(apiKey, {
1463
+ appOrigin = "https://app.makeswift.com",
1464
+ apiOrigin = "https://api.makeswift.com",
1465
+ getFonts
1466
+ } = {}) {
1056
1467
  const cors = Cors({ origin: appOrigin });
1057
1468
  const previewModeProxy = createProxyServer();
1058
1469
  previewModeProxy.on("proxyReq", (proxyReq) => {
@@ -1088,7 +1499,8 @@ Read more about dynamic catch-all routes here: https://nextjs.org/docs/routing/d
1088
1499
  return res.status(401).json({ message: "Unauthorized" });
1089
1500
  return res.json({
1090
1501
  version,
1091
- previewMode: true
1502
+ previewMode: true,
1503
+ interactionMode: true
1092
1504
  });
1093
1505
  }
1094
1506
  case "revalidate": {
@@ -1161,6 +1573,35 @@ Read more here: https://nextjs.org/blog/next-12-2#on-demand-incremental-static-r
1161
1573
  const response = { elementTree: generatedElementTree };
1162
1574
  return res.json(response);
1163
1575
  }
1576
+ case "snapshot": {
1577
+ let validateBody = function(body2) {
1578
+ if (body2.pageId == null) {
1579
+ return res.status(400).json({ message: "Must define pageId." });
1580
+ }
1581
+ if (body2.currentSnapshot == null && body2.publishedElementTree == null) {
1582
+ return res.status(400).json({ message: "Either currentSnapshot or publishedElementTree must be defined." });
1583
+ }
1584
+ };
1585
+ const body = req.body;
1586
+ validateBody(body);
1587
+ const client = new Makeswift(apiKey, {
1588
+ apiOrigin
1589
+ });
1590
+ const snapshot = await client.unstable_createSnapshot({
1591
+ publishedResources: body.publishedResources,
1592
+ deletedResources: body.deletedResources,
1593
+ publishedElementTree: body.publishedElementTree,
1594
+ currentSnapshot: body.currentSnapshot
1595
+ });
1596
+ const usedResources = client.unstable_getSnapshotResourceMapping(snapshot);
1597
+ const response = {
1598
+ pageId: body.pageId,
1599
+ snapshot,
1600
+ livePageChanges: body.livePageChanges,
1601
+ usedResources
1602
+ };
1603
+ return res.json(response);
1604
+ }
1164
1605
  default:
1165
1606
  return res.status(404).json({ message: "Not Found" });
1166
1607
  }
@@ -1295,6 +1736,37 @@ const Page = memo(({
1295
1736
  }, snapshot.document.data.key)
1296
1737
  });
1297
1738
  });
1739
+ const unstable_Page = memo(({
1740
+ pageData
1741
+ }) => {
1742
+ function resourcesToCacheData(resources) {
1743
+ if (resources == null)
1744
+ return void 0;
1745
+ return {
1746
+ Swatch: resources.Swatch,
1747
+ File: resources.File,
1748
+ Typography: resources.Typography,
1749
+ PagePathnameSlice: resources.PagePathnameSlice,
1750
+ GlobalElement: resources.GlobalElement,
1751
+ Table: resources.Table,
1752
+ Snippet: resources.Snippet,
1753
+ Page: resources.Page,
1754
+ Site: resources.Site
1755
+ };
1756
+ }
1757
+ const client = useMemo(() => new MakeswiftClient({
1758
+ uri: new URL("graphql", pageData.options.apiOrigin).href,
1759
+ cacheData: resourcesToCacheData(pageData.snapshot.resources)
1760
+ }), [pageData]);
1761
+ return /* @__PURE__ */ jsx(RuntimeProvider, {
1762
+ client,
1763
+ rootElements: /* @__PURE__ */ new Map([[pageData.pageId, pageData.snapshot.elementTree]]),
1764
+ preview: pageData.options.preview,
1765
+ children: /* @__PURE__ */ jsx(unstable_Page$1, {
1766
+ pageData
1767
+ })
1768
+ });
1769
+ });
1298
1770
  const keys = (o) => Object.keys(o);
1299
1771
  const coalesce = (...args) => {
1300
1772
  let i;
@@ -2202,6 +2674,7 @@ function registerComponent$1(runtime) {
2202
2674
  text: RichText(() => ({
2203
2675
  preset: {
2204
2676
  document: {
2677
+ object: "document",
2205
2678
  nodes: [
2206
2679
  {
2207
2680
  object: "block",
@@ -3574,6 +4047,9 @@ function useIsInBuilder() {
3574
4047
  function useIsPreview() {
3575
4048
  return useSelector((state) => getIsPreview(state));
3576
4049
  }
4050
+ function useBuilderEditMode() {
4051
+ return useSelector((state) => getBuilderEditMode(state));
4052
+ }
3577
4053
  function useDispatch() {
3578
4054
  const store = useContext(StoreContext);
3579
4055
  return store.dispatch;
@@ -3705,5 +4181,5 @@ const DocumentReference = memo(forwardRef(function DocumentReference2({
3705
4181
  document: document2
3706
4182
  });
3707
4183
  }));
3708
- export { Page 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, pollBoxModel as U, useTypography as V, shallowMergeFallbacks as W, MakeswiftClient as X, getStaticPaths as Y, getStaticProps as Z, getServerSideProps as _, ReactRuntime as a, Makeswift as a0, PreviewModeScript as a1, Document$1 as a2, MakeswiftApiHandler as a3, 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 };
4184
+ 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 };
3709
4185
  //# sourceMappingURL=index.es.js.map