@makeswift/runtime 0.1.1 → 0.1.4

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 (95) hide show
  1. package/dist/Box.cjs.js +1 -0
  2. package/dist/Box.cjs.js.map +1 -1
  3. package/dist/Box.es.js +1 -0
  4. package/dist/Box.es.js.map +1 -1
  5. package/dist/Button.cjs.js +1 -1
  6. package/dist/Button.es.js +2 -2
  7. package/dist/Carousel.cjs.js +1 -1
  8. package/dist/Carousel.es.js +2 -2
  9. package/dist/Countdown.cjs.js +1 -0
  10. package/dist/Countdown.cjs.js.map +1 -1
  11. package/dist/Countdown.es.js +2 -1
  12. package/dist/Countdown.es.js.map +1 -1
  13. package/dist/Divider.cjs.js +1 -0
  14. package/dist/Divider.cjs.js.map +1 -1
  15. package/dist/Divider.es.js +2 -1
  16. package/dist/Divider.es.js.map +1 -1
  17. package/dist/Embed.cjs.js +1 -0
  18. package/dist/Embed.cjs.js.map +1 -1
  19. package/dist/Embed.es.js +2 -1
  20. package/dist/Embed.es.js.map +1 -1
  21. package/dist/Form.cjs.js +1 -1
  22. package/dist/Form.es.js +2 -2
  23. package/dist/Image.cjs2.js +1 -1
  24. package/dist/Image.es2.js +2 -2
  25. package/dist/Navigation.cjs.js +1 -1
  26. package/dist/Navigation.es.js +2 -2
  27. package/dist/Root.cjs.js +1 -0
  28. package/dist/Root.cjs.js.map +1 -1
  29. package/dist/Root.es.js +1 -0
  30. package/dist/Root.es.js.map +1 -1
  31. package/dist/SocialLinks.es.js +1 -1
  32. package/dist/Text.cjs.js +1 -1
  33. package/dist/Text.es.js +2 -2
  34. package/dist/Video.cjs.js +1 -0
  35. package/dist/Video.cjs.js.map +1 -1
  36. package/dist/Video.es.js +1 -0
  37. package/dist/Video.es.js.map +1 -1
  38. package/dist/components.cjs.js +4 -4
  39. package/dist/components.es.js +4 -4
  40. package/dist/controls.cjs.js +2 -0
  41. package/dist/controls.cjs.js.map +1 -1
  42. package/dist/controls.es.js +1 -1
  43. package/dist/cssMediaRules.es.js +1 -1
  44. package/dist/descriptors.cjs.js.map +1 -1
  45. package/dist/descriptors.es.js.map +1 -1
  46. package/dist/index.cjs.js +144 -21
  47. package/dist/index.cjs.js.map +1 -1
  48. package/dist/index.cjs3.js +2 -13
  49. package/dist/index.cjs3.js.map +1 -1
  50. package/dist/index.es.js +145 -23
  51. package/dist/index.es.js.map +1 -1
  52. package/dist/index.es2.js +1 -1
  53. package/dist/index.es3.js +3 -13
  54. package/dist/index.es3.js.map +1 -1
  55. package/dist/next.cjs.js +1 -0
  56. package/dist/next.cjs.js.map +1 -1
  57. package/dist/next.es.js +1 -0
  58. package/dist/next.es.js.map +1 -1
  59. package/dist/react-builder-preview.cjs.js +1 -0
  60. package/dist/react-builder-preview.cjs.js.map +1 -1
  61. package/dist/react-builder-preview.es.js +1 -0
  62. package/dist/react-builder-preview.es.js.map +1 -1
  63. package/dist/react.cjs.js +1 -0
  64. package/dist/react.cjs.js.map +1 -1
  65. package/dist/react.es.js +1 -0
  66. package/dist/react.es.js.map +1 -1
  67. package/dist/style.cjs.js +5 -2
  68. package/dist/style.cjs.js.map +1 -1
  69. package/dist/style.es.js +5 -2
  70. package/dist/style.es.js.map +1 -1
  71. package/dist/text-input.cjs.js +6 -0
  72. package/dist/text-input.cjs.js.map +1 -1
  73. package/dist/text-input.es.js +5 -1
  74. package/dist/text-input.es.js.map +1 -1
  75. package/dist/types/controls/control.d.ts +3 -2
  76. package/dist/types/controls/control.d.ts.map +1 -1
  77. package/dist/types/controls/index.d.ts +1 -0
  78. package/dist/types/controls/index.d.ts.map +1 -1
  79. package/dist/types/controls/link.d.ts +50 -0
  80. package/dist/types/controls/link.d.ts.map +1 -0
  81. package/dist/types/controls/style.d.ts +65 -0
  82. package/dist/types/controls/style.d.ts.map +1 -1
  83. package/dist/types/next.d.ts.map +1 -1
  84. package/dist/types/prop-controllers/descriptors.d.ts +4 -3
  85. package/dist/types/prop-controllers/descriptors.d.ts.map +1 -1
  86. package/dist/types/runtimes/react/controls/control.d.ts +3 -2
  87. package/dist/types/runtimes/react/controls/control.d.ts.map +1 -1
  88. package/dist/types/runtimes/react/controls/link.d.ts +11 -0
  89. package/dist/types/runtimes/react/controls/link.d.ts.map +1 -0
  90. package/dist/types/runtimes/react/controls/style.d.ts.map +1 -1
  91. package/dist/types/runtimes/react/controls.d.ts.map +1 -1
  92. package/dist/types/runtimes/react/index.d.ts.map +1 -1
  93. package/dist/useBoxShadow.es.js +1 -1
  94. package/dist/useMediaQuery.es.js +1 -1
  95. package/package.json +1 -1
package/dist/index.cjs.js CHANGED
@@ -57,6 +57,7 @@ var shape = require("./shape.cjs.js");
57
57
  var image = require("./image.cjs.js");
58
58
  var style = require("./style.cjs.js");
59
59
  var ColorHelper = require("color");
60
+ var scrollIntoView = require("scroll-into-view-if-needed");
60
61
  var reactDom = require("react-dom");
61
62
  var parse = require("html-react-parser");
62
63
  var Head = require("next/head");
@@ -91,6 +92,7 @@ var NextDocument__default = /* @__PURE__ */ _interopDefaultLegacy(NextDocument);
91
92
  var styled__default = /* @__PURE__ */ _interopDefaultLegacy(styled);
92
93
  var createEmotionServer__default = /* @__PURE__ */ _interopDefaultLegacy(createEmotionServer);
93
94
  var ColorHelper__default = /* @__PURE__ */ _interopDefaultLegacy(ColorHelper);
95
+ var scrollIntoView__default = /* @__PURE__ */ _interopDefaultLegacy(scrollIntoView);
94
96
  var parse__default = /* @__PURE__ */ _interopDefaultLegacy(parse);
95
97
  var Head__default = /* @__PURE__ */ _interopDefaultLegacy(Head);
96
98
  var uuid__default = /* @__PURE__ */ _interopDefaultLegacy(uuid);
@@ -869,29 +871,57 @@ class Document$1 extends NextDocument__default["default"] {
869
871
  }
870
872
  }
871
873
  const REVALIDATE_SECONDS = 1;
874
+ function getApiOrigin() {
875
+ var _a;
876
+ const apiOriginString = (_a = process["env"].MAKESWIFT_API_HOST) != null ? _a : "https://api.makeswift.com";
877
+ try {
878
+ const url = new URL(apiOriginString);
879
+ return url.origin;
880
+ } catch (error) {
881
+ const errorMessage = `"MAKESWIFT_API_HOST" environment variable must be a valid URL. Expected something like "https://api.makeswift.com" but instead received "${apiOriginString}".`;
882
+ throw new Error(errorMessage);
883
+ }
884
+ }
885
+ const uuidRegExp = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/;
886
+ function getApiKey() {
887
+ const apiKey = process["env"].MAKESWIFT_SITE_API_KEY;
888
+ if (apiKey == null) {
889
+ const errorMessage = '"MAKESWIFT_SITE_API_KEY" environment variable must be set. Please add your site API key to your `.env.local` file. More info: https://www.makeswift.com/docs/guides/manual-setup#add-environment-variables';
890
+ throw new Error(errorMessage);
891
+ }
892
+ if (!uuidRegExp.test(apiKey)) {
893
+ const errorMEssage = `Invalid Makeswift site API key "${apiKey}". Please check your \`.env.local\` file for the "MAKESWIFT_SITE_API_KEY" environment variable. More info: https://www.makeswift.com/docs/guides/manual-setup#add-environment-variables`;
894
+ throw new Error(errorMEssage);
895
+ }
896
+ return apiKey;
897
+ }
872
898
  async function getServerSideProps({
873
899
  query: {
874
900
  pageId
875
901
  }
876
902
  }) {
877
- const url = `${process["env"].MAKESWIFT_API_HOST}/v0/preview-page-data?id=${pageId}`;
903
+ const url = `${getApiOrigin()}/v0/preview-page-data?id=${pageId}`;
878
904
  const res = await fetch(url, {
879
905
  headers: {
880
- "x-api-key": process["env"].MAKESWIFT_SITE_API_KEY
906
+ "x-api-key": getApiKey()
881
907
  }
882
908
  });
883
- if (res.status !== 200) {
909
+ if (res.status === 404) {
884
910
  console.error(await res.json());
885
911
  return {
886
912
  notFound: true
887
913
  };
888
914
  }
915
+ if (!res.ok) {
916
+ const json = await res.json();
917
+ throw new Error(json.message);
918
+ }
889
919
  const page = await res.json();
890
920
  if (page == null)
891
921
  return {
892
922
  notFound: true
893
923
  };
894
- const makeswiftApiEndpoint = `${process["env"].MAKESWIFT_API_HOST}/graphql`;
924
+ const makeswiftApiEndpoint = `${getApiOrigin()}/graphql`;
895
925
  const client2 = new MakeswiftClient({
896
926
  uri: makeswiftApiEndpoint
897
927
  });
@@ -917,26 +947,30 @@ async function getStaticProps({
917
947
  const {
918
948
  path = []
919
949
  } = params;
920
- const url = `${process["env"].MAKESWIFT_API_HOST}/v0/live-page-data?path=${path.join("/")}`;
950
+ const url = `${getApiOrigin()}/v0/live-page-data?path=${path.join("/")}`;
921
951
  const res = await fetch(url, {
922
952
  headers: {
923
- "x-api-key": process["env"].MAKESWIFT_SITE_API_KEY
953
+ "x-api-key": getApiKey()
924
954
  }
925
955
  });
926
- if (res.status !== 200) {
956
+ if (res.status === 404) {
927
957
  console.error(await res.json());
928
958
  return {
929
959
  notFound: true,
930
960
  revalidate: REVALIDATE_SECONDS
931
961
  };
932
962
  }
963
+ if (!res.ok) {
964
+ const json = await res.json();
965
+ throw new Error(json.message);
966
+ }
933
967
  const page = await res.json();
934
968
  if (page == null)
935
969
  return {
936
970
  notFound: true,
937
971
  revalidate: REVALIDATE_SECONDS
938
972
  };
939
- const makeswiftApiEndpoint = `${process["env"].MAKESWIFT_API_HOST}/graphql`;
973
+ const makeswiftApiEndpoint = `${getApiOrigin()}/graphql`;
940
974
  const client2 = new MakeswiftClient({
941
975
  uri: makeswiftApiEndpoint
942
976
  });
@@ -2158,6 +2192,16 @@ function useFile(fileId) {
2158
2192
  }
2159
2193
  const isSSR = typeof window === "undefined" || /ServerSideRendering/.test(window.navigator && window.navigator.userAgent);
2160
2194
  const useIsomorphicLayoutEffect = isSSR ? React.useEffect : React.useLayoutEffect;
2195
+ function usePage(pageId) {
2196
+ const { error, data = {} } = useQuery(PAGE_PATHNAMES_BY_ID, {
2197
+ skip: pageId == null,
2198
+ variables: { ids: [pageId] }
2199
+ });
2200
+ if (pageId == null || error != null)
2201
+ return null;
2202
+ const { pagePathnamesById: [page] = [] } = data;
2203
+ return page;
2204
+ }
2161
2205
  function responsiveStyle(responsiveValues, join$1, strategy) {
2162
2206
  return join(responsiveValues, join$1, strategy).reduce((acc, { deviceId, value }) => {
2163
2207
  const device = getDevice(deviceId);
@@ -2444,10 +2488,11 @@ function useStyleControlCssObject(style$1, controlDefinition) {
2444
2488
  style$1 == null ? void 0 : style$1.margin,
2445
2489
  style$1 == null ? void 0 : style$1.padding,
2446
2490
  useBorder(style$1 == null ? void 0 : style$1.border),
2447
- style$1 == null ? void 0 : style$1.borderRadius
2448
- ], ([width, margin, padding, border, borderRadius]) => {
2449
- var _a, _b, _c, _d, _e2, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
2450
- return __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, properties.includes(style.StyleControlProperty.Width) && {
2491
+ style$1 == null ? void 0 : style$1.borderRadius,
2492
+ style$1 == null ? void 0 : style$1.textStyle
2493
+ ], ([width, margin, padding, border, borderRadius, textStyle]) => {
2494
+ var _a, _b, _c, _d, _e2, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
2495
+ return __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, properties.includes(style.StyleControlProperty.Width) && {
2451
2496
  width: (_a = widthToString(width)) != null ? _a : "100%"
2452
2497
  }), properties.includes(style.StyleControlProperty.Margin) && {
2453
2498
  marginTop: (_b = marginToString(margin == null ? void 0 : margin.marginTop)) != null ? _b : 0,
@@ -2469,7 +2514,10 @@ function useStyleControlCssObject(style$1, controlDefinition) {
2469
2514
  borderTopRightRadius: (_o = borderRadiusToString(borderRadius == null ? void 0 : borderRadius.borderTopRightRadius)) != null ? _o : 0,
2470
2515
  borderBottomRightRadius: (_p = borderRadiusToString(borderRadius == null ? void 0 : borderRadius.borderBottomRightRadius)) != null ? _p : 0,
2471
2516
  borderBottomLeftRadius: (_q = borderRadiusToString(borderRadius == null ? void 0 : borderRadius.borderBottomLeftRadius)) != null ? _q : 0
2472
- });
2517
+ }), properties.includes(style.StyleControlProperty.TextStyle) && __spreadProps(__spreadValues(__spreadValues(__spreadValues(__spreadValues({}, (textStyle == null ? void 0 : textStyle.fontFamily) && { fontFamily: textStyle.fontFamily }), (textStyle == null ? void 0 : textStyle.letterSpacing) && { letterSpacing: textStyle.letterSpacing }), (textStyle == null ? void 0 : textStyle.fontSize) && { fontSize: fontSizeToString(textStyle.fontSize) }), (textStyle == null ? void 0 : textStyle.fontWeight) && { fontWeight: textStyle.fontWeight }), {
2518
+ textTransform: (_r = textStyle == null ? void 0 : textStyle.textTransform) != null ? _r : [],
2519
+ fontStyle: (_s = textStyle == null ? void 0 : textStyle.fontStyle) != null ? _s : []
2520
+ }));
2473
2521
  }));
2474
2522
  function widthToString(widthProperty) {
2475
2523
  if (widthProperty == null)
@@ -2499,6 +2547,9 @@ function useStyleControlCssObject(style$1, controlDefinition) {
2499
2547
  return null;
2500
2548
  return `${borderRadius.value}${borderRadius.unit}`;
2501
2549
  }
2550
+ function fontSizeToString(fontSize) {
2551
+ return `${fontSize.value}${fontSize.unit}`;
2552
+ }
2502
2553
  }
2503
2554
  const useInsertionEffectSpecifier = "useInsertionEffect";
2504
2555
  const useInsertionEffect = (_e = React__namespace[useInsertionEffectSpecifier]) != null ? _e : React__namespace.useLayoutEffect;
@@ -2561,6 +2612,71 @@ function useImageControlValue(data, definition) {
2561
2612
  dimensions: { width: file.dimensions.width, height: file.dimensions.height }
2562
2613
  };
2563
2614
  }
2615
+ function useLinkControlValue(link, _definition) {
2616
+ var _a;
2617
+ const page = usePage(link && link.type === "OPEN_PAGE" ? link.payload.pageId : null);
2618
+ const elementKey = (link == null ? void 0 : link.type) === "SCROLL_TO_ELEMENT" ? (_a = link.payload.elementIdConfig) == null ? void 0 : _a.elementKey : null;
2619
+ const elementId = useElementId(elementKey);
2620
+ let href = "#";
2621
+ let target;
2622
+ let block;
2623
+ if (link) {
2624
+ switch (link.type) {
2625
+ case "OPEN_PAGE": {
2626
+ if (page)
2627
+ href = `/${page.pathname}`;
2628
+ target = link.payload.openInNewTab ? "_blank" : "_self";
2629
+ break;
2630
+ }
2631
+ case "OPEN_URL": {
2632
+ href = link.payload.url;
2633
+ target = link.payload.openInNewTab ? "_blank" : "_self";
2634
+ break;
2635
+ }
2636
+ case "SEND_EMAIL": {
2637
+ const { to, subject = "", body = "" } = link.payload;
2638
+ if (to != null)
2639
+ href = `mailto:${to}?subject=${subject}&body=${body}`;
2640
+ break;
2641
+ }
2642
+ case "CALL_PHONE": {
2643
+ href = `tel:${link.payload.phoneNumber}`;
2644
+ break;
2645
+ }
2646
+ case "SCROLL_TO_ELEMENT": {
2647
+ href = `#${elementId != null ? elementId : ""}`;
2648
+ block = link.payload.block;
2649
+ break;
2650
+ }
2651
+ default:
2652
+ throw new RangeError(`Invalid link type "${link.type}."`);
2653
+ }
2654
+ }
2655
+ const handleClick = React.useCallback((event) => {
2656
+ if (event.defaultPrevented)
2657
+ return;
2658
+ if (link && link.type === "SCROLL_TO_ELEMENT") {
2659
+ let hash;
2660
+ try {
2661
+ hash = new URL(`http://www.example.com/${href}`).hash;
2662
+ } catch (error) {
2663
+ console.error(`Link received invalid href: ${href}`, error);
2664
+ }
2665
+ if (href != null && href === hash) {
2666
+ event.preventDefault();
2667
+ const view = event.view;
2668
+ scrollIntoView__default["default"](view.document.querySelector(hash), {
2669
+ behavior: "smooth",
2670
+ block
2671
+ });
2672
+ if (view.location.hash !== hash)
2673
+ view.history.pushState({}, "", hash);
2674
+ }
2675
+ }
2676
+ }, [link, href, block]);
2677
+ const res = React.useMemo(() => ({ href, target, onClick: handleClick }), [href, target, handleClick]);
2678
+ return res;
2679
+ }
2564
2680
  function ListControlValue({
2565
2681
  definition,
2566
2682
  data,
@@ -2645,6 +2761,12 @@ function ControlValue({
2645
2761
  parameters: [data, definition],
2646
2762
  children: (value) => children(value)
2647
2763
  }, definition.type);
2764
+ case textInput.LinkControlType:
2765
+ return /* @__PURE__ */ jsxRuntime.jsx(RenderHook, {
2766
+ hook: useLinkControlValue,
2767
+ parameters: [data, definition],
2768
+ children: (value) => children(value)
2769
+ }, definition.type);
2648
2770
  case shape.ComboboxControlType:
2649
2771
  return /* @__PURE__ */ jsxRuntime.jsx(RenderHook, {
2650
2772
  hook: useComboboxControlValue,
@@ -2695,6 +2817,7 @@ function PropsValue({
2695
2817
  case shape.ComboboxControlType:
2696
2818
  case shape.ShapeControlType:
2697
2819
  case shape.ListControlType:
2820
+ case textInput.LinkControlType:
2698
2821
  return /* @__PURE__ */ jsxRuntime.jsx(ControlValue, {
2699
2822
  definition: descriptor,
2700
2823
  data: props[propName],
@@ -2889,17 +3012,17 @@ function useDispatch() {
2889
3012
  const store = React.useContext(Context);
2890
3013
  return store.dispatch;
2891
3014
  }
2892
- function useSuppressRefWarning(ownerName) {
2893
- const originalErrorRef = React.useRef(console.error);
2894
- const patchedRef = React.useRef(false);
2895
- if (patchedRef.current === false) {
3015
+ const originalError = console.error;
3016
+ let patched = false;
3017
+ function suppressRefWarning(ownerName) {
3018
+ if (patched === false) {
2896
3019
  console.error = (...args) => {
2897
3020
  if (typeof args[0] === "string" && args[0].includes("Function components cannot be given refs.") && args[0].includes(`Check the render method of \`${ownerName}\`.`)) {
2898
3021
  return;
2899
3022
  }
2900
- return originalErrorRef.current(...args);
3023
+ return originalError(...args);
2901
3024
  };
2902
- patchedRef.current = true;
3025
+ patched = true;
2903
3026
  }
2904
3027
  }
2905
3028
  const ElementData = React.memo(React.forwardRef(function ElementData2({
@@ -2909,7 +3032,7 @@ const ElementData = React.memo(React.forwardRef(function ElementData2({
2909
3032
  const [handle, setHandle] = React.useState(null);
2910
3033
  const [foundDomNode, setFoundDomNode] = React.useState(null);
2911
3034
  React.useImperativeHandle(ref, () => handle != null ? handle : foundDomNode, [handle, foundDomNode]);
2912
- useSuppressRefWarning(`\`ForwardRef(${ElementData2.name})\``);
3035
+ suppressRefWarning(`\`ForwardRef(${ElementData2.name})\``);
2913
3036
  if (Component == null) {
2914
3037
  return /* @__PURE__ */ jsxRuntime.jsx(FallbackComponent, {
2915
3038
  ref,
@@ -3033,7 +3156,6 @@ exports.DocumentReference = DocumentReference;
3033
3156
  exports.Element = Element;
3034
3157
  exports.FILES_BY_ID = FILES_BY_ID;
3035
3158
  exports.MakeswiftClient = MakeswiftClient;
3036
- exports.PAGE_PATHNAMES_BY_ID = PAGE_PATHNAMES_BY_ID;
3037
3159
  exports.PAGE_SNIPPETS_QUERY = PAGE_SNIPPETS_QUERY;
3038
3160
  exports.Page = Page$1;
3039
3161
  exports.Page$1 = Page;
@@ -3067,6 +3189,7 @@ exports.useFormContext = useFormContext;
3067
3189
  exports.useIsInBuilder = useIsInBuilder;
3068
3190
  exports.useIsomorphicLayoutEffect = useIsomorphicLayoutEffect;
3069
3191
  exports.useMutation = useMutation;
3192
+ exports.usePage = usePage;
3070
3193
  exports.usePageId = usePageId;
3071
3194
  exports.useQuery = useQuery;
3072
3195
  exports.useResponsiveColor = useResponsiveColor;