Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ToolOptionsProvider in the Studio layout component #223

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions src/components/Tool/index.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import {Flex} from '@sanity/ui'
import React, {ComponentProps} from 'react'
import React from 'react'
import Browser from '../Browser'
import {ToolOptionsProvider} from '../../contexts/ToolOptionsContext'
import {Tool as SanityTool} from 'sanity'
import {MediaToolOptions} from '@types'

const Tool = ({tool: {options}}: ComponentProps<SanityTool<MediaToolOptions>['component']>) => {
const Tool = () => {
return (
<ToolOptionsProvider options={options}>
<Flex direction="column" height="fill" flex={1}>
<Browser />
</Flex>
</ToolOptionsProvider>
<Flex direction="column" height="fill" flex={1}>
<Browser />
</Flex>
)
}

Expand Down
4 changes: 1 addition & 3 deletions src/contexts/ToolOptionsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type ContextProps = {
const ToolOptionsContext = createContext<ContextProps | null>(null)

type Props = {
options?: MediaToolOptions
options?: MediaToolOptions | void
}

export const ToolOptionsProvider = ({options, children}: PropsWithChildren<Props>) => {
Expand All @@ -30,5 +30,3 @@ export const useToolOptions = () => {

return context
}

export default ToolOptionsContext
49 changes: 2 additions & 47 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,2 @@
import {definePlugin} from 'sanity'
import {ImageIcon} from '@sanity/icons'
import type {AssetSource} from 'sanity'
import FormBuilderTool from './components/FormBuilderTool'
import Tool from './components/Tool'
import mediaTag from './schemas/tag'
import {MediaToolOptions} from '@types'

const plugin = {
icon: ImageIcon,
name: 'media',
title: 'Media'
}

export const mediaAssetSource: AssetSource = {
...plugin,
component: FormBuilderTool
}

export const media = definePlugin<MediaToolOptions | void>(options => ({
name: 'media',
form: {
file: {
assetSources: prev => {
return [...prev, mediaAssetSource]
}
},
image: {
assetSources: prev => {
return [...prev, mediaAssetSource]
}
}
},
schema: {
types: [mediaTag]
},
tools: prev => {
return [
...prev,
{
...plugin,
options,
component: Tool
}
]
}
}))
export {media, mediaAssetSource} from './plugin'
export type {MediaToolOptions} from '@types'
53 changes: 53 additions & 0 deletions src/plugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from 'react'
import {AssetSource, Tool as SanityTool, definePlugin} from 'sanity'
import {ImageIcon} from '@sanity/icons'
import FormBuilderTool from './components/FormBuilderTool'
import Tool from './components/Tool'
import mediaTag from './schemas/tag'
import {MediaToolOptions} from '@types'
import {ToolOptionsProvider} from './contexts/ToolOptionsContext'

const plugin = {
icon: ImageIcon,
name: 'media',
title: 'Media'
}

export const mediaAssetSource = {
...plugin,
component: FormBuilderTool
} satisfies AssetSource

const tool = {
...plugin,
component: Tool
} satisfies SanityTool

export const media = definePlugin<MediaToolOptions | void>(options => ({
name: 'media',
studio: {
components: {
layout: props => (
<ToolOptionsProvider options={options}>{props.renderDefault(props)}</ToolOptionsProvider>
)
}
},
form: {
file: {
assetSources: prev => {
return [...prev, mediaAssetSource]
}
},
image: {
assetSources: prev => {
return [...prev, mediaAssetSource]
}
}
},
schema: {
types: [mediaTag]
},
tools: prev => {
return [...prev, tool]
}
}))
Loading