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

feat: allow nested languageField on documents #178

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All new rewrite exclusively for Sanity Studio v3
- [Basic configuration](#basic-configuration)
- [Advanced configuration](#advanced-configuration)
- [Language field](#language-field)
- [Excluding fields](#excluding-fields)
- [Querying translations](#querying-translations)
- [Querying with GROQ](#querying-with-groq)
- [Querying with GraphQL](#querying-with-graphql)
Expand Down Expand Up @@ -154,7 +155,7 @@ export const createConfig({

### Language field

The schema types that use document internationalization must also have a `string` field type with the same name configured in the `languageField` setting. Unless you want content creators to be able to change the language of a document, you may hide or disable this field since the plugin will handle writing patches to it.
The schema types that use document internationalization must also have a `string` field type with the same path name configured in the `languageField` setting. Unless you want content creators to be able to change the language of a document, you may hide or disable this field since the plugin will handle writing patches to it.

```ts
// ./schema/lesson.ts
Expand All @@ -166,7 +167,44 @@ defineField({
type: 'string',
readOnly: true,
hidden: true,
})
}),

// Nested value paths name are also possible e.g:
{
name: 'option'
type: 'object',
fields: [
// ...other fields
// languageField: 'option.language'
defineField({
name: 'language',
type: 'string',
readOnly: true,
hidden: true,
}),
],
},

// or in array...
{
name: 'options',
type: 'array',
of: [
name: 'option'
type: 'object',
fields: [
// languageField: 'options[0].language'
defineField({
name: 'language',
type: 'string',
readOnly: true,
hidden: true,
}),
// ...other fields
],
],
}

```

### Excluding fields
Expand Down
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@sanity/incompatible-plugin": "^1.0.4",
"@sanity/ui": "^2.1.0",
"@sanity/uuid": "^3.0.2",
"just-safe-get": "^4.2.0",
"sanity-plugin-internationalized-array": "^2.0.0",
"sanity-plugin-utils": "^1.6.4"
},
Expand Down
3 changes: 2 additions & 1 deletion src/actions/DeleteTranslationAction.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {TrashIcon} from '@sanity/icons'
import {type ButtonTone, useToast} from '@sanity/ui'
import get from 'just-safe-get'
import {useCallback, useState} from 'react'
import {
type DocumentActionComponent,
Expand All @@ -20,7 +21,7 @@ export const DeleteTranslationAction: DocumentActionComponent = (props) => {
const [isDialogOpen, setDialogOpen] = useState(false)
const [translations, setTranslations] = useState<SanityDocument[]>([])
const onClose = useCallback(() => setDialogOpen(false), [])
const documentLanguage = doc ? doc[languageField] : null
const documentLanguage = doc ? get(doc, languageField) : null

const toast = useToast()
const client = useClient({apiVersion: API_VERSION})
Expand Down
3 changes: 2 additions & 1 deletion src/badges/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import get from 'just-safe-get'
import type {DocumentBadgeDescription, DocumentBadgeProps} from 'sanity'

import {useDocumentInternationalizationContext} from '../components/DocumentInternationalizationContext'
Expand All @@ -8,7 +9,7 @@ export function LanguageBadge(
const source = props?.draft || props?.published
const {languageField, supportedLanguages} =
useDocumentInternationalizationContext()
const languageId = source?.[languageField]
const languageId = source ? get(source, languageField) : null

if (!languageId) {
return null
Expand Down
3 changes: 2 additions & 1 deletion src/components/DocumentInternationalizationMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useClickOutside,
} from '@sanity/ui'
import {uuid} from '@sanity/uuid'
import get from 'just-safe-get'
import {type FormEvent, useCallback, useMemo, useState} from 'react'
import {useEditState} from 'sanity'

Expand Down Expand Up @@ -71,7 +72,7 @@ export function DocumentInternationalizationMenu(
const documentIsInOneMetadataDocument = useMemo(() => {
return Array.isArray(data) && data.length <= 1
}, [data])
const sourceLanguageId = source?.[languageField] as string | undefined
const sourceLanguageId = source ? get(source, languageField) : undefined
const sourceLanguageIsValid = supportedLanguages.some(
(l) => l.id === sourceLanguageId
)
Expand Down