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: make the initial value template generation optional #168

Open
wants to merge 2 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export const createConfig({
defineField({ name: 'slug', type: 'slug' })
],

// Optional
// Opt-out of static or parameterized initial value template generation
initialValueTemplates: false // defaults to true, also accepts "static", "parameterized" for just one or the other

// Optional
// Define API Version for all queries
// https://www.sanity.io/docs/api-versioning
Expand Down
55 changes: 32 additions & 23 deletions src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const documentInternationalization = definePlugin<PluginConfig>(
languageField,
bulkPublish,
metadataFields,
initialValueTemplates,
} = pluginConfig

if (schemaTypes.length === 0) {
Expand Down Expand Up @@ -124,32 +125,40 @@ export const documentInternationalization = definePlugin<PluginConfig>(
return prev
}

const parameterizedTemplates = schemaTypes.map((schemaType) => ({
id: `${schemaType}-parameterized`,
title: `${
schema?.get(schemaType)?.title ?? schemaType
}: with Language`,
schemaType,
parameters: [
{name: `languageId`, title: `Language ID`, type: `string`},
],
value: ({languageId}: {languageId: string}) => ({
[languageField]: languageId,
}),
}))

const staticTemplates = schemaTypes.flatMap((schemaType) => {
return supportedLanguages.map((language) => ({
id: `${schemaType}-${language.id}`,
title: `${language.title} ${
if (initialValueTemplates === false) {
return prev
}

if (initialValueTemplates !== 'static') {
const parameterizedTemplates = schemaTypes.map((schemaType) => ({
id: `${schemaType}-parameterized`,
title: `${
schema?.get(schemaType)?.title ?? schemaType
}`,
}: with Language`,
schemaType,
value: {
[languageField]: language.id,
},
parameters: [
{name: `languageId`, title: `Language ID`, type: `string`},
],
value: ({languageId}: {languageId: string}) => ({
[languageField]: languageId,
}),
}))
})
}

if (initialValueTemplates !== 'parameterized') {
const staticTemplates = schemaTypes.flatMap((schemaType) => {
return supportedLanguages.map((language) => ({
id: `${schemaType}-${language.id}`,
title: `${language.title} ${
schema?.get(schemaType)?.title ?? schemaType
}`,
schemaType,
value: {
[languageField]: language.id,
},
}))
})
}

return [...prev, ...parameterizedTemplates, ...staticTemplates]
},
Expand Down