sanity-plugin-internationalized-array 1.10.1 → 1.10.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/lib/index.esm.js +3 -3
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/flattenSchemaType.ts +7 -9
package/lib/index.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import*as suspend from'suspend-react';import{suspend as suspend$1}from'suspend-react';import{jsx,jsxs,Fragment}from'react/jsx-runtime';import{isSanityDocument,setIfMissing,insert,PatchEvent,useClient,useFormBuilder,defineDocumentFieldAction,useFormValue,set,ArrayOfObjectsItem,MemberItemError,defineField,unset,isDocumentSchemaType,
|
|
1
|
+
import*as suspend from'suspend-react';import{suspend as suspend$1}from'suspend-react';import{jsx,jsxs,Fragment}from'react/jsx-runtime';import{isSanityDocument,setIfMissing,insert,PatchEvent,useClient,useFormBuilder,defineDocumentFieldAction,useFormValue,set,ArrayOfObjectsItem,MemberItemError,defineField,unset,isDocumentSchemaType,definePlugin,isObjectInputProps}from'sanity';import{useLanguageFilterStudioContext}from'@sanity/language-filter';import{Grid,Button,useToast,Stack,Box,Text,Card,Code,Spinner,Label,MenuButton,Menu,MenuItem,Flex}from'@sanity/ui';import equal from'fast-deep-equal';import{useMemo,useCallback,createContext,useContext,useDeferredValue,memo,useState,useEffect,createElement}from'react';import{useDocumentPane}from'sanity/desk';import{AddIcon,TranslateIcon,RemoveCircleIcon}from'@sanity/icons';const namespace="sanity-plugin-internationalized-array";const version="v0";const preload=fn=>suspend.preload(()=>fn(),[version,namespace]);const clear=()=>suspend.clear([version,namespace]);const peek=selectedValue=>suspend.peek([version,namespace,selectedValue]);const MAX_COLUMNS=7;const CONFIG_DEFAULT={languages:[],select:{},defaultLanguages:[],fieldTypes:[],apiVersion:"2022-11-27",buttonLocations:["field"],buttonAddAll:true};function createValueSchemaTypeName(schemaType){return"".concat(schemaType.name,"Value");}function AddButtons(props){const{languages,readOnly,value,onClick}=props;return languages.length>0?/* @__PURE__ */jsx(Grid,{columns:Math.min(languages.length,MAX_COLUMNS),gap:2,children:languages.map(language=>/* @__PURE__ */jsx(Button,{tone:"primary",mode:"ghost",fontSize:1,disabled:readOnly||Boolean(value==null?void 0:value.find(item=>item._key===language.id)),text:language.id.toUpperCase(),icon:languages.length>MAX_COLUMNS?void 0:AddIcon,value:language.id,onClick},language.id))}):null;}function DocumentAddButtons(props){const{filteredLanguages}=useInternationalizedArrayContext();const{fields}=props.schemaType;const value=isSanityDocument(props.value)?props.value:void 0;const toast=useToast();const{onChange}=useDocumentPane();const internationalizedArrayFields=useMemo(()=>fields.filter(field=>field.type.name.startsWith("internationalizedArray")),[fields]);const handleDocumentButtonClick=useCallback(event=>{const languageId=event.currentTarget.value;if(!languageId){toast.push({status:"error",title:"No language selected"});return;}if(internationalizedArrayFields.length===0){toast.push({status:"error",title:"No internationalizedArray fields found in document root"});return;}const emptyLanguageFields=internationalizedArrayFields.filter(field=>{const fieldValue=value==null?void 0:value[field.name];const fieldValueLanguage=fieldValue&&Array.isArray(fieldValue)?fieldValue.find(v=>v._key===languageId):void 0;return!fieldValueLanguage;});const patches=emptyLanguageFields.map(field=>{const fieldKey=field.name;return[setIfMissing([],[fieldKey]),insert([{_key:languageId,_type:createValueSchemaTypeName(field.type)}],"after",[fieldKey,-1])];}).flat();onChange(PatchEvent.from(patches));},[internationalizedArrayFields,onChange,toast,value]);return/* @__PURE__ */jsxs(Stack,{space:3,children:[/* @__PURE__ */jsx(Box,{children:/* @__PURE__ */jsx(Text,{size:1,weight:"semibold",children:"Add translation to internationalized fields"})}),/* @__PURE__ */jsx(AddButtons,{languages:filteredLanguages,readOnly:false,value:void 0,onClick:handleDocumentButtonClick})]});}var commonjsGlobal=typeof globalThis!=='undefined'?globalThis:typeof window!=='undefined'?window:typeof global!=='undefined'?global:typeof self!=='undefined'?self:{};var lodash={exports:{}};/**
|
|
2
2
|
* @license
|
|
3
3
|
* Lodash <https://lodash.com/>
|
|
4
4
|
* Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
|
|
@@ -9493,9 +9493,9 @@ fields:[typeof type==="string"?// Define a simple field if all we have is the na
|
|
|
9493
9493
|
defineField({name:"value",type,components:{// TODO: Address this typing issue with the inner object
|
|
9494
9494
|
// @ts-expect-error
|
|
9495
9495
|
field:InternationalizedField}}):// Pass in the configured options, but overwrite the name
|
|
9496
|
-
{...type,name:"value",components:{field:InternationalizedField}}],preview:{select:{title:"value",subtitle:"_key"}}});};function flattenSchemaType(schemaType){if(!isDocumentSchemaType(schemaType)){console.error("Schema type is not a document");return[];}return extractInnerFields(schemaType.fields,[],3);}function extractInnerFields(fields,path,maxDepth){if(path.length>=maxDepth){return[];}return fields.reduce((acc,field)=>{const thisFieldWithPath={path:[...path,field.name],...field};if(field.type.jsonType==="object"){const innerFields=extractInnerFields(field.type.fields,[...path,field.name],maxDepth);return[...acc,thisFieldWithPath,...innerFields];}else if(field.type.jsonType==="array"&&
|
|
9496
|
+
{...type,name:"value",components:{field:InternationalizedField}}],preview:{select:{title:"value",subtitle:"_key"}}});};function flattenSchemaType(schemaType){if(!isDocumentSchemaType(schemaType)){console.error("Schema type is not a document");return[];}return extractInnerFields(schemaType.fields,[],3);}function extractInnerFields(fields,path,maxDepth){if(path.length>=maxDepth){return[];}return fields.reduce((acc,field)=>{const thisFieldWithPath={path:[...path,field.name],...field};if(field.type.jsonType==="object"){const innerFields=extractInnerFields(field.type.fields,[...path,field.name],maxDepth);return[...acc,thisFieldWithPath,...innerFields];}else if(field.type.jsonType==="array"&&field.type.of.length&&field.type.of.some(item=>"fields"in item)){const innerFields=extractInnerFields(// TODO: Fix TS assertion for array fields
|
|
9497
9497
|
// @ts-expect-error
|
|
9498
|
-
field.type.of,[...path,field.name],maxDepth);return[...acc,thisFieldWithPath,...innerFields];}return[...acc,thisFieldWithPath];},[]);}const internationalizedArray=definePlugin(config=>{const pluginConfig={...CONFIG_DEFAULT,...config};const{apiVersion="2022-11-27",select,languages,fieldTypes,defaultLanguages,buttonLocations}=pluginConfig;return{name:"sanity-plugin-internationalized-array",// Preload languages for use throughout the Studio
|
|
9498
|
+
field.type.of[0].fields,[...path,field.name],maxDepth);return[...acc,thisFieldWithPath,...innerFields];}return[...acc,thisFieldWithPath];},[]);}const internationalizedArray=definePlugin(config=>{const pluginConfig={...CONFIG_DEFAULT,...config};const{apiVersion="2022-11-27",select,languages,fieldTypes,defaultLanguages,buttonLocations}=pluginConfig;return{name:"sanity-plugin-internationalized-array",// Preload languages for use throughout the Studio
|
|
9499
9499
|
studio:Array.isArray(languages)?void 0:{components:{layout:props=>/* @__PURE__ */jsxs(Fragment,{children:[/* @__PURE__ */jsx(Preload,{apiVersion,languages}),props.renderDefault(props)]})}},// Optional: render "add language" buttons as field actions
|
|
9500
9500
|
document:{unstable_fieldActions:buttonLocations.includes("unstable__fieldAction")?prev=>[...prev,internationalizedArrayFieldAction]:void 0},// Wrap document editor with a language provider
|
|
9501
9501
|
form:{components:{input:props=>{const isRootInput=props.id==="root"&&isObjectInputProps(props);if(!isRootInput){return props.renderDefault(props);}const flatFieldTypeNames=flattenSchemaType(props.schemaType).map(field=>field.type.name);const hasInternationalizedArray=flatFieldTypeNames.some(name=>name.startsWith("internationalizedArray"));if(!hasInternationalizedArray){return props.renderDefault(props);}return InternationalizedArrayProvider({...props,internationalizedArray:pluginConfig});}}},// Register custom schema types for the outer array and the inner object
|