sanity-plugin-internationalized-array 1.9.0 → 1.9.1

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.js CHANGED
@@ -9468,7 +9468,7 @@ if(freeModule){// Export for Node.js.
9468
9468
  (freeModule.exports=_)._=_;// Export for CommonJS support.
9469
9469
  freeExports._=_;}else{// Export to the global object.
9470
9470
  root._=_;}}).call(commonjsGlobal);})(lodash,lodash.exports);var lodashExports=lodash.exports;const getSelectedValue=(select,document)=>{if(!select||!document){return{};}const selection=select||{};const selectedValue={};for(const[key,path]of Object.entries(selection)){let value=lodashExports.get(document,path);if(Array.isArray(value)){value=value.filter(item=>typeof item==="object"?(item==null?void 0:item._type)==="reference"&&"_ref"in item:true);}selectedValue[key]=value;}return selectedValue;};const InternationalizedArrayContext=react.createContext({...CONFIG_DEFAULT,languages:[],filteredLanguages:[]});function useInternationalizedArrayContext(){return react.useContext(InternationalizedArrayContext);}function InternationalizedArrayProvider(props){const{internationalizedArray}=props;const client=sanity.useClient({apiVersion:internationalizedArray.apiVersion});const{value:document}=sanity.useFormBuilder();const deferredDocument=react.useDeferredValue(document);const selectedValue=react.useMemo(()=>getSelectedValue(internationalizedArray.select,deferredDocument),[internationalizedArray.select,deferredDocument]);const languages=Array.isArray(internationalizedArray.languages)?internationalizedArray.languages:suspend.suspend(// eslint-disable-next-line require-await
9471
- async()=>{if(typeof internationalizedArray.languages==="function"){return internationalizedArray.languages(client,selectedValue);}return internationalizedArray.languages;},[version,namespace],{equal:equal__default.default});const{selectedLanguageIds,options:languageFilterOptions}=languageFilter.useLanguageFilterStudioContext();const filteredLanguages=react.useMemo(()=>{const documentType=deferredDocument?deferredDocument._type:void 0;const languageFilterEnabled=typeof documentType==="string"&&languageFilterOptions.documentTypes.includes(documentType);return languageFilterEnabled?languages.filter(language=>selectedLanguageIds.includes(language.id)):languages;},[deferredDocument,languageFilterOptions,languages,selectedLanguageIds]);const showDocumentButtons=internationalizedArray.buttonLocations.includes("document");return/* @__PURE__ */jsxRuntime.jsx(InternationalizedArrayContext.Provider,{value:{...internationalizedArray,languages,filteredLanguages},children:showDocumentButtons?/* @__PURE__ */jsxRuntime.jsxs(ui.Stack,{space:5,children:[/* @__PURE__ */jsxRuntime.jsx(DocumentAddButtons,{schemaType:props.schemaType,value:props.value}),props.renderDefault(props)]}):props.renderDefault(props)});}var Preload=react.memo(function Preload(props){const client=sanity.useClient({apiVersion:props.apiVersion});if(!Array.isArray(peek({}))){preload(async()=>Array.isArray(props.languages)?props.languages:props.languages(client,{}));}return null;});function checkAllLanguagesArePresent(languages,value){const filteredLanguageIds=languages.map(l=>l.id);const languagesInUseIds=value?value.map(v=>v._key):[];return languagesInUseIds.length===filteredLanguageIds.length&&languagesInUseIds.every(l=>filteredLanguageIds.includes(l));}function createAddAllTitle(value,languages){if(value==null?void 0:value.length){return"Add missing ".concat(languages.length-value.length===1?"language":"languages");}return languages.length===1?"Add ".concat(languages[0].title," Field"):"Add all languages";}function createAddLanguagePatches(config){const{addLanguageKeys,schemaType,languages,filteredLanguages,value,path=[]}=config;const itemBase={_type:createValueSchemaTypeName(schemaType)};const newItems=Array.isArray(addLanguageKeys)&&addLanguageKeys.length>0?// Just one for this language
9471
+ async()=>{if(typeof internationalizedArray.languages==="function"){return internationalizedArray.languages(client,selectedValue);}return internationalizedArray.languages;},[version,namespace,selectedValue],{equal:equal__default.default});const{selectedLanguageIds,options:languageFilterOptions}=languageFilter.useLanguageFilterStudioContext();const filteredLanguages=react.useMemo(()=>{const documentType=deferredDocument?deferredDocument._type:void 0;const languageFilterEnabled=typeof documentType==="string"&&languageFilterOptions.documentTypes.includes(documentType);return languageFilterEnabled?languages.filter(language=>selectedLanguageIds.includes(language.id)):languages;},[deferredDocument,languageFilterOptions,languages,selectedLanguageIds]);const showDocumentButtons=internationalizedArray.buttonLocations.includes("document");return/* @__PURE__ */jsxRuntime.jsx(InternationalizedArrayContext.Provider,{value:{...internationalizedArray,languages,filteredLanguages},children:showDocumentButtons?/* @__PURE__ */jsxRuntime.jsxs(ui.Stack,{space:5,children:[/* @__PURE__ */jsxRuntime.jsx(DocumentAddButtons,{schemaType:props.schemaType,value:props.value}),props.renderDefault(props)]}):props.renderDefault(props)});}var Preload=react.memo(function Preload(props){const client=sanity.useClient({apiVersion:props.apiVersion});if(!Array.isArray(peek({}))){preload(async()=>Array.isArray(props.languages)?props.languages:props.languages(client,{}));}return null;});function checkAllLanguagesArePresent(languages,value){const filteredLanguageIds=languages.map(l=>l.id);const languagesInUseIds=value?value.map(v=>v._key):[];return languagesInUseIds.length===filteredLanguageIds.length&&languagesInUseIds.every(l=>filteredLanguageIds.includes(l));}function createAddAllTitle(value,languages){if(value==null?void 0:value.length){return"Add missing ".concat(languages.length-value.length===1?"language":"languages");}return languages.length===1?"Add ".concat(languages[0].title," Field"):"Add all languages";}function createAddLanguagePatches(config){const{addLanguageKeys,schemaType,languages,filteredLanguages,value,path=[]}=config;const itemBase={_type:createValueSchemaTypeName(schemaType)};const newItems=Array.isArray(addLanguageKeys)&&addLanguageKeys.length>0?// Just one for this language
9472
9472
  addLanguageKeys.map(id=>({...itemBase,_key:id})):// Or one for every missing language
9473
9473
  filteredLanguages.filter(language=>(value==null?void 0:value.length)?!value.find(v=>v._key===language.id):true).map(language=>({...itemBase,_key:language.id}));const languagesInUse=(value==null?void 0:value.length)?value.map(v=>v):[];const insertions=newItems.map(item=>{const languageIndex=languages.findIndex(l=>item._key===l.id);const remainingLanguages=languages.slice(languageIndex+1);const nextLanguageIndex=languagesInUse.findIndex(l=>// eslint-disable-next-line max-nested-callbacks
9474
9474
  remainingLanguages.find(r=>r.id===l._key));if(nextLanguageIndex<0){languagesInUse.push(item);}else{languagesInUse.splice(nextLanguageIndex,0,item);}return nextLanguageIndex<0?// No next language (-1), add to end of array
@@ -9491,9 +9491,11 @@ fields:[typeof type==="string"?// Define a simple field if all we have is the na
9491
9491
  sanity.defineField({name:"value",type,components:{// TODO: Address this typing issue with the inner object
9492
9492
  // @ts-expect-error
9493
9493
  field:InternationalizedField}}):// Pass in the configured options, but overwrite the name
9494
- {...type,name:"value",components:{field:InternationalizedField}}],preview:{select:{title:"value",subtitle:"_key"}}});};const internationalizedArray=sanity.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
9494
+ {...type,name:"value",components:{field:InternationalizedField}}],preview:{select:{title:"value",subtitle:"_key"}}});};function flattenSchemaType(schemaType){if(!sanity.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"&&sanity.isArraySchemaType(field)){const innerFields=extractInnerFields(// TODO: Fix TS assertion for array fields
9495
+ // @ts-expect-error
9496
+ field.type.of,[...path,field.name],maxDepth);return[...acc,thisFieldWithPath,...innerFields];}return[...acc,thisFieldWithPath];},[]);}const internationalizedArray=sanity.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
9495
9497
  studio:Array.isArray(languages)?void 0:{components:{layout:props=>/* @__PURE__ */jsxRuntime.jsxs(jsxRuntime.Fragment,{children:[/* @__PURE__ */jsxRuntime.jsx(Preload,{apiVersion,languages}),props.renderDefault(props)]})}},// Optional: render "add language" buttons as field actions
9496
9498
  document:{unstable_fieldActions:buttonLocations.includes("unstable__fieldAction")?prev=>[...prev,internationalizedArrayFieldAction]:void 0},// Wrap document editor with a language provider
9497
- form:{components:{input:props=>{const isRootInput=props.id==="root"&&sanity.isObjectInputProps(props);if(!isRootInput){return props.renderDefault(props);}const rootFieldTypeNames=props.schemaType.fields.map(field=>field.type.name);const hasInternationalizedArray=rootFieldTypeNames.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
9499
+ form:{components:{input:props=>{const isRootInput=props.id==="root"&&sanity.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
9498
9500
  schema:{types:[...fieldTypes.map(type=>array({type,apiVersion,select,languages,defaultLanguages})),...fieldTypes.map(type=>object({type}))]}};});exports.clear=clear;exports.internationalizedArray=internationalizedArray;
9499
9501
  //# sourceMappingURL=index.js.map