sanity-plugin-internationalized-array 2.0.0 → 2.0.1-canary.0

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.
@@ -35,7 +35,12 @@ export default (config: ArrayFactoryConfig): FieldDefinition<'array'> => {
35
35
  input: InternationalizedArray,
36
36
  },
37
37
  // These options are required for validation rules – not the custom input component
38
- options: {apiVersion, select, languages},
38
+ options: {
39
+ // @ts-expect-error - find out why it fails
40
+ apiVersion,
41
+ select,
42
+ languages,
43
+ },
39
44
  of: [
40
45
  defineField({
41
46
  ...(typeof type === 'string' ? {} : type),
@@ -43,6 +48,7 @@ export default (config: ArrayFactoryConfig): FieldDefinition<'array'> => {
43
48
  type: objectName,
44
49
  }),
45
50
  ],
51
+ // @ts-expect-error - find out why it fails
46
52
  validation: (rule: Rule) =>
47
53
  rule.custom<Value[]>(async (value, context) => {
48
54
  if (!value) {
@@ -18,9 +18,9 @@ export default (config: ObjectFactoryConfig): FieldDefinition<'object'> => {
18
18
  title: `Internationalized array ${type}`,
19
19
  type: 'object',
20
20
  components: {
21
+ // @ts-expect-error - find out why it fails
21
22
  item: InternationalizedInput,
22
23
  },
23
- // @ts-expect-error - Address this typing issue with the inner object
24
24
  fields: [
25
25
  typeof type === `string`
26
26
  ? // Define a simple field if all we have is the name as a string
package/src/types.ts CHANGED
@@ -27,7 +27,7 @@ export type ArrayConfig = {
27
27
 
28
28
  export type Value = {
29
29
  _key: string
30
- value?: string
30
+ value?: unknown
31
31
  }
32
32
 
33
33
  export type LanguageCallback = (
@@ -31,16 +31,24 @@ export function createAddLanguagePatches(config: AddConfig): FormInsertPatch[] {
31
31
  const itemBase = {_type: createValueSchemaTypeName(schemaType)}
32
32
 
33
33
  // Create new items
34
- const newItems =
35
- Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0
36
- ? // Just one for this language
37
- addLanguageKeys.map((id) => ({...itemBase, _key: id}))
38
- : // Or one for every missing language
39
- filteredLanguages
40
- .filter((language) =>
41
- value?.length ? !value.find((v) => v._key === language.id) : true
42
- )
43
- .map((language) => ({...itemBase, _key: language.id}))
34
+ const getNewItems = () => {
35
+ if (Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0) {
36
+ return addLanguageKeys.map((id) => ({
37
+ ...itemBase,
38
+ _key: id,
39
+ }))
40
+ }
41
+
42
+ return filteredLanguages
43
+ .filter((language) =>
44
+ value?.length ? !value.find((v) => v._key === language.id) : true
45
+ )
46
+ .map((language) => ({
47
+ ...itemBase,
48
+ _key: language.id,
49
+ }))
50
+ }
51
+ const newItems = getNewItems()
44
52
 
45
53
  // Insert new items in the correct order
46
54
  const languagesInUse = value?.length ? value.map((v) => v) : []
@@ -0,0 +1,66 @@
1
+ import {SanityDocument} from 'sanity'
2
+
3
+ export interface DocumentsToTranslate {
4
+ path: (string | number)[]
5
+ pathString: string
6
+ _key: string
7
+ _type: string
8
+ [key: string]: unknown
9
+ }
10
+
11
+ export const getDocumentsToTranslate = (
12
+ value: SanityDocument | unknown,
13
+ rootPath: (string | number)[] = []
14
+ ): DocumentsToTranslate[] => {
15
+ if (Array.isArray(value)) {
16
+ const arrayRootPath = [...rootPath]
17
+
18
+ // if item contains internationalized return array
19
+ const internationalizedValues = value.filter((item) => {
20
+ if (Array.isArray(item)) return false
21
+
22
+ if (typeof item === 'object') {
23
+ const type = item?._type as string | undefined
24
+ return (
25
+ type?.startsWith('internationalizedArray') && type?.endsWith('Value')
26
+ )
27
+ }
28
+ return false
29
+ })
30
+
31
+ if (internationalizedValues.length > 0) {
32
+ return internationalizedValues.map((internationalizedValue) => {
33
+ return {
34
+ ...internationalizedValue,
35
+ path: arrayRootPath,
36
+ pathString: arrayRootPath.join('.'),
37
+ }
38
+ })
39
+ }
40
+
41
+ if (value.length > 0) {
42
+ return value
43
+ .map((item, index) =>
44
+ getDocumentsToTranslate(item, [...arrayRootPath, index])
45
+ )
46
+ .flat()
47
+ }
48
+
49
+ return []
50
+ }
51
+ if (typeof value === 'object' && value) {
52
+ const startsWithUnderscoreRegex = /^_/
53
+ const itemKeys = Object.keys(value).filter(
54
+ (key) => !key.match(startsWithUnderscoreRegex)
55
+ ) as (keyof typeof value)[]
56
+
57
+ return itemKeys
58
+ .map((item) => {
59
+ const selectedValue = value[item] as unknown
60
+ const path = [...rootPath, item]
61
+ return getDocumentsToTranslate(selectedValue, path)
62
+ })
63
+ .flat()
64
+ }
65
+ return []
66
+ }