@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.cjs.js CHANGED
@@ -41,16 +41,17 @@ var withSelector = require("use-sync-external-store/shim/with-selector");
41
41
  var reactPage = require("./react-page.cjs.js");
42
42
  var actions = require("./actions.cjs.js");
43
43
  var dynamic = require("next/dynamic");
44
- var client = require("@apollo/client");
45
- var batchHttp = require("@apollo/client/link/batch-http");
46
44
  var slate = require("slate");
47
- var graphql = require("./graphql.cjs.js");
48
45
  var slot = require("./slot.cjs.js");
49
46
  var control = require("./control.cjs.js");
47
+ var redux = require("redux");
48
+ var thunk = require("redux-thunk");
49
+ var types = require("./types.cjs.js");
50
50
  var jsxRuntime = require("react/jsx-runtime");
51
51
  var css = require("@emotion/css");
52
52
  var textInput = require("./text-input.cjs.js");
53
53
  var combobox = require("./combobox.cjs.js");
54
+ var shim = require("use-sync-external-store/shim");
54
55
  var ColorHelper = require("color");
55
56
  var scrollIntoView = require("scroll-into-view-if-needed");
56
57
  var boxModels = require("./box-models.cjs.js");
@@ -90,6 +91,7 @@ function _interopNamespace(e) {
90
91
  }
91
92
  var React__namespace = /* @__PURE__ */ _interopNamespace(React);
92
93
  var dynamic__default = /* @__PURE__ */ _interopDefaultLegacy(dynamic);
94
+ var thunk__default = /* @__PURE__ */ _interopDefaultLegacy(thunk);
93
95
  var ColorHelper__default = /* @__PURE__ */ _interopDefaultLegacy(ColorHelper);
94
96
  var scrollIntoView__default = /* @__PURE__ */ _interopDefaultLegacy(scrollIntoView);
95
97
  var parse__default = /* @__PURE__ */ _interopDefaultLegacy(parse);
@@ -143,182 +145,7 @@ function BodySnippet({
143
145
  }, [code, cleanup]);
144
146
  return null;
145
147
  }
146
- const SWATCHES_BY_ID = client.gql`
147
- query SwatchesById($ids: [ID!]!) {
148
- swatches(ids: $ids) {
149
- id
150
- hue
151
- saturation
152
- lightness
153
- }
154
- }
155
- `;
156
- const FILE_BY_ID = client.gql`
157
- query FileById($id: ID!) {
158
- file(id: $id) {
159
- id
160
- name
161
- publicUrl
162
- extension
163
- dimensions {
164
- width
165
- height
166
- }
167
- }
168
- }
169
- `;
170
- const FILES_BY_ID = client.gql`
171
- query FilesById($ids: [ID!]!) {
172
- files(ids: $ids) {
173
- id
174
- name
175
- publicUrl
176
- extension
177
- dimensions {
178
- width
179
- height
180
- }
181
- }
182
- }
183
- `;
184
- const PAGE_PATHNAMES_BY_ID = client.gql`
185
- query PagePathnamesById($ids: [ID!]!) {
186
- pagePathnamesById(ids: $ids) {
187
- id
188
- pathname
189
- }
190
- }
191
- `;
192
- const TYPOGRAPHY_FRAGMENT = client.gql`
193
- fragment Typography on Typography {
194
- id
195
- name
196
- style {
197
- deviceId
198
- value {
199
- fontFamily
200
- fontSize {
201
- value
202
- unit
203
- }
204
- color {
205
- swatchId
206
- alpha
207
- }
208
- lineHeight
209
- letterSpacing
210
- fontWeight
211
- textAlign
212
- uppercase
213
- underline
214
- strikethrough
215
- italic
216
- }
217
- }
218
- }
219
- `;
220
- const TYPOGRAPHIES_BY_ID = client.gql`
221
- query TypographiesById($ids: [ID!]!) {
222
- typographies(ids: $ids) {
223
- ...Typography
224
- }
225
- }
226
-
227
- ${TYPOGRAPHY_FRAGMENT}
228
- `;
229
- const TABLE_BY_ID = client.gql`
230
- query TableById($id: ID!) {
231
- table(id: $id) {
232
- id
233
- name
234
- columns {
235
- id
236
- name
237
- ... on MultipleSelectTableColumn {
238
- options {
239
- id
240
- name
241
- }
242
- }
243
- ... on SingleSelectTableColumn {
244
- options {
245
- id
246
- name
247
- }
248
- }
249
- }
250
- }
251
- }
252
- `;
253
- const ELEMENT_REFERENCE_GLOBAL_ELEMENT = client.gql`
254
- query ElementReferenceGlobalElement($id: ID!) {
255
- globalElement(id: $id) {
256
- id
257
- data
258
- }
259
- }
260
- `;
261
- const INTROSPECTION_QUERY = client.gql`
262
- query Introspection(
263
- $swatchIds: [ID!]!
264
- $fileIds: [ID!]!
265
- $pageIds: [ID!]!
266
- $typographyIds: [ID!]!
267
- $tableIds: [ID!]!
268
- ) {
269
- swatches(ids: $swatchIds) {
270
- id
271
- hue
272
- saturation
273
- lightness
274
- }
275
-
276
- files(ids: $fileIds) {
277
- id
278
- name
279
- publicUrl
280
- extension
281
- dimensions {
282
- width
283
- height
284
- }
285
- }
286
-
287
- pagePathnamesById(ids: $pageIds) {
288
- id
289
- pathname
290
- }
291
-
292
- typographies(ids: $typographyIds) {
293
- ...Typography
294
- }
295
-
296
- tables(ids: $tableIds) {
297
- id
298
- name
299
- columns {
300
- id
301
- name
302
- ... on MultipleSelectTableColumn {
303
- options {
304
- id
305
- name
306
- }
307
- }
308
- ... on SingleSelectTableColumn {
309
- options {
310
- id
311
- name
312
- }
313
- }
314
- }
315
- }
316
- }
317
-
318
- ${TYPOGRAPHY_FRAGMENT}
319
- `;
320
- async function introspect(element, client2, store) {
321
- var _a, _b, _c2;
148
+ async function introspect(element, client, store) {
322
149
  const descriptors = reactPage.getPropControllerDescriptors(store.getState());
323
150
  const swatchIds = /* @__PURE__ */ new Set();
324
151
  const fileIds = /* @__PURE__ */ new Set();
@@ -370,11 +197,8 @@ async function introspect(element, client2, store) {
370
197
  };
371
198
  let element2;
372
199
  if (reactPage.isElementReference(current)) {
373
- const query = await client2.query({
374
- query: ELEMENT_REFERENCE_GLOBAL_ELEMENT,
375
- variables: { id: current.value }
376
- });
377
- const elementData = (_b = (_a = query.data) == null ? void 0 : _a.globalElement) == null ? void 0 : _b.data;
200
+ const globalElement = await client.fetchGlobalElement(current.value);
201
+ const elementData = globalElement == null ? void 0 : globalElement.data;
378
202
  if (elementData == null)
379
203
  continue;
380
204
  element2 = elementData;
@@ -386,14 +210,11 @@ async function introspect(element, client2, store) {
386
210
  continue;
387
211
  getResourcesFromElementDescriptors(elementDescriptors, element2.props);
388
212
  }
389
- const typographiesResult = await client2.query({
390
- query: TYPOGRAPHIES_BY_ID,
391
- variables: { ids: [...typographyIds] }
392
- });
393
- (_c2 = typographiesResult == null ? void 0 : typographiesResult.data) == null ? void 0 : _c2.typographies.forEach((typography) => {
213
+ const typographies = await client.fetchTypographies([...typographyIds]);
214
+ typographies.forEach((typography) => {
394
215
  typography == null ? void 0 : typography.style.forEach((style) => {
395
- var _a2;
396
- const swatchId = (_a2 = style.value.color) == null ? void 0 : _a2.swatchId;
216
+ var _a;
217
+ const swatchId = (_a = style.value.color) == null ? void 0 : _a.swatchId;
397
218
  if (swatchId != null)
398
219
  swatchIds.add(swatchId);
399
220
  });
@@ -406,164 +227,473 @@ async function introspect(element, client2, store) {
406
227
  pageIds: [...pageIds]
407
228
  };
408
229
  }
409
- const typePolicies = {
410
- Query: {
411
- fields: {
412
- swatches(existingData, {
413
- args,
414
- toReference
415
- }) {
416
- return existingData != null ? existingData : args == null ? void 0 : args.ids.map((id) => toReference({
417
- __typename: "Swatch",
418
- id
419
- }, true));
420
- },
421
- file(existingData, {
422
- args,
423
- toReference
424
- }) {
425
- return existingData != null ? existingData : toReference({
426
- __typename: "File",
427
- id: args == null ? void 0 : args.id
428
- }, true);
429
- },
430
- files(existingData, {
431
- args,
432
- toReference
433
- }) {
434
- return existingData != null ? existingData : args == null ? void 0 : args.ids.map((id) => toReference({
435
- __typename: "File",
436
- id
437
- }, true));
438
- },
439
- typographies(existingData, {
440
- args,
441
- toReference
442
- }) {
443
- return existingData != null ? existingData : args == null ? void 0 : args.ids.map((id) => toReference({
444
- __typename: "Typography",
230
+ function getInitialState(serializedState = []) {
231
+ return new Map(serializedState.map(([resourceType, resources]) => [resourceType, new Map(resources)]));
232
+ }
233
+ function getSerializedState$1(state) {
234
+ return Array.from(state.entries()).map(([resourceType, resources]) => [
235
+ resourceType,
236
+ Array.from(resources.entries())
237
+ ]);
238
+ }
239
+ function getHasAPIResource$1(state, resourceType, resourceId) {
240
+ var _a, _b;
241
+ return (_b = (_a = state.get(resourceType)) == null ? void 0 : _a.has(resourceId)) != null ? _b : false;
242
+ }
243
+ function getAPIResource$1(state, resourceType, resourceId) {
244
+ var _a;
245
+ const resource = (_a = state.get(resourceType)) == null ? void 0 : _a.get(resourceId);
246
+ return (resource == null ? void 0 : resource.__typename) === resourceType ? resource : null;
247
+ }
248
+ function reducer$1(state = getInitialState(), action) {
249
+ switch (action.type) {
250
+ case actions.ActionTypes.RESTORE_API_RESOURCES_CACHE:
251
+ return getInitialState(action.payload.serializedState);
252
+ case actions.ActionTypes.API_RESOURCE_FULFILLED: {
253
+ const { resourceType, resourceId, resource } = action.payload;
254
+ return new Map(state).set(resourceType, new Map(state.get(resourceType)).set(resourceId, resource));
255
+ }
256
+ case actions.ActionTypes.INTROSPECTED_RESOURCES_FULFILLED: {
257
+ const { introspectedResourceIds, introspectedResources } = action.payload;
258
+ const swatches = new Map(state.get(types.APIResourceType.Swatch));
259
+ const files = new Map(state.get(types.APIResourceType.File));
260
+ const typographies = new Map(state.get(types.APIResourceType.Typography));
261
+ const pagePathnameSlices = new Map(state.get(types.APIResourceType.PagePathnameSlice));
262
+ const tables = new Map(state.get(types.APIResourceType.Table));
263
+ introspectedResourceIds.swatchIds.forEach((swatchId, idx) => {
264
+ swatches.set(swatchId, introspectedResources.swatches[idx]);
265
+ });
266
+ introspectedResourceIds.fileIds.forEach((fileId, idx) => {
267
+ files.set(fileId, introspectedResources.files[idx]);
268
+ });
269
+ introspectedResourceIds.typographyIds.forEach((typographyId, idx) => {
270
+ typographies.set(typographyId, introspectedResources.typographies[idx]);
271
+ });
272
+ introspectedResourceIds.pageIds.forEach((pageId, idx) => {
273
+ const pagePathnameSlice = introspectedResources.pagePathnamesById[idx];
274
+ if (pagePathnameSlice == null) {
275
+ pagePathnameSlices.set(pageId, pagePathnameSlice);
276
+ } else {
277
+ const nodeId = Buffer.from(`Page:${pagePathnameSlice.id}`).toString("base64");
278
+ pagePathnameSlices.set(nodeId, __spreadProps(__spreadValues({}, pagePathnameSlice), { id: nodeId }));
279
+ }
280
+ });
281
+ introspectedResourceIds.tableIds.forEach((tableId, idx) => {
282
+ tables.set(tableId, introspectedResources.tables[idx]);
283
+ });
284
+ return new Map(state).set(types.APIResourceType.Swatch, swatches).set(types.APIResourceType.File, files).set(types.APIResourceType.Typography, typographies).set(types.APIResourceType.PagePathnameSlice, pagePathnameSlices).set(types.APIResourceType.Table, tables);
285
+ }
286
+ case actions.ActionTypes.TYPOGRAPHIES_FULFILLED: {
287
+ const typographies = new Map(state.get(types.APIResourceType.Typography));
288
+ action.payload.typographyIds.forEach((typographyId, idx) => {
289
+ typographies.set(typographyId, action.payload.typographies[idx]);
290
+ });
291
+ return new Map(state).set(types.APIResourceType.Typography, typographies);
292
+ }
293
+ case actions.ActionTypes.CHANGE_API_RESOURCE:
294
+ 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));
295
+ case actions.ActionTypes.EVICT_API_RESOURCE: {
296
+ const [resourceType, resourceId] = action.payload.id.split(":");
297
+ if (!(resourceType in types.APIResourceType))
298
+ return state;
299
+ const resources = new Map(state.get(resourceType));
300
+ const deleted = resources.delete(resourceId);
301
+ return deleted ? new Map(state).set(resourceType, resources) : state;
302
+ }
303
+ default:
304
+ return state;
305
+ }
306
+ }
307
+ const SwatchFragment = `
308
+ fragment Swatch on Swatch {
309
+ __typename
310
+ id
311
+ hue
312
+ saturation
313
+ lightness
314
+ }
315
+ `;
316
+ const FileFragment = `
317
+ fragment File on File {
318
+ __typename
319
+ id
320
+ name
321
+ publicUrl
322
+ extension
323
+ dimensions {
324
+ width
325
+ height
326
+ }
327
+ }
328
+ `;
329
+ const TypographyFragment = `
330
+ fragment Typography on Typography {
331
+ __typename
332
+ id
333
+ name
334
+ style {
335
+ deviceId
336
+ value {
337
+ fontFamily
338
+ fontSize {
339
+ value
340
+ unit
341
+ }
342
+ color {
343
+ swatchId
344
+ alpha
345
+ }
346
+ lineHeight
347
+ letterSpacing
348
+ fontWeight
349
+ textAlign
350
+ uppercase
351
+ underline
352
+ strikethrough
353
+ italic
354
+ }
355
+ }
356
+ }
357
+ `;
358
+ const PagePathnameSliceFragment = `
359
+ fragment PagePathnameSlice on PagePathnameSlice {
360
+ __typename
361
+ id
362
+ pathname
363
+ }
364
+ `;
365
+ const GlobalElementFragment = `
366
+ fragment GlobalElement on GlobalElement {
367
+ __typename
368
+ id
369
+ data
370
+ }
371
+ `;
372
+ const TableFragment = `
373
+ fragment Table on Table {
374
+ __typename
375
+ id
376
+ name
377
+ columns {
378
+ __typename
379
+ id
380
+ name
381
+
382
+ ... on MultipleSelectTableColumn {
383
+ options {
445
384
  id
446
- }, true));
447
- },
448
- pagePathnamesById(existingData, {
449
- args,
450
- toReference
451
- }) {
452
- return existingData != null ? existingData : args == null ? void 0 : args.ids.map((id) => toReference({
453
- __typename: "PagePathnameSlice",
385
+ name
386
+ }
387
+ }
388
+
389
+ ... on SingleSelectTableColumn {
390
+ options {
454
391
  id
455
- }, true));
456
- },
457
- globalElement(existingData, {
458
- args,
459
- toReference
460
- }) {
461
- return existingData != null ? existingData : toReference({
462
- __typename: "GlobalElement",
463
- id: args == null ? void 0 : args.id
464
- }, true);
465
- },
466
- table(existingData, {
467
- args,
468
- toReference
469
- }) {
470
- return existingData != null ? existingData : toReference({
471
- __typename: "Table",
472
- id: args == null ? void 0 : args.id
473
- }, true);
474
- },
475
- page(existingData, {
476
- args,
477
- toReference
478
- }) {
479
- return existingData != null ? existingData : toReference({
480
- __typename: "Page",
481
- id: args == null ? void 0 : args.id
482
- }, true);
483
- },
484
- site(existingData, {
485
- args,
486
- toReference
487
- }) {
488
- return existingData != null ? existingData : toReference({
489
- __typename: "Site",
490
- id: args == null ? void 0 : args.id
491
- }, true);
392
+ name
393
+ }
492
394
  }
493
395
  }
494
396
  }
495
- };
496
- function createApolloClient({
497
- uri,
498
- cacheData
397
+ `;
398
+ const IntrospectedResourcesQuery = `
399
+ query IntrospectedResources(
400
+ $swatchIds: [ID!]!
401
+ $fileIds: [ID!]!
402
+ $typographyIds: [ID!]!
403
+ $pageIds: [ID!]!
404
+ $tableIds: [ID!]!
405
+ ) {
406
+ swatches(ids: $swatchIds) {
407
+ ...Swatch
408
+ }
409
+
410
+ files(ids: $fileIds) {
411
+ ...File
412
+ }
413
+
414
+ typographies(ids: $typographyIds) {
415
+ ...Typography
416
+ }
417
+
418
+ pagePathnamesById(ids: $pageIds) {
419
+ ...PagePathnameSlice
420
+ }
421
+
422
+ tables(ids: $tableIds) {
423
+ ...Table
424
+ }
425
+ }
426
+
427
+ ${SwatchFragment}
428
+ ${FileFragment}
429
+ ${TypographyFragment}
430
+ ${PagePathnameSliceFragment}
431
+ ${TableFragment}
432
+ `;
433
+ const SwatchQuery = `
434
+ query Swatch($swatchId: ID!) {
435
+ swatch(id: $swatchId) {
436
+ ...Swatch
437
+ }
438
+ }
439
+
440
+ ${SwatchFragment}
441
+ `;
442
+ const FileQuery = `
443
+ query File($fileId: ID!) {
444
+ file(id: $fileId) {
445
+ ...File
446
+ }
447
+ }
448
+
449
+ ${FileFragment}
450
+ `;
451
+ const TypographyQuery = `
452
+ query Typography($typographyId: ID!) {
453
+ typography(id: $typographyId) {
454
+ ...Typography
455
+ }
456
+ }
457
+
458
+ ${TypographyFragment}
459
+ `;
460
+ const PagePathnamesByIdQuery = `
461
+ query PagePathnamesById($pageIds: [ID!]!) {
462
+ pagePathnamesById(ids: $pageIds) {
463
+ ...PagePathnameSlice
464
+ }
465
+ }
466
+
467
+ ${PagePathnameSliceFragment}
468
+ `;
469
+ const TableQuery = `
470
+ query Table($tableId: ID!) {
471
+ table(id: $tableId) {
472
+ ...Table
473
+ }
474
+ }
475
+
476
+ ${TableFragment}
477
+ `;
478
+ const TypographiesQuery = `
479
+ query Typographies($typographyIds: [ID!]!) {
480
+ typographies(ids: $typographyIds) {
481
+ ...Typography
482
+ }
483
+ }
484
+
485
+ ${TypographyFragment}
486
+ `;
487
+ const GlobalElementQuery = `
488
+ query GlobalElement($globalElementId: ID!) {
489
+ globalElement(id: $globalElementId) {
490
+ ...GlobalElement
491
+ }
492
+ }
493
+
494
+ ${GlobalElementFragment}
495
+ `;
496
+ const CreateTableRecordMutation = `
497
+ mutation CreateTableRecord($input: CreateTableRecordInput!) {
498
+ createTableRecord(input: $input) {
499
+ tableRecord {
500
+ id
501
+ }
502
+ }
503
+ }
504
+ `;
505
+ const reducer = reducer$1;
506
+ function getSerializedState(state) {
507
+ return getSerializedState$1(state);
508
+ }
509
+ function getHasAPIResource(state, resourceType, resourceId) {
510
+ return getHasAPIResource$1(state, resourceType, resourceId);
511
+ }
512
+ function getAPIResource(state, resourceType, resourceId) {
513
+ return getAPIResource$1(state, resourceType, resourceId);
514
+ }
515
+ function fetchAPIResource(resourceType, resourceId) {
516
+ return async (dispatch, getState, client) => {
517
+ var _a;
518
+ const state = getState();
519
+ if (getHasAPIResource(state, resourceType, resourceId)) {
520
+ return getAPIResource(state, resourceType, resourceId);
521
+ }
522
+ let resource;
523
+ switch (resourceType) {
524
+ case types.APIResourceType.Swatch:
525
+ resource = (await client.request(SwatchQuery, {
526
+ swatchId: resourceId
527
+ })).swatch;
528
+ break;
529
+ case types.APIResourceType.File:
530
+ resource = (await client.request(FileQuery, {
531
+ fileId: resourceId
532
+ })).file;
533
+ break;
534
+ case types.APIResourceType.Typography:
535
+ resource = (await client.request(TypographyQuery, {
536
+ typographyId: resourceId
537
+ })).typography;
538
+ break;
539
+ case types.APIResourceType.GlobalElement:
540
+ resource = (await client.request(GlobalElementQuery, { globalElementId: resourceId })).globalElement;
541
+ break;
542
+ case types.APIResourceType.PagePathnameSlice:
543
+ resource = (_a = (await client.request(PagePathnamesByIdQuery, { pageIds: [resourceId] })).pagePathnamesById[0]) != null ? _a : null;
544
+ break;
545
+ case types.APIResourceType.Table:
546
+ resource = (await client.request(TableQuery, {
547
+ tableId: resourceId
548
+ })).table;
549
+ break;
550
+ default:
551
+ resource = null;
552
+ }
553
+ dispatch(actions.apiResourceFulfilled(resourceType, resourceId, resource));
554
+ return resource;
555
+ };
556
+ }
557
+ function fetchIntrospectedResources(introspectedResourceIds) {
558
+ return async (dispatch, _getState, client) => {
559
+ const introspectedResources = await client.request(IntrospectedResourcesQuery, introspectedResourceIds);
560
+ dispatch(actions.introspectedResourcesFulfilled(introspectedResourceIds, introspectedResources));
561
+ };
562
+ }
563
+ function fetchTypographies(typographyIds) {
564
+ return async (dispatch, getState, client) => {
565
+ const state = getState();
566
+ if (typographyIds.every((typographyId) => getHasAPIResource(state, types.APIResourceType.Typography, typographyId))) {
567
+ return typographyIds.map((typographyId) => getAPIResource(state, types.APIResourceType.Typography, typographyId));
568
+ }
569
+ const { typographies } = await client.request(TypographiesQuery, { typographyIds });
570
+ dispatch(actions.typographiesFulfilled(typographyIds, typographies));
571
+ return typographies;
572
+ };
573
+ }
574
+ function configureStore({
575
+ graphqlClient,
576
+ serializedState
499
577
  }) {
500
- const cache = new client.InMemoryCache({
501
- typePolicies
502
- });
503
- if (cacheData)
504
- cache.restore(cacheData);
505
- return new client.ApolloClient({
506
- link: new batchHttp.BatchHttpLink({
507
- uri,
508
- batchMax: 100
509
- }),
510
- cache
511
- });
578
+ return redux.createStore(reducer, getInitialState(serializedState), redux.applyMiddleware(thunk__default["default"].withExtraArgument(graphqlClient)));
579
+ }
580
+ class GraphQLClient {
581
+ constructor(endpoint) {
582
+ __publicField(this, "endpoint");
583
+ this.endpoint = endpoint;
584
+ }
585
+ async request(query, variables = {}) {
586
+ const response = await fetch(this.endpoint, {
587
+ method: "POST",
588
+ headers: { "Content-Type": "application/json" },
589
+ body: JSON.stringify({ query, variables })
590
+ });
591
+ if (!response.ok && response.status !== 400) {
592
+ throw new Error(`${response.status} ${response.statusText}`);
593
+ }
594
+ const body = await response.json();
595
+ if (body.errors != null) {
596
+ console.error(body);
597
+ throw new Error("GraphQL response contains errors, check the console.");
598
+ }
599
+ return body.data;
600
+ }
512
601
  }
513
602
  class MakeswiftClient {
514
603
  constructor({
515
604
  uri,
516
605
  cacheData
517
606
  }) {
518
- __publicField(this, "apolloClient");
519
- this.apolloClient = createApolloClient({
520
- uri,
521
- cacheData
607
+ __publicField(this, "graphqlClient");
608
+ __publicField(this, "makeswiftApiClient");
609
+ __publicField(this, "subscribe");
610
+ this.graphqlClient = new GraphQLClient(uri);
611
+ this.makeswiftApiClient = configureStore({
612
+ graphqlClient: this.graphqlClient,
613
+ serializedState: cacheData
522
614
  });
615
+ this.subscribe = this.makeswiftApiClient.subscribe;
523
616
  }
524
617
  async prefetch(element) {
525
- const introspectionData = await introspect(element, this.apolloClient, storeContextDefaultValue);
526
- const res = await this.apolloClient.query({
527
- query: INTROSPECTION_QUERY,
528
- variables: introspectionData
529
- });
530
- res.data.pagePathnamesById.forEach((pagePathnameSlice) => {
531
- if (pagePathnameSlice == null)
532
- return;
533
- const id = Buffer.from(`Page:${pagePathnameSlice.id}`).toString("base64");
534
- this.apolloClient.cache.writeFragment({
535
- fragment: graphql.PagePathnameSliceFragmentDoc,
536
- data: __spreadProps(__spreadValues({}, pagePathnameSlice), {
537
- id
538
- })
539
- });
540
- });
618
+ const introspectionData = await introspect(element, this, storeContextDefaultValue);
619
+ await this.makeswiftApiClient.dispatch(fetchIntrospectedResources(introspectionData));
541
620
  slate.KeyUtils.resetGenerator();
542
- return this.apolloClient.cache.extract();
621
+ return getSerializedState(this.makeswiftApiClient.getState());
543
622
  }
544
623
  updateCacheData(cacheData) {
545
- this.apolloClient.cache.restore(cacheData);
624
+ this.makeswiftApiClient.dispatch(actions.restoreAPIResourcesCache(cacheData));
625
+ }
626
+ readSwatch(swatchId) {
627
+ return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.Swatch, swatchId);
628
+ }
629
+ async fetchSwatch(swatchId) {
630
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(types.APIResourceType.Swatch, swatchId));
631
+ }
632
+ readFile(fileId) {
633
+ return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.File, fileId);
634
+ }
635
+ async fetchFile(fileId) {
636
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(types.APIResourceType.File, fileId));
637
+ }
638
+ readTypography(typographyId) {
639
+ return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.Typography, typographyId);
640
+ }
641
+ async fetchTypography(typographyId) {
642
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(types.APIResourceType.Typography, typographyId));
643
+ }
644
+ async fetchTypographies(typographyIds) {
645
+ return await this.makeswiftApiClient.dispatch(fetchTypographies(typographyIds));
646
+ }
647
+ readGlobalElement(globalElementId) {
648
+ return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.GlobalElement, globalElementId);
649
+ }
650
+ async fetchGlobalElement(globalElementId) {
651
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(types.APIResourceType.GlobalElement, globalElementId));
652
+ }
653
+ readPagePathnameSlice(pageId) {
654
+ return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.PagePathnameSlice, pageId);
655
+ }
656
+ async fetchPagePathnameSlice(pageId) {
657
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(types.APIResourceType.PagePathnameSlice, pageId));
658
+ }
659
+ readTable(tableId) {
660
+ return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.Table, tableId);
661
+ }
662
+ async fetchTable(tableId) {
663
+ return await this.makeswiftApiClient.dispatch(fetchAPIResource(types.APIResourceType.Table, tableId));
664
+ }
665
+ async createTableRecord(tableId, columns) {
666
+ await this.graphqlClient.request(CreateTableRecordMutation, {
667
+ input: {
668
+ data: {
669
+ tableId,
670
+ columns
671
+ }
672
+ }
673
+ });
674
+ }
675
+ readSite(siteId) {
676
+ return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.Site, siteId);
677
+ }
678
+ readPage(pageId) {
679
+ return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.Page, pageId);
680
+ }
681
+ readSnippet(snippetId) {
682
+ return getAPIResource(this.makeswiftApiClient.getState(), types.APIResourceType.Snippet, snippetId);
546
683
  }
547
684
  }
548
- const Context$2 = React.createContext(void 0);
549
- function useQuery(query, options) {
550
- const client$1 = React.useContext(Context$2);
551
- return client.useQuery(query, __spreadValues({
552
- client: client$1 == null ? void 0 : client$1.apolloClient
553
- }, options));
554
- }
555
- function useMutation(mutation, options) {
556
- const client$1 = React.useContext(Context$2);
557
- return client.useMutation(mutation, __spreadValues({
558
- client: client$1 == null ? void 0 : client$1.apolloClient
559
- }, options));
685
+ const Context$2 = React.createContext(new MakeswiftClient({
686
+ uri: "https://api.makeswift.com/graphql"
687
+ }));
688
+ function useMakeswiftClient() {
689
+ return React.useContext(Context$2);
560
690
  }
561
691
  function MakeswiftProvider({
562
- client: client2,
692
+ client,
563
693
  children
564
694
  }) {
565
695
  return /* @__PURE__ */ jsxRuntime.jsx(Context$2.Provider, {
566
- value: client2,
696
+ value: client,
567
697
  children
568
698
  });
569
699
  }
@@ -639,76 +769,23 @@ const filterUsedSnippetProperties = ({
639
769
  location,
640
770
  cleanup
641
771
  });
642
- const PAGE_SNIPPETS_QUERY = client.gql`
643
- query PageById($id: ID!) {
644
- page(id: $id) {
645
- __typename
646
- id
647
- snippets {
648
- __typename
649
- id
650
- name
651
- code
652
- cleanup
653
- location
654
- shouldAddToNewPages
655
- liveEnabled
656
- builderEnabled
657
- }
658
- }
659
- }
660
- `;
661
- const SITE_FONTS_QUERY = client.gql`
662
- query SiteById($id: ID!) {
663
- site(id: $id) {
664
- id
665
- googleFonts {
666
- edges {
667
- activeVariants {
668
- specifier
669
- }
670
- node {
671
- family
672
- variants {
673
- specifier
674
- }
675
- }
676
- }
677
- }
678
- }
679
- }
680
- `;
681
772
  function Page$1({
682
773
  document: page
683
774
  }) {
684
775
  var _a;
685
776
  const isInBuilder = useIsInBuilder();
686
777
  const [snippets, setSnippets] = React.useState(page.snippets);
687
- useQuery(PAGE_SNIPPETS_QUERY, {
688
- variables: {
689
- id: page.id
690
- },
691
- skip: isInBuilder === false,
692
- fetchPolicy: "cache-only",
693
- onCompleted(data) {
694
- if (data == null)
695
- return;
696
- const oldSnippets = snippets.map(filterUsedSnippetProperties);
697
- const newSnippets = data.page.snippets.map(filterUsedSnippetProperties);
698
- if (deepEqual(newSnippets, oldSnippets))
699
- return;
700
- setSnippets(data.page.snippets);
701
- }
702
- });
703
- const {
704
- data: siteData
705
- } = useQuery(SITE_FONTS_QUERY, {
706
- variables: {
707
- id: page.site.id
708
- },
709
- skip: isInBuilder === false,
710
- fetchPolicy: "cache-only"
711
- });
778
+ const cachedPage = useCachedPage(isInBuilder ? page.id : null);
779
+ React.useEffect(() => {
780
+ if (cachedPage == null)
781
+ return;
782
+ const oldSnippets = snippets.map(filterUsedSnippetProperties);
783
+ const newSnippets = cachedPage.snippets.map(filterUsedSnippetProperties);
784
+ if (deepEqual(newSnippets, oldSnippets))
785
+ return;
786
+ setSnippets(cachedPage.snippets);
787
+ }, [cachedPage]);
788
+ const site = useCachedSite(isInBuilder ? page.site.id : null);
712
789
  const favicon = (_a = page.meta.favicon) != null ? _a : defaultFavicon;
713
790
  const {
714
791
  title,
@@ -721,7 +798,7 @@ function Page$1({
721
798
  isIndexingBlocked
722
799
  } = page.seo;
723
800
  const fontFamilyParamValue = React.useMemo(() => {
724
- if ((siteData == null ? void 0 : siteData.site) == null) {
801
+ if (site == null) {
725
802
  return page.fonts.map(({
726
803
  family,
727
804
  variants
@@ -729,7 +806,7 @@ function Page$1({
729
806
  return `${family.replace(/ /g, "+")}:${variants.join()}`;
730
807
  }).join("|");
731
808
  }
732
- return siteData.site.googleFonts.edges.filter((edge) => edge != null).map(({
809
+ return site.googleFonts.edges.filter((edge) => edge != null).map(({
733
810
  activeVariants,
734
811
  node: {
735
812
  family,
@@ -739,7 +816,7 @@ function Page$1({
739
816
  const activeVariantSpecifiers = variants.filter((variant) => activeVariants.some((activeVariant) => activeVariant.specifier === variant.specifier)).map((variant) => variant.specifier).join();
740
817
  return `${family.replace(/ /g, "+")}:${activeVariantSpecifiers}`;
741
818
  }).join("|");
742
- }, [siteData, page]);
819
+ }, [site, page]);
743
820
  const filteredSnippets = React.useMemo(() => snippets.filter((snippet) => isInBuilder ? snippet.builderEnabled : snippet.liveEnabled), [snippets, isInBuilder]);
744
821
  const headSnippets = React.useMemo(() => filteredSnippets.filter((snippet) => snippet.location === SnippetLocation.Head), [filteredSnippets]);
745
822
  const previousHeadSnippets = React.useRef(null);
@@ -845,6 +922,18 @@ function Page$1({
845
922
  }, snippet.id))]
846
923
  });
847
924
  }
925
+ function useCachedPage(pageId) {
926
+ const client = useMakeswiftClient();
927
+ const getSnapshot = () => pageId == null ? null : client.readPage(pageId);
928
+ const page = shim.useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
929
+ return page;
930
+ }
931
+ function useCachedSite(siteId) {
932
+ const client = useMakeswiftClient();
933
+ const getSnapshot = () => siteId == null ? null : client.readSite(siteId);
934
+ const site = shim.useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
935
+ return site;
936
+ }
848
937
  class Makeswift {
849
938
  constructor(apiKey, { apiOrigin = "https://api.makeswift.com" } = {}) {
850
939
  __publicField(this, "apiKey");
@@ -876,8 +965,8 @@ Received "${apiKey}" instead.`);
876
965
  return json;
877
966
  }
878
967
  async createSnapshot(document2) {
879
- const client2 = new MakeswiftClient({ uri: new URL("graphql", this.apiOrigin).href });
880
- const cacheData = await client2.prefetch(document2.data);
968
+ const client = new MakeswiftClient({ uri: new URL("graphql", this.apiOrigin).href });
969
+ const cacheData = await client.prefetch(document2.data);
881
970
  return { document: document2, apiOrigin: this.apiOrigin.href, cacheData };
882
971
  }
883
972
  async getPageSnapshotByPageId(pageId, { preview = false } = {}) {
@@ -996,7 +1085,7 @@ class Document$1 extends NextDocument__default["default"] {
996
1085
  });
997
1086
  }
998
1087
  }
999
- const version = "0.3.1";
1088
+ const version = "0.4.0";
1000
1089
  function isErrorWithMessage(error) {
1001
1090
  return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
1002
1091
  }
@@ -1230,15 +1319,15 @@ async function getServerSideProps(ctx) {
1230
1319
  const Page = React.memo(({
1231
1320
  snapshot
1232
1321
  }) => {
1233
- const [client2] = React.useState(() => new MakeswiftClient({
1322
+ const [client] = React.useState(() => new MakeswiftClient({
1234
1323
  uri: new URL("graphql", snapshot.apiOrigin).href,
1235
1324
  cacheData: snapshot.cacheData
1236
1325
  }));
1237
1326
  React.useEffect(() => {
1238
- client2.updateCacheData(snapshot.cacheData);
1239
- }, [client2, snapshot]);
1327
+ client.updateCacheData(snapshot.cacheData);
1328
+ }, [client, snapshot]);
1240
1329
  return /* @__PURE__ */ jsxRuntime.jsx(RuntimeProvider, {
1241
- client: client2,
1330
+ client,
1242
1331
  rootElements: /* @__PURE__ */ new Map([[snapshot.document.id, snapshot.document.data]]),
1243
1332
  children: /* @__PURE__ */ jsxRuntime.jsx(Page$1, {
1244
1333
  document: snapshot.document
@@ -2622,32 +2711,112 @@ const FallbackComponent = React.forwardRef(function FallbackComponent2({
2622
2711
  })]
2623
2712
  });
2624
2713
  });
2625
- function isNonNullable(value) {
2626
- return value != null;
2714
+ function useSwatch(swatchId) {
2715
+ const client = useMakeswiftClient();
2716
+ const readSwatch = () => swatchId == null ? null : client.readSwatch(swatchId);
2717
+ const swatch = shim.useSyncExternalStore(client.subscribe, readSwatch, readSwatch);
2718
+ React.useEffect(() => {
2719
+ if (swatchId != null)
2720
+ client.fetchSwatch(swatchId).catch(console.error);
2721
+ }, [client, swatchId]);
2722
+ return swatch;
2723
+ }
2724
+ function useSwatches(swatchIds) {
2725
+ const client = useMakeswiftClient();
2726
+ const lastSnapshot = React.useRef();
2727
+ function getSnapshot() {
2728
+ const swatches2 = swatchIds.map((swatchId) => client.readSwatch(swatchId));
2729
+ if (lastSnapshot.current != null && lastSnapshot.current.length === swatches2.length && lastSnapshot.current.every((swatch, idx) => swatches2[idx] === swatch)) {
2730
+ return lastSnapshot.current;
2731
+ }
2732
+ return lastSnapshot.current = swatches2;
2733
+ }
2734
+ const swatches = shim.useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
2735
+ React.useEffect(() => {
2736
+ Promise.all(swatchIds.map((swatchId) => client.fetchSwatch(swatchId))).catch(console.error);
2737
+ }, [client, swatchIds]);
2738
+ return swatches;
2739
+ }
2740
+ function useFile(fileId) {
2741
+ const client = useMakeswiftClient();
2742
+ const readFile = () => fileId == null ? null : client.readFile(fileId);
2743
+ const file = shim.useSyncExternalStore(client.subscribe, readFile, readFile);
2744
+ React.useEffect(() => {
2745
+ if (fileId != null)
2746
+ client.fetchFile(fileId);
2747
+ }, [client, fileId]);
2748
+ return file;
2749
+ }
2750
+ function useFiles(fileIds) {
2751
+ const client = useMakeswiftClient();
2752
+ const lastSnapshot = React.useRef();
2753
+ function getSnapshot() {
2754
+ const files2 = fileIds.map((fileId) => client.readFile(fileId));
2755
+ if (lastSnapshot.current != null && lastSnapshot.current.length === files2.length && lastSnapshot.current.every((file, idx) => files2[idx] === file)) {
2756
+ return lastSnapshot.current;
2757
+ }
2758
+ return lastSnapshot.current = files2;
2759
+ }
2760
+ const files = shim.useSyncExternalStore(client.subscribe, getSnapshot, getSnapshot);
2761
+ React.useEffect(() => {
2762
+ Promise.all(fileIds.map((fileId) => client.fetchFile(fileId))).catch(console.error);
2763
+ }, [client, fileIds]);
2764
+ return files;
2765
+ }
2766
+ function useTypography(typographyId) {
2767
+ const client = useMakeswiftClient();
2768
+ const readTypography = () => typographyId == null ? null : client.readTypography(typographyId);
2769
+ const typography = shim.useSyncExternalStore(client.subscribe, readTypography, readTypography);
2770
+ React.useEffect(() => {
2771
+ if (typographyId != null)
2772
+ client.fetchTypography(typographyId).catch(console.error);
2773
+ }, [client, typographyId]);
2774
+ return typography;
2775
+ }
2776
+ function useGlobalElement(globalElementId) {
2777
+ const client = useMakeswiftClient();
2778
+ const readGlobalElement = () => globalElementId == null ? null : client.readGlobalElement(globalElementId);
2779
+ const globalElement = shim.useSyncExternalStore(client.subscribe, readGlobalElement, readGlobalElement);
2780
+ React.useEffect(() => {
2781
+ if (globalElementId != null)
2782
+ client.fetchGlobalElement(globalElementId).catch(console.error);
2783
+ }, [client, globalElementId]);
2784
+ return globalElement;
2785
+ }
2786
+ function usePagePathnameSlice(pageId) {
2787
+ const client = useMakeswiftClient();
2788
+ const readPagePathnameSlice = () => pageId == null ? null : client.readPagePathnameSlice(pageId);
2789
+ const pagePathnameSlice = shim.useSyncExternalStore(client.subscribe, readPagePathnameSlice, readPagePathnameSlice);
2790
+ React.useEffect(() => {
2791
+ if (pageId != null)
2792
+ client.fetchPagePathnameSlice(pageId).catch(console.error);
2793
+ }, [client, pageId]);
2794
+ return pagePathnameSlice;
2795
+ }
2796
+ function useTable(tableId) {
2797
+ const client = useMakeswiftClient();
2798
+ const readTable = () => tableId == null ? null : client.readTable(tableId);
2799
+ const table = shim.useSyncExternalStore(client.subscribe, readTable, readTable);
2800
+ React.useEffect(() => {
2801
+ if (tableId != null)
2802
+ client.fetchTable(tableId).catch(console.error);
2803
+ }, [client, tableId]);
2804
+ return table;
2627
2805
  }
2628
2806
  function mapSideColor(swatches, _d) {
2629
2807
  var _e = _d, { color } = _e, restOfSide = __objRest(_e, ["color"]);
2630
2808
  return __spreadProps(__spreadValues({}, restOfSide), {
2631
2809
  color: color && {
2632
2810
  alpha: color.alpha,
2633
- swatch: swatches.find((s) => s && s.id === color.swatchId)
2811
+ swatch: swatches.filter(reactPage.isNonNullable).find((s) => s && s.id === color.swatchId)
2634
2812
  }
2635
2813
  });
2636
2814
  }
2637
2815
  function useBorder(value) {
2638
- const swatchIds = value == null ? [] : [
2639
- ...Array.from(new Set(value.map(({ value: { borderTop, borderLeft, borderBottom, borderRight } }) => [
2640
- borderTop && borderTop.color && borderTop.color.swatchId,
2641
- borderBottom && borderBottom.color && borderBottom.color.swatchId,
2642
- borderLeft && borderLeft.color && borderLeft.color.swatchId,
2643
- borderRight && borderRight.color && borderRight.color.swatchId
2644
- ]).reduce((a, b) => a.concat(b)).filter(isNonNullable)))
2645
- ];
2646
- const skip = value == null || swatchIds.length === 0;
2647
- const { error, data = {} } = useQuery(SWATCHES_BY_ID, { skip, variables: { ids: swatchIds } });
2648
- if (value == null || error != null)
2816
+ const swatchIds = reactPage.getBorderSwatchIds(value);
2817
+ const swatches = useSwatches(swatchIds);
2818
+ if (value == null)
2649
2819
  return null;
2650
- const { swatches = [] } = data;
2651
2820
  return value.map((_a) => {
2652
2821
  var _b = _a, { value: { borderTop, borderBottom, borderLeft, borderRight } } = _b, rest = __objRest(_b, ["value"]);
2653
2822
  return __spreadProps(__spreadValues({}, rest), {
@@ -2669,14 +2838,10 @@ const ShadowDefaultValue = {
2669
2838
  color: null
2670
2839
  };
2671
2840
  function useBoxShadow(value) {
2672
- const swatchIds = value == null ? [] : [
2673
- ...Array.from(new Set(value.map(({ value: shadows }) => shadows.map(({ payload: { color } }) => color && color.swatchId)).reduce((a, b) => a.concat(b), []).filter(isNonNullable)))
2674
- ];
2675
- const skip = value == null || swatchIds.length === 0;
2676
- const { error, data = {} } = useQuery(SWATCHES_BY_ID, { skip, variables: { ids: swatchIds } });
2677
- if (value == null || error != null)
2841
+ const swatchIds = reactPage.getBoxShadowsSwatchIds(value);
2842
+ const swatches = useSwatches(swatchIds);
2843
+ if (value == null)
2678
2844
  return null;
2679
- const { swatches = [] } = data;
2680
2845
  return value.map((_a) => {
2681
2846
  var _b = _a, { value: shadows } = _b, restOfValue = __objRest(_b, ["value"]);
2682
2847
  return __spreadProps(__spreadValues({}, restOfValue), {
@@ -2689,7 +2854,7 @@ function useBoxShadow(value) {
2689
2854
  return __spreadProps(__spreadValues({}, restOfShadow), {
2690
2855
  payload: {
2691
2856
  color: color != null ? {
2692
- swatch: swatches.find((s) => s && s.id === color.swatchId),
2857
+ swatch: swatches.filter(reactPage.isNonNullable).find((s) => s && s.id === color.swatchId),
2693
2858
  alpha: color.alpha
2694
2859
  } : null,
2695
2860
  inset: inset != null ? inset : ShadowDefaultValue.inset,
@@ -2704,40 +2869,19 @@ function useBoxShadow(value) {
2704
2869
  });
2705
2870
  }
2706
2871
  function useResponsiveColor(color) {
2707
- const swatchIds = color == null ? [] : [...Array.from(new Set(color.map(({ value: v }) => v && v.swatchId).filter(isNonNullable)))];
2708
- const skip = swatchIds.length === 0;
2709
- const { error, data = {} } = useQuery(SWATCHES_BY_ID, { skip, variables: { ids: swatchIds } });
2710
- if (color == null || error != null)
2872
+ const swatchIds = reactPage.getResponsiveColorSwatchIds(color);
2873
+ const swatches = useSwatches(swatchIds);
2874
+ if (color == null)
2711
2875
  return null;
2712
- const { swatches = [] } = data;
2713
2876
  return color.map((_a) => {
2714
2877
  var _b = _a, { value: v } = _b, rest = __objRest(_b, ["value"]);
2715
2878
  const { swatchId, alpha } = v;
2716
2879
  const swatch = swatches.find((s) => s && s.id === swatchId);
2717
2880
  return swatch == null ? null : __spreadProps(__spreadValues({}, rest), { value: { swatch, alpha } });
2718
- }).filter(isNonNullable);
2719
- }
2720
- function useFile(fileId) {
2721
- const { error, data = {} } = useQuery(FILE_BY_ID, {
2722
- skip: fileId == null,
2723
- variables: { id: fileId }
2724
- });
2725
- if (fileId == null || error != null)
2726
- return null;
2727
- return data.file;
2881
+ }).filter(reactPage.isNonNullable);
2728
2882
  }
2729
2883
  const isSSR = typeof window === "undefined" || /ServerSideRendering/.test(window.navigator && window.navigator.userAgent);
2730
2884
  const useIsomorphicLayoutEffect = isSSR ? React.useEffect : React.useLayoutEffect;
2731
- function usePage(pageId) {
2732
- const { error, data = {} } = useQuery(PAGE_PATHNAMES_BY_ID, {
2733
- skip: pageId == null,
2734
- variables: { ids: [pageId] }
2735
- });
2736
- if (pageId == null || error != null)
2737
- return null;
2738
- const { pagePathnamesById: [page] = [] } = data;
2739
- return page;
2740
- }
2741
2885
  const getIndexes = (spans, index) => {
2742
2886
  const flattened = spans.reduce((a, s) => a.concat(s), []);
2743
2887
  if (index < 0 || index > flattened.length)
@@ -3022,11 +3166,10 @@ function useCheckboxControlValue(data, definition) {
3022
3166
  }
3023
3167
  function useColorValue(data, definition) {
3024
3168
  var _a, _b;
3025
- const result = useQuery(SWATCHES_BY_ID, {
3026
- variables: { ids: [data == null ? void 0 : data.swatchId] },
3027
- skip: (data == null ? void 0 : data.swatchId) == null
3028
- });
3029
- if (data === void 0) {
3169
+ const swatchId = (_a = data == null ? void 0 : data.swatchId) != null ? _a : null;
3170
+ const swatch = useSwatch(swatchId);
3171
+ const alpha = (_b = data == null ? void 0 : data.alpha) != null ? _b : 1;
3172
+ if (swatch == null) {
3030
3173
  const { defaultValue } = definition.config;
3031
3174
  if (defaultValue === void 0)
3032
3175
  return void 0;
@@ -3038,8 +3181,7 @@ function useColorValue(data, definition) {
3038
3181
  }
3039
3182
  return defaultColor.rgb().string();
3040
3183
  }
3041
- const [swatch] = (_b = (_a = result.data) == null ? void 0 : _a.swatches) != null ? _b : [null];
3042
- return ColorHelper__default["default"]({ 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();
3184
+ return ColorHelper__default["default"]({ h: swatch.hue, s: swatch.saturation, l: swatch.lightness }).alpha(alpha).rgb().string();
3043
3185
  }
3044
3186
  function useComboboxControlValue(data) {
3045
3187
  return data == null ? void 0 : data.value;
@@ -3047,7 +3189,8 @@ function useComboboxControlValue(data) {
3047
3189
  function useImageControlValue(data, definition) {
3048
3190
  var _a;
3049
3191
  const format = (_a = definition.config.format) != null ? _a : slot.ImageControlValueFormat.URL;
3050
- const file = useFile(data);
3192
+ const fileId = data != null ? data : null;
3193
+ const file = useFile(fileId);
3051
3194
  if (format === slot.ImageControlValueFormat.URL) {
3052
3195
  return file == null ? void 0 : file.publicUrl;
3053
3196
  }
@@ -3060,7 +3203,8 @@ function useImageControlValue(data, definition) {
3060
3203
  }
3061
3204
  function useLinkControlValue(link, _definition) {
3062
3205
  var _a;
3063
- const page = usePage(link && link.type === "OPEN_PAGE" ? link.payload.pageId : null);
3206
+ const pageId = link && link.type === "OPEN_PAGE" ? link.payload.pageId : null;
3207
+ const page = usePagePathnameSlice(pageId != null ? pageId : null);
3064
3208
  const elementKey = (link == null ? void 0 : link.type) === "SCROLL_TO_ELEMENT" ? (_a = link.payload.elementIdConfig) == null ? void 0 : _a.elementKey : null;
3065
3209
  const elementId = useElementId(elementKey);
3066
3210
  let href = "#";
@@ -3645,7 +3789,7 @@ const ReactRuntime = createReactRuntime(storeContextDefaultValue);
3645
3789
  registerBuiltinComponents(ReactRuntime);
3646
3790
  const Context = React.createContext(storeContextDefaultValue);
3647
3791
  function RuntimeProvider({
3648
- client: client2,
3792
+ client,
3649
3793
  children,
3650
3794
  rootElements
3651
3795
  }) {
@@ -3675,14 +3819,14 @@ function RuntimeProvider({
3675
3819
  });
3676
3820
  setStore((store2) => ReactBuilderPreview.configureStore({
3677
3821
  preloadedState: store2.getState(),
3678
- client: client2.apolloClient
3822
+ client
3679
3823
  }));
3680
3824
  }
3681
- }, [client2]);
3825
+ }, [client]);
3682
3826
  return /* @__PURE__ */ jsxRuntime.jsx(Context.Provider, {
3683
3827
  value: store,
3684
3828
  children: /* @__PURE__ */ jsxRuntime.jsx(MakeswiftProvider, {
3685
- client: client2,
3829
+ client,
3686
3830
  children
3687
3831
  })
3688
3832
  });
@@ -3776,23 +3920,9 @@ const DisableRegisterElement = React.createContext(false);
3776
3920
  const ElementReference = React.memo(React.forwardRef(function ElementReference2({
3777
3921
  elementReference
3778
3922
  }, ref) {
3779
- var _a;
3780
- const {
3781
- error,
3782
- data
3783
- } = useQuery(ELEMENT_REFERENCE_GLOBAL_ELEMENT, {
3784
- variables: {
3785
- id: elementReference.value
3786
- }
3787
- });
3788
- const globalElementData = (_a = data == null ? void 0 : data.globalElement) == null ? void 0 : _a.data;
3923
+ const globalElement = useGlobalElement(elementReference.value);
3924
+ const globalElementData = globalElement == null ? void 0 : globalElement.data;
3789
3925
  const elementReferenceDocument = useDocument(elementReference.key);
3790
- if (error != null) {
3791
- return /* @__PURE__ */ jsxRuntime.jsx(FallbackComponent, {
3792
- ref,
3793
- text: "Something went wrong!"
3794
- });
3795
- }
3796
3926
  if (globalElementData == null) {
3797
3927
  return /* @__PURE__ */ jsxRuntime.jsx(FallbackComponent, {
3798
3928
  ref,
@@ -3876,12 +4006,10 @@ exports.DEVICES = DEVICES;
3876
4006
  exports.Document = Document$1;
3877
4007
  exports.DocumentReference = DocumentReference;
3878
4008
  exports.Element = Element;
3879
- exports.FILES_BY_ID = FILES_BY_ID;
3880
4009
  exports.Makeswift = Makeswift;
3881
4010
  exports.MakeswiftApiHandler = MakeswiftApiHandler;
3882
4011
  exports.MakeswiftClient = MakeswiftClient;
3883
4012
  exports.MakeswiftComponentType = MakeswiftComponentType;
3884
- exports.PAGE_SNIPPETS_QUERY = PAGE_SNIPPETS_QUERY;
3885
4013
  exports.Page = Page$1;
3886
4014
  exports.Page$1 = Page;
3887
4015
  exports.PageProvider = PageProvider;
@@ -3889,12 +4017,8 @@ exports.PreviewModeScript = PreviewModeScript;
3889
4017
  exports.Provider = Provider;
3890
4018
  exports.ReactRuntime = ReactRuntime;
3891
4019
  exports.RuntimeProvider = RuntimeProvider;
3892
- exports.SITE_FONTS_QUERY = SITE_FONTS_QUERY;
3893
- exports.SWATCHES_BY_ID = SWATCHES_BY_ID;
3894
4020
  exports.Shapes = Shapes;
3895
4021
  exports.Sizes = Sizes;
3896
- exports.TABLE_BY_ID = TABLE_BY_ID;
3897
- exports.TYPOGRAPHIES_BY_ID = TYPOGRAPHIES_BY_ID;
3898
4022
  exports.colorToString = colorToString;
3899
4023
  exports.deepEqual = deepEqual;
3900
4024
  exports.findDeviceOverride = findDeviceOverride;
@@ -3904,7 +4028,6 @@ exports.getServerSideProps = getServerSideProps;
3904
4028
  exports.getStaticPaths = getStaticPaths;
3905
4029
  exports.getStaticProps = getStaticProps;
3906
4030
  exports.insertStyles = insertStyles;
3907
- exports.isNonNullable = isNonNullable;
3908
4031
  exports.responsiveGridItem = responsiveGridItem;
3909
4032
  exports.responsiveStyle = responsiveStyle;
3910
4033
  exports.responsiveTextStyle = responsiveTextStyle;
@@ -3915,13 +4038,16 @@ exports.useBorder = useBorder;
3915
4038
  exports.useBoxShadow = useBoxShadow;
3916
4039
  exports.useElementId = useElementId;
3917
4040
  exports.useFile = useFile;
4041
+ exports.useFiles = useFiles;
3918
4042
  exports.useFormContext = useFormContext;
3919
4043
  exports.useIsInBuilder = useIsInBuilder;
3920
4044
  exports.useIsomorphicLayoutEffect = useIsomorphicLayoutEffect;
3921
- exports.useMutation = useMutation;
3922
- exports.usePage = usePage;
4045
+ exports.useMakeswiftClient = useMakeswiftClient;
3923
4046
  exports.usePageId = usePageId;
3924
- exports.useQuery = useQuery;
4047
+ exports.usePagePathnameSlice = usePagePathnameSlice;
3925
4048
  exports.useResponsiveColor = useResponsiveColor;
3926
4049
  exports.useStyle = useStyle;
4050
+ exports.useSwatches = useSwatches;
4051
+ exports.useTable = useTable;
4052
+ exports.useTypography = useTypography;
3927
4053
  //# sourceMappingURL=index.cjs.js.map