sanity-plugin-media 2.3.0 → 2.3.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/README.md +23 -0
- package/dist/index.d.ts +16 -3
- package/dist/index.esm.js +142 -102
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +141 -101
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/DialogAssetEdit/index.tsx +19 -0
- package/src/components/SearchFacetsControl/index.tsx +8 -0
- package/src/components/Tool/index.tsx +5 -10
- package/src/config/searchFacets.ts +10 -0
- package/src/constants.ts +1 -0
- package/src/contexts/ToolOptionsContext.tsx +23 -7
- package/src/formSchema/index.ts +1 -0
- package/src/index.ts +2 -47
- package/src/modules/assets/index.ts +4 -0
- package/src/plugin.tsx +53 -0
- package/src/types/index.ts +6 -0
- package/src/utils/constructFilter.ts +1 -1
package/package.json
CHANGED
|
@@ -27,6 +27,7 @@ import FormFieldInputText from '../FormFieldInputText'
|
|
|
27
27
|
import FormFieldInputTextarea from '../FormFieldInputTextarea'
|
|
28
28
|
import FormSubmitButton from '../FormSubmitButton'
|
|
29
29
|
import Image from '../Image'
|
|
30
|
+
import {useToolOptions} from '../../contexts/ToolOptionsContext'
|
|
30
31
|
|
|
31
32
|
type Props = {
|
|
32
33
|
children: ReactNode
|
|
@@ -59,10 +60,14 @@ const DialogAssetEdit = (props: Props) => {
|
|
|
59
60
|
|
|
60
61
|
const assetTagOptions = useTypedSelector(selectTagSelectOptions(currentAsset))
|
|
61
62
|
|
|
63
|
+
// Check if credit line options are configured
|
|
64
|
+
const {creditLine} = useToolOptions()
|
|
65
|
+
|
|
62
66
|
const generateDefaultValues = useCallback(
|
|
63
67
|
(asset?: Asset): AssetFormData => {
|
|
64
68
|
return {
|
|
65
69
|
altText: asset?.altText || '',
|
|
70
|
+
creditLine: asset?.creditLine || '',
|
|
66
71
|
description: asset?.description || '',
|
|
67
72
|
originalFilename: asset?.originalFilename || '',
|
|
68
73
|
opt: {media: {tags: assetTagOptions}},
|
|
@@ -342,6 +347,20 @@ const DialogAssetEdit = (props: Props) => {
|
|
|
342
347
|
rows={5}
|
|
343
348
|
value={currentAsset?.description}
|
|
344
349
|
/>
|
|
350
|
+
{/* CreditLine */}
|
|
351
|
+
{creditLine?.enabled && (
|
|
352
|
+
<FormFieldInputText
|
|
353
|
+
{...register('creditLine')}
|
|
354
|
+
error={errors?.creditLine?.message}
|
|
355
|
+
label="Credit"
|
|
356
|
+
name="creditLine"
|
|
357
|
+
value={currentAsset?.creditLine}
|
|
358
|
+
disabled={
|
|
359
|
+
formUpdating ||
|
|
360
|
+
creditLine?.excludeSources?.includes(currentAsset?.source?.name)
|
|
361
|
+
}
|
|
362
|
+
/>
|
|
363
|
+
)}
|
|
345
364
|
</Stack>
|
|
346
365
|
</TabPanel>
|
|
347
366
|
|
|
@@ -7,6 +7,7 @@ import {FACETS} from '../../constants'
|
|
|
7
7
|
import {usePortalPopoverProps} from '../../hooks/usePortalPopoverProps'
|
|
8
8
|
import useTypedSelector from '../../hooks/useTypedSelector'
|
|
9
9
|
import {searchActions} from '../../modules/search'
|
|
10
|
+
import {useToolOptions} from '../../contexts/ToolOptionsContext'
|
|
10
11
|
|
|
11
12
|
const SearchFacetsControl = () => {
|
|
12
13
|
// Redux
|
|
@@ -17,11 +18,18 @@ const SearchFacetsControl = () => {
|
|
|
17
18
|
|
|
18
19
|
const popoverProps = usePortalPopoverProps()
|
|
19
20
|
|
|
21
|
+
const {creditLine} = useToolOptions()
|
|
22
|
+
|
|
20
23
|
const isTool = !selectedDocument
|
|
21
24
|
|
|
22
25
|
const filteredFacets = FACETS
|
|
23
26
|
// Filter facets based on current context, whether it's invoked as a tool, or via selection through via custom asset source.
|
|
24
27
|
.filter(facet => {
|
|
28
|
+
// Remove credit line filter if it's not enabled
|
|
29
|
+
if (!creditLine?.enabled && facet?.type === 'string' && facet?.name === 'creditLine') {
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
|
|
25
33
|
if (facet.type === 'group' || facet.type === 'divider') {
|
|
26
34
|
return true
|
|
27
35
|
}
|
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import {Flex} from '@sanity/ui'
|
|
2
|
-
import React
|
|
2
|
+
import React from 'react'
|
|
3
3
|
import Browser from '../Browser'
|
|
4
|
-
import {ToolOptionsProvider} from '../../contexts/ToolOptionsContext'
|
|
5
|
-
import {Tool as SanityTool} from 'sanity'
|
|
6
|
-
import {MediaToolOptions} from '@types'
|
|
7
4
|
|
|
8
|
-
const Tool = (
|
|
5
|
+
const Tool = () => {
|
|
9
6
|
return (
|
|
10
|
-
<
|
|
11
|
-
<
|
|
12
|
-
|
|
13
|
-
</Flex>
|
|
14
|
-
</ToolOptionsProvider>
|
|
7
|
+
<Flex direction="column" height="fill" flex={1}>
|
|
8
|
+
<Browser />
|
|
9
|
+
</Flex>
|
|
15
10
|
)
|
|
16
11
|
}
|
|
17
12
|
|
|
@@ -19,6 +19,16 @@ export const inputs: Record<SearchFacetName, SearchFacetInputProps> = {
|
|
|
19
19
|
type: 'string',
|
|
20
20
|
value: ''
|
|
21
21
|
},
|
|
22
|
+
creditLine: {
|
|
23
|
+
assetTypes: ['file', 'image'],
|
|
24
|
+
field: 'creditLine',
|
|
25
|
+
name: 'creditLine',
|
|
26
|
+
operatorType: 'empty',
|
|
27
|
+
operatorTypes: ['empty', 'notEmpty', null, 'includes', 'doesNotInclude'],
|
|
28
|
+
title: 'Credit',
|
|
29
|
+
type: 'string',
|
|
30
|
+
value: ''
|
|
31
|
+
},
|
|
22
32
|
description: {
|
|
23
33
|
assetTypes: ['file', 'image'],
|
|
24
34
|
field: 'description',
|
package/src/constants.ts
CHANGED
|
@@ -4,19 +4,37 @@ import {DropzoneOptions} from 'react-dropzone'
|
|
|
4
4
|
|
|
5
5
|
type ContextProps = {
|
|
6
6
|
dropzone: Pick<DropzoneOptions, 'maxSize'>
|
|
7
|
+
creditLine: MediaToolOptions['creditLine']
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
const ToolOptionsContext = createContext<ContextProps | null>(null)
|
|
10
11
|
|
|
11
12
|
type Props = {
|
|
12
|
-
options?: MediaToolOptions
|
|
13
|
+
options?: MediaToolOptions | void
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
export const ToolOptionsProvider = ({options, children}: PropsWithChildren<Props>) => {
|
|
16
|
-
const value = useMemo<ContextProps>(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
const value = useMemo<ContextProps>(() => {
|
|
18
|
+
let creditLineExcludeSources
|
|
19
|
+
|
|
20
|
+
if (options?.creditLine?.excludeSources) {
|
|
21
|
+
creditLineExcludeSources = Array.isArray(options?.creditLine?.excludeSources)
|
|
22
|
+
? options.creditLine.excludeSources
|
|
23
|
+
: [options?.creditLine?.excludeSources]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
dropzone: {maxSize: options?.maximumUploadSize},
|
|
28
|
+
creditLine: {
|
|
29
|
+
enabled: options?.creditLine?.enabled || false,
|
|
30
|
+
excludeSources: creditLineExcludeSources
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}, [
|
|
34
|
+
options?.creditLine?.enabled,
|
|
35
|
+
options?.creditLine?.excludeSources,
|
|
36
|
+
options?.maximumUploadSize
|
|
37
|
+
])
|
|
20
38
|
|
|
21
39
|
return <ToolOptionsContext.Provider value={value}>{children}</ToolOptionsContext.Provider>
|
|
22
40
|
}
|
|
@@ -30,5 +48,3 @@ export const useToolOptions = () => {
|
|
|
30
48
|
|
|
31
49
|
return context
|
|
32
50
|
}
|
|
33
|
-
|
|
34
|
-
export default ToolOptionsContext
|
package/src/formSchema/index.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,47 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import type {AssetSource} from 'sanity'
|
|
4
|
-
import FormBuilderTool from './components/FormBuilderTool'
|
|
5
|
-
import Tool from './components/Tool'
|
|
6
|
-
import mediaTag from './schemas/tag'
|
|
7
|
-
import {MediaToolOptions} from '@types'
|
|
8
|
-
|
|
9
|
-
const plugin = {
|
|
10
|
-
icon: ImageIcon,
|
|
11
|
-
name: 'media',
|
|
12
|
-
title: 'Media'
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const mediaAssetSource: AssetSource = {
|
|
16
|
-
...plugin,
|
|
17
|
-
component: FormBuilderTool
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export const media = definePlugin<MediaToolOptions | void>(options => ({
|
|
21
|
-
name: 'media',
|
|
22
|
-
form: {
|
|
23
|
-
file: {
|
|
24
|
-
assetSources: prev => {
|
|
25
|
-
return [...prev, mediaAssetSource]
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
image: {
|
|
29
|
-
assetSources: prev => {
|
|
30
|
-
return [...prev, mediaAssetSource]
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
|
-
schema: {
|
|
35
|
-
types: [mediaTag]
|
|
36
|
-
},
|
|
37
|
-
tools: prev => {
|
|
38
|
-
return [
|
|
39
|
-
...prev,
|
|
40
|
-
{
|
|
41
|
-
...plugin,
|
|
42
|
-
options,
|
|
43
|
-
component: Tool
|
|
44
|
-
}
|
|
45
|
-
]
|
|
46
|
-
}
|
|
47
|
-
}))
|
|
1
|
+
export {media, mediaAssetSource} from './plugin'
|
|
2
|
+
export type {MediaToolOptions} from '@types'
|
|
@@ -239,6 +239,7 @@ const assetsSlice = createSlice({
|
|
|
239
239
|
_createdAt,
|
|
240
240
|
_updatedAt,
|
|
241
241
|
altText,
|
|
242
|
+
creditLine,
|
|
242
243
|
description,
|
|
243
244
|
extension,
|
|
244
245
|
metadata {
|
|
@@ -252,6 +253,9 @@ const assetsSlice = createSlice({
|
|
|
252
253
|
},
|
|
253
254
|
originalFilename,
|
|
254
255
|
size,
|
|
256
|
+
source {
|
|
257
|
+
name
|
|
258
|
+
},
|
|
255
259
|
title,
|
|
256
260
|
url
|
|
257
261
|
} ${pipe} ${sort} ${selector},
|
package/src/plugin.tsx
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import {AssetSource, Tool as SanityTool, definePlugin} from 'sanity'
|
|
3
|
+
import {ImageIcon} from '@sanity/icons'
|
|
4
|
+
import FormBuilderTool from './components/FormBuilderTool'
|
|
5
|
+
import Tool from './components/Tool'
|
|
6
|
+
import mediaTag from './schemas/tag'
|
|
7
|
+
import {MediaToolOptions} from '@types'
|
|
8
|
+
import {ToolOptionsProvider} from './contexts/ToolOptionsContext'
|
|
9
|
+
|
|
10
|
+
const plugin = {
|
|
11
|
+
icon: ImageIcon,
|
|
12
|
+
name: 'media',
|
|
13
|
+
title: 'Media'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const mediaAssetSource = {
|
|
17
|
+
...plugin,
|
|
18
|
+
component: FormBuilderTool
|
|
19
|
+
} satisfies AssetSource
|
|
20
|
+
|
|
21
|
+
const tool = {
|
|
22
|
+
...plugin,
|
|
23
|
+
component: Tool
|
|
24
|
+
} satisfies SanityTool
|
|
25
|
+
|
|
26
|
+
export const media = definePlugin<MediaToolOptions | void>(options => ({
|
|
27
|
+
name: 'media',
|
|
28
|
+
studio: {
|
|
29
|
+
components: {
|
|
30
|
+
layout: props => (
|
|
31
|
+
<ToolOptionsProvider options={options}>{props.renderDefault(props)}</ToolOptionsProvider>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
form: {
|
|
36
|
+
file: {
|
|
37
|
+
assetSources: prev => {
|
|
38
|
+
return [...prev, mediaAssetSource]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
image: {
|
|
42
|
+
assetSources: prev => {
|
|
43
|
+
return [...prev, mediaAssetSource]
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
schema: {
|
|
48
|
+
types: [mediaTag]
|
|
49
|
+
},
|
|
50
|
+
tools: prev => {
|
|
51
|
+
return [...prev, tool]
|
|
52
|
+
}
|
|
53
|
+
}))
|
package/src/types/index.ts
CHANGED
|
@@ -12,6 +12,10 @@ import {RootReducerState} from '../modules/types'
|
|
|
12
12
|
|
|
13
13
|
export type MediaToolOptions = {
|
|
14
14
|
maximumUploadSize?: number
|
|
15
|
+
creditLine: {
|
|
16
|
+
enabled: boolean
|
|
17
|
+
excludeSources?: string | string[]
|
|
18
|
+
}
|
|
15
19
|
}
|
|
16
20
|
|
|
17
21
|
type CustomFields = {
|
|
@@ -152,6 +156,7 @@ export type FileAsset = SanityAssetDocument &
|
|
|
152
156
|
export type ImageAsset = SanityImageAssetDocument &
|
|
153
157
|
CustomFields & {
|
|
154
158
|
_type: 'sanity.imageAsset'
|
|
159
|
+
creditLine?: string
|
|
155
160
|
}
|
|
156
161
|
|
|
157
162
|
export type MarkDef = {_key: string; _type: string}
|
|
@@ -238,6 +243,7 @@ export type SearchFacetInputStringProps = SearchFacetInputCommon & {
|
|
|
238
243
|
|
|
239
244
|
export type SearchFacetName =
|
|
240
245
|
| 'altText'
|
|
246
|
+
| 'creditLine'
|
|
241
247
|
| 'description'
|
|
242
248
|
| 'fileName'
|
|
243
249
|
| 'height'
|
|
@@ -85,7 +85,7 @@ const constructFilter = ({
|
|
|
85
85
|
// references(*[_type == "media.tag" && name.current == "${searchQuery.trim()}"]._id)
|
|
86
86
|
...(searchQuery
|
|
87
87
|
? [
|
|
88
|
-
groq`[_id, altText, assetId, description, originalFilename, title, url] match '*${searchQuery.trim()}*'`
|
|
88
|
+
groq`[_id, altText, assetId, creditLine, description, originalFilename, title, url] match '*${searchQuery.trim()}*'`
|
|
89
89
|
]
|
|
90
90
|
: []),
|
|
91
91
|
// Search facets
|