@makeswift/runtime 0.3.1 → 0.4.0

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 (181) hide show
  1. package/dist/Box.cjs.js +3 -4
  2. package/dist/Box.cjs.js.map +1 -1
  3. package/dist/Box.es.js +4 -5
  4. package/dist/Box.es.js.map +1 -1
  5. package/dist/Button.cjs.js +2 -3
  6. package/dist/Button.cjs.js.map +1 -1
  7. package/dist/Button.es.js +3 -4
  8. package/dist/Button.es.js.map +1 -1
  9. package/dist/Carousel.cjs.js +3 -4
  10. package/dist/Carousel.cjs.js.map +1 -1
  11. package/dist/Carousel.es.js +4 -5
  12. package/dist/Carousel.es.js.map +1 -1
  13. package/dist/Countdown.cjs.js +2 -3
  14. package/dist/Countdown.cjs.js.map +1 -1
  15. package/dist/Countdown.es.js +3 -4
  16. package/dist/Countdown.es.js.map +1 -1
  17. package/dist/Divider.cjs.js +2 -3
  18. package/dist/Divider.cjs.js.map +1 -1
  19. package/dist/Divider.es.js +3 -4
  20. package/dist/Divider.es.js.map +1 -1
  21. package/dist/Embed.cjs.js +2 -3
  22. package/dist/Embed.cjs.js.map +1 -1
  23. package/dist/Embed.es.js +3 -4
  24. package/dist/Embed.es.js.map +1 -1
  25. package/dist/Form.cjs.js +5 -33
  26. package/dist/Form.cjs.js.map +1 -1
  27. package/dist/Form.es.js +6 -34
  28. package/dist/Form.es.js.map +1 -1
  29. package/dist/Image.cjs.js +3 -4
  30. package/dist/Image.cjs.js.map +1 -1
  31. package/dist/Image.es.js +4 -5
  32. package/dist/Image.es.js.map +1 -1
  33. package/dist/Navigation.cjs.js +3 -4
  34. package/dist/Navigation.cjs.js.map +1 -1
  35. package/dist/Navigation.es.js +4 -5
  36. package/dist/Navigation.es.js.map +1 -1
  37. package/dist/Root.cjs.js +2 -3
  38. package/dist/Root.cjs.js.map +1 -1
  39. package/dist/Root.es.js +3 -4
  40. package/dist/Root.es.js.map +1 -1
  41. package/dist/SocialLinks.cjs.js +2 -3
  42. package/dist/SocialLinks.cjs.js.map +1 -1
  43. package/dist/SocialLinks.es.js +3 -4
  44. package/dist/SocialLinks.es.js.map +1 -1
  45. package/dist/Text.cjs.js +18 -31
  46. package/dist/Text.cjs.js.map +1 -1
  47. package/dist/Text.es.js +19 -32
  48. package/dist/Text.es.js.map +1 -1
  49. package/dist/Video.cjs.js +2 -3
  50. package/dist/Video.cjs.js.map +1 -1
  51. package/dist/Video.es.js +3 -4
  52. package/dist/Video.es.js.map +1 -1
  53. package/dist/actions.cjs.js +27 -1
  54. package/dist/actions.cjs.js.map +1 -1
  55. package/dist/actions.es.js +24 -2
  56. package/dist/actions.es.js.map +1 -1
  57. package/dist/api.cjs.js +380 -3
  58. package/dist/api.cjs.js.map +1 -1
  59. package/dist/api.es.js +379 -2
  60. package/dist/api.es.js.map +1 -1
  61. package/dist/components.cjs.js +3 -8
  62. package/dist/components.cjs.js.map +1 -1
  63. package/dist/components.es.js +4 -5
  64. package/dist/components.es.js.map +1 -1
  65. package/dist/grid-item.cjs.js +14 -34
  66. package/dist/grid-item.cjs.js.map +1 -1
  67. package/dist/grid-item.es.js +15 -35
  68. package/dist/grid-item.es.js.map +1 -1
  69. package/dist/index.cjs.js +611 -485
  70. package/dist/index.cjs.js.map +1 -1
  71. package/dist/index.cjs2.js +2 -1
  72. package/dist/index.cjs2.js.map +1 -1
  73. package/dist/index.es.js +596 -467
  74. package/dist/index.es.js.map +1 -1
  75. package/dist/index.es2.js +3 -2
  76. package/dist/index.es2.js.map +1 -1
  77. package/dist/index.es3.js +1 -1
  78. package/dist/next.cjs.js +2 -3
  79. package/dist/next.cjs.js.map +1 -1
  80. package/dist/next.es.js +3 -4
  81. package/dist/next.es.js.map +1 -1
  82. package/dist/react-builder-preview.cjs.js +5 -20
  83. package/dist/react-builder-preview.cjs.js.map +1 -1
  84. package/dist/react-builder-preview.es.js +8 -23
  85. package/dist/react-builder-preview.es.js.map +1 -1
  86. package/dist/react-page.cjs.js +88 -64
  87. package/dist/react-page.cjs.js.map +1 -1
  88. package/dist/react-page.es.js +82 -65
  89. package/dist/react-page.es.js.map +1 -1
  90. package/dist/react.cjs.js +2 -3
  91. package/dist/react.cjs.js.map +1 -1
  92. package/dist/react.es.js +2 -3
  93. package/dist/react.es.js.map +1 -1
  94. package/dist/slot.cjs.js.map +1 -1
  95. package/dist/slot.es.js.map +1 -1
  96. package/dist/types/src/api/graphql/client.d.ts +6 -0
  97. package/dist/types/src/api/graphql/client.d.ts.map +1 -0
  98. package/dist/types/src/api/graphql/documents/fragments.d.ts +10 -0
  99. package/dist/types/src/api/graphql/documents/fragments.d.ts.map +1 -0
  100. package/dist/types/src/api/graphql/documents/index.d.ts +3 -0
  101. package/dist/types/src/api/graphql/documents/index.d.ts.map +1 -0
  102. package/dist/types/src/api/graphql/documents/queries.d.ts +12 -0
  103. package/dist/types/src/api/graphql/documents/queries.d.ts.map +1 -0
  104. package/dist/types/src/api/graphql/fragments.d.ts +4 -0
  105. package/dist/types/src/api/graphql/fragments.d.ts.map +1 -0
  106. package/dist/types/src/api/graphql/generated/fragment-document-nodes.d.ts +11 -0
  107. package/dist/types/src/api/graphql/generated/fragment-document-nodes.d.ts.map +1 -0
  108. package/dist/types/src/api/graphql/generated/types.d.ts +508 -0
  109. package/dist/types/src/api/graphql/generated/types.d.ts.map +1 -0
  110. package/dist/types/src/api/{types.d.ts → graphql/types.d.ts} +6 -2
  111. package/dist/types/src/api/graphql/types.d.ts.map +1 -0
  112. package/dist/types/src/api/index.d.ts +2 -2
  113. package/dist/types/src/api/index.d.ts.map +1 -1
  114. package/dist/types/src/api/introspection.d.ts +4 -3
  115. package/dist/types/src/api/introspection.d.ts.map +1 -1
  116. package/dist/types/src/api/react.d.ts +49 -14
  117. package/dist/types/src/api/react.d.ts.map +1 -1
  118. package/dist/types/src/components/builtin/Button/Button.d.ts +1 -1
  119. package/dist/types/src/components/builtin/Form/Form.d.ts.map +1 -1
  120. package/dist/types/src/components/builtin/Form/components/Field/components/Checkbox/index.d.ts +1 -1
  121. package/dist/types/src/components/builtin/Text/components/RichTextEditor/components/Mark/hooks/useTypographyMark.d.ts +2 -2
  122. package/dist/types/src/components/builtin/Text/components/RichTextEditor/components/Mark/hooks/useTypographyMark.d.ts.map +1 -1
  123. package/dist/types/src/components/hooks/index.d.ts +0 -2
  124. package/dist/types/src/components/hooks/index.d.ts.map +1 -1
  125. package/dist/types/src/components/hooks/useBackgrounds.d.ts +10 -10
  126. package/dist/types/src/components/hooks/useBackgrounds.d.ts.map +1 -1
  127. package/dist/types/src/components/hooks/useBorder.d.ts.map +1 -1
  128. package/dist/types/src/components/hooks/useBoxShadow.d.ts.map +1 -1
  129. package/dist/types/src/components/hooks/useResponsiveColor.d.ts.map +1 -1
  130. package/dist/types/src/components/page/Page.d.ts +0 -20
  131. package/dist/types/src/components/page/Page.d.ts.map +1 -1
  132. package/dist/types/src/components/shared/BackgroundsContainer/index.d.ts +7 -7
  133. package/dist/types/src/components/shared/Link/index.d.ts.map +1 -1
  134. package/dist/types/src/next/client.d.ts +2 -2
  135. package/dist/types/src/next/client.d.ts.map +1 -1
  136. package/dist/types/src/prop-controllers/descriptors.d.ts +1 -1
  137. package/dist/types/src/prop-controllers/descriptors.d.ts.map +1 -1
  138. package/dist/types/src/prop-controllers/introspection.d.ts +8 -1
  139. package/dist/types/src/prop-controllers/introspection.d.ts.map +1 -1
  140. package/dist/types/src/runtimes/react/controls/color.d.ts +1 -1
  141. package/dist/types/src/runtimes/react/controls/color.d.ts.map +1 -1
  142. package/dist/types/src/runtimes/react/controls/image.d.ts.map +1 -1
  143. package/dist/types/src/runtimes/react/controls/link.d.ts.map +1 -1
  144. package/dist/types/src/runtimes/react/hooks/makeswift-api.d.ts +10 -0
  145. package/dist/types/src/runtimes/react/hooks/makeswift-api.d.ts.map +1 -0
  146. package/dist/types/src/runtimes/react/index.d.ts.map +1 -1
  147. package/dist/types/src/state/actions.d.ts +41 -2
  148. package/dist/types/src/state/actions.d.ts.map +1 -1
  149. package/dist/types/src/state/makeswift-api-client.d.ts +31 -0
  150. package/dist/types/src/state/makeswift-api-client.d.ts.map +1 -0
  151. package/dist/types/src/state/modules/api-resources.d.ts +14 -0
  152. package/dist/types/src/state/modules/api-resources.d.ts.map +1 -0
  153. package/dist/types/src/state/react-builder-preview.d.ts +2 -2
  154. package/dist/types/src/state/react-builder-preview.d.ts.map +1 -1
  155. package/dist/types.cjs.js +14 -0
  156. package/dist/types.cjs.js.map +1 -0
  157. package/dist/types.es.js +13 -0
  158. package/dist/types.es.js.map +1 -0
  159. package/dist/useMediaQuery.es.js +1 -1
  160. package/package.json +2 -3
  161. package/dist/constants.cjs.js +0 -15
  162. package/dist/constants.cjs.js.map +0 -1
  163. package/dist/constants.es.js +0 -14
  164. package/dist/constants.es.js.map +0 -1
  165. package/dist/graphql.cjs.js +0 -365
  166. package/dist/graphql.cjs.js.map +0 -1
  167. package/dist/graphql.es.js +0 -356
  168. package/dist/graphql.es.js.map +0 -1
  169. package/dist/types/src/api/constants.d.ts +0 -4
  170. package/dist/types/src/api/constants.d.ts.map +0 -1
  171. package/dist/types/src/api/generated/graphql.d.ts +0 -175
  172. package/dist/types/src/api/generated/graphql.d.ts.map +0 -1
  173. package/dist/types/src/api/types.d.ts.map +0 -1
  174. package/dist/types/src/components/hooks/useFile.d.ts +0 -13
  175. package/dist/types/src/components/hooks/useFile.d.ts.map +0 -1
  176. package/dist/types/src/components/hooks/usePage.d.ts +0 -7
  177. package/dist/types/src/components/hooks/usePage.d.ts.map +0 -1
  178. package/dist/types/src/components/hooks/useTypography.d.ts +0 -24
  179. package/dist/types/src/components/hooks/useTypography.d.ts.map +0 -1
  180. package/dist/types/src/components/utils/queries.d.ts +0 -10
  181. package/dist/types/src/components/utils/queries.d.ts.map +0 -1
package/dist/index.es.js CHANGED
@@ -38,19 +38,20 @@ var _c;
38
38
  import * as React from "react";
39
39
  import { useEffect, createContext, useContext, useState, useMemo, useRef, Children, createElement, forwardRef, memo, useLayoutEffect, useCallback, Component, useImperativeHandle } from "react";
40
40
  import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector";
41
- 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, k as getComponentPropControllerDescriptors, l as getPropControllers, m as configureStore, n as getDocument, o as getElementId, p as getIsInBuilder, q as copyElementTree, c as createDocument, r as getReactComponent } from "./react-page.es.js";
42
- import { f as registerDocumentEffect, g as registerComponentHandleEffect, h as mountComponentEffect, j as registerComponentEffect, k as registerReactComponentEffect } from "./actions.es.js";
41
+ 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, k as getBorderSwatchIds, l as isNonNullable, m as getBoxShadowsSwatchIds, n as getResponsiveColorSwatchIds, o as getComponentPropControllerDescriptors, p as getPropControllers, q as configureStore$1, r as getDocument, s as getElementId, t as getIsInBuilder, u as copyElementTree, c as createDocument, v as getReactComponent } from "./react-page.es.js";
42
+ import { A as ActionTypes, f as introspectedResourcesFulfilled, g as apiResourceFulfilled, t as typographiesFulfilled, h as restoreAPIResourcesCache, j as registerDocumentEffect, k as registerComponentHandleEffect, l as mountComponentEffect, n as registerComponentEffect, o as registerReactComponentEffect } from "./actions.es.js";
43
43
  import dynamic from "next/dynamic";
44
- import { gql, useQuery as useQuery$1, useMutation as useMutation$1, ApolloClient, InMemoryCache } from "@apollo/client";
45
- import { BatchHttpLink } from "@apollo/client/link/batch-http";
46
44
  import { KeyUtils } from "slate";
47
- import { P as PagePathnameSliceFragmentDoc } from "./graphql.es.js";
48
45
  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, a as StyleControlType } from "./slot.es.js";
49
46
  import { S as ShapeControlType, L as ListControlType, b as LinkControlType } from "./control.es.js";
47
+ import { createStore, applyMiddleware } from "redux";
48
+ import thunk from "redux-thunk";
49
+ import { A as APIResourceType } from "./types.es.js";
50
50
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
51
51
  import { cache, cx } from "@emotion/css";
52
52
  import { S as SelectControlType, T as TextAreaControlType, a as TextInputControlType, N as NumberControlType, C as CheckboxControlType } from "./text-input.es.js";
53
53
  import { C as ComboboxControlType } from "./combobox.es.js";
54
+ import { useSyncExternalStore } from "use-sync-external-store/shim";
54
55
  import ColorHelper from "color";
55
56
  import scrollIntoView from "scroll-into-view-if-needed";
56
57
  import { g as getBox } from "./box-models.es.js";
@@ -109,182 +110,7 @@ function BodySnippet({
109
110
  }, [code, cleanup]);
110
111
  return null;
111
112
  }
112
- const SWATCHES_BY_ID = gql`
113
- query SwatchesById($ids: [ID!]!) {
114
- swatches(ids: $ids) {
115
- id
116
- hue
117
- saturation
118
- lightness
119
- }
120
- }
121
- `;
122
- const FILE_BY_ID = gql`
123
- query FileById($id: ID!) {
124
- file(id: $id) {
125
- id
126
- name
127
- publicUrl
128
- extension
129
- dimensions {
130
- width
131
- height
132
- }
133
- }
134
- }
135
- `;
136
- const FILES_BY_ID = gql`
137
- query FilesById($ids: [ID!]!) {
138
- files(ids: $ids) {
139
- id
140
- name
141
- publicUrl
142
- extension
143
- dimensions {
144
- width
145
- height
146
- }
147
- }
148
- }
149
- `;
150
- const PAGE_PATHNAMES_BY_ID = gql`
151
- query PagePathnamesById($ids: [ID!]!) {
152
- pagePathnamesById(ids: $ids) {
153
- id
154
- pathname
155
- }
156
- }
157
- `;
158
- const TYPOGRAPHY_FRAGMENT = gql`
159
- fragment Typography on Typography {
160
- id
161
- name
162
- style {
163
- deviceId
164
- value {
165
- fontFamily
166
- fontSize {
167
- value
168
- unit
169
- }
170
- color {
171
- swatchId
172
- alpha
173
- }
174
- lineHeight
175
- letterSpacing
176
- fontWeight
177
- textAlign
178
- uppercase
179
- underline
180
- strikethrough
181
- italic
182
- }
183
- }
184
- }
185
- `;
186
- const TYPOGRAPHIES_BY_ID = gql`
187
- query TypographiesById($ids: [ID!]!) {
188
- typographies(ids: $ids) {
189
- ...Typography
190
- }
191
- }
192
-
193
- ${TYPOGRAPHY_FRAGMENT}
194
- `;
195
- const TABLE_BY_ID = gql`
196
- query TableById($id: ID!) {
197
- table(id: $id) {
198
- id
199
- name
200
- columns {
201
- id
202
- name
203
- ... on MultipleSelectTableColumn {
204
- options {
205
- id
206
- name
207
- }
208
- }
209
- ... on SingleSelectTableColumn {
210
- options {
211
- id
212
- name
213
- }
214
- }
215
- }
216
- }
217
- }
218
- `;
219
- const ELEMENT_REFERENCE_GLOBAL_ELEMENT = gql`
220
- query ElementReferenceGlobalElement($id: ID!) {
221
- globalElement(id: $id) {
222
- id
223
- data
224
- }
225
- }
226
- `;
227
- const INTROSPECTION_QUERY = gql`
228
- query Introspection(
229
- $swatchIds: [ID!]!
230
- $fileIds: [ID!]!
231
- $pageIds: [ID!]!
232
- $typographyIds: [ID!]!
233
- $tableIds: [ID!]!
234
- ) {
235
- swatches(ids: $swatchIds) {
236
- id
237
- hue
238
- saturation
239
- lightness
240
- }
241
-
242
- files(ids: $fileIds) {
243
- id
244
- name
245
- publicUrl
246
- extension
247
- dimensions {
248
- width
249
- height
250
- }
251
- }
252
-
253
- pagePathnamesById(ids: $pageIds) {
254
- id
255
- pathname
256
- }
257
-
258
- typographies(ids: $typographyIds) {
259
- ...Typography
260
- }
261
-
262
- tables(ids: $tableIds) {
263
- id
264
- name
265
- columns {
266
- id
267
- name
268
- ... on MultipleSelectTableColumn {
269
- options {
270
- id
271
- name
272
- }
273
- }
274
- ... on SingleSelectTableColumn {
275
- options {
276
- id
277
- name
278
- }
279
- }
280
- }
281
- }
282
- }
283
-
284
- ${TYPOGRAPHY_FRAGMENT}
285
- `;
286
113
  async function introspect(element, client, store) {
287
- var _a, _b, _c2;
288
114
  const descriptors = getPropControllerDescriptors(store.getState());
289
115
  const swatchIds = /* @__PURE__ */ new Set();
290
116
  const fileIds = /* @__PURE__ */ new Set();
@@ -336,11 +162,8 @@ async function introspect(element, client, store) {
336
162
  };
337
163
  let element2;
338
164
  if (isElementReference(current)) {
339
- const query = await client.query({
340
- query: ELEMENT_REFERENCE_GLOBAL_ELEMENT,
341
- variables: { id: current.value }
342
- });
343
- const elementData = (_b = (_a = query.data) == null ? void 0 : _a.globalElement) == null ? void 0 : _b.data;
165
+ const globalElement = await client.fetchGlobalElement(current.value);
166
+ const elementData = globalElement == null ? void 0 : globalElement.data;
344
167
  if (elementData == null)
345
168
  continue;
346
169
  element2 = elementData;
@@ -352,14 +175,11 @@ async function introspect(element, client, store) {
352
175
  continue;
353
176
  getResourcesFromElementDescriptors(elementDescriptors, element2.props);
354
177
  }
355
- const typographiesResult = await client.query({
356
- query: TYPOGRAPHIES_BY_ID,
357
- variables: { ids: [...typographyIds] }
358
- });
359
- (_c2 = typographiesResult == null ? void 0 : typographiesResult.data) == null ? void 0 : _c2.typographies.forEach((typography) => {
178
+ const typographies = await client.fetchTypographies([...typographyIds]);
179
+ typographies.forEach((typography) => {
360
180
  typography == null ? void 0 : typography.style.forEach((style) => {
361
- var _a2;
362
- const swatchId = (_a2 = style.value.color) == null ? void 0 : _a2.swatchId;
181
+ var _a;
182
+ const swatchId = (_a = style.value.color) == null ? void 0 : _a.swatchId;
363
183
  if (swatchId != null)
364
184
  swatchIds.add(swatchId);
365
185
  });
@@ -372,157 +192,466 @@ async function introspect(element, client, store) {
372
192
  pageIds: [...pageIds]
373
193
  };
374
194
  }
375
- const typePolicies = {
376
- Query: {
377
- fields: {
378
- swatches(existingData, {
379
- args,
380
- toReference
381
- }) {
382
- return existingData != null ? existingData : args == null ? void 0 : args.ids.map((id) => toReference({
383
- __typename: "Swatch",
384
- id
385
- }, true));
386
- },
387
- file(existingData, {
388
- args,
389
- toReference
390
- }) {
391
- return existingData != null ? existingData : toReference({
392
- __typename: "File",
393
- id: args == null ? void 0 : args.id
394
- }, true);
395
- },
396
- files(existingData, {
397
- args,
398
- toReference
399
- }) {
400
- return existingData != null ? existingData : args == null ? void 0 : args.ids.map((id) => toReference({
401
- __typename: "File",
402
- id
403
- }, true));
404
- },
405
- typographies(existingData, {
406
- args,
407
- toReference
408
- }) {
409
- return existingData != null ? existingData : args == null ? void 0 : args.ids.map((id) => toReference({
410
- __typename: "Typography",
195
+ function getInitialState(serializedState = []) {
196
+ return new Map(serializedState.map(([resourceType, resources]) => [resourceType, new Map(resources)]));
197
+ }
198
+ function getSerializedState$1(state) {
199
+ return Array.from(state.entries()).map(([resourceType, resources]) => [
200
+ resourceType,
201
+ Array.from(resources.entries())
202
+ ]);
203
+ }
204
+ function getHasAPIResource$1(state, resourceType, resourceId) {
205
+ var _a, _b;
206
+ return (_b = (_a = state.get(resourceType)) == null ? void 0 : _a.has(resourceId)) != null ? _b : false;
207
+ }
208
+ function getAPIResource$1(state, resourceType, resourceId) {
209
+ var _a;
210
+ const resource = (_a = state.get(resourceType)) == null ? void 0 : _a.get(resourceId);
211
+ return (resource == null ? void 0 : resource.__typename) === resourceType ? resource : null;
212
+ }
213
+ function reducer$1(state = getInitialState(), action) {
214
+ switch (action.type) {
215
+ case ActionTypes.RESTORE_API_RESOURCES_CACHE:
216
+ return getInitialState(action.payload.serializedState);
217
+ case ActionTypes.API_RESOURCE_FULFILLED: {
218
+ const { resourceType, resourceId, resource } = action.payload;
219
+ return new Map(state).set(resourceType, new Map(state.get(resourceType)).set(resourceId, resource));
220
+ }
221
+ case ActionTypes.INTROSPECTED_RESOURCES_FULFILLED: {
222
+ const { introspectedResourceIds, introspectedResources } = action.payload;
223
+ const swatches = new Map(state.get(APIResourceType.Swatch));
224
+ const files = new Map(state.get(APIResourceType.File));
225
+ const typographies = new Map(state.get(APIResourceType.Typography));
226
+ const pagePathnameSlices = new Map(state.get(APIResourceType.PagePathnameSlice));
227
+ const tables = new Map(state.get(APIResourceType.Table));
228
+ introspectedResourceIds.swatchIds.forEach((swatchId, idx) => {
229
+ swatches.set(swatchId, introspectedResources.swatches[idx]);
230
+ });
231
+ introspectedResourceIds.fileIds.forEach((fileId, idx) => {
232
+ files.set(fileId, introspectedResources.files[idx]);
233
+ });
234
+ introspectedResourceIds.typographyIds.forEach((typographyId, idx) => {
235
+ typographies.set(typographyId, introspectedResources.typographies[idx]);
236
+ });
237
+ introspectedResourceIds.pageIds.forEach((pageId, idx) => {
238
+ const pagePathnameSlice = introspectedResources.pagePathnamesById[idx];
239
+ if (pagePathnameSlice == null) {
240
+ pagePathnameSlices.set(pageId, pagePathnameSlice);
241
+ } else {
242
+ const nodeId = Buffer.from(`Page:${pagePathnameSlice.id}`).toString("base64");
243
+ pagePathnameSlices.set(nodeId, __spreadProps(__spreadValues({}, pagePathnameSlice), { id: nodeId }));
244
+ }
245
+ });
246
+ introspectedResourceIds.tableIds.forEach((tableId, idx) => {
247
+ tables.set(tableId, introspectedResources.tables[idx]);
248
+ });
249
+ return new Map(state).set(APIResourceType.Swatch, swatches).set(APIResourceType.File, files).set(APIResourceType.Typography, typographies).set(APIResourceType.PagePathnameSlice, pagePathnameSlices).set(APIResourceType.Table, tables);
250
+ }
251
+ case ActionTypes.TYPOGRAPHIES_FULFILLED: {
252
+ const typographies = new Map(state.get(APIResourceType.Typography));
253
+ action.payload.typographyIds.forEach((typographyId, idx) => {
254
+ typographies.set(typographyId, action.payload.typographies[idx]);
255
+ });
256
+ return new Map(state).set(APIResourceType.Typography, typographies);
257
+ }
258
+ case ActionTypes.CHANGE_API_RESOURCE:
259
+ return new Map(state).set(action.payload.resource.__typename, new Map(state.get(action.payload.resource.__typename)).set(action.payload.resource.id, action.payload.resource));
260
+ case ActionTypes.EVICT_API_RESOURCE: {
261
+ const [resourceType, resourceId] = action.payload.id.split(":");
262
+ if (!(resourceType in APIResourceType))
263
+ return state;
264
+ const resources = new Map(state.get(resourceType));
265
+ const deleted = resources.delete(resourceId);
266
+ return deleted ? new Map(state).set(resourceType, resources) : state;
267
+ }
268
+ default:
269
+ return state;
270
+ }
271
+ }
272
+ const SwatchFragment = `
273
+ fragment Swatch on Swatch {
274
+ __typename
275
+ id
276
+ hue
277
+ saturation
278
+ lightness
279
+ }
280
+ `;
281
+ const FileFragment = `
282
+ fragment File on File {
283
+ __typename
284
+ id
285
+ name
286
+ publicUrl
287
+ extension
288
+ dimensions {
289
+ width
290
+ height
291
+ }
292
+ }
293
+ `;
294
+ const TypographyFragment = `
295
+ fragment Typography on Typography {
296
+ __typename
297
+ id
298
+ name
299
+ style {
300
+ deviceId
301
+ value {
302
+ fontFamily
303
+ fontSize {
304
+ value
305
+ unit
306
+ }
307
+ color {
308
+ swatchId
309
+ alpha
310
+ }
311
+ lineHeight
312
+ letterSpacing
313
+ fontWeight
314
+ textAlign
315
+ uppercase
316
+ underline
317
+ strikethrough
318
+ italic
319
+ }
320
+ }
321
+ }
322
+ `;
323
+ const PagePathnameSliceFragment = `
324
+ fragment PagePathnameSlice on PagePathnameSlice {
325
+ __typename
326
+ id
327
+ pathname
328
+ }
329
+ `;
330
+ const GlobalElementFragment = `
331
+ fragment GlobalElement on GlobalElement {
332
+ __typename
333
+ id
334
+ data
335
+ }
336
+ `;
337
+ const TableFragment = `
338
+ fragment Table on Table {
339
+ __typename
340
+ id
341
+ name
342
+ columns {
343
+ __typename
344
+ id
345
+ name
346
+
347
+ ... on MultipleSelectTableColumn {
348
+ options {
411
349
  id
412
- }, true));
413
- },
414
- pagePathnamesById(existingData, {
415
- args,
416
- toReference
417
- }) {
418
- return existingData != null ? existingData : args == null ? void 0 : args.ids.map((id) => toReference({
419
- __typename: "PagePathnameSlice",
350
+ name
351
+ }
352
+ }
353
+
354
+ ... on SingleSelectTableColumn {
355
+ options {
420
356
  id
421
- }, true));
422
- },
423
- globalElement(existingData, {
424
- args,
425
- toReference
426
- }) {
427
- return existingData != null ? existingData : toReference({
428
- __typename: "GlobalElement",
429
- id: args == null ? void 0 : args.id
430
- }, true);
431
- },
432
- table(existingData, {
433
- args,
434
- toReference
435
- }) {
436
- return existingData != null ? existingData : toReference({
437
- __typename: "Table",
438
- id: args == null ? void 0 : args.id
439
- }, true);
440
- },
441
- page(existingData, {
442
- args,
443
- toReference
444
- }) {
445
- return existingData != null ? existingData : toReference({
446
- __typename: "Page",
447
- id: args == null ? void 0 : args.id
448
- }, true);
449
- },
450
- site(existingData, {
451
- args,
452
- toReference
453
- }) {
454
- return existingData != null ? existingData : toReference({
455
- __typename: "Site",
456
- id: args == null ? void 0 : args.id
457
- }, true);
357
+ name
358
+ }
458
359
  }
459
360
  }
460
361
  }
461
- };
462
- function createApolloClient({
463
- uri,
464
- cacheData
362
+ `;
363
+ const IntrospectedResourcesQuery = `
364
+ query IntrospectedResources(
365
+ $swatchIds: [ID!]!
366
+ $fileIds: [ID!]!
367
+ $typographyIds: [ID!]!
368
+ $pageIds: [ID!]!
369
+ $tableIds: [ID!]!
370
+ ) {
371
+ swatches(ids: $swatchIds) {
372
+ ...Swatch
373
+ }
374
+
375
+ files(ids: $fileIds) {
376
+ ...File
377
+ }
378
+
379
+ typographies(ids: $typographyIds) {
380
+ ...Typography
381
+ }
382
+
383
+ pagePathnamesById(ids: $pageIds) {
384
+ ...PagePathnameSlice
385
+ }
386
+
387
+ tables(ids: $tableIds) {
388
+ ...Table
389
+ }
390
+ }
391
+
392
+ ${SwatchFragment}
393
+ ${FileFragment}
394
+ ${TypographyFragment}
395
+ ${PagePathnameSliceFragment}
396
+ ${TableFragment}
397
+ `;
398
+ const SwatchQuery = `
399
+ query Swatch($swatchId: ID!) {
400
+ swatch(id: $swatchId) {
401
+ ...Swatch
402
+ }
403
+ }
404
+
405
+ ${SwatchFragment}
406
+ `;
407
+ const FileQuery = `
408
+ query File($fileId: ID!) {
409
+ file(id: $fileId) {
410
+ ...File
411
+ }
412
+ }
413
+
414
+ ${FileFragment}
415
+ `;
416
+ const TypographyQuery = `
417
+ query Typography($typographyId: ID!) {
418
+ typography(id: $typographyId) {
419
+ ...Typography
420
+ }
421
+ }
422
+
423
+ ${TypographyFragment}
424
+ `;
425
+ const PagePathnamesByIdQuery = `
426
+ query PagePathnamesById($pageIds: [ID!]!) {
427
+ pagePathnamesById(ids: $pageIds) {
428
+ ...PagePathnameSlice
429
+ }
430
+ }
431
+
432
+ ${PagePathnameSliceFragment}
433
+ `;
434
+ const TableQuery = `
435
+ query Table($tableId: ID!) {
436
+ table(id: $tableId) {
437
+ ...Table
438
+ }
439
+ }
440
+
441
+ ${TableFragment}
442
+ `;
443
+ const TypographiesQuery = `
444
+ query Typographies($typographyIds: [ID!]!) {
445
+ typographies(ids: $typographyIds) {
446
+ ...Typography
447
+ }
448
+ }
449
+
450
+ ${TypographyFragment}
451
+ `;
452
+ const GlobalElementQuery = `
453
+ query GlobalElement($globalElementId: ID!) {
454
+ globalElement(id: $globalElementId) {
455
+ ...GlobalElement
456
+ }
457
+ }
458
+
459
+ ${GlobalElementFragment}
460
+ `;
461
+ const CreateTableRecordMutation = `
462
+ mutation CreateTableRecord($input: CreateTableRecordInput!) {
463
+ createTableRecord(input: $input) {
464
+ tableRecord {
465
+ id
466
+ }
467
+ }
468
+ }
469
+ `;
470
+ const reducer = reducer$1;
471
+ function getSerializedState(state) {
472
+ return getSerializedState$1(state);
473
+ }
474
+ function getHasAPIResource(state, resourceType, resourceId) {
475
+ return getHasAPIResource$1(state, resourceType, resourceId);
476
+ }
477
+ function getAPIResource(state, resourceType, resourceId) {
478
+ return getAPIResource$1(state, resourceType, resourceId);
479
+ }
480
+ function fetchAPIResource(resourceType, resourceId) {
481
+ return async (dispatch, getState, client) => {
482
+ var _a;
483
+ const state = getState();
484
+ if (getHasAPIResource(state, resourceType, resourceId)) {
485
+ return getAPIResource(state, resourceType, resourceId);
486
+ }
487
+ let resource;
488
+ switch (resourceType) {
489
+ case APIResourceType.Swatch:
490
+ resource = (await client.request(SwatchQuery, {
491
+ swatchId: resourceId
492
+ })).swatch;
493
+ break;
494
+ case APIResourceType.File:
495
+ resource = (await client.request(FileQuery, {
496
+ fileId: resourceId
497
+ })).file;
498
+ break;
499
+ case APIResourceType.Typography:
500
+ resource = (await client.request(TypographyQuery, {
501
+ typographyId: resourceId
502
+ })).typography;
503
+ break;
504
+ case APIResourceType.GlobalElement:
505
+ resource = (await client.request(GlobalElementQuery, { globalElementId: resourceId })).globalElement;
506
+ break;
507
+ case APIResourceType.PagePathnameSlice:
508
+ resource = (_a = (await client.request(PagePathnamesByIdQuery, { pageIds: [resourceId] })).pagePathnamesById[0]) != null ? _a : null;
509
+ break;
510
+ case APIResourceType.Table:
511
+ resource = (await client.request(TableQuery, {
512
+ tableId: resourceId
513
+ })).table;
514
+ break;
515
+ default:
516
+ resource = null;
517
+ }
518
+ dispatch(apiResourceFulfilled(resourceType, resourceId, resource));
519
+ return resource;
520
+ };
521
+ }
522
+ function fetchIntrospectedResources(introspectedResourceIds) {
523
+ return async (dispatch, _getState, client) => {
524
+ const introspectedResources = await client.request(IntrospectedResourcesQuery, introspectedResourceIds);
525
+ dispatch(introspectedResourcesFulfilled(introspectedResourceIds, introspectedResources));
526
+ };
527
+ }
528
+ function fetchTypographies(typographyIds) {
529
+ return async (dispatch, getState, client) => {
530
+ const state = getState();
531
+ if (typographyIds.every((typographyId) => getHasAPIResource(state, APIResourceType.Typography, typographyId))) {
532
+ return typographyIds.map((typographyId) => getAPIResource(state, APIResourceType.Typography, typographyId));
533
+ }
534
+ const { typographies } = await client.request(TypographiesQuery, { typographyIds });
535
+ dispatch(typographiesFulfilled(typographyIds, typographies));
536
+ return typographies;
537
+ };
538
+ }
539
+ function configureStore({
540
+ graphqlClient,
541
+ serializedState
465
542
  }) {
466
- const cache2 = new InMemoryCache({
467
- typePolicies
468
- });
469
- if (cacheData)
470
- cache2.restore(cacheData);
471
- return new ApolloClient({
472
- link: new BatchHttpLink({
473
- uri,
474
- batchMax: 100
475
- }),
476
- cache: cache2
477
- });
543
+ return createStore(reducer, getInitialState(serializedState), applyMiddleware(thunk.withExtraArgument(graphqlClient)));
544
+ }
545
+ class GraphQLClient {
546
+ constructor(endpoint) {
547
+ __publicField(this, "endpoint");
548
+ this.endpoint = endpoint;
549
+ }
550
+ async request(query, variables = {}) {
551
+ const response = await fetch(this.endpoint, {
552
+ method: "POST",
553
+ headers: { "Content-Type": "application/json" },
554
+ body: JSON.stringify({ query, variables })
555
+ });
556
+ if (!response.ok && response.status !== 400) {
557
+ throw new Error(`${response.status} ${response.statusText}`);
558
+ }
559
+ const body = await response.json();
560
+ if (body.errors != null) {
561
+ console.error(body);
562
+ throw new Error("GraphQL response contains errors, check the console.");
563
+ }
564
+ return body.data;
565
+ }
478
566
  }
479
567
  class MakeswiftClient {
480
568
  constructor({
481
569
  uri,
482
570
  cacheData
483
571
  }) {
484
- __publicField(this, "apolloClient");
485
- this.apolloClient = createApolloClient({
486
- uri,
487
- cacheData
572
+ __publicField(this, "graphqlClient");
573
+ __publicField(this, "makeswiftApiClient");
574
+ __publicField(this, "subscribe");
575
+ this.graphqlClient = new GraphQLClient(uri);
576
+ this.makeswiftApiClient = configureStore({
577
+ graphqlClient: this.graphqlClient,
578
+ serializedState: cacheData
488
579
  });
580
+ this.subscribe = this.makeswiftApiClient.subscribe;
489
581
  }
490
582
  async prefetch(element) {
491
- const introspectionData = await introspect(element, this.apolloClient, storeContextDefaultValue);
492
- const res = await this.apolloClient.query({
493
- query: INTROSPECTION_QUERY,
494
- variables: introspectionData
495
- });
496
- res.data.pagePathnamesById.forEach((pagePathnameSlice) => {
497
- if (pagePathnameSlice == null)
498
- return;
499
- const id = Buffer.from(`Page:${pagePathnameSlice.id}`).toString("base64");
500
- this.apolloClient.cache.writeFragment({
501
- fragment: PagePathnameSliceFragmentDoc,
502
- data: __spreadProps(__spreadValues({}, pagePathnameSlice), {
503
- id
504
- })
505
- });
506
- });
583
+ const introspectionData = await introspect(element, this, storeContextDefaultValue);
584
+ await this.makeswiftApiClient.dispatch(fetchIntrospectedResources(introspectionData));
507
585
  KeyUtils.resetGenerator();
508
- return this.apolloClient.cache.extract();
586
+ return getSerializedState(this.makeswiftApiClient.getState());
509
587
  }
510
588
  updateCacheData(cacheData) {
511
- this.apolloClient.cache.restore(cacheData);
589
+ this.makeswiftApiClient.dispatch(restoreAPIResourcesCache(cacheData));
590
+ }
591
+ readSwatch(swatchId) {
592
+ return getAPIResource(this.makeswiftApiClient.getState(), APIResourceType.Swatch, swatchId);
593
+ }
594
+ async fetchSwatch(swatchId) {
595
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(APIResourceType.Swatch, swatchId));
596
+ }
597
+ readFile(fileId) {
598
+ return getAPIResource(this.makeswiftApiClient.getState(), APIResourceType.File, fileId);
599
+ }
600
+ async fetchFile(fileId) {
601
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(APIResourceType.File, fileId));
602
+ }
603
+ readTypography(typographyId) {
604
+ return getAPIResource(this.makeswiftApiClient.getState(), APIResourceType.Typography, typographyId);
605
+ }
606
+ async fetchTypography(typographyId) {
607
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(APIResourceType.Typography, typographyId));
608
+ }
609
+ async fetchTypographies(typographyIds) {
610
+ return await this.makeswiftApiClient.dispatch(fetchTypographies(typographyIds));
611
+ }
612
+ readGlobalElement(globalElementId) {
613
+ return getAPIResource(this.makeswiftApiClient.getState(), APIResourceType.GlobalElement, globalElementId);
614
+ }
615
+ async fetchGlobalElement(globalElementId) {
616
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(APIResourceType.GlobalElement, globalElementId));
617
+ }
618
+ readPagePathnameSlice(pageId) {
619
+ return getAPIResource(this.makeswiftApiClient.getState(), APIResourceType.PagePathnameSlice, pageId);
620
+ }
621
+ async fetchPagePathnameSlice(pageId) {
622
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(APIResourceType.PagePathnameSlice, pageId));
623
+ }
624
+ readTable(tableId) {
625
+ return getAPIResource(this.makeswiftApiClient.getState(), APIResourceType.Table, tableId);
626
+ }
627
+ async fetchTable(tableId) {
628
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(APIResourceType.Table, tableId));
629
+ }
630
+ async createTableRecord(tableId, columns) {
631
+ await this.graphqlClient.request(CreateTableRecordMutation, {
632
+ input: {
633
+ data: {
634
+ tableId,
635
+ columns
636
+ }
637
+ }
638
+ });
639
+ }
640
+ readSite(siteId) {
641
+ return getAPIResource(this.makeswiftApiClient.getState(), APIResourceType.Site, siteId);
642
+ }
643
+ readPage(pageId) {
644
+ return getAPIResource(this.makeswiftApiClient.getState(), APIResourceType.Page, pageId);
645
+ }
646
+ readSnippet(snippetId) {
647
+ return getAPIResource(this.makeswiftApiClient.getState(), APIResourceType.Snippet, snippetId);
512
648
  }
513
649
  }
514
- const Context$2 = createContext(void 0);
515
- function useQuery(query, options) {
516
- const client = useContext(Context$2);
517
- return useQuery$1(query, __spreadValues({
518
- client: client == null ? void 0 : client.apolloClient
519
- }, options));
520
- }
521
- function useMutation(mutation, options) {
522
- const client = useContext(Context$2);
523
- return useMutation$1(mutation, __spreadValues({
524
- client: client == null ? void 0 : client.apolloClient
525
- }, options));
650
+ const Context$2 = createContext(new MakeswiftClient({
651
+ uri: "https://api.makeswift.com/graphql"
652
+ }));
653
+ function useMakeswiftClient() {
654
+ return useContext(Context$2);
526
655
  }
527
656
  function MakeswiftProvider({
528
657
  client,
@@ -605,76 +734,23 @@ const filterUsedSnippetProperties = ({
605
734
  location,
606
735
  cleanup
607
736
  });
608
- const PAGE_SNIPPETS_QUERY = gql`
609
- query PageById($id: ID!) {
610
- page(id: $id) {
611
- __typename
612
- id
613
- snippets {
614
- __typename
615
- id
616
- name
617
- code
618
- cleanup
619
- location
620
- shouldAddToNewPages
621
- liveEnabled
622
- builderEnabled
623
- }
624
- }
625
- }
626
- `;
627
- const SITE_FONTS_QUERY = gql`
628
- query SiteById($id: ID!) {
629
- site(id: $id) {
630
- id
631
- googleFonts {
632
- edges {
633
- activeVariants {
634
- specifier
635
- }
636
- node {
637
- family
638
- variants {
639
- specifier
640
- }
641
- }
642
- }
643
- }
644
- }
645
- }
646
- `;
647
737
  function Page$1({
648
738
  document: page
649
739
  }) {
650
740
  var _a;
651
741
  const isInBuilder = useIsInBuilder();
652
742
  const [snippets, setSnippets] = useState(page.snippets);
653
- useQuery(PAGE_SNIPPETS_QUERY, {
654
- variables: {
655
- id: page.id
656
- },
657
- skip: isInBuilder === false,
658
- fetchPolicy: "cache-only",
659
- onCompleted(data) {
660
- if (data == null)
661
- return;
662
- const oldSnippets = snippets.map(filterUsedSnippetProperties);
663
- const newSnippets = data.page.snippets.map(filterUsedSnippetProperties);
664
- if (deepEqual(newSnippets, oldSnippets))
665
- return;
666
- setSnippets(data.page.snippets);
667
- }
668
- });
669
- const {
670
- data: siteData
671
- } = useQuery(SITE_FONTS_QUERY, {
672
- variables: {
673
- id: page.site.id
674
- },
675
- skip: isInBuilder === false,
676
- fetchPolicy: "cache-only"
677
- });
743
+ const cachedPage = useCachedPage(isInBuilder ? page.id : null);
744
+ useEffect(() => {
745
+ if (cachedPage == null)
746
+ return;
747
+ const oldSnippets = snippets.map(filterUsedSnippetProperties);
748
+ const newSnippets = cachedPage.snippets.map(filterUsedSnippetProperties);
749
+ if (deepEqual(newSnippets, oldSnippets))
750
+ return;
751
+ setSnippets(cachedPage.snippets);
752
+ }, [cachedPage]);
753
+ const site = useCachedSite(isInBuilder ? page.site.id : null);
678
754
  const favicon = (_a = page.meta.favicon) != null ? _a : defaultFavicon;
679
755
  const {
680
756
  title,
@@ -687,7 +763,7 @@ function Page$1({
687
763
  isIndexingBlocked
688
764
  } = page.seo;
689
765
  const fontFamilyParamValue = useMemo(() => {
690
- if ((siteData == null ? void 0 : siteData.site) == null) {
766
+ if (site == null) {
691
767
  return page.fonts.map(({
692
768
  family,
693
769
  variants
@@ -695,7 +771,7 @@ function Page$1({
695
771
  return `${family.replace(/ /g, "+")}:${variants.join()}`;
696
772
  }).join("|");
697
773
  }
698
- return siteData.site.googleFonts.edges.filter((edge) => edge != null).map(({
774
+ return site.googleFonts.edges.filter((edge) => edge != null).map(({
699
775
  activeVariants,
700
776
  node: {
701
777
  family,
@@ -705,7 +781,7 @@ function Page$1({
705
781
  const activeVariantSpecifiers = variants.filter((variant) => activeVariants.some((activeVariant) => activeVariant.specifier === variant.specifier)).map((variant) => variant.specifier).join();
706
782
  return `${family.replace(/ /g, "+")}:${activeVariantSpecifiers}`;
707
783
  }).join("|");
708
- }, [siteData, page]);
784
+ }, [site, page]);
709
785
  const filteredSnippets = useMemo(() => snippets.filter((snippet) => isInBuilder ? snippet.builderEnabled : snippet.liveEnabled), [snippets, isInBuilder]);
710
786
  const headSnippets = useMemo(() => filteredSnippets.filter((snippet) => snippet.location === SnippetLocation.Head), [filteredSnippets]);
711
787
  const previousHeadSnippets = useRef(null);
@@ -811,6 +887,18 @@ function Page$1({
811
887
  }, snippet.id))]
812
888
  });
813
889
  }
890
+ function useCachedPage(pageId) {
891
+ const client = useMakeswiftClient();
892
+ const getSnapshot = () => pageId == null ? null : client.readPage(pageId);
893
+ const page = useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
894
+ return page;
895
+ }
896
+ function useCachedSite(siteId) {
897
+ const client = useMakeswiftClient();
898
+ const getSnapshot = () => siteId == null ? null : client.readSite(siteId);
899
+ const site = useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
900
+ return site;
901
+ }
814
902
  class Makeswift {
815
903
  constructor(apiKey, { apiOrigin = "https://api.makeswift.com" } = {}) {
816
904
  __publicField(this, "apiKey");
@@ -962,7 +1050,7 @@ class Document$1 extends NextDocument {
962
1050
  });
963
1051
  }
964
1052
  }
965
- const version = "0.3.1";
1053
+ const version = "0.4.0";
966
1054
  function isErrorWithMessage(error) {
967
1055
  return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
968
1056
  }
@@ -2562,32 +2650,112 @@ const FallbackComponent = forwardRef(function FallbackComponent2({
2562
2650
  })]
2563
2651
  });
2564
2652
  });
2565
- function isNonNullable(value) {
2566
- return value != null;
2653
+ function useSwatch(swatchId) {
2654
+ const client = useMakeswiftClient();
2655
+ const readSwatch = () => swatchId == null ? null : client.readSwatch(swatchId);
2656
+ const swatch = useSyncExternalStore(client.subscribe, readSwatch, readSwatch);
2657
+ useEffect(() => {
2658
+ if (swatchId != null)
2659
+ client.fetchSwatch(swatchId).catch(console.error);
2660
+ }, [client, swatchId]);
2661
+ return swatch;
2662
+ }
2663
+ function useSwatches(swatchIds) {
2664
+ const client = useMakeswiftClient();
2665
+ const lastSnapshot = useRef();
2666
+ function getSnapshot() {
2667
+ const swatches2 = swatchIds.map((swatchId) => client.readSwatch(swatchId));
2668
+ if (lastSnapshot.current != null && lastSnapshot.current.length === swatches2.length && lastSnapshot.current.every((swatch, idx) => swatches2[idx] === swatch)) {
2669
+ return lastSnapshot.current;
2670
+ }
2671
+ return lastSnapshot.current = swatches2;
2672
+ }
2673
+ const swatches = useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
2674
+ useEffect(() => {
2675
+ Promise.all(swatchIds.map((swatchId) => client.fetchSwatch(swatchId))).catch(console.error);
2676
+ }, [client, swatchIds]);
2677
+ return swatches;
2678
+ }
2679
+ function useFile(fileId) {
2680
+ const client = useMakeswiftClient();
2681
+ const readFile = () => fileId == null ? null : client.readFile(fileId);
2682
+ const file = useSyncExternalStore(client.subscribe, readFile, readFile);
2683
+ useEffect(() => {
2684
+ if (fileId != null)
2685
+ client.fetchFile(fileId);
2686
+ }, [client, fileId]);
2687
+ return file;
2688
+ }
2689
+ function useFiles(fileIds) {
2690
+ const client = useMakeswiftClient();
2691
+ const lastSnapshot = useRef();
2692
+ function getSnapshot() {
2693
+ const files2 = fileIds.map((fileId) => client.readFile(fileId));
2694
+ if (lastSnapshot.current != null && lastSnapshot.current.length === files2.length && lastSnapshot.current.every((file, idx) => files2[idx] === file)) {
2695
+ return lastSnapshot.current;
2696
+ }
2697
+ return lastSnapshot.current = files2;
2698
+ }
2699
+ const files = useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
2700
+ useEffect(() => {
2701
+ Promise.all(fileIds.map((fileId) => client.fetchFile(fileId))).catch(console.error);
2702
+ }, [client, fileIds]);
2703
+ return files;
2704
+ }
2705
+ function useTypography(typographyId) {
2706
+ const client = useMakeswiftClient();
2707
+ const readTypography = () => typographyId == null ? null : client.readTypography(typographyId);
2708
+ const typography = useSyncExternalStore(client.subscribe, readTypography, readTypography);
2709
+ useEffect(() => {
2710
+ if (typographyId != null)
2711
+ client.fetchTypography(typographyId).catch(console.error);
2712
+ }, [client, typographyId]);
2713
+ return typography;
2714
+ }
2715
+ function useGlobalElement(globalElementId) {
2716
+ const client = useMakeswiftClient();
2717
+ const readGlobalElement = () => globalElementId == null ? null : client.readGlobalElement(globalElementId);
2718
+ const globalElement = useSyncExternalStore(client.subscribe, readGlobalElement, readGlobalElement);
2719
+ useEffect(() => {
2720
+ if (globalElementId != null)
2721
+ client.fetchGlobalElement(globalElementId).catch(console.error);
2722
+ }, [client, globalElementId]);
2723
+ return globalElement;
2724
+ }
2725
+ function usePagePathnameSlice(pageId) {
2726
+ const client = useMakeswiftClient();
2727
+ const readPagePathnameSlice = () => pageId == null ? null : client.readPagePathnameSlice(pageId);
2728
+ const pagePathnameSlice = useSyncExternalStore(client.subscribe, readPagePathnameSlice, readPagePathnameSlice);
2729
+ useEffect(() => {
2730
+ if (pageId != null)
2731
+ client.fetchPagePathnameSlice(pageId).catch(console.error);
2732
+ }, [client, pageId]);
2733
+ return pagePathnameSlice;
2734
+ }
2735
+ function useTable(tableId) {
2736
+ const client = useMakeswiftClient();
2737
+ const readTable = () => tableId == null ? null : client.readTable(tableId);
2738
+ const table = useSyncExternalStore(client.subscribe, readTable, readTable);
2739
+ useEffect(() => {
2740
+ if (tableId != null)
2741
+ client.fetchTable(tableId).catch(console.error);
2742
+ }, [client, tableId]);
2743
+ return table;
2567
2744
  }
2568
2745
  function mapSideColor(swatches, _d) {
2569
2746
  var _e = _d, { color } = _e, restOfSide = __objRest(_e, ["color"]);
2570
2747
  return __spreadProps(__spreadValues({}, restOfSide), {
2571
2748
  color: color && {
2572
2749
  alpha: color.alpha,
2573
- swatch: swatches.find((s) => s && s.id === color.swatchId)
2750
+ swatch: swatches.filter(isNonNullable).find((s) => s && s.id === color.swatchId)
2574
2751
  }
2575
2752
  });
2576
2753
  }
2577
2754
  function useBorder(value) {
2578
- const swatchIds = value == null ? [] : [
2579
- ...Array.from(new Set(value.map(({ value: { borderTop, borderLeft, borderBottom, borderRight } }) => [
2580
- borderTop && borderTop.color && borderTop.color.swatchId,
2581
- borderBottom && borderBottom.color && borderBottom.color.swatchId,
2582
- borderLeft && borderLeft.color && borderLeft.color.swatchId,
2583
- borderRight && borderRight.color && borderRight.color.swatchId
2584
- ]).reduce((a, b) => a.concat(b)).filter(isNonNullable)))
2585
- ];
2586
- const skip = value == null || swatchIds.length === 0;
2587
- const { error, data = {} } = useQuery(SWATCHES_BY_ID, { skip, variables: { ids: swatchIds } });
2588
- if (value == null || error != null)
2755
+ const swatchIds = getBorderSwatchIds(value);
2756
+ const swatches = useSwatches(swatchIds);
2757
+ if (value == null)
2589
2758
  return null;
2590
- const { swatches = [] } = data;
2591
2759
  return value.map((_a) => {
2592
2760
  var _b = _a, { value: { borderTop, borderBottom, borderLeft, borderRight } } = _b, rest = __objRest(_b, ["value"]);
2593
2761
  return __spreadProps(__spreadValues({}, rest), {
@@ -2609,14 +2777,10 @@ const ShadowDefaultValue = {
2609
2777
  color: null
2610
2778
  };
2611
2779
  function useBoxShadow(value) {
2612
- const swatchIds = value == null ? [] : [
2613
- ...Array.from(new Set(value.map(({ value: shadows }) => shadows.map(({ payload: { color } }) => color && color.swatchId)).reduce((a, b) => a.concat(b), []).filter(isNonNullable)))
2614
- ];
2615
- const skip = value == null || swatchIds.length === 0;
2616
- const { error, data = {} } = useQuery(SWATCHES_BY_ID, { skip, variables: { ids: swatchIds } });
2617
- if (value == null || error != null)
2780
+ const swatchIds = getBoxShadowsSwatchIds(value);
2781
+ const swatches = useSwatches(swatchIds);
2782
+ if (value == null)
2618
2783
  return null;
2619
- const { swatches = [] } = data;
2620
2784
  return value.map((_a) => {
2621
2785
  var _b = _a, { value: shadows } = _b, restOfValue = __objRest(_b, ["value"]);
2622
2786
  return __spreadProps(__spreadValues({}, restOfValue), {
@@ -2629,7 +2793,7 @@ function useBoxShadow(value) {
2629
2793
  return __spreadProps(__spreadValues({}, restOfShadow), {
2630
2794
  payload: {
2631
2795
  color: color != null ? {
2632
- swatch: swatches.find((s) => s && s.id === color.swatchId),
2796
+ swatch: swatches.filter(isNonNullable).find((s) => s && s.id === color.swatchId),
2633
2797
  alpha: color.alpha
2634
2798
  } : null,
2635
2799
  inset: inset != null ? inset : ShadowDefaultValue.inset,
@@ -2644,12 +2808,10 @@ function useBoxShadow(value) {
2644
2808
  });
2645
2809
  }
2646
2810
  function useResponsiveColor(color) {
2647
- const swatchIds = color == null ? [] : [...Array.from(new Set(color.map(({ value: v }) => v && v.swatchId).filter(isNonNullable)))];
2648
- const skip = swatchIds.length === 0;
2649
- const { error, data = {} } = useQuery(SWATCHES_BY_ID, { skip, variables: { ids: swatchIds } });
2650
- if (color == null || error != null)
2811
+ const swatchIds = getResponsiveColorSwatchIds(color);
2812
+ const swatches = useSwatches(swatchIds);
2813
+ if (color == null)
2651
2814
  return null;
2652
- const { swatches = [] } = data;
2653
2815
  return color.map((_a) => {
2654
2816
  var _b = _a, { value: v } = _b, rest = __objRest(_b, ["value"]);
2655
2817
  const { swatchId, alpha } = v;
@@ -2657,27 +2819,8 @@ function useResponsiveColor(color) {
2657
2819
  return swatch == null ? null : __spreadProps(__spreadValues({}, rest), { value: { swatch, alpha } });
2658
2820
  }).filter(isNonNullable);
2659
2821
  }
2660
- function useFile(fileId) {
2661
- const { error, data = {} } = useQuery(FILE_BY_ID, {
2662
- skip: fileId == null,
2663
- variables: { id: fileId }
2664
- });
2665
- if (fileId == null || error != null)
2666
- return null;
2667
- return data.file;
2668
- }
2669
2822
  const isSSR = typeof window === "undefined" || /ServerSideRendering/.test(window.navigator && window.navigator.userAgent);
2670
2823
  const useIsomorphicLayoutEffect = isSSR ? useEffect : useLayoutEffect;
2671
- function usePage(pageId) {
2672
- const { error, data = {} } = useQuery(PAGE_PATHNAMES_BY_ID, {
2673
- skip: pageId == null,
2674
- variables: { ids: [pageId] }
2675
- });
2676
- if (pageId == null || error != null)
2677
- return null;
2678
- const { pagePathnamesById: [page] = [] } = data;
2679
- return page;
2680
- }
2681
2824
  const getIndexes = (spans, index) => {
2682
2825
  const flattened = spans.reduce((a, s) => a.concat(s), []);
2683
2826
  if (index < 0 || index > flattened.length)
@@ -2962,11 +3105,10 @@ function useCheckboxControlValue(data, definition) {
2962
3105
  }
2963
3106
  function useColorValue(data, definition) {
2964
3107
  var _a, _b;
2965
- const result = useQuery(SWATCHES_BY_ID, {
2966
- variables: { ids: [data == null ? void 0 : data.swatchId] },
2967
- skip: (data == null ? void 0 : data.swatchId) == null
2968
- });
2969
- if (data === void 0) {
3108
+ const swatchId = (_a = data == null ? void 0 : data.swatchId) != null ? _a : null;
3109
+ const swatch = useSwatch(swatchId);
3110
+ const alpha = (_b = data == null ? void 0 : data.alpha) != null ? _b : 1;
3111
+ if (swatch == null) {
2970
3112
  const { defaultValue } = definition.config;
2971
3113
  if (defaultValue === void 0)
2972
3114
  return void 0;
@@ -2978,8 +3120,7 @@ function useColorValue(data, definition) {
2978
3120
  }
2979
3121
  return defaultColor.rgb().string();
2980
3122
  }
2981
- const [swatch] = (_b = (_a = result.data) == null ? void 0 : _a.swatches) != null ? _b : [null];
2982
- return ColorHelper({ h: swatch == null ? void 0 : swatch.hue, s: swatch == null ? void 0 : swatch.saturation, l: swatch == null ? void 0 : swatch.lightness }).alpha(data.alpha).rgb().string();
3123
+ return ColorHelper({ h: swatch.hue, s: swatch.saturation, l: swatch.lightness }).alpha(alpha).rgb().string();
2983
3124
  }
2984
3125
  function useComboboxControlValue(data) {
2985
3126
  return data == null ? void 0 : data.value;
@@ -2987,7 +3128,8 @@ function useComboboxControlValue(data) {
2987
3128
  function useImageControlValue(data, definition) {
2988
3129
  var _a;
2989
3130
  const format = (_a = definition.config.format) != null ? _a : ImageControlValueFormat.URL;
2990
- const file = useFile(data);
3131
+ const fileId = data != null ? data : null;
3132
+ const file = useFile(fileId);
2991
3133
  if (format === ImageControlValueFormat.URL) {
2992
3134
  return file == null ? void 0 : file.publicUrl;
2993
3135
  }
@@ -3000,7 +3142,8 @@ function useImageControlValue(data, definition) {
3000
3142
  }
3001
3143
  function useLinkControlValue(link, _definition) {
3002
3144
  var _a;
3003
- const page = usePage(link && link.type === "OPEN_PAGE" ? link.payload.pageId : null);
3145
+ const pageId = link && link.type === "OPEN_PAGE" ? link.payload.pageId : null;
3146
+ const page = usePagePathnameSlice(pageId != null ? pageId : null);
3004
3147
  const elementKey = (link == null ? void 0 : link.type) === "SCROLL_TO_ELEMENT" ? (_a = link.payload.elementIdConfig) == null ? void 0 : _a.elementKey : null;
3005
3148
  const elementId = useElementId(elementKey);
3006
3149
  let href = "#";
@@ -3555,7 +3698,7 @@ const FindDomNode = forwardRef(function FindDomNode2(props, ref) {
3555
3698
  innerRef: ref
3556
3699
  }));
3557
3700
  });
3558
- const storeContextDefaultValue = configureStore();
3701
+ const storeContextDefaultValue = configureStore$1();
3559
3702
  function createReactRuntime(store) {
3560
3703
  return {
3561
3704
  registerComponent(component, {
@@ -3590,7 +3733,7 @@ function RuntimeProvider({
3590
3733
  rootElements
3591
3734
  }) {
3592
3735
  const [store, setStore] = useState(() => {
3593
- const store2 = configureStore({
3736
+ const store2 = configureStore$1({
3594
3737
  preloadedState: storeContextDefaultValue.getState(),
3595
3738
  rootElements
3596
3739
  });
@@ -3613,7 +3756,7 @@ function RuntimeProvider({
3613
3756
  const ReactBuilderPreview = await import("./react-builder-preview.es.js");
3614
3757
  setStore((store2) => ReactBuilderPreview.configureStore({
3615
3758
  preloadedState: store2.getState(),
3616
- client: client.apolloClient
3759
+ client
3617
3760
  }));
3618
3761
  }
3619
3762
  }, [client]);
@@ -3714,23 +3857,9 @@ const DisableRegisterElement = createContext(false);
3714
3857
  const ElementReference = memo(forwardRef(function ElementReference2({
3715
3858
  elementReference
3716
3859
  }, ref) {
3717
- var _a;
3718
- const {
3719
- error,
3720
- data
3721
- } = useQuery(ELEMENT_REFERENCE_GLOBAL_ELEMENT, {
3722
- variables: {
3723
- id: elementReference.value
3724
- }
3725
- });
3726
- const globalElementData = (_a = data == null ? void 0 : data.globalElement) == null ? void 0 : _a.data;
3860
+ const globalElement = useGlobalElement(elementReference.value);
3861
+ const globalElementData = globalElement == null ? void 0 : globalElement.data;
3727
3862
  const elementReferenceDocument = useDocument(elementReference.key);
3728
- if (error != null) {
3729
- return /* @__PURE__ */ jsx(FallbackComponent, {
3730
- ref,
3731
- text: "Something went wrong!"
3732
- });
3733
- }
3734
3863
  if (globalElementData == null) {
3735
3864
  return /* @__PURE__ */ jsx(FallbackComponent, {
3736
3865
  ref,
@@ -3801,5 +3930,5 @@ const DocumentReference = memo(forwardRef(function DocumentReference2({
3801
3930
  document: document2
3802
3931
  });
3803
3932
  }));
3804
- export { getStaticProps as $, responsiveGridItem as A, useQuery as B, Contrasts as C, DocumentReference as D, Element as E, useMutation as F, Provider as G, Alignments as H, DEVICES as I, findDeviceOverride as J, serializeStyles as K, insertStyles as L, MakeswiftComponentType as M, TYPOGRAPHIES_BY_ID as N, SWATCHES_BY_ID as O, PageProvider as P, shallowMergeFallbacks as Q, RuntimeProvider as R, SITE_FONTS_QUERY as S, TABLE_BY_ID as T, isNonNullable as U, FILES_BY_ID as V, getDeviceMediaQuery as W, useElementId as X, deepEqual as Y, MakeswiftClient as Z, getStaticPaths as _, ReactRuntime as a, getServerSideProps as a0, Page as a1, Makeswift as a2, PreviewModeScript as a3, Document$1 as a4, MakeswiftApiHandler as a5, forwardNextDynamicRef 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, useFile as m, usePage as n, PAGE_SNIPPETS_QUERY as o, Page$1 as p, useStyle as q, responsiveStyle as r, responsiveWidth as s, responsiveTextStyle as t, useIsInBuilder as u, colorToString as v, useIsomorphicLayoutEffect as w, Shapes as x, Sizes as y, useFormContext as z };
3933
+ export { PreviewModeScript as $, Alignments as A, useFile as B, Contrasts as C, DocumentReference as D, Element as E, DEVICES as F, findDeviceOverride as G, serializeStyles as H, insertStyles as I, useTypography as J, useSwatches as K, shallowMergeFallbacks as L, MakeswiftComponentType as M, useFiles as N, getDeviceMediaQuery as O, PageProvider as P, usePagePathnameSlice as Q, RuntimeProvider as R, Shapes as S, useElementId as T, deepEqual as U, MakeswiftClient as V, getStaticPaths as W, getStaticProps as X, getServerSideProps as Y, Page as Z, Makeswift as _, ReactRuntime as a, Document$1 as a0, MakeswiftApiHandler as a1, forwardNextDynamicRef as a2, 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, Page$1 as m, useStyle as n, responsiveWidth as o, responsiveTextStyle as p, colorToString as q, responsiveStyle as r, useIsomorphicLayoutEffect as s, Sizes as t, useIsInBuilder as u, useFormContext as v, responsiveGridItem as w, useTable as x, useMakeswiftClient as y, Provider as z };
3805
3934
  //# sourceMappingURL=index.es.js.map