sanity-plugin-cloudinary 1.4.1 → 2.0.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.
package/lib/index.esm.js DELETED
@@ -1,774 +0,0 @@
1
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
- import React, { useCallback, useState, useRef, useEffect } from 'react';
3
- import { PatchEvent, unset, set, DiffFromTo, defineType, setIfMissing, insert, ArrayOfObjectsFunctions, definePlugin, isArrayOfObjectsSchemaType } from 'sanity';
4
- import { Flex, Text, Stack, Button, Grid, Dialog, Box, Spinner } from '@sanity/ui';
5
- import { DocumentIcon, PlugIcon } from '@sanity/icons';
6
- import { styled } from 'styled-components';
7
- import { nanoid } from 'nanoid';
8
- import { SettingsView, useSecrets } from '@sanity/studio-secrets';
9
- function VideoPlayer(props) {
10
- const {
11
- src
12
- } = props;
13
- const style = {
14
- width: "100%",
15
- height: "auto"
16
- };
17
- return /* @__PURE__ */jsx("video", {
18
- controls: true,
19
- style,
20
- children: /* @__PURE__ */jsx("source", {
21
- src,
22
- type: "video/mp4"
23
- })
24
- });
25
- }
26
- const widgetSrc = "https://media-library.cloudinary.com/global/all.js";
27
- function assetUrl(asset) {
28
- if (asset.derived && asset.derived.length > 0) {
29
- const [derived] = asset.derived;
30
- if (derived.secure_url) {
31
- return derived.secure_url;
32
- }
33
- return derived.url;
34
- }
35
- if (asset.secure_url) {
36
- return asset.secure_url;
37
- }
38
- return asset.url;
39
- }
40
- const openMediaSelector = (cloudName, apiKey, multiple, insertHandler, selectedAsset) => {
41
- loadJS(widgetSrc, () => {
42
- const options = {
43
- cloud_name: cloudName,
44
- api_key: apiKey,
45
- insert_caption: "Select",
46
- multiple
47
- };
48
- if (selectedAsset) {
49
- options.asset = {
50
- public_id: selectedAsset.public_id,
51
- type: selectedAsset.type,
52
- resource_type: selectedAsset.resource_type
53
- };
54
- }
55
- window.cloudinary.openMediaLibrary(options, {
56
- insertHandler
57
- });
58
- });
59
- };
60
- const createMediaLibrary = _ref => {
61
- let {
62
- cloudName,
63
- apiKey,
64
- inlineContainer,
65
- libraryCreated,
66
- insertHandler
67
- } = _ref;
68
- loadJS(widgetSrc, () => {
69
- const options = {
70
- cloud_name: cloudName,
71
- api_key: apiKey,
72
- insert_caption: "Select",
73
- inline_container: inlineContainer,
74
- remove_header: true
75
- };
76
- libraryCreated(window.cloudinary.createMediaLibrary(options, {
77
- insertHandler
78
- }));
79
- });
80
- };
81
- function loadJS(url, callback) {
82
- const existingScript = document.getElementById("damWidget");
83
- if (!existingScript) {
84
- const script = document.createElement("script");
85
- script.src = url;
86
- script.id = "damWidget";
87
- document.body.appendChild(script);
88
- script.onload = () => {
89
- if (callback) {
90
- return callback();
91
- }
92
- return true;
93
- };
94
- }
95
- if (existingScript && callback) {
96
- return callback();
97
- }
98
- return true;
99
- }
100
- function encodeSourceId(asset) {
101
- const {
102
- resource_type,
103
- public_id,
104
- type
105
- } = asset;
106
- return btoa(JSON.stringify({
107
- public_id,
108
- resource_type,
109
- type
110
- }));
111
- }
112
- function encodeFilename(asset) {
113
- return `${asset.public_id.split("/").slice(-1)[0]}.${asset.format}`;
114
- }
115
- function decodeSourceId(sourceId) {
116
- let sourceIdDecoded;
117
- try {
118
- sourceIdDecoded = JSON.parse(atob(sourceId));
119
- } catch (err) {}
120
- return sourceIdDecoded;
121
- }
122
- const AssetPreview = _ref2 => {
123
- let {
124
- value,
125
- layout
126
- } = _ref2;
127
- const url = value && assetUrl(value);
128
- if (!value || !url) {
129
- return null;
130
- }
131
- switch (value.resource_type) {
132
- case "video":
133
- return /* @__PURE__ */jsx(Flex, {
134
- align: "center",
135
- style: {
136
- maxWidth: layout === "default" ? "80px" : "100%"
137
- },
138
- children: /* @__PURE__ */jsx(VideoPlayer, {
139
- src: url,
140
- kind: "player"
141
- })
142
- });
143
- case "raw":
144
- return /* @__PURE__ */jsxs(Flex, {
145
- align: "center",
146
- children: [/* @__PURE__ */jsx(DocumentIcon, {}), /* @__PURE__ */jsx(Text, {
147
- size: 1,
148
- style: {
149
- marginLeft: "0.5em"
150
- },
151
- children: value.display_name ?? "Raw file"
152
- })]
153
- });
154
- default:
155
- return /* @__PURE__ */jsx(Flex, {
156
- align: "center",
157
- children: /* @__PURE__ */jsx("img", {
158
- alt: "preview",
159
- src:
160
- // Cloudinary returns resource_type as "image" even for PDFs,
161
- // so we check the format to handle PDFs specifically.
162
- // If it's a PDF, convert the first page to JPG and overlay a "PDF" label for thumbnail clarity.
163
- value.format === "pdf" ? url?.replace("image/upload", "image/upload/f_jpg,pg_1,l_text:Verdana_75_letter_spacing_14:PDF") : url,
164
- style: {
165
- maxWidth: layout === "default" ? "80px" : "100%",
166
- height: "auto"
167
- }
168
- })
169
- });
170
- }
171
- };
172
- const SetupButtonContainer = styled.div`
173
- position: relative;
174
- display: block;
175
- font-size: 0.8em;
176
- transform: translate(0%, -10%);
177
- `;
178
- const WidgetInput = props => {
179
- const {
180
- onChange,
181
- readOnly,
182
- value,
183
- openMediaSelector
184
- } = props;
185
- const removeValue = useCallback(() => {
186
- onChange(PatchEvent.from([unset()]));
187
- }, [onChange]);
188
- return /* @__PURE__ */jsxs(Stack, {
189
- children: [/* @__PURE__ */jsx(SetupButtonContainer, {
190
- children: /* @__PURE__ */jsx(Flex, {
191
- flex: 1,
192
- justify: "flex-end",
193
- children: /* @__PURE__ */jsx(Button, {
194
- color: "primary",
195
- icon: PlugIcon,
196
- mode: "bleed",
197
- title: "Configure",
198
- onClick: props.onSetup,
199
- tabIndex: 1
200
- })
201
- })
202
- }), /* @__PURE__ */jsx(Flex, {
203
- style: {
204
- textAlign: "center",
205
- width: "100%"
206
- },
207
- marginBottom: 2,
208
- children: /* @__PURE__ */jsx(AssetPreview, {
209
- value
210
- })
211
- }), /* @__PURE__ */jsxs(Grid, {
212
- gap: 1,
213
- style: {
214
- gridTemplateColumns: "repeat(auto-fit, minmax(100px, 1fr))"
215
- },
216
- children: [/* @__PURE__ */jsx(Button, {
217
- disabled: readOnly,
218
- mode: "ghost",
219
- title: "Select an asset",
220
- tone: "default",
221
- onClick: openMediaSelector,
222
- text: "Select\u2026"
223
- }), /* @__PURE__ */jsx(Button, {
224
- disabled: readOnly || !value,
225
- tone: "critical",
226
- mode: "ghost",
227
- title: "Remove asset",
228
- text: "Remove",
229
- onClick: removeValue
230
- })]
231
- })]
232
- });
233
- };
234
- const pluginConfigKeys = [{
235
- key: "cloudName",
236
- title: "Cloud name",
237
- description: ""
238
- }, {
239
- key: "apiKey",
240
- title: "API key",
241
- description: ""
242
- }];
243
- const namespace = "cloudinary";
244
- const SecretsConfigView = props => {
245
- return /* @__PURE__ */jsx(SettingsView, {
246
- title: "Cloudinary config",
247
- namespace,
248
- keys: pluginConfigKeys,
249
- onClose: props.onClose
250
- });
251
- };
252
- const CloudinaryInput = props => {
253
- const [showSettings, setShowSettings] = useState(false);
254
- const {
255
- secrets
256
- } = useSecrets(namespace);
257
- const {
258
- onChange,
259
- schemaType: type
260
- } = props;
261
- const value = props.value || void 0;
262
- const handleSelect = useCallback(payload => {
263
- const [asset] = payload.assets;
264
- if (!asset) {
265
- return;
266
- }
267
- let updatedAsset = asset;
268
- const assetWithoutNulls = Object.fromEntries(Object.entries(asset).filter(_ref3 => {
269
- let [_, assetValue] = _ref3;
270
- return assetValue !== null;
271
- }));
272
- const requiredFields = {
273
- public_id: asset.public_id,
274
- resource_type: asset.resource_type,
275
- type: asset.type,
276
- url: asset.url,
277
- secure_url: asset.secure_url,
278
- format: asset.format,
279
- width: asset.width,
280
- height: asset.height,
281
- bytes: asset.bytes,
282
- tags: asset.tags
283
- };
284
- updatedAsset = {
285
- ...assetWithoutNulls,
286
- ...requiredFields
287
- };
288
- if (asset.context) {
289
- const objectWithRenamedKeys = Object.fromEntries(Object.entries(asset.context.custom).map(_ref4 => {
290
- let [contextKey, contextValue] = _ref4;
291
- return [contextKey.replace(/[^a-zA-Z0-9_]|-/g, "_"), contextValue];
292
- }));
293
- updatedAsset = {
294
- ...updatedAsset,
295
- context: {
296
- ...asset.context,
297
- custom: objectWithRenamedKeys
298
- }
299
- };
300
- }
301
- if (asset.derived) {
302
- const derivedWithType = asset.derived.map(derivedItem => ({
303
- ...derivedItem,
304
- _type: "derived"
305
- }));
306
- updatedAsset = {
307
- ...updatedAsset,
308
- derived: derivedWithType
309
- };
310
- }
311
- onChange(PatchEvent.from([set(Object.assign({
312
- _type: type.name,
313
- _version: 1,
314
- ...(value?._key ? {
315
- _key: value._key
316
- } : {
317
- _key: nanoid()
318
- })
319
- }, updatedAsset))]));
320
- }, [onChange, type, value?._key]);
321
- const action = secrets ? () => openMediaSelector(secrets.cloudName, secrets.apiKey, false,
322
- // single selection
323
- handleSelect, value) : () => setShowSettings(true);
324
- return /* @__PURE__ */jsxs(Fragment, {
325
- children: [showSettings && /* @__PURE__ */jsx(SecretsConfigView, {
326
- onClose: () => setShowSettings(false)
327
- }), /* @__PURE__ */jsx(WidgetInput, {
328
- onSetup: () => setShowSettings(true),
329
- openMediaSelector: action,
330
- ...props
331
- })]
332
- });
333
- };
334
- const CloudinaryDiffPreview = _ref5 => {
335
- let {
336
- value
337
- } = _ref5;
338
- if (!value) {
339
- return null;
340
- }
341
- const url = assetUrl(value);
342
- if (value.resource_type === "video" && url) {
343
- return /* @__PURE__ */jsx("section", {
344
- style: {
345
- display: "flex",
346
- flexWrap: "wrap",
347
- justifyContent: "space-between"
348
- },
349
- children: /* @__PURE__ */jsx(VideoPlayer, {
350
- src: url,
351
- kind: "diff"
352
- })
353
- });
354
- }
355
- return /* @__PURE__ */jsx("img", {
356
- alt: "preview",
357
- src: url,
358
- style: {
359
- maxWidth: "100%",
360
- height: "auto"
361
- }
362
- });
363
- };
364
- const AssetDiff = _ref6 => {
365
- let {
366
- diff,
367
- schemaType
368
- } = _ref6;
369
- return /* @__PURE__ */jsx(DiffFromTo, {
370
- diff,
371
- schemaType,
372
- previewComponent: CloudinaryDiffPreview
373
- });
374
- };
375
- const cloudinaryAssetSchema = defineType({
376
- type: "object",
377
- name: "cloudinary.asset",
378
- fields: [{
379
- type: "string",
380
- name: "public_id"
381
- }, {
382
- type: "string",
383
- name: "resource_type"
384
- // "image", "?"
385
- }, {
386
- type: "string",
387
- name: "type"
388
- // "upload", "?"
389
- }, {
390
- type: "string",
391
- name: "format"
392
- // "jpg"
393
- }, {
394
- type: "number",
395
- name: "version"
396
- }, {
397
- type: "url",
398
- name: "url"
399
- }, {
400
- type: "url",
401
- name: "secure_url"
402
- }, {
403
- type: "number",
404
- name: "width"
405
- }, {
406
- type: "number",
407
- name: "height"
408
- }, {
409
- type: "number",
410
- name: "bytes"
411
- }, {
412
- type: "number",
413
- name: "duration"
414
- // can be null
415
- }, {
416
- type: "array",
417
- name: "tags",
418
- of: [{
419
- type: "string"
420
- }]
421
- }, {
422
- type: "datetime",
423
- name: "created_at"
424
- }, {
425
- type: "array",
426
- name: "derived",
427
- of: [{
428
- type: "cloudinary.assetDerived",
429
- name: "derived"
430
- }]
431
- }, {
432
- type: "string",
433
- name: "access_mode"
434
- }, {
435
- type: "cloudinary.assetContext",
436
- name: "context"
437
- }
438
- // metadata array of unknown content
439
- ],
440
- ...{
441
- components: {
442
- input: CloudinaryInput,
443
- diff: AssetDiff,
444
- preview: AssetPreview
445
- }
446
- },
447
- //TODO revert this change when rc.1 is released
448
- preview: {
449
- select: {
450
- url: "url",
451
- resource_type: "resource_type",
452
- derived: "derived.0.url"
453
- },
454
- prepare(_ref7) {
455
- let {
456
- url,
457
- derived,
458
- resource_type
459
- } = _ref7;
460
- return {
461
- title: url,
462
- value: {
463
- title: url,
464
- resource_type,
465
- url: derived || url
466
- }
467
- };
468
- }
469
- }
470
- });
471
- const cloudinaryAssetDerivedSchema = defineType({
472
- type: "object",
473
- name: "cloudinary.assetDerived",
474
- fields: [{
475
- type: "string",
476
- name: "raw_transformation"
477
- }, {
478
- type: "url",
479
- name: "url"
480
- }, {
481
- type: "url",
482
- name: "secure_url"
483
- }]
484
- });
485
- function CloudinaryIcon() {
486
- return /* @__PURE__ */jsx("svg", {
487
- version: "1.1",
488
- id: "Layer_1",
489
- x: "0px",
490
- y: "0px",
491
- width: "1em",
492
- height: "1em",
493
- viewBox: "0 0 141.732 141.747",
494
- enableBackground: "new 0 0 141.732 141.747",
495
- children: /* @__PURE__ */jsxs("g", {
496
- children: [/* @__PURE__ */jsx("path", {
497
- fill: "#0071CE",
498
- d: "M115.585,109.242c-1.609,0-3.107-1.024-3.635-2.637c-0.657-2.008,0.438-4.169,2.447-4.826\n c7.278-2.382,11.98-8.761,11.98-16.252c0-9.487-7.718-17.206-17.205-17.206c-0.659,0-1.368,0.052-2.231,0.164l-3.741,0.485\n l-0.537-3.735c-2.299-16.016-16.251-28.094-32.454-28.094c-13.395,0-25.32,8.019-30.377,20.43l-0.952,2.335l-2.52,0.046\n c-11.581,0.213-21.003,9.804-21.003,21.379c0,8.45,4.906,16.156,12.498,19.631c1.921,0.88,2.766,3.15,1.886,5.071\n c-0.88,1.921-3.149,2.764-5.07,1.887C14.363,103.202,7.703,92.766,7.703,81.331c0-14.88,11.465-27.345,26.028-28.876\n c6.71-14.03,20.773-22.965,36.477-22.965c18.796,0,35.135,13.178,39.372,31.184c13.519,0.219,24.45,11.284,24.45,24.854\n c0,10.693-6.934,20.146-17.253,23.523C116.382,109.18,115.98,109.242,115.585,109.242z"
499
- }), /* @__PURE__ */jsx("path", {
500
- fill: "#DC8327",
501
- d: "M57.12,111.02c-0.001-0.001-0.001-0.001-0.002-0.001c-0.001,0-0.002-0.001-0.003-0.001h-0.001\n c0,0-0.001-0.001-0.001-0.001l-0.001-0.001c0,0-0.001,0-0.001-0.001h-0.001l-0.001-0.001c0.001-0.001-0.001-0.001-0.001-0.001\n l-0.001-0.001c0,0-0.001,0-0.001,0l-0.001-0.001c0.001,0.001-0.001-0.001-0.001-0.001s-0.002-0.001-0.003-0.001l-0.001-0.001H57.1\n c-0.001-0.001-0.001-0.001-0.001-0.001l-0.001-0.001c-0.003-0.001-0.002-0.001-0.003-0.001c-0.001,0.001-0.001-0.001-0.002-0.001\n l-0.001-0.001c0,0-0.001-0.001-0.002-0.001l-0.001-0.001c-0.001-0.001-0.003-0.001-0.004-0.001s-0.003-0.001-0.004-0.001\n c-0.001-0.001-0.001-0.001-0.002-0.001h-0.001c-0.001-0.001-0.002-0.001-0.003-0.001c-0.001-0.001-0.003-0.001-0.003-0.001\n c-0.001,0-0.001,0-0.001,0l-0.002-0.001c-0.001,0-0.001-0.001-0.001-0.001h-0.001c-0.059-0.021-0.122-0.034-0.188-0.037h-0.002\n h-0.002c-0.001,0-0.001,0-0.002,0c0,0,0,0-0.001,0c0,0-0.001,0-0.001,0h-0.001c-0.001-0.001-0.001-0.001-0.001-0.001\n c-0.001,0-0.002,0-0.002,0c-0.001,0-0.002,0-0.002,0h-0.001h-0.001h-0.001c-0.001,0-0.002,0-0.002,0h-0.001H56.86h-0.001h-0.001\n c-0.001,0-0.001,0-0.001,0h-0.001h-0.001h-0.001h-0.001c-0.001,0-0.001,0-0.001,0c-1.656,0-3.011-1.348-3.021-3V74.29h2.567\n c0.004,0,0.009,0,0.013,0c0.393,0.017,0.661-0.285,0.661-0.648c0-0.271-0.166-0.503-0.402-0.6l-12.379-8.544\n c-0.222-0.153-0.515-0.153-0.737,0l-12.476,8.611c-0.234,0.161-0.335,0.456-0.251,0.727c0.085,0.271,0.335,0.455,0.619,0.455h2.58\n l0.002,33.674c0.013,2.328,1.883,4.228,4.262,4.288c0.027,0.003,0.053,0.005,0.08,0.005h18.481c0.004,0,0.007,0,0.011,0\n c0.17-0.003,0.324-0.071,0.438-0.18c0,0,0,0,0.001-0.001c0.002-0.002,0.004-0.004,0.005-0.005c0.001-0.001,0.002-0.001,0.003-0.003\n c0,0,0.001-0.001,0.001-0.001l0.001-0.001l0.001-0.001l0.001-0.001l0.001-0.001c0.001-0.001,0.001-0.001,0.001-0.001\n c0.002-0.001,0.001-0.001,0.002-0.002c0,0,0,0,0.001-0.001l0.001-0.001c0,0,0,0,0.001-0.001c0.112-0.116,0.182-0.273,0.183-0.447\n v-0.002v-0.001v-0.001v-0.001v-0.001v-0.001v-0.001v-0.002C57.498,111.345,57.343,111.121,57.12,111.02z"
502
- }), /* @__PURE__ */jsx("path", {
503
- fill: "#F4B21B",
504
- d: "M83.889,111.02c0,0-0.001-0.001-0.002-0.001c-0.001,0-0.002-0.001-0.003-0.001h-0.001\n c-0.001-0.001-0.001-0.001-0.001-0.001l-0.001-0.001h-0.001c0,0-0.001-0.001-0.001-0.001c0,0-0.001-0.001-0.001-0.001\n c0.001-0.001-0.001-0.001-0.002-0.001l-0.001-0.001h-0.001c-0.001,0-0.001-0.001-0.001-0.001c-0.002,0.001-0.002-0.001-0.002-0.001\n l-0.002-0.001l-0.001-0.001h-0.001c0,0-0.001-0.001-0.001-0.001l-0.001-0.001c-0.001-0.001-0.002-0.001-0.002-0.001\n c-0.001,0.001-0.001-0.001-0.003-0.001l-0.001-0.001l-0.001-0.001c0,0-0.001,0-0.002-0.001c-0.001-0.001-0.003-0.001-0.004-0.001\n s-0.003-0.001-0.004-0.001c-0.001-0.001-0.001-0.001-0.002-0.001c-0.001,0-0.001,0-0.002-0.001c0,0-0.001,0-0.002-0.001\n c-0.003-0.001-0.001-0.001-0.002-0.001c-0.003,0-0.001,0-0.002-0.001c0,0-0.001-0.001-0.002-0.001l-0.001-0.001h-0.001\n c-0.059-0.021-0.122-0.034-0.188-0.037h-0.002c-0.001,0-0.001,0-0.001,0c-0.001,0-0.002,0-0.002,0s-0.001,0-0.001,0h-0.001h-0.001\n l-0.001-0.001c-0.001,0-0.002,0-0.002,0c-0.001,0-0.002,0-0.002,0h-0.001c-0.001,0-0.001,0-0.001,0h-0.001\n c-0.001,0-0.002,0-0.002,0s-0.001,0-0.002,0h-0.001c0,0-0.001,0-0.001,0h-0.001c-0.001,0-0.001,0-0.001,0h-0.001h-0.001h-0.001\n h-0.001c-0.001,0-0.001,0-0.001,0c-1.655,0-3.01-1.348-3.02-3V81.829h2.579c0.009-0.001,0.016-0.001,0.026,0\n c0.358,0,0.648-0.29,0.648-0.648c0-0.271-0.166-0.503-0.402-0.6l-12.38-8.544c-0.222-0.153-0.515-0.153-0.737,0L57.86,80.647\n c-0.234,0.161-0.335,0.456-0.251,0.727c0.085,0.271,0.335,0.455,0.619,0.455h2.568l0.002,26.135\n c0.011,2.329,1.884,4.23,4.264,4.289c0.026,0.003,0.052,0.004,0.078,0.004h18.481c0.004,0,0.007,0,0.011,0\n c0.17-0.003,0.324-0.071,0.438-0.18c0,0,0,0,0.001-0.001c0.002-0.002,0.006-0.004,0.005-0.005c0.001-0.001,0.002-0.001,0.003-0.003\n c0.001-0.001,0.001-0.001,0.001-0.001l0.001-0.001l0.001-0.001c0,0,0.001,0,0.001-0.001l0.001-0.001\n c0.001,0,0.001-0.001,0.001-0.001c0.003-0.001,0.002-0.001,0.002-0.002c0,0,0,0,0.001-0.001c0,0,0,0,0.001-0.001\n c0,0,0,0,0.001-0.001c0.112-0.116,0.182-0.273,0.183-0.447v-0.002v-0.001v-0.001v-0.001v-0.001v-0.001v-0.001v-0.002\n C84.267,111.345,84.112,111.121,83.889,111.02z"
505
- }), /* @__PURE__ */jsx("path", {
506
- fill: "#F2D864",
507
- d: "M110.667,111.02l-0.002-0.001c-0.001,0-0.002-0.001-0.003-0.001h-0.001\n c-0.001-0.001-0.001-0.001-0.001-0.001l-0.001-0.001c-0.001,0-0.001,0-0.001-0.001h-0.001l-0.001-0.001\n c-0.001-0.001-0.001-0.001-0.001-0.001s-0.001,0-0.001-0.001h-0.001c0,0-0.001-0.001-0.001-0.001\n c-0.001,0.001-0.001-0.001-0.002-0.001c0.001-0.001-0.001-0.001-0.002-0.001l-0.001-0.001c-0.001,0-0.001,0-0.001,0\n c-0.001-0.001-0.001-0.001-0.001-0.001l-0.001-0.001c-0.003-0.001-0.002-0.001-0.002-0.001c-0.001,0.001-0.001-0.001-0.003-0.001\n l-0.001-0.001c0.001-0.001-0.001-0.001-0.002-0.001c0,0-0.001,0-0.001-0.001c-0.001-0.001-0.003-0.001-0.004-0.001\n s-0.003-0.001-0.004-0.001l-0.002-0.001c0,0-0.001,0-0.002-0.001l-0.002-0.001c-0.003-0.001-0.003-0.001-0.002-0.001\n c-0.003,0-0.003,0-0.002-0.001c-0.001,0-0.001-0.001-0.001-0.001c-0.001,0-0.002-0.001-0.002-0.001h-0.001\n c-0.059-0.021-0.122-0.034-0.188-0.037h-0.001c-0.001,0-0.002,0-0.002,0c-0.001,0-0.002,0-0.003,0h-0.001h-0.001h-0.001\n c-0.001-0.001-0.001-0.001-0.002-0.001c0,0-0.001,0-0.002,0c0,0-0.001,0-0.002,0h-0.001c0,0-0.001,0-0.001,0h-0.001\n c-0.001,0-0.002,0-0.002,0h-0.002c0,0,0,0-0.001,0c0,0-0.001,0-0.001,0h-0.001c0,0-0.001,0-0.001,0h-0.001h-0.001h-0.001H110.4\n c0,0-0.001,0-0.001,0c-1.655,0-3.01-1.348-3.02-3V89.365h2.573c0.004,0,0.009,0,0.013,0c0.365-0.009,0.661-0.285,0.661-0.648\n c0-0.271-0.166-0.503-0.402-0.6l-12.38-8.544c-0.221-0.153-0.515-0.153-0.737,0l-12.476,8.61c-0.234,0.161-0.335,0.456-0.251,0.727\n c0.085,0.271,0.335,0.455,0.619,0.455h2.573l0.002,18.599c0.013,2.329,1.885,4.231,4.264,4.289\n c0.026,0.003,0.052,0.004,0.078,0.004h18.481c0.004,0,0.007,0,0.011,0c0.17-0.003,0.324-0.071,0.438-0.18l0.001-0.001\n c0.002-0.002,0.005-0.004,0.005-0.005c0.001-0.001,0.002-0.001,0.003-0.003c0,0,0.001-0.001,0.001-0.001l0.001-0.001l0.001-0.001\n l0.001-0.001l0.001-0.001l0.001-0.001c0.003-0.001,0.001-0.001,0.002-0.002c0,0,0,0,0.001-0.001l0.001-0.001c0,0,0,0,0.001-0.001\n c0.112-0.116,0.182-0.273,0.183-0.447v-0.002v-0.001v-0.001v-0.001v-0.001v-0.001v-0.001v-0.002\n C111.045,111.345,110.889,111.121,110.667,111.02z"
508
- })]
509
- })
510
- });
511
- }
512
- const Widget = styled.div`
513
- height: 70vh;
514
- `;
515
- function CloudinaryAssetSource(props) {
516
- const {
517
- onClose,
518
- dialogHeaderTitle
519
- } = props;
520
- const [loadingMessage, setLoadingMessage] = useState("Loading Cloudinary Media Libary");
521
- const library = useRef(void 0);
522
- const contentRef = useRef(null);
523
- const {
524
- secrets
525
- } = useSecrets(namespace);
526
- const cloudName = secrets?.cloudName;
527
- const apiKey = secrets?.apiKey;
528
- const [widgetId] = useState(() => `cloundinaryWidget-${Date.now()}`);
529
- const [showSettings, setShowSettings] = useState(false);
530
- const propsRef = useRef(props);
531
- useEffect(() => {
532
- propsRef.current = props;
533
- }, [props]);
534
- const handleClose = useCallback(() => {
535
- if (library.current) {
536
- library.current.hide();
537
- }
538
- onClose();
539
- }, [onClose, library]);
540
- useEffect(() => {
541
- if (!cloudName || !apiKey) {
542
- return;
543
- }
544
- createMediaLibrary({
545
- cloudName,
546
- apiKey,
547
- inlineContainer: `#${widgetId}`,
548
- libraryCreated: lib => {
549
- library.current = lib;
550
- const selectedAssets = propsRef.current.selectedAssets;
551
- const firstSelectedAsset = selectedAssets ? selectedAssets[0] : null;
552
- const iframe = contentRef.current && contentRef.current.firstChild;
553
- if (iframe && iframe instanceof HTMLIFrameElement) {
554
- setLoadingMessage(void 0);
555
- let asset;
556
- if (propsRef.current.selectionType === "single" && firstSelectedAsset && firstSelectedAsset.source && firstSelectedAsset.source.id) {
557
- asset = decodeSourceId(firstSelectedAsset.source.id);
558
- }
559
- const folder = asset ? {
560
- path: asset.public_id.split("/").slice(0, -1).join("/"),
561
- resource_type: "image"
562
- } : {
563
- path: "",
564
- resource_type: "image"
565
- };
566
- if (lib && contentRef.current) {
567
- lib.show({
568
- folder,
569
- asset
570
- });
571
- contentRef.current.style.visibility = "visible";
572
- }
573
- }
574
- },
575
- insertHandler: _ref8 => {
576
- let {
577
- assets
578
- } = _ref8;
579
- if (!library.current) {
580
- return;
581
- }
582
- const imageAssets = assets.filter(asset => asset.resource_type === "image");
583
- if (imageAssets.length === 0) {
584
- throw new Error("The selection did not contain any images.");
585
- }
586
- library.current.hide();
587
- propsRef.current.onSelect(imageAssets.map(asset => {
588
- const url = asset.derived && asset.derived[0] ? asset.derived[0].secure_url : asset.secure_url;
589
- return {
590
- kind: "url",
591
- value: url,
592
- assetDocumentProps: {
593
- _type: "sanity.imageAsset",
594
- originalFilename: encodeFilename(asset),
595
- source: {
596
- id: encodeSourceId(asset),
597
- name: `cloudinary:${cloudName}`
598
- }
599
- }
600
- };
601
- }));
602
- }
603
- });
604
- }, [cloudName, apiKey, widgetId]);
605
- const hasConfig = apiKey && cloudName;
606
- return /* @__PURE__ */jsx(Dialog, {
607
- id: "cloudinary-asset-source",
608
- header: dialogHeaderTitle ?? "Select image from Cloudinary",
609
- onClose: handleClose,
610
- open: true,
611
- width: 4,
612
- children: /* @__PURE__ */jsxs(Box, {
613
- padding: 4,
614
- children: [showSettings && /* @__PURE__ */jsx(SecretsConfigView, {
615
- onClose: () => setShowSettings(false)
616
- }), /* @__PURE__ */jsx(Flex, {
617
- flex: 1,
618
- justify: "flex-end",
619
- children: /* @__PURE__ */jsx(Button, {
620
- color: "primary",
621
- icon: PlugIcon,
622
- mode: "bleed",
623
- title: "Configure",
624
- onClick: () => setShowSettings(true),
625
- tabIndex: 1,
626
- text: hasConfig ? void 0 : "Configure Cloudinary plugin"
627
- })
628
- }), hasConfig && loadingMessage && /* @__PURE__ */jsxs(Stack, {
629
- space: 3,
630
- children: [/* @__PURE__ */jsx(Flex, {
631
- align: "center",
632
- justify: "center",
633
- children: /* @__PURE__ */jsx(Spinner, {
634
- muted: true
635
- })
636
- }), /* @__PURE__ */jsx(Text, {
637
- size: 1,
638
- muted: true,
639
- align: "center",
640
- children: loadingMessage
641
- })]
642
- }), /* @__PURE__ */jsx(Widget, {
643
- style: {
644
- visibility: "hidden"
645
- },
646
- ref: contentRef,
647
- id: widgetId
648
- })]
649
- })
650
- });
651
- }
652
- const cloudinaryAssetContext = defineType({
653
- type: "object",
654
- name: "cloudinary.assetContext",
655
- fields: [{
656
- type: "cloudinary.assetContextCustom",
657
- name: "custom"
658
- }]
659
- });
660
- const cloudinaryAssetContextCustom = defineType({
661
- type: "object",
662
- name: "cloudinary.assetContextCustom",
663
- fields: [{
664
- type: "string",
665
- name: "alt"
666
- }, {
667
- type: "string",
668
- name: "caption"
669
- }]
670
- });
671
- const AssetListFunctions = props => {
672
- const {
673
- onValueCreate,
674
- onChange
675
- } = props;
676
- const {
677
- secrets,
678
- loading
679
- } = useSecrets(namespace);
680
- const [showSettings, setShowSettings] = React.useState(false);
681
- const show = useCallback(() => setShowSettings(true), [setShowSettings]);
682
- const hide = useCallback(() => setShowSettings(false), [setShowSettings]);
683
- const cloudinaryType = props.schemaType.of.find(t => t.name === cloudinaryAssetSchema.name);
684
- if (!cloudinaryType) {
685
- throw new Error(`AssetListFunctions can only be used in array.of ${cloudinaryAssetSchema.name}, but it was array.of
686
- ${props.schemaType.of.map(t => t.name)}`);
687
- }
688
- const handleSelect = useCallback(selected => {
689
- const items = selected.assets.map(asset => Object.assign({}, asset, {
690
- // Schema version. In case we ever change our schema.
691
- _version: 1
692
- }, onValueCreate(cloudinaryType)
693
- // onValueCreate is mistyped
694
- ));
695
- onChange(PatchEvent.from([setIfMissing([]), insert(items, "after", [-1])]));
696
- }, [onValueCreate, onChange, cloudinaryType]);
697
- const handleOpenSelector = useCallback(() => secrets && openMediaSelector(secrets.cloudName, secrets.apiKey, true,
698
- // multi-selection
699
- handleSelect), [secrets, handleSelect]);
700
- return /* @__PURE__ */jsxs(Flex, {
701
- gap: 2,
702
- flex: 1,
703
- children: [showSettings && /* @__PURE__ */jsx(SecretsConfigView, {
704
- onClose: hide
705
- }), /* @__PURE__ */jsx(Box, {
706
- flex: 1,
707
- children: /* @__PURE__ */jsx(ArrayOfObjectsFunctions, {
708
- ...props
709
- })
710
- }), cloudinaryType && /* @__PURE__ */jsxs(Fragment, {
711
- children: [/* @__PURE__ */jsx(Box, {
712
- flex: 1,
713
- children: /* @__PURE__ */jsx(Button, {
714
- style: {
715
- width: "100%"
716
- },
717
- disabled: props.readOnly || loading,
718
- mode: "bleed",
719
- text: "Add multiple",
720
- onClick: handleOpenSelector
721
- })
722
- }), /* @__PURE__ */jsx(Box, {
723
- children: /* @__PURE__ */jsx(Button, {
724
- onClick: show,
725
- icon: PlugIcon,
726
- mode: "bleed",
727
- title: "Configure"
728
- })
729
- })]
730
- })]
731
- });
732
- };
733
- const cloudinarySchemaPlugin = definePlugin({
734
- name: "cloudinary-schema",
735
- form: {
736
- components: {
737
- input: props => {
738
- const {
739
- schemaType
740
- } = props;
741
- if (isArrayOfObjectsSchemaType(schemaType)) {
742
- const arrayProps = props;
743
- const cloudinaryType = arrayProps.schemaType.of.find(t => t.name === cloudinaryAssetSchema.name);
744
- if (cloudinaryType) {
745
- return arrayProps.renderDefault({
746
- ...arrayProps,
747
- arrayFunctions: AssetListFunctions
748
- });
749
- }
750
- }
751
- return props.renderDefault(props);
752
- }
753
- }
754
- },
755
- schema: {
756
- types: [cloudinaryAssetSchema, cloudinaryAssetDerivedSchema, cloudinaryAssetContext, cloudinaryAssetContextCustom]
757
- }
758
- });
759
- const cloudinaryImageSource = {
760
- name: "cloudinary-image",
761
- title: "Cloudinary",
762
- icon: CloudinaryIcon,
763
- component: CloudinaryAssetSource
764
- };
765
- const cloudinaryAssetSourcePlugin = definePlugin({
766
- name: "cloudinart-asset-source",
767
- form: {
768
- image: {
769
- assetSources: [cloudinaryImageSource]
770
- }
771
- }
772
- });
773
- export { cloudinaryAssetContext, cloudinaryAssetContextCustom, cloudinaryAssetDerivedSchema, cloudinaryAssetSchema, cloudinaryAssetSourcePlugin, cloudinaryImageSource, cloudinarySchemaPlugin };
774
- //# sourceMappingURL=index.esm.js.map