sanity-plugin-media 4.2.0 → 4.3.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/README.md +52 -0
- package/dist/index.d.mts +97 -0
- package/dist/index.d.ts +97 -0
- package/dist/index.js +215 -87
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +218 -90
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/components/AutoTagInputWrapper/index.tsx +82 -0
- package/src/components/Browser/index.tsx +12 -69
- package/src/components/Browser/useBrowserInit.ts +126 -0
- package/src/components/FormBuilderTool/index.tsx +1 -1
- package/src/contexts/ToolOptionsContext.tsx +3 -0
- package/src/index.ts +4 -1
- package/src/types/index.ts +5 -0
- package/src/utils/applyMediaTags.ts +86 -0
- package/src/utils/mediaField.ts +72 -0
package/README.md
CHANGED
|
@@ -142,6 +142,58 @@ export default defineConfig({
|
|
|
142
142
|
})
|
|
143
143
|
```
|
|
144
144
|
|
|
145
|
+
### Auto-tagging (Optional)
|
|
146
|
+
|
|
147
|
+
You can automatically apply tags to an asset when it is selected via an image or file field. Import `mediaField` to define a field with both auto-tagging and browser pre-filtering wired up in one call:
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import {mediaField} from 'sanity-plugin-media'
|
|
151
|
+
|
|
152
|
+
defineType({
|
|
153
|
+
name: 'product',
|
|
154
|
+
type: 'document',
|
|
155
|
+
fields: [
|
|
156
|
+
mediaField({
|
|
157
|
+
name: 'image',
|
|
158
|
+
type: 'image',
|
|
159
|
+
mediaTags: ['product'],
|
|
160
|
+
}),
|
|
161
|
+
],
|
|
162
|
+
})
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
The `mediaTags` array serves double duty: it pre-filters the media browser to show only assets with those tags, and it applies those tags to the asset when one is selected.
|
|
166
|
+
|
|
167
|
+
For more control, import `AutoTagInput` directly and apply it to individual fields:
|
|
168
|
+
|
|
169
|
+
```ts
|
|
170
|
+
import {AutoTagInput} from 'sanity-plugin-media'
|
|
171
|
+
|
|
172
|
+
defineField({
|
|
173
|
+
name: 'image',
|
|
174
|
+
type: 'image',
|
|
175
|
+
options: {mediaTags: ['product']}, // pre-filters the browser
|
|
176
|
+
components: {input: AutoTagInput}, // applies tags on select
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
// Or pass mediaTags as a prop to override the field options:
|
|
180
|
+
defineField({
|
|
181
|
+
name: 'image',
|
|
182
|
+
type: 'image',
|
|
183
|
+
components: {
|
|
184
|
+
input: (props) => <AutoTagInput {...props} mediaTags={['product']} />,
|
|
185
|
+
},
|
|
186
|
+
})
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
By default, tags that don't already exist will be created when an asset is selected. To disable this, set `createTagsOnUpload: false` in the plugin config:
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
media({
|
|
193
|
+
createTagsOnUpload: false,
|
|
194
|
+
})
|
|
195
|
+
```
|
|
196
|
+
|
|
145
197
|
### Localization (Optional)
|
|
146
198
|
|
|
147
199
|
You can enable localization support by passing a `locales` array to the plugin config, following the [Sanity recommended scheme](https://www.sanity.io/docs/studio/localization#k4da239411955):
|
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import {AssetSourceComponentProps} from 'sanity'
|
|
2
2
|
import type {ComponentType} from 'react'
|
|
3
3
|
import {Control} from 'react-hook-form'
|
|
4
|
+
import type {FieldDefinitionBase} from 'sanity'
|
|
4
5
|
import {FieldErrors} from 'react-hook-form'
|
|
6
|
+
import type {FileDefinition} from 'sanity'
|
|
5
7
|
import {ForwardRefExoticComponent} from 'react'
|
|
8
|
+
import type {ImageDefinition} from 'sanity'
|
|
9
|
+
import {InputProps} from 'sanity'
|
|
6
10
|
import {JSX as JSX_2} from 'react'
|
|
7
11
|
import {Plugin as Plugin_2} from 'sanity'
|
|
8
12
|
import {RefAttributes} from 'react'
|
|
@@ -10,12 +14,39 @@ import type {SanityAssetDocument} from '@sanity/client'
|
|
|
10
14
|
import type {SanityImageAssetDocument} from '@sanity/client'
|
|
11
15
|
import {SVGProps} from 'react'
|
|
12
16
|
import {UseFormRegister} from 'react-hook-form'
|
|
17
|
+
import type {WidenInitialValue} from 'sanity'
|
|
18
|
+
import type {WidenValidation} from 'sanity'
|
|
13
19
|
import * as z from 'zod'
|
|
14
20
|
|
|
15
21
|
declare type Asset = FileAsset | ImageAsset
|
|
16
22
|
|
|
17
23
|
declare type AssetFormData = z.infer<ReturnType<typeof getAssetFormSchema>>
|
|
18
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Input component that automatically applies media tags when an asset is selected or uploaded.
|
|
27
|
+
*
|
|
28
|
+
* Apply explicitly to image/file fields that should be auto-tagged:
|
|
29
|
+
* ```ts
|
|
30
|
+
* import {AutoTagInput} from 'sanity-plugin-media'
|
|
31
|
+
*
|
|
32
|
+
* defineField({
|
|
33
|
+
* type: 'image',
|
|
34
|
+
* options: { mediaTags: ['product'] }, // also pre-filters the media browser
|
|
35
|
+
* components: { input: AutoTagInput },
|
|
36
|
+
* })
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Pass `mediaTags` as a prop to override or use without `options`:
|
|
40
|
+
* ```ts
|
|
41
|
+
* components: { input: (props) => <AutoTagInput {...props} mediaTags={['product']} /> }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function AutoTagInput(props: AutoTagInputProps): JSX_2
|
|
45
|
+
|
|
46
|
+
export declare type AutoTagInputProps = InputProps & {
|
|
47
|
+
mediaTags?: string[]
|
|
48
|
+
}
|
|
49
|
+
|
|
19
50
|
declare type CustomFields = {
|
|
20
51
|
altText?: LocalizedString
|
|
21
52
|
description?: LocalizedString
|
|
@@ -48,6 +79,24 @@ declare type FileAsset = SanityAssetDocument &
|
|
|
48
79
|
_type: 'sanity.fileAsset'
|
|
49
80
|
}
|
|
50
81
|
|
|
82
|
+
declare type FileMediaFieldConfig = Omit<FileDefinition, 'options'> &
|
|
83
|
+
FieldDefinitionBase & {
|
|
84
|
+
name: string
|
|
85
|
+
mediaTags: string[]
|
|
86
|
+
options?: FileDefinition['options']
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
declare type FileMediaFieldResult = Omit<FileDefinition, 'options'> &
|
|
90
|
+
FieldDefinitionBase & {
|
|
91
|
+
options?: FileDefinition['options'] & {
|
|
92
|
+
mediaTags: string[]
|
|
93
|
+
}
|
|
94
|
+
components: {
|
|
95
|
+
input: typeof AutoTagInput
|
|
96
|
+
}
|
|
97
|
+
} & WidenValidation &
|
|
98
|
+
WidenInitialValue
|
|
99
|
+
|
|
51
100
|
declare function getAssetFormSchema(
|
|
52
101
|
locales?: {
|
|
53
102
|
id: string
|
|
@@ -229,6 +278,24 @@ declare type ImageAsset = SanityImageAssetDocument &
|
|
|
229
278
|
creditLine?: LocalizedString
|
|
230
279
|
}
|
|
231
280
|
|
|
281
|
+
declare type ImageMediaFieldConfig = Omit<ImageDefinition, 'options'> &
|
|
282
|
+
FieldDefinitionBase & {
|
|
283
|
+
name: string
|
|
284
|
+
mediaTags: string[]
|
|
285
|
+
options?: ImageDefinition['options']
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
declare type ImageMediaFieldResult = Omit<ImageDefinition, 'options'> &
|
|
289
|
+
FieldDefinitionBase & {
|
|
290
|
+
options?: ImageDefinition['options'] & {
|
|
291
|
+
mediaTags: string[]
|
|
292
|
+
}
|
|
293
|
+
components: {
|
|
294
|
+
input: typeof AutoTagInput
|
|
295
|
+
}
|
|
296
|
+
} & WidenValidation &
|
|
297
|
+
WidenInitialValue
|
|
298
|
+
|
|
232
299
|
declare type Locale_2 = {
|
|
233
300
|
title: string
|
|
234
301
|
id: string
|
|
@@ -248,8 +315,38 @@ export declare const mediaAssetSource: {
|
|
|
248
315
|
title: string
|
|
249
316
|
}
|
|
250
317
|
|
|
318
|
+
/**
|
|
319
|
+
* Defines an image or file field with automatic media tag application when an asset is selected.
|
|
320
|
+
*
|
|
321
|
+
* Pass `mediaTags` at the top level — they are moved into `options.mediaTags` (for media browser
|
|
322
|
+
* pre-filtering) and wire up {@link AutoTagInput} as the field component automatically:
|
|
323
|
+
* ```ts
|
|
324
|
+
* import {mediaField} from 'sanity-plugin-media'
|
|
325
|
+
*
|
|
326
|
+
* mediaField({
|
|
327
|
+
* name: 'coverImage',
|
|
328
|
+
* type: 'image',
|
|
329
|
+
* mediaTags: ['product-cover'],
|
|
330
|
+
* options: { hotspot: true },
|
|
331
|
+
* })
|
|
332
|
+
* ```
|
|
333
|
+
*
|
|
334
|
+
* For file fields, set `type: 'file'`:
|
|
335
|
+
* ```ts
|
|
336
|
+
* mediaField({ name: 'drawing', type: 'file', mediaTags: ['model-drawing'] })
|
|
337
|
+
* ```
|
|
338
|
+
*/
|
|
339
|
+
export declare function mediaField(config: ImageMediaFieldConfig): ImageMediaFieldResult
|
|
340
|
+
|
|
341
|
+
export declare function mediaField(config: FileMediaFieldConfig): FileMediaFieldResult
|
|
342
|
+
|
|
343
|
+
export declare type MediaTagsOptions = {
|
|
344
|
+
mediaTags?: string[]
|
|
345
|
+
}
|
|
346
|
+
|
|
251
347
|
export declare type MediaToolOptions = {
|
|
252
348
|
maximumUploadSize?: number
|
|
349
|
+
createTagsOnUpload?: boolean
|
|
253
350
|
components?: {
|
|
254
351
|
details?: ComponentType<
|
|
255
352
|
DetailsProps & {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import {AssetSourceComponentProps} from 'sanity'
|
|
2
2
|
import type {ComponentType} from 'react'
|
|
3
3
|
import {Control} from 'react-hook-form'
|
|
4
|
+
import type {FieldDefinitionBase} from 'sanity'
|
|
4
5
|
import {FieldErrors} from 'react-hook-form'
|
|
6
|
+
import type {FileDefinition} from 'sanity'
|
|
5
7
|
import {ForwardRefExoticComponent} from 'react'
|
|
8
|
+
import type {ImageDefinition} from 'sanity'
|
|
9
|
+
import {InputProps} from 'sanity'
|
|
6
10
|
import {JSX as JSX_2} from 'react'
|
|
7
11
|
import {Plugin as Plugin_2} from 'sanity'
|
|
8
12
|
import {RefAttributes} from 'react'
|
|
@@ -10,12 +14,39 @@ import type {SanityAssetDocument} from '@sanity/client'
|
|
|
10
14
|
import type {SanityImageAssetDocument} from '@sanity/client'
|
|
11
15
|
import {SVGProps} from 'react'
|
|
12
16
|
import {UseFormRegister} from 'react-hook-form'
|
|
17
|
+
import type {WidenInitialValue} from 'sanity'
|
|
18
|
+
import type {WidenValidation} from 'sanity'
|
|
13
19
|
import * as z from 'zod'
|
|
14
20
|
|
|
15
21
|
declare type Asset = FileAsset | ImageAsset
|
|
16
22
|
|
|
17
23
|
declare type AssetFormData = z.infer<ReturnType<typeof getAssetFormSchema>>
|
|
18
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Input component that automatically applies media tags when an asset is selected or uploaded.
|
|
27
|
+
*
|
|
28
|
+
* Apply explicitly to image/file fields that should be auto-tagged:
|
|
29
|
+
* ```ts
|
|
30
|
+
* import {AutoTagInput} from 'sanity-plugin-media'
|
|
31
|
+
*
|
|
32
|
+
* defineField({
|
|
33
|
+
* type: 'image',
|
|
34
|
+
* options: { mediaTags: ['product'] }, // also pre-filters the media browser
|
|
35
|
+
* components: { input: AutoTagInput },
|
|
36
|
+
* })
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Pass `mediaTags` as a prop to override or use without `options`:
|
|
40
|
+
* ```ts
|
|
41
|
+
* components: { input: (props) => <AutoTagInput {...props} mediaTags={['product']} /> }
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function AutoTagInput(props: AutoTagInputProps): JSX_2
|
|
45
|
+
|
|
46
|
+
export declare type AutoTagInputProps = InputProps & {
|
|
47
|
+
mediaTags?: string[]
|
|
48
|
+
}
|
|
49
|
+
|
|
19
50
|
declare type CustomFields = {
|
|
20
51
|
altText?: LocalizedString
|
|
21
52
|
description?: LocalizedString
|
|
@@ -48,6 +79,24 @@ declare type FileAsset = SanityAssetDocument &
|
|
|
48
79
|
_type: 'sanity.fileAsset'
|
|
49
80
|
}
|
|
50
81
|
|
|
82
|
+
declare type FileMediaFieldConfig = Omit<FileDefinition, 'options'> &
|
|
83
|
+
FieldDefinitionBase & {
|
|
84
|
+
name: string
|
|
85
|
+
mediaTags: string[]
|
|
86
|
+
options?: FileDefinition['options']
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
declare type FileMediaFieldResult = Omit<FileDefinition, 'options'> &
|
|
90
|
+
FieldDefinitionBase & {
|
|
91
|
+
options?: FileDefinition['options'] & {
|
|
92
|
+
mediaTags: string[]
|
|
93
|
+
}
|
|
94
|
+
components: {
|
|
95
|
+
input: typeof AutoTagInput
|
|
96
|
+
}
|
|
97
|
+
} & WidenValidation &
|
|
98
|
+
WidenInitialValue
|
|
99
|
+
|
|
51
100
|
declare function getAssetFormSchema(
|
|
52
101
|
locales?: {
|
|
53
102
|
id: string
|
|
@@ -229,6 +278,24 @@ declare type ImageAsset = SanityImageAssetDocument &
|
|
|
229
278
|
creditLine?: LocalizedString
|
|
230
279
|
}
|
|
231
280
|
|
|
281
|
+
declare type ImageMediaFieldConfig = Omit<ImageDefinition, 'options'> &
|
|
282
|
+
FieldDefinitionBase & {
|
|
283
|
+
name: string
|
|
284
|
+
mediaTags: string[]
|
|
285
|
+
options?: ImageDefinition['options']
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
declare type ImageMediaFieldResult = Omit<ImageDefinition, 'options'> &
|
|
289
|
+
FieldDefinitionBase & {
|
|
290
|
+
options?: ImageDefinition['options'] & {
|
|
291
|
+
mediaTags: string[]
|
|
292
|
+
}
|
|
293
|
+
components: {
|
|
294
|
+
input: typeof AutoTagInput
|
|
295
|
+
}
|
|
296
|
+
} & WidenValidation &
|
|
297
|
+
WidenInitialValue
|
|
298
|
+
|
|
232
299
|
declare type Locale_2 = {
|
|
233
300
|
title: string
|
|
234
301
|
id: string
|
|
@@ -248,8 +315,38 @@ export declare const mediaAssetSource: {
|
|
|
248
315
|
title: string
|
|
249
316
|
}
|
|
250
317
|
|
|
318
|
+
/**
|
|
319
|
+
* Defines an image or file field with automatic media tag application when an asset is selected.
|
|
320
|
+
*
|
|
321
|
+
* Pass `mediaTags` at the top level — they are moved into `options.mediaTags` (for media browser
|
|
322
|
+
* pre-filtering) and wire up {@link AutoTagInput} as the field component automatically:
|
|
323
|
+
* ```ts
|
|
324
|
+
* import {mediaField} from 'sanity-plugin-media'
|
|
325
|
+
*
|
|
326
|
+
* mediaField({
|
|
327
|
+
* name: 'coverImage',
|
|
328
|
+
* type: 'image',
|
|
329
|
+
* mediaTags: ['product-cover'],
|
|
330
|
+
* options: { hotspot: true },
|
|
331
|
+
* })
|
|
332
|
+
* ```
|
|
333
|
+
*
|
|
334
|
+
* For file fields, set `type: 'file'`:
|
|
335
|
+
* ```ts
|
|
336
|
+
* mediaField({ name: 'drawing', type: 'file', mediaTags: ['model-drawing'] })
|
|
337
|
+
* ```
|
|
338
|
+
*/
|
|
339
|
+
export declare function mediaField(config: ImageMediaFieldConfig): ImageMediaFieldResult
|
|
340
|
+
|
|
341
|
+
export declare function mediaField(config: FileMediaFieldConfig): FileMediaFieldResult
|
|
342
|
+
|
|
343
|
+
export declare type MediaTagsOptions = {
|
|
344
|
+
mediaTags?: string[]
|
|
345
|
+
}
|
|
346
|
+
|
|
251
347
|
export declare type MediaToolOptions = {
|
|
252
348
|
maximumUploadSize?: number
|
|
349
|
+
createTagsOnUpload?: boolean
|
|
253
350
|
components?: {
|
|
254
351
|
details?: ComponentType<
|
|
255
352
|
DetailsProps & {
|