From 9bf2d6b14e130cc990a36650d65b765cea3be53b Mon Sep 17 00:00:00 2001 From: Jamie Warburton Date: Tue, 19 Dec 2023 21:27:13 +0000 Subject: [PATCH 1/2] feat: make the initial value template generation optional Currently there is no way to opt-out of the initial value template generation, and instead they need to be removed if not being used. This commit adds a configuration option to opt-out of either the static, parametized or both template generation. By default, both are still generated. `initialValueTemplates: false` will prevent any generation `initialValueTemplates: 'static'` will only generate the static templates `initialValueTemplates: 'parameterized'` will only generate the parameterized templates --- src/plugin.tsx | 55 +++++++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/plugin.tsx b/src/plugin.tsx index 624bdff..79bdc01 100644 --- a/src/plugin.tsx +++ b/src/plugin.tsx @@ -21,6 +21,7 @@ export const documentInternationalization = definePlugin( languageField, bulkPublish, metadataFields, + initialValueTemplates, } = pluginConfig if (schemaTypes.length === 0) { @@ -124,32 +125,40 @@ export const documentInternationalization = definePlugin( 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] }, From 071b2cdd2b47b430383c67e0293c650f508062ca Mon Sep 17 00:00:00 2001 From: Jamie Warburton Date: Tue, 19 Dec 2023 21:36:31 +0000 Subject: [PATCH 2/2] feat: add initialValueTemplates option to README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index e216f87..bd00d05 100644 --- a/README.md +++ b/README.md @@ -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