sanity-plugin-internationalized-array 2.0.0 → 2.0.1-canary.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.d.mts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.esm.js +123 -109
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +122 -109
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +123 -109
- package/lib/index.mjs.map +1 -1
- package/package.json +25 -31
- package/src/cache.ts +1 -1
- package/src/components/AddButtons.tsx +4 -1
- package/src/components/DocumentAddButtons.tsx +53 -52
- package/src/components/InternationalizedArray.tsx +3 -1
- package/src/components/InternationalizedArrayContext.tsx +10 -19
- package/src/fieldActions/index.ts +8 -2
- package/src/plugin.tsx +6 -4
- package/src/schema/array.ts +7 -1
- package/src/schema/object.ts +1 -1
- package/src/types.ts +1 -1
- package/src/utils/createAddLanguagePatches.ts +18 -10
- package/src/utils/getDocumentsToTranslate.ts +66 -0
package/lib/index.js
CHANGED
|
@@ -5,8 +5,7 @@ function _interopDefaultCompat(e) {
|
|
|
5
5
|
return e && typeof e == "object" && "default" in e ? e : { default: e };
|
|
6
6
|
}
|
|
7
7
|
function _interopNamespaceCompat(e) {
|
|
8
|
-
if (e && typeof e == "object" && "default" in e)
|
|
9
|
-
return e;
|
|
8
|
+
if (e && typeof e == "object" && "default" in e) return e;
|
|
10
9
|
var n = /* @__PURE__ */ Object.create(null);
|
|
11
10
|
return e && Object.keys(e).forEach(function(k) {
|
|
12
11
|
if (k !== "default") {
|
|
@@ -21,7 +20,7 @@ function _interopNamespaceCompat(e) {
|
|
|
21
20
|
}), n.default = e, Object.freeze(n);
|
|
22
21
|
}
|
|
23
22
|
var suspend__namespace = /* @__PURE__ */ _interopNamespaceCompat(suspend), equal__default = /* @__PURE__ */ _interopDefaultCompat(equal), get__default = /* @__PURE__ */ _interopDefaultCompat(get);
|
|
24
|
-
const namespace = "sanity-plugin-internationalized-array", version = "
|
|
23
|
+
const namespace = "sanity-plugin-internationalized-array", version = "v1", preload = (fn) => suspend__namespace.preload(() => fn(), [version, namespace]), clear = () => suspend__namespace.clear([version, namespace]), peek = (selectedValue) => suspend__namespace.peek([version, namespace, selectedValue]), MAX_COLUMNS = 7, CONFIG_DEFAULT = {
|
|
25
24
|
languages: [],
|
|
26
25
|
select: {},
|
|
27
26
|
defaultLanguages: [],
|
|
@@ -29,10 +28,35 @@ const namespace = "sanity-plugin-internationalized-array", version = "v0", prelo
|
|
|
29
28
|
apiVersion: "2022-11-27",
|
|
30
29
|
buttonLocations: ["field"],
|
|
31
30
|
buttonAddAll: !0
|
|
31
|
+
}, getDocumentsToTranslate = (value, rootPath = []) => {
|
|
32
|
+
if (Array.isArray(value)) {
|
|
33
|
+
const arrayRootPath = [...rootPath], internationalizedValues = value.filter((item) => {
|
|
34
|
+
if (Array.isArray(item)) return !1;
|
|
35
|
+
if (typeof item == "object") {
|
|
36
|
+
const type = item?._type;
|
|
37
|
+
return type?.startsWith("internationalizedArray") && type?.endsWith("Value");
|
|
38
|
+
}
|
|
39
|
+
return !1;
|
|
40
|
+
});
|
|
41
|
+
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => ({
|
|
42
|
+
...internationalizedValue,
|
|
43
|
+
path: arrayRootPath,
|
|
44
|
+
pathString: arrayRootPath.join(".")
|
|
45
|
+
})) : value.length > 0 ? value.map(
|
|
46
|
+
(item, index) => getDocumentsToTranslate(item, [...arrayRootPath, index])
|
|
47
|
+
).flat() : [];
|
|
48
|
+
}
|
|
49
|
+
if (typeof value == "object" && value) {
|
|
50
|
+
const startsWithUnderscoreRegex = /^_/;
|
|
51
|
+
return Object.keys(value).filter(
|
|
52
|
+
(key) => !key.match(startsWithUnderscoreRegex)
|
|
53
|
+
).map((item) => {
|
|
54
|
+
const selectedValue = value[item], path = [...rootPath, item];
|
|
55
|
+
return getDocumentsToTranslate(selectedValue, path);
|
|
56
|
+
}).flat();
|
|
57
|
+
}
|
|
58
|
+
return [];
|
|
32
59
|
};
|
|
33
|
-
function createValueSchemaTypeName(schemaType) {
|
|
34
|
-
return `${schemaType.name}Value`;
|
|
35
|
-
}
|
|
36
60
|
function AddButtons(props) {
|
|
37
61
|
const { languages, readOnly, value, onClick } = props;
|
|
38
62
|
return languages.length > 0 ? /* @__PURE__ */ jsxRuntime.jsx(ui.Grid, { columns: Math.min(languages.length, MAX_COLUMNS), gap: 2, children: languages.map((language) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
@@ -41,7 +65,7 @@ function AddButtons(props) {
|
|
|
41
65
|
tone: "primary",
|
|
42
66
|
mode: "ghost",
|
|
43
67
|
fontSize: 1,
|
|
44
|
-
disabled: readOnly || !!
|
|
68
|
+
disabled: readOnly || !!value?.find((item) => item._key === language.id),
|
|
45
69
|
text: language.id.toUpperCase(),
|
|
46
70
|
icon: languages.length > MAX_COLUMNS ? void 0 : icons.AddIcon,
|
|
47
71
|
value: language.id,
|
|
@@ -50,14 +74,10 @@ function AddButtons(props) {
|
|
|
50
74
|
language.id
|
|
51
75
|
)) }) : null;
|
|
52
76
|
}
|
|
77
|
+
var AddButtons$1 = react.memo(AddButtons);
|
|
53
78
|
function DocumentAddButtons(props) {
|
|
54
|
-
const { filteredLanguages } = useInternationalizedArrayContext(),
|
|
55
|
-
() =>
|
|
56
|
-
(field) => field.type.name.startsWith("internationalizedArray")
|
|
57
|
-
),
|
|
58
|
-
[fields]
|
|
59
|
-
), handleDocumentButtonClick = react.useCallback(
|
|
60
|
-
(event) => {
|
|
79
|
+
const { filteredLanguages } = useInternationalizedArrayContext(), value = sanity.isSanityDocument(props.value) ? props.value : void 0, toast = ui.useToast(), { onChange } = structure.useDocumentPane(), documentsToTranslation = getDocumentsToTranslate(value, []), handleDocumentButtonClick = react.useCallback(
|
|
80
|
+
async (event) => {
|
|
61
81
|
const languageId = event.currentTarget.value;
|
|
62
82
|
if (!languageId) {
|
|
63
83
|
toast.push({
|
|
@@ -66,42 +86,43 @@ function DocumentAddButtons(props) {
|
|
|
66
86
|
});
|
|
67
87
|
return;
|
|
68
88
|
}
|
|
69
|
-
|
|
89
|
+
const alreadyTranslated = documentsToTranslation.filter(
|
|
90
|
+
(translation) => translation?._key === languageId
|
|
91
|
+
), removeDuplicates = documentsToTranslation.reduce((filteredTranslations, translation) => alreadyTranslated.filter(
|
|
92
|
+
(alreadyTranslation) => alreadyTranslation.pathString === translation.pathString
|
|
93
|
+
).length > 0 || filteredTranslations.filter(
|
|
94
|
+
(filteredTranslation) => filteredTranslation.path === translation.path
|
|
95
|
+
).length > 0 ? filteredTranslations : [...filteredTranslations, translation], []);
|
|
96
|
+
if (removeDuplicates.length === 0) {
|
|
70
97
|
toast.push({
|
|
71
98
|
status: "error",
|
|
72
99
|
title: "No internationalizedArray fields found in document root"
|
|
73
100
|
});
|
|
74
101
|
return;
|
|
75
102
|
}
|
|
76
|
-
const patches =
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
"after",
|
|
93
|
-
[fieldKey, -1]
|
|
94
|
-
)
|
|
95
|
-
];
|
|
96
|
-
}).flat();
|
|
97
|
-
onChange(sanity.PatchEvent.from(patches));
|
|
103
|
+
const patches = [];
|
|
104
|
+
for (const toTranslate of removeDuplicates) {
|
|
105
|
+
const path = toTranslate.path, ifMissing = sanity.setIfMissing([], path), insertValue = sanity.insert(
|
|
106
|
+
[
|
|
107
|
+
{
|
|
108
|
+
_key: languageId,
|
|
109
|
+
_type: toTranslate._type,
|
|
110
|
+
value: void 0
|
|
111
|
+
}
|
|
112
|
+
],
|
|
113
|
+
"after",
|
|
114
|
+
[...path, -1]
|
|
115
|
+
);
|
|
116
|
+
patches.push(ifMissing), patches.push(insertValue);
|
|
117
|
+
}
|
|
118
|
+
onChange(sanity.PatchEvent.from(patches.flat()));
|
|
98
119
|
},
|
|
99
|
-
[
|
|
120
|
+
[documentsToTranslation, onChange, toast]
|
|
100
121
|
);
|
|
101
122
|
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
|
|
102
123
|
/* @__PURE__ */ jsxRuntime.jsx(ui.Box, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: 1, weight: "semibold", children: "Add translation to internationalized fields" }) }),
|
|
103
124
|
/* @__PURE__ */ jsxRuntime.jsx(
|
|
104
|
-
AddButtons,
|
|
125
|
+
AddButtons$1,
|
|
105
126
|
{
|
|
106
127
|
languages: filteredLanguages,
|
|
107
128
|
readOnly: !1,
|
|
@@ -118,7 +139,7 @@ const getSelectedValue = (select, document) => {
|
|
|
118
139
|
for (const [key, path] of Object.entries(selection)) {
|
|
119
140
|
let value = get__default.default(document, path);
|
|
120
141
|
Array.isArray(value) && (value = value.filter(
|
|
121
|
-
(item) => typeof item == "object" ?
|
|
142
|
+
(item) => typeof item == "object" ? item?._type === "reference" && "_ref" in item : !0
|
|
122
143
|
)), selectedValue[key] = value;
|
|
123
144
|
}
|
|
124
145
|
return selectedValue;
|
|
@@ -131,7 +152,7 @@ function useInternationalizedArrayContext() {
|
|
|
131
152
|
return react.useContext(InternationalizedArrayContext);
|
|
132
153
|
}
|
|
133
154
|
function InternationalizedArrayProvider(props) {
|
|
134
|
-
const { internationalizedArray: internationalizedArray2 } = props, client = sanity.useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = sanity.useWorkspace(), {
|
|
155
|
+
const { internationalizedArray: internationalizedArray2 } = props, client = sanity.useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = sanity.useWorkspace(), { formState } = structure.useDocumentPane(), deferredDocument = react.useDeferredValue(formState?.value), selectedValue = react.useMemo(
|
|
135
156
|
() => getSelectedValue(internationalizedArray2.select, deferredDocument),
|
|
136
157
|
[internationalizedArray2.select, deferredDocument]
|
|
137
158
|
), languages = Array.isArray(internationalizedArray2.languages) ? internationalizedArray2.languages : suspend.suspend(
|
|
@@ -144,27 +165,14 @@ function InternationalizedArrayProvider(props) {
|
|
|
144
165
|
return typeof documentType == "string" && languageFilterOptions.documentTypes.includes(documentType) ? languages.filter(
|
|
145
166
|
(language) => selectedLanguageIds.includes(language.id)
|
|
146
167
|
) : languages;
|
|
147
|
-
}, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document")
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
{
|
|
151
|
-
value: {
|
|
152
|
-
...internationalizedArray2,
|
|
153
|
-
languages,
|
|
154
|
-
filteredLanguages
|
|
155
|
-
},
|
|
156
|
-
children: showDocumentButtons ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 5, children: [
|
|
157
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
158
|
-
DocumentAddButtons,
|
|
159
|
-
{
|
|
160
|
-
schemaType: props.schemaType,
|
|
161
|
-
value: props.value
|
|
162
|
-
}
|
|
163
|
-
),
|
|
164
|
-
props.renderDefault(props)
|
|
165
|
-
] }) : props.renderDefault(props)
|
|
166
|
-
}
|
|
168
|
+
}, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document"), context = react.useMemo(
|
|
169
|
+
() => ({ ...internationalizedArray2, languages, filteredLanguages }),
|
|
170
|
+
[filteredLanguages, internationalizedArray2, languages]
|
|
167
171
|
);
|
|
172
|
+
return /* @__PURE__ */ jsxRuntime.jsx(InternationalizedArrayContext.Provider, { value: context, children: showDocumentButtons ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 5, children: [
|
|
173
|
+
/* @__PURE__ */ jsxRuntime.jsx(DocumentAddButtons, { value: props.value }),
|
|
174
|
+
props.renderDefault(props)
|
|
175
|
+
] }) : props.renderDefault(props) });
|
|
168
176
|
}
|
|
169
177
|
var Preload = react.memo(function(props) {
|
|
170
178
|
const client = sanity.useClient({ apiVersion: props.apiVersion });
|
|
@@ -177,7 +185,10 @@ function checkAllLanguagesArePresent(languages, value) {
|
|
|
177
185
|
return languagesInUseIds.length === filteredLanguageIds.length && languagesInUseIds.every((l) => filteredLanguageIds.includes(l));
|
|
178
186
|
}
|
|
179
187
|
function createAddAllTitle(value, languages) {
|
|
180
|
-
return value
|
|
188
|
+
return value?.length ? `Add missing ${languages.length - value.length === 1 ? "language" : "languages"}` : languages.length === 1 ? `Add ${languages[0].title} Field` : "Add all languages";
|
|
189
|
+
}
|
|
190
|
+
function createValueSchemaTypeName(schemaType) {
|
|
191
|
+
return `${schemaType.name}Value`;
|
|
181
192
|
}
|
|
182
193
|
function createAddLanguagePatches(config) {
|
|
183
194
|
const {
|
|
@@ -187,15 +198,15 @@ function createAddLanguagePatches(config) {
|
|
|
187
198
|
filteredLanguages,
|
|
188
199
|
value,
|
|
189
200
|
path = []
|
|
190
|
-
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? (
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
) : (
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
), languagesInUse = value
|
|
201
|
+
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => ({
|
|
202
|
+
...itemBase,
|
|
203
|
+
_key: id
|
|
204
|
+
})) : filteredLanguages.filter(
|
|
205
|
+
(language) => value?.length ? !value.find((v) => v._key === language.id) : !0
|
|
206
|
+
).map((language) => ({
|
|
207
|
+
...itemBase,
|
|
208
|
+
_key: language.id
|
|
209
|
+
})), languagesInUse = value?.length ? value.map((v) => v) : [];
|
|
199
210
|
return newItems.map((item) => {
|
|
200
211
|
const languageIndex = languages.findIndex((l) => item._key === l.id), remainingLanguages = languages.slice(languageIndex + 1), nextLanguageIndex = languagesInUse.findIndex(
|
|
201
212
|
(l) => (
|
|
@@ -213,7 +224,7 @@ function createAddLanguagePatches(config) {
|
|
|
213
224
|
});
|
|
214
225
|
}
|
|
215
226
|
const createTranslateFieldActions = (fieldActionProps, { languages, filteredLanguages }) => languages.map((language) => {
|
|
216
|
-
const value = sanity.useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!
|
|
227
|
+
const value = sanity.useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!value?.find((item) => item._key === language.id) : !1, hidden = !filteredLanguages.some((f) => f.id === language.id), { onChange } = structure.useDocumentPane(), onAction = react.useCallback(() => {
|
|
217
228
|
const { schemaType, path } = fieldActionProps, addLanguageKeys = [language.id], patches = createAddLanguagePatches({
|
|
218
229
|
addLanguageKeys,
|
|
219
230
|
schemaType,
|
|
@@ -255,8 +266,7 @@ const createTranslateFieldActions = (fieldActionProps, { languages, filteredLang
|
|
|
255
266
|
}, internationalizedArrayFieldAction = sanity.defineDocumentFieldAction({
|
|
256
267
|
name: "internationalizedArray",
|
|
257
268
|
useAction(fieldActionProps) {
|
|
258
|
-
|
|
259
|
-
const isInternationalizedArrayField = (_b = (_a = fieldActionProps == null ? void 0 : fieldActionProps.schemaType) == null ? void 0 : _a.type) == null ? void 0 : _b.name.startsWith(
|
|
269
|
+
const isInternationalizedArrayField = fieldActionProps?.schemaType?.type?.name.startsWith(
|
|
260
270
|
"internationalizedArray"
|
|
261
271
|
), { languages, filteredLanguages } = useInternationalizedArrayContext(), translateFieldActions = createTranslateFieldActions(
|
|
262
272
|
fieldActionProps,
|
|
@@ -332,11 +342,10 @@ function InternationalizedArray(props) {
|
|
|
332
342
|
}) : members,
|
|
333
343
|
[languageFilterEnabled, members, languageFilterOptions, selectedLanguageIds]
|
|
334
344
|
), handleAddLanguage = react.useCallback(
|
|
335
|
-
(param) => {
|
|
336
|
-
|
|
337
|
-
if (!(filteredLanguages != null && filteredLanguages.length))
|
|
345
|
+
async (param) => {
|
|
346
|
+
if (!filteredLanguages?.length)
|
|
338
347
|
return;
|
|
339
|
-
const addLanguageKeys = Array.isArray(param) ? param : [
|
|
348
|
+
const addLanguageKeys = Array.isArray(param) ? param : [param?.currentTarget?.value].filter(Boolean), patches = createAddLanguagePatches({
|
|
340
349
|
addLanguageKeys,
|
|
341
350
|
schemaType,
|
|
342
351
|
languages,
|
|
@@ -354,23 +363,23 @@ function InternationalizedArray(props) {
|
|
|
354
363
|
};
|
|
355
364
|
}, [defaultLanguages, documentCreatedAt, handleAddLanguage, value]);
|
|
356
365
|
const handleRestoreOrder = react.useCallback(() => {
|
|
357
|
-
if (!
|
|
366
|
+
if (!value?.length || !languages?.length)
|
|
358
367
|
return;
|
|
359
368
|
const updatedValue = value.reduce((acc, v) => {
|
|
360
|
-
const newIndex = languages.findIndex((l) => l.id ===
|
|
369
|
+
const newIndex = languages.findIndex((l) => l.id === v?._key);
|
|
361
370
|
return newIndex > -1 && (acc[newIndex] = v), acc;
|
|
362
371
|
}, []).filter(Boolean);
|
|
363
|
-
|
|
372
|
+
value?.length !== updatedValue.length && toast.push({
|
|
364
373
|
title: "There was an error reordering languages",
|
|
365
374
|
status: "warning"
|
|
366
375
|
}), onChange(sanity.set(updatedValue));
|
|
367
|
-
}, [toast, languages, onChange, value]), allKeysAreLanguages = react.useMemo(() => !
|
|
368
|
-
() => languages && languages.length > 1 ? languages.filter((l) => value
|
|
376
|
+
}, [toast, languages, onChange, value]), allKeysAreLanguages = react.useMemo(() => !value?.length || !languages?.length ? !0 : value?.every((v) => languages.find((l) => l?.id === v?._key)), [value, languages]), languagesInUse = react.useMemo(
|
|
377
|
+
() => languages && languages.length > 1 ? languages.filter((l) => value?.find((v) => v._key === l.id)) : [],
|
|
369
378
|
[languages, value]
|
|
370
|
-
), languagesOutOfOrder = react.useMemo(() => !
|
|
379
|
+
), languagesOutOfOrder = react.useMemo(() => !value?.length || !languagesInUse.length ? [] : value.map(
|
|
371
380
|
(v, vIndex) => vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v
|
|
372
381
|
).filter(Boolean), [value, languagesInUse]), languagesAreValid = react.useMemo(
|
|
373
|
-
() => !
|
|
382
|
+
() => !languages?.length || languages?.length && languages.every((item) => item.id && item.title),
|
|
374
383
|
[languages]
|
|
375
384
|
);
|
|
376
385
|
react.useEffect(() => {
|
|
@@ -385,9 +394,9 @@ function InternationalizedArray(props) {
|
|
|
385
394
|
const addButtonsAreVisible = (
|
|
386
395
|
// Plugin was configured to display buttons here (default!)
|
|
387
396
|
buttonLocations.includes("field") && // There's at least one language visible
|
|
388
|
-
|
|
397
|
+
filteredLanguages?.length > 0 && // Not every language has a value yet
|
|
389
398
|
!allLanguagesArePresent
|
|
390
|
-
), fieldHasMembers =
|
|
399
|
+
), fieldHasMembers = members?.length > 0;
|
|
391
400
|
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
|
|
392
401
|
fieldHasMembers ? /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: filteredMembers.map((member) => member.kind === "item" ? /* @__PURE__ */ react.createElement(
|
|
393
402
|
sanity.ArrayOfObjectsItem,
|
|
@@ -400,7 +409,7 @@ function InternationalizedArray(props) {
|
|
|
400
409
|
!addButtonsAreVisible && !fieldHasMembers ? /* @__PURE__ */ jsxRuntime.jsx(ui.Card, { border: !0, tone: "transparent", padding: 3, radius: 2, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: 1, children: "This internationalized field currently has no translations." }) }) : null,
|
|
401
410
|
addButtonsAreVisible ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
|
|
402
411
|
/* @__PURE__ */ jsxRuntime.jsx(
|
|
403
|
-
AddButtons,
|
|
412
|
+
AddButtons$1,
|
|
404
413
|
{
|
|
405
414
|
languages: filteredLanguages,
|
|
406
415
|
value,
|
|
@@ -423,8 +432,7 @@ function InternationalizedArray(props) {
|
|
|
423
432
|
] });
|
|
424
433
|
}
|
|
425
434
|
function getLanguagesFieldOption(schemaType) {
|
|
426
|
-
|
|
427
|
-
return schemaType ? ((_a = schemaType.options) == null ? void 0 : _a.languages) || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
435
|
+
return schemaType ? schemaType.options?.languages || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
428
436
|
}
|
|
429
437
|
var array = (config) => {
|
|
430
438
|
const { apiVersion, select, languages, type } = config, typeName = typeof type == "string" ? type : type.name, arrayName = createFieldName(typeName), objectName = createFieldName(typeName, !0);
|
|
@@ -436,7 +444,12 @@ var array = (config) => {
|
|
|
436
444
|
input: InternationalizedArray
|
|
437
445
|
},
|
|
438
446
|
// These options are required for validation rules – not the custom input component
|
|
439
|
-
options: {
|
|
447
|
+
options: {
|
|
448
|
+
// @ts-expect-error - find out why it fails
|
|
449
|
+
apiVersion,
|
|
450
|
+
select,
|
|
451
|
+
languages
|
|
452
|
+
},
|
|
440
453
|
of: [
|
|
441
454
|
sanity.defineField({
|
|
442
455
|
...typeof type == "string" ? {} : type,
|
|
@@ -444,15 +457,16 @@ var array = (config) => {
|
|
|
444
457
|
type: objectName
|
|
445
458
|
})
|
|
446
459
|
],
|
|
460
|
+
// @ts-expect-error - find out why it fails
|
|
447
461
|
validation: (rule) => rule.custom(async (value, context) => {
|
|
448
462
|
if (!value)
|
|
449
463
|
return !0;
|
|
450
464
|
const selectedValue = getSelectedValue(select, context.document), client = context.getClient({ apiVersion });
|
|
451
465
|
let contextLanguages = [];
|
|
452
|
-
const languagesFieldOption = getLanguagesFieldOption(context
|
|
466
|
+
const languagesFieldOption = getLanguagesFieldOption(context?.type);
|
|
453
467
|
if (Array.isArray(languagesFieldOption) ? contextLanguages = languagesFieldOption : Array.isArray(peek(selectedValue)) ? contextLanguages = peek(selectedValue) || [] : typeof languagesFieldOption == "function" && (contextLanguages = await languagesFieldOption(client, selectedValue)), value && value.length > contextLanguages.length)
|
|
454
468
|
return `Cannot be more than ${contextLanguages.length === 1 ? "1 item" : `${contextLanguages.length} items`}`;
|
|
455
|
-
const nonLanguageKeys = value
|
|
469
|
+
const nonLanguageKeys = value?.length ? value.filter(
|
|
456
470
|
(item) => !contextLanguages.find((language) => item._key === language.id)
|
|
457
471
|
) : [];
|
|
458
472
|
if (nonLanguageKeys.length)
|
|
@@ -460,10 +474,10 @@ var array = (config) => {
|
|
|
460
474
|
message: "Array item keys must be valid languages registered to the field type",
|
|
461
475
|
paths: nonLanguageKeys.map((item) => [{ _key: item._key }])
|
|
462
476
|
};
|
|
463
|
-
const valuesByLanguage = value
|
|
477
|
+
const valuesByLanguage = value?.length ? value.filter((item) => !!item?._key).reduce((acc, cur) => acc[cur._key] ? { ...acc, [cur._key]: [...acc[cur._key], cur] } : {
|
|
464
478
|
...acc,
|
|
465
479
|
[cur._key]: [cur]
|
|
466
|
-
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) =>
|
|
480
|
+
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => item?.length > 1).flat();
|
|
467
481
|
return duplicateValues.length ? {
|
|
468
482
|
message: "There can only be one field per language",
|
|
469
483
|
paths: duplicateValues.map((item) => [{ _key: item._key }])
|
|
@@ -479,7 +493,7 @@ function InternationalizedField(props) {
|
|
|
479
493
|
}) : props.children;
|
|
480
494
|
}
|
|
481
495
|
function getToneFromValidation(validations) {
|
|
482
|
-
if (!
|
|
496
|
+
if (!validations?.length)
|
|
483
497
|
return;
|
|
484
498
|
const validationLevels = validations.map((v) => v.level);
|
|
485
499
|
if (validationLevels.includes("error"))
|
|
@@ -500,16 +514,12 @@ function InternationalizedInput(props) {
|
|
|
500
514
|
// TODO: Remove this as it shouldn't be necessary?
|
|
501
515
|
value: props.value
|
|
502
516
|
}, { validation, value, onChange, readOnly } = inlineProps, { languages } = useInternationalizedArrayContext(), languageKeysInUse = react.useMemo(
|
|
503
|
-
() =>
|
|
504
|
-
var _a;
|
|
505
|
-
return (_a = parentValue == null ? void 0 : parentValue.map((v) => v._key)) != null ? _a : [];
|
|
506
|
-
},
|
|
517
|
+
() => parentValue?.map((v) => v._key) ?? [],
|
|
507
518
|
[parentValue]
|
|
508
|
-
), keyIsValid = languages
|
|
519
|
+
), keyIsValid = languages?.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = react.useCallback(
|
|
509
520
|
(event) => {
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
!value || !(languages != null && languages.length) || !languages.find((l) => l.id === languageId) || onChange([sanity.set(languageId, ["_key"])]);
|
|
521
|
+
const languageId = event?.currentTarget?.value;
|
|
522
|
+
!value || !languages?.length || !languages.find((l) => l.id === languageId) || onChange([sanity.set(languageId, ["_key"])]);
|
|
513
523
|
},
|
|
514
524
|
[onChange, value, languages]
|
|
515
525
|
), handleUnset = react.useCallback(() => {
|
|
@@ -557,9 +567,9 @@ var object = (config) => {
|
|
|
557
567
|
title: `Internationalized array ${type}`,
|
|
558
568
|
type: "object",
|
|
559
569
|
components: {
|
|
570
|
+
// @ts-expect-error - find out why it fails
|
|
560
571
|
item: InternationalizedInput
|
|
561
572
|
},
|
|
562
|
-
// @ts-expect-error - Address this typing issue with the inner object
|
|
563
573
|
fields: [
|
|
564
574
|
typeof type == "string" ? (
|
|
565
575
|
// Define a simple field if all we have is the name as a string
|
|
@@ -645,10 +655,13 @@ const internationalizedArray = sanity.definePlugin((config) => {
|
|
|
645
655
|
(field) => field.type.name
|
|
646
656
|
).some(
|
|
647
657
|
(name) => name.startsWith("internationalizedArray")
|
|
648
|
-
) ? props.renderDefault(props) :
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
658
|
+
) ? props.renderDefault(props) : /* @__PURE__ */ jsxRuntime.jsx(
|
|
659
|
+
InternationalizedArrayProvider,
|
|
660
|
+
{
|
|
661
|
+
...props,
|
|
662
|
+
internationalizedArray: pluginConfig
|
|
663
|
+
}
|
|
664
|
+
)
|
|
652
665
|
}
|
|
653
666
|
},
|
|
654
667
|
// Register custom schema types for the outer array and the inner object
|