sanity-plugin-shopify-assets 1.2.0 → 1.2.2

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.
package/dist/index.js CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5, _templateObject6;
4
- function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
5
3
  Object.defineProperty(exports, '__esModule', {
6
4
  value: true
7
5
  });
@@ -31,29 +29,81 @@ var videojs__default = /*#__PURE__*/_interopDefaultCompat(videojs);
31
29
  var PhotoAlbum__default = /*#__PURE__*/_interopDefaultCompat(PhotoAlbum);
32
30
  var InfiniteScroll__default = /*#__PURE__*/_interopDefaultCompat(InfiniteScroll);
33
31
  var axios__default = /*#__PURE__*/_interopDefaultCompat(axios);
34
- const Root = styledComponents.styled.div(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n overflow: hidden;\n background-origin: content-box;\n background-repeat: no-repeat;\n background-clip: border-box;\n background-size: cover;\n background-color: ", ";\n position: relative;\n outline: none !important;\n border: ", ";\n box-sizing: content-box;\n user-drag: none;\n\n &:hover {\n opacity: 0.85;\n }\n\n &:focus,\n &:active {\n border: 1px solid var(--input-border-color-focus);\n box-shadow: inset 0 0 0 3px var(--input-border-color-focus);\n }\n"])), _ref => {
32
+ const Root = styledComponents.styled.div`
33
+ overflow: hidden;
34
+ background-origin: content-box;
35
+ background-repeat: no-repeat;
36
+ background-clip: border-box;
37
+ background-size: cover;
38
+ background-color: ${_ref => {
35
39
  let {
36
40
  theme
37
41
  } = _ref;
38
42
  return theme.sanity.color.card.enabled.bg2;
39
- }, _ref2 => {
43
+ }};
44
+ position: relative;
45
+ outline: none !important;
46
+ border: ${_ref2 => {
40
47
  let {
41
48
  theme
42
49
  } = _ref2;
43
- return "1px solid ".concat(theme.sanity.color.card.enabled.border);
44
- });
45
- const InfoLine = styledComponents.styled(ui.Card)(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n ", ";\n user-drag: none;\n position: absolute;\n background-color: var(--infoline-bg);\n top: 0;\n left: 0;\n max-width: 65%;\n overflow-wrap: break-word;\n\n [data-ui='Text'] {\n color: var(--infoline-fg);\n }\n"])), _ref3 => {
50
+ return `1px solid ${theme.sanity.color.card.enabled.border}`;
51
+ }};
52
+ box-sizing: content-box;
53
+ user-drag: none;
54
+
55
+ &:hover {
56
+ opacity: 0.85;
57
+ }
58
+
59
+ &:focus,
60
+ &:active {
61
+ border: 1px solid var(--input-border-color-focus);
62
+ box-shadow: inset 0 0 0 3px var(--input-border-color-focus);
63
+ }
64
+ `;
65
+ const InfoLine = styledComponents.styled(ui.Card)`
66
+ ${_ref3 => {
46
67
  let {
47
68
  theme
48
69
  } = _ref3;
49
- return "\n --infoline-fg: ".concat(theme.sanity.color.card.enabled.fg, ";\n --infoline-bg: ").concat(theme.sanity.color.card.enabled.bg, ";\n ");
50
- });
51
- const DurationLine = styledComponents.styled(ui.Card)(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n ", ";\n user-drag: none;\n position: absolute;\n background-color: var(--durationline-bg);\n top: 0;\n right: 0;\n\n [data-ui='Text'] {\n color: var(--durationline-fg);\n }\n"])), _ref4 => {
70
+ return `
71
+ --infoline-fg: ${theme.sanity.color.card.enabled.fg};
72
+ --infoline-bg: ${theme.sanity.color.card.enabled.bg};
73
+ `;
74
+ }};
75
+ user-drag: none;
76
+ position: absolute;
77
+ background-color: var(--infoline-bg);
78
+ top: 0;
79
+ left: 0;
80
+ max-width: 65%;
81
+ overflow-wrap: break-word;
82
+
83
+ [data-ui='Text'] {
84
+ color: var(--infoline-fg);
85
+ }
86
+ `;
87
+ const DurationLine = styledComponents.styled(ui.Card)`
88
+ ${_ref4 => {
52
89
  let {
53
90
  theme
54
91
  } = _ref4;
55
- return "\n --durationline-fg: ".concat(theme.sanity.color.card.enabled.bg, ";\n --durationline-bg: ").concat(theme.sanity.color.card.enabled.fg, ";\n ");
56
- });
92
+ return `
93
+ --durationline-fg: ${theme.sanity.color.card.enabled.bg};
94
+ --durationline-bg: ${theme.sanity.color.card.enabled.fg};
95
+ `;
96
+ }};
97
+ user-drag: none;
98
+ position: absolute;
99
+ background-color: var(--durationline-bg);
100
+ top: 0;
101
+ right: 0;
102
+
103
+ [data-ui='Text'] {
104
+ color: var(--durationline-fg);
105
+ }
106
+ `;
57
107
  const VideoPlayer = _ref5 => {
58
108
  let {
59
109
  src,
@@ -62,8 +112,7 @@ const VideoPlayer = _ref5 => {
62
112
  const videoNode = React__default.default.useRef(null);
63
113
  const player = React__default.default.useRef();
64
114
  React.useEffect(() => {
65
- var _a;
66
- player.current = videojs__default.default((_a = videoNode.current) != null ? _a : "", {
115
+ player.current = videojs__default.default(videoNode.current ?? "", {
67
116
  sources: [{
68
117
  src
69
118
  }],
@@ -98,30 +147,34 @@ const VideoPlayer = _ref5 => {
98
147
  })]
99
148
  });
100
149
  };
101
- const StyledBox = styledComponents.styled(ui.Box)(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n background-color: ", ";\n border: ", ";\n display: flex;\n justify-content: center;\n margin-bottom: ", ";\n position: relative;\n"])), _ref6 => {
150
+ const StyledBox = styledComponents.styled(ui.Box)`
151
+ background-color: ${_ref6 => {
102
152
  let {
103
153
  theme
104
154
  } = _ref6;
105
- var _a, _b, _c;
106
- return (_c = (_b = (_a = theme.sanity.color) == null ? void 0 : _a.card) == null ? void 0 : _b.enabled) == null ? void 0 : _c.bg2;
107
- }, _ref7 => {
155
+ return theme.sanity.color?.card?.enabled?.bg2;
156
+ }};
157
+ border: ${_ref7 => {
108
158
  let {
109
159
  theme
110
160
  } = _ref7;
111
- var _a, _b, _c, _d;
112
- return "1px solid ".concat((_d = (_c = (_b = (_a = theme.sanity) == null ? void 0 : _a.color) == null ? void 0 : _b.card) == null ? void 0 : _c.enabled) == null ? void 0 : _d.border);
113
- }, _ref8 => {
161
+ return `1px solid ${theme.sanity?.color?.card?.enabled?.border}`;
162
+ }};
163
+ display: flex;
164
+ justify-content: center;
165
+ margin-bottom: ${_ref8 => {
114
166
  let {
115
167
  theme
116
168
  } = _ref8;
117
169
  return theme.sanity.space[4];
118
- });
170
+ }};
171
+ position: relative;
172
+ `;
119
173
  const RenderAsset = _ref9 => {
120
174
  let {
121
175
  value,
122
176
  url
123
177
  } = _ref9;
124
- var _a;
125
178
  switch (value.type) {
126
179
  case "video":
127
180
  return /* @__PURE__ */jsxRuntime.jsx(VideoPlayer, {
@@ -133,7 +186,7 @@ const RenderAsset = _ref9 => {
133
186
  justify: "center",
134
187
  children: /* @__PURE__ */jsxRuntime.jsx("img", {
135
188
  alt: "preview",
136
- src: (_a = value == null ? void 0 : value.preview) == null ? void 0 : _a.url,
189
+ src: value?.preview?.url,
137
190
  style: {
138
191
  maxWidth: "100%",
139
192
  height: "auto",
@@ -144,10 +197,10 @@ const RenderAsset = _ref9 => {
144
197
  });
145
198
  }
146
199
  };
147
- const AssetPreview = _ref10 => {
200
+ const AssetPreview = _ref0 => {
148
201
  let {
149
202
  value
150
- } = _ref10;
203
+ } = _ref0;
151
204
  const url = value && value.url;
152
205
  if (!value || !url) {
153
206
  return null;
@@ -171,8 +224,8 @@ const AssetPreview = _ref10 => {
171
224
  margin: 2,
172
225
  children: /* @__PURE__ */jsxRuntime.jsxs(ui.Text, {
173
226
  size: 1,
174
- title: "Select ".concat(filename),
175
- children: [filename, " ", fileSize && "(".concat(prettyBytes__default.default(fileSize), ")")]
227
+ title: `Select ${filename}`,
228
+ children: [filename, " ", fileSize && `(${prettyBytes__default.default(fileSize)})`]
176
229
  })
177
230
  }), duration && /* @__PURE__ */jsxRuntime.jsx(DurationLine, {
178
231
  padding: 2,
@@ -180,7 +233,7 @@ const AssetPreview = _ref10 => {
180
233
  margin: 2,
181
234
  children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
182
235
  size: 1,
183
- title: "Video duration: ".concat(filename),
236
+ title: `Video duration: ${filename}`,
184
237
  children: prettyMilliseconds__default.default(duration, {
185
238
  colonNotation: true,
186
239
  secondsDecimalDigits: 0
@@ -196,12 +249,16 @@ const fetchSearch = props => {
196
249
  shop,
197
250
  query,
198
251
  cursor,
199
- resultsPerPage
252
+ resultsPerPage,
253
+ token
200
254
  } = props;
201
255
  return rxjs.defer(() => {
202
- return axios__default.default.get("https://".concat(projectId, ".api.sanity.io/v1/shopify/assets/").concat(dataset, "?shop=").concat(shop, "&query=").concat(encodeURIComponent(query)).concat(cursor && "&cursor=".concat(cursor), "&limit=").concat(resultsPerPage), {
256
+ return axios__default.default.get(`https://${projectId}.api.sanity.io/v1/shopify/assets/${dataset}?shop=${shop}&query=${encodeURIComponent(query)}${cursor && `&cursor=${cursor}`}&limit=${resultsPerPage}`, {
203
257
  withCredentials: true,
204
- method: "GET"
258
+ method: "GET",
259
+ headers: token ? {
260
+ Authorization: `Bearer ${token}`
261
+ } : {}
205
262
  });
206
263
  }).pipe(operators.map(result => result.data));
207
264
  };
@@ -211,11 +268,15 @@ const fetchList = props => {
211
268
  dataset,
212
269
  shop,
213
270
  cursor,
214
- resultsPerPage
271
+ resultsPerPage,
272
+ token
215
273
  } = props;
216
- return rxjs.defer(() => axios__default.default.get("https://".concat(projectId, ".api.sanity.io/v1/shopify/assets/").concat(dataset, "?shop=").concat(shop).concat(cursor && "&cursor=".concat(cursor), "&limit=").concat(resultsPerPage), {
274
+ return rxjs.defer(() => axios__default.default.get(`https://${projectId}.api.sanity.io/v1/shopify/assets/${dataset}?shop=${shop}${cursor && `&cursor=${cursor}`}&limit=${resultsPerPage}`, {
217
275
  withCredentials: true,
218
- method: "GET"
276
+ method: "GET",
277
+ headers: token ? {
278
+ Authorization: `Bearer ${token}`
279
+ } : {}
219
280
  })).pipe(operators.map(result => result.data));
220
281
  };
221
282
  const search = props => {
@@ -225,10 +286,11 @@ const search = props => {
225
286
  shop,
226
287
  query,
227
288
  cursor,
228
- resultsPerPage
289
+ resultsPerPage,
290
+ token
229
291
  } = props;
230
- return rxjs.concat(query.pipe(operators.withLatestFrom(cursor), operators.debounceTime(500), operators.distinctUntilChanged(), operators.switchMap(_ref11 => {
231
- let [q, c] = _ref11;
292
+ return rxjs.concat(query.pipe(operators.withLatestFrom(cursor), operators.debounceTime(500), operators.distinctUntilChanged(), operators.switchMap(_ref1 => {
293
+ let [q, c] = _ref1;
232
294
  if (q) {
233
295
  return fetchSearch({
234
296
  projectId,
@@ -236,7 +298,8 @@ const search = props => {
236
298
  shop,
237
299
  query: q,
238
300
  cursor: c,
239
- resultsPerPage
301
+ resultsPerPage,
302
+ token
240
303
  }).pipe(operators.distinctUntilChanged());
241
304
  }
242
305
  return fetchList({
@@ -244,7 +307,8 @@ const search = props => {
244
307
  dataset,
245
308
  shop,
246
309
  cursor: c,
247
- resultsPerPage
310
+ resultsPerPage,
311
+ token
248
312
  });
249
313
  })));
250
314
  };
@@ -254,7 +318,7 @@ const DialogHeader = props => {
254
318
  shopifyDomain
255
319
  } = props;
256
320
  const handleOpenInNewTab = React.useCallback(() => {
257
- window.open("https://".concat(shopifyDomain, "/admin/settings/files"), "_blank");
321
+ window.open(`https://${shopifyDomain}/admin/settings/files`, "_blank");
258
322
  }, [shopifyDomain]);
259
323
  return /* @__PURE__ */jsxRuntime.jsxs(ui.Flex, {
260
324
  align: "center",
@@ -282,10 +346,7 @@ const DialogHeader = props => {
282
346
  })]
283
347
  });
284
348
  };
285
- const extractName = name => {
286
- var _a, _b, _c;
287
- return (_c = (_b = (_a = name == null ? void 0 : name.split("/")) == null ? void 0 : _a.pop()) == null ? void 0 : _b.split("?")[0]) != null ? _c : "";
288
- };
349
+ const extractName = name => name?.split("/")?.pop()?.split("?")[0] ?? "";
289
350
  function File(props) {
290
351
  const {
291
352
  onClick,
@@ -307,12 +368,12 @@ function File(props) {
307
368
  }, [onClick, data, filename]);
308
369
  return /* @__PURE__ */jsxRuntime.jsxs(Root, {
309
370
  ref: rootElm,
310
- title: "".concat(filename),
371
+ title: `${filename}`,
311
372
  tabIndex: 0,
312
373
  style: {
313
- width: "".concat(width, "px"),
314
- height: "".concat(height, "px"),
315
- backgroundImage: "url(\"".concat(preview == null ? void 0 : preview.url, "\")")
374
+ width: `${width}px`,
375
+ height: `${height}px`,
376
+ backgroundImage: `url("${preview?.url}")`
316
377
  },
317
378
  onClick: handleClick,
318
379
  children: [/* @__PURE__ */jsxRuntime.jsx(InfoLine, {
@@ -321,8 +382,8 @@ function File(props) {
321
382
  margin: 2,
322
383
  children: /* @__PURE__ */jsxRuntime.jsxs(ui.Text, {
323
384
  size: 1,
324
- title: "Select ".concat(filename),
325
- children: [filename, " ", meta.fileSize && "(".concat(prettyBytes__default.default(meta.fileSize), ")")]
385
+ title: `Select ${filename}`,
386
+ children: [filename, " ", meta.fileSize && `(${prettyBytes__default.default(meta.fileSize)})`]
326
387
  })
327
388
  }), meta.duration && /* @__PURE__ */jsxRuntime.jsx(DurationLine, {
328
389
  padding: 2,
@@ -330,7 +391,7 @@ function File(props) {
330
391
  margin: 2,
331
392
  children: /* @__PURE__ */jsxRuntime.jsx(ui.Text, {
332
393
  size: 1,
333
- title: "Video duration: ".concat(filename),
394
+ title: `Video duration: ${filename}`,
334
395
  children: prettyMilliseconds__default.default(meta.duration, {
335
396
  colonNotation: true,
336
397
  secondsDecimalDigits: 0
@@ -339,8 +400,15 @@ function File(props) {
339
400
  })]
340
401
  });
341
402
  }
342
- const Search = styledComponents.styled(ui.Stack)(_templateObject5 || (_templateObject5 = _taggedTemplateLiteral(["\n position: sticky;\n top: 0;\n z-index: 1;\n"])));
343
- styledComponents.styled.div(_templateObject6 || (_templateObject6 = _taggedTemplateLiteral(["\n overflow-y: auto;\n max-height: 80vh;\n"])));
403
+ const Search = styledComponents.styled(ui.Stack)`
404
+ position: sticky;
405
+ top: 0;
406
+ z-index: 1;
407
+ `;
408
+ styledComponents.styled.div`
409
+ overflow-y: auto;
410
+ max-height: 80vh;
411
+ `;
344
412
  const RESULTS_PER_PAGE = 42;
345
413
  const PHOTO_SPACING = 2;
346
414
  const PHOTO_PADDING = 1;
@@ -355,6 +423,10 @@ function ShopifyAssetPicker(props) {
355
423
  } = props;
356
424
  const projectId = sanity.useProjectId();
357
425
  const dataset = sanity.useDataset();
426
+ const client = sanity.useClient({
427
+ apiVersion: "2021-06-07"
428
+ });
429
+ const token = client.config().token;
358
430
  const [error, setError] = React.useState("");
359
431
  const [query, setQuery] = React.useState("");
360
432
  const [searchResults, setSearchResults] = React.useState([]);
@@ -372,7 +444,8 @@ function ShopifyAssetPicker(props) {
372
444
  shop: shopifyDomain,
373
445
  query: searchSubject$,
374
446
  cursor: cursorSubject$,
375
- resultsPerPage: RESULTS_PER_PAGE
447
+ resultsPerPage: RESULTS_PER_PAGE,
448
+ token
376
449
  }).subscribe({
377
450
  next: results => {
378
451
  setSearchResults(prevResults => [...prevResults, ...results.assets]);
@@ -380,11 +453,11 @@ function ShopifyAssetPicker(props) {
380
453
  setIsLoading(false);
381
454
  },
382
455
  error: err => {
383
- setError("".concat(err.response.data.message || err.message || "An error occurred", " - check plugin configuration"));
456
+ setError(`${err.response?.data?.message || err.message || "An error occurred"} - check plugin configuration`);
384
457
  }
385
458
  });
386
459
  return () => searchSubscription.unsubscribe();
387
- }, [searchSubject$, cursorSubject$, shopifyDomain, projectId, dataset]);
460
+ }, [searchSubject$, cursorSubject$, shopifyDomain, projectId, dataset, token]);
388
461
  const handleSearchTermChanged = React.useCallback(event => {
389
462
  const newQuery = event.currentTarget.value;
390
463
  setQuery(newQuery);
@@ -400,11 +473,11 @@ function ShopifyAssetPicker(props) {
400
473
  searchSubject$.next(query);
401
474
  }, [cursorSubject$, pageInfo, searchSubject$, query]);
402
475
  const handleSelect = React.useCallback(file => {
403
- file._key = value == null ? void 0 : value._key;
476
+ file._key = value?._key;
404
477
  file._type = schemaType.name;
405
478
  onChange(sanity.PatchEvent.from([sanity.set(file)]));
406
479
  onClose();
407
- }, [onChange, onClose, schemaType.name, value == null ? void 0 : value._key]);
480
+ }, [onChange, onClose, schemaType.name, value?._key]);
408
481
  const renderFile = React.useCallback(fileProps => {
409
482
  const {
410
483
  photo,
@@ -475,7 +548,7 @@ function ShopifyAssetPicker(props) {
475
548
  }), /* @__PURE__ */jsxRuntime.jsx(InfiniteScroll__default.default, {
476
549
  dataLength: searchResults.length,
477
550
  next: handleScollerLoadMore,
478
- hasMore: pageInfo ? pageInfo == null ? void 0 : pageInfo.hasNextPage : true,
551
+ hasMore: pageInfo ? pageInfo?.hasNextPage : true,
479
552
  scrollThreshold: 0.99,
480
553
  height: "60vh",
481
554
  loader: /* @__PURE__ */jsxRuntime.jsx(ui.Flex, {
@@ -501,21 +574,18 @@ function ShopifyAssetPicker(props) {
501
574
  spacing: PHOTO_SPACING,
502
575
  padding: PHOTO_PADDING,
503
576
  targetRowHeight: handleWidth,
504
- photos: searchResults.map(file => {
505
- var _a, _b, _c;
506
- return {
507
- src: (_a = file == null ? void 0 : file.preview) == null ? void 0 : _a.url,
508
- width: ((_b = file == null ? void 0 : file.preview) == null ? void 0 : _b.width) || 2048,
509
- height: ((_c = file == null ? void 0 : file.preview) == null ? void 0 : _c.height) || 2048,
510
- key: file.id,
511
- data: file
512
- };
513
- }),
577
+ photos: searchResults.map(file => ({
578
+ src: file?.preview?.url,
579
+ width: file?.preview?.width || 2048,
580
+ height: file?.preview?.height || 2048,
581
+ key: file.id,
582
+ data: file
583
+ })),
514
584
  renderPhoto: renderFile,
515
585
  componentsProps: {
516
586
  containerProps: {
517
587
  style: {
518
- marginBottom: "".concat(PHOTO_SPACING, "px")
588
+ marginBottom: `${PHOTO_SPACING}px`
519
589
  }
520
590
  }
521
591
  }
@@ -624,15 +694,14 @@ function ShopifyAssetInput(props) {
624
694
  })]
625
695
  });
626
696
  }
627
- const CloudinaryDiffPreview = _ref12 => {
697
+ const CloudinaryDiffPreview = _ref10 => {
628
698
  let {
629
699
  value
630
- } = _ref12;
631
- var _a, _b;
700
+ } = _ref10;
632
701
  if (!value) {
633
702
  return null;
634
703
  }
635
- if ((_a = value == null ? void 0 : value.preview) == null ? void 0 : _a.url) {
704
+ if (value?.preview?.url) {
636
705
  return /* @__PURE__ */jsxRuntime.jsx(ui.Flex, {
637
706
  justify: "center",
638
707
  align: "center",
@@ -642,7 +711,7 @@ const CloudinaryDiffPreview = _ref12 => {
642
711
  space: 2,
643
712
  children: [/* @__PURE__ */jsxRuntime.jsx("img", {
644
713
  alt: "preview",
645
- src: (_b = value == null ? void 0 : value.preview) == null ? void 0 : _b.url,
714
+ src: value?.preview?.url,
646
715
  style: {
647
716
  objectFit: "contain",
648
717
  margin: "auto",
@@ -666,11 +735,11 @@ const CloudinaryDiffPreview = _ref12 => {
666
735
  })
667
736
  });
668
737
  };
669
- const AssetDiff = _ref13 => {
738
+ const AssetDiff = _ref11 => {
670
739
  let {
671
740
  diff,
672
741
  schemaType
673
- } = _ref13;
742
+ } = _ref11;
674
743
  return /* @__PURE__ */jsxRuntime.jsx(sanity.DiffFromTo, {
675
744
  diff,
676
745
  schemaType,
@@ -729,14 +798,14 @@ const shopifyAssetSchema = config => {
729
798
  filename: "filename",
730
799
  type: "type"
731
800
  },
732
- prepare(_ref14) {
801
+ prepare(_ref12) {
733
802
  let {
734
803
  url,
735
804
  meta,
736
805
  preview,
737
806
  filename,
738
807
  type
739
- } = _ref14;
808
+ } = _ref12;
740
809
  return {
741
810
  title: filename,
742
811
  subtitle: type,