sanity-plugin-cloudinary 1.0.2 → 1.1.1

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/src/index.ts CHANGED
@@ -1,15 +1,56 @@
1
1
  import {cloudinaryAssetSchema} from './schema/cloudinaryAsset'
2
2
  import {cloudinaryAssetDerivedSchema} from './schema/cloudinaryAssetDerived'
3
- import {definePlugin, AssetSource} from 'sanity'
3
+ import {
4
+ definePlugin,
5
+ AssetSource,
6
+ ArrayOfObjectsInputProps,
7
+ isArrayOfObjectsSchemaType,
8
+ } from 'sanity'
4
9
  import {CloudinaryIcon} from './components/asset-source/Icon'
5
10
  import {CloudinaryAssetSource} from './components/asset-source/CloudinaryAssetSource'
11
+ import {cloudinaryAssetContext} from './schema/cloudinaryAssetContext'
12
+ import {cloudinaryAssetContextCustom} from './schema/cloudinaryAssetContextCustom'
13
+ import {AssetListFunctions} from './components/AssetListFunctions'
6
14
 
7
- export {cloudinaryAssetSchema, cloudinaryAssetDerivedSchema}
15
+ export {type CloudinaryAssetContext} from './schema/cloudinaryAssetContext'
16
+ export {type CloudinaryAssetDerived} from './schema/cloudinaryAssetDerived'
17
+ export {type CloudinaryAssetContextCustom} from './schema/cloudinaryAssetContextCustom'
18
+
19
+ export type {AssetDocument, CloudinaryAsset} from './types'
20
+
21
+ export {
22
+ cloudinaryAssetSchema,
23
+ cloudinaryAssetDerivedSchema,
24
+ cloudinaryAssetContext,
25
+ cloudinaryAssetContextCustom,
26
+ }
8
27
 
9
28
  export const cloudinarySchemaPlugin = definePlugin({
10
29
  name: 'cloudinary-schema',
30
+ form: {
31
+ components: {
32
+ input: (props) => {
33
+ const {schemaType} = props
34
+ if (isArrayOfObjectsSchemaType(schemaType)) {
35
+ const arrayProps = props as ArrayOfObjectsInputProps
36
+ const cloudinaryType = arrayProps.schemaType.of.find(
37
+ (t: {name: string}) => t.name === cloudinaryAssetSchema.name
38
+ )
39
+ if (cloudinaryType) {
40
+ return arrayProps.renderDefault({...arrayProps, arrayFunctions: AssetListFunctions})
41
+ }
42
+ }
43
+ return props.renderDefault(props)
44
+ },
45
+ },
46
+ },
11
47
  schema: {
12
- types: [cloudinaryAssetSchema, cloudinaryAssetDerivedSchema],
48
+ types: [
49
+ cloudinaryAssetSchema,
50
+ cloudinaryAssetDerivedSchema,
51
+ cloudinaryAssetContext,
52
+ cloudinaryAssetContextCustom,
53
+ ],
13
54
  },
14
55
  })
15
56
 
@@ -74,7 +74,10 @@ export const cloudinaryAssetSchema = defineType({
74
74
  type: 'string',
75
75
  name: 'access_mode',
76
76
  },
77
- // context array of unknown content
77
+ {
78
+ type: 'cloudinary.assetContext',
79
+ name: 'context',
80
+ },
78
81
  // metadata array of unknown content
79
82
  ],
80
83
  ...({
@@ -0,0 +1,16 @@
1
+ import {defineType} from 'sanity'
2
+
3
+ export interface CloudinaryAssetContext {
4
+ custom: object
5
+ }
6
+
7
+ export const cloudinaryAssetContext = defineType({
8
+ type: 'object',
9
+ name: 'cloudinary.assetContext',
10
+ fields: [
11
+ {
12
+ type: 'cloudinary.assetContextCustom',
13
+ name: 'custom',
14
+ },
15
+ ],
16
+ })
@@ -0,0 +1,21 @@
1
+ import {defineType} from 'sanity'
2
+
3
+ export type CloudinaryAssetContextCustom = {
4
+ alt: string
5
+ caption: string
6
+ }
7
+
8
+ export const cloudinaryAssetContextCustom = defineType({
9
+ type: 'object',
10
+ name: 'cloudinary.assetContextCustom',
11
+ fields: [
12
+ {
13
+ type: 'string',
14
+ name: 'alt',
15
+ },
16
+ {
17
+ type: 'string',
18
+ name: 'caption',
19
+ },
20
+ ],
21
+ })
@@ -1,11 +1,5 @@
1
- import {CSSProperties} from 'react'
2
1
  import {CloudinaryAssetDerived} from './schema/cloudinaryAssetDerived'
3
2
 
4
- declare module '*.css' {
5
- const content: {[className: string]: CSSProperties}
6
- export default content
7
- }
8
-
9
3
  export type CloudinaryDerivative = {
10
4
  url: string
11
5
  secure_url: string
package/src/utils.ts CHANGED
@@ -1,10 +1,10 @@
1
- /* eslint-disable camelcase */
1
+ /* eslint-disable camelcase,@typescript-eslint/explicit-module-boundary-types */
2
2
  import {
3
3
  CloudinaryAsset,
4
4
  CloudinaryAssetResponse,
5
5
  CloudinaryMediaLibrary,
6
6
  InsertHandlerParams,
7
- } from './typings'
7
+ } from './types'
8
8
 
9
9
  const widgetSrc = 'https://media-library.cloudinary.com/global/all.js'
10
10
 
@@ -1,71 +0,0 @@
1
- import React from 'react'
2
- import {Button} from '@sanity/ui'
3
- import {PatchEvent, setIfMissing, insert} from 'sanity'
4
-
5
- import {useSecrets} from '@sanity/studio-secrets'
6
- import SecretsConfigView, {namespace} from './components/SecretsConfigView'
7
- import {cloudinaryAssetSchema} from './schema/cloudinaryAsset'
8
- import {openMediaSelector} from './utils'
9
- import {InsertHandlerParams} from './typings'
10
-
11
- interface ApiConfig {
12
- cloudName: string
13
- apiKey: string
14
- }
15
-
16
- const AssetListFunctions = (props: any) => {
17
- const {secrets, loading} = useSecrets<ApiConfig>(namespace)
18
- const [showSettings, setShowSettings] = React.useState(false)
19
-
20
- const cloudinaryType = props.type.of.find(
21
- (t: {name: string}) => t.name === cloudinaryAssetSchema.name
22
- )
23
-
24
- const handleSelect = (selected: InsertHandlerParams) => {
25
- const {onCreateValue, onChange} = props
26
- const items = selected.assets.map((asset) =>
27
- Object.assign(
28
- {},
29
- asset,
30
- {
31
- // Schema version. In case we ever change our schema.
32
- _version: 1,
33
- },
34
- onCreateValue(cloudinaryType)
35
- )
36
- )
37
- onChange(PatchEvent.from([setIfMissing([]), insert(items, 'after', [-1])]))
38
- }
39
-
40
- const actions = (
41
- <>
42
- <Button
43
- disabled={props.readOnly || loading}
44
- mode="ghost"
45
- onClick={() =>
46
- secrets &&
47
- openMediaSelector(
48
- secrets.cloudName,
49
- secrets.apiKey,
50
- true, // multi-selection
51
- handleSelect
52
- )
53
- }
54
- >
55
- Add multiple
56
- </Button>
57
- <Button onClick={() => setShowSettings(true)}>Configure</Button>
58
- </>
59
- )
60
-
61
- return (
62
- <>
63
- {showSettings && <SecretsConfigView onClose={() => setShowSettings(false)} />}
64
- {/* <DefaultArrayFunctions {...props}>*/}
65
- {cloudinaryType && actions}
66
- {/* </DefaultArrayFunctions>*/}
67
- </>
68
- )
69
- }
70
-
71
- export default AssetListFunctions