sanity-plugin-internationalized-array 3.0.1 → 3.0.3
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 +148 -72
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +148 -72
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +148 -72
- package/lib/index.mjs.map +1 -1
- package/package.json +7 -9
- package/src/utils/flattenSchemaType.ts +7 -5
package/lib/index.esm.js
CHANGED
|
@@ -17,18 +17,26 @@ const namespace = "sanity-plugin-internationalized-array", version = "v1", prelo
|
|
|
17
17
|
apiVersion: "2022-11-27",
|
|
18
18
|
buttonLocations: ["field"],
|
|
19
19
|
buttonAddAll: !0
|
|
20
|
-
}
|
|
20
|
+
};
|
|
21
|
+
var __defProp$9 = Object.defineProperty, __defProps$8 = Object.defineProperties, __getOwnPropDescs$8 = Object.getOwnPropertyDescriptors, __getOwnPropSymbols$9 = Object.getOwnPropertySymbols, __hasOwnProp$9 = Object.prototype.hasOwnProperty, __propIsEnum$9 = Object.prototype.propertyIsEnumerable, __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$9 = (a, b) => {
|
|
22
|
+
for (var prop in b || (b = {}))
|
|
23
|
+
__hasOwnProp$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
24
|
+
if (__getOwnPropSymbols$9)
|
|
25
|
+
for (var prop of __getOwnPropSymbols$9(b))
|
|
26
|
+
__propIsEnum$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
27
|
+
return a;
|
|
28
|
+
}, __spreadProps$8 = (a, b) => __defProps$8(a, __getOwnPropDescs$8(b));
|
|
29
|
+
const getDocumentsToTranslate = (value, rootPath = []) => {
|
|
21
30
|
if (Array.isArray(value)) {
|
|
22
31
|
const arrayRootPath = [...rootPath], internationalizedValues = value.filter((item) => {
|
|
23
32
|
if (Array.isArray(item)) return !1;
|
|
24
33
|
if (typeof item == "object") {
|
|
25
|
-
const type = item
|
|
26
|
-
return type
|
|
34
|
+
const type = item == null ? void 0 : item._type;
|
|
35
|
+
return (type == null ? void 0 : type.startsWith("internationalizedArray")) && (type == null ? void 0 : type.endsWith("Value"));
|
|
27
36
|
}
|
|
28
37
|
return !1;
|
|
29
38
|
});
|
|
30
|
-
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => ({
|
|
31
|
-
...internationalizedValue,
|
|
39
|
+
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => __spreadProps$8(__spreadValues$9({}, internationalizedValue), {
|
|
32
40
|
path: arrayRootPath,
|
|
33
41
|
pathString: arrayRootPath.join(".")
|
|
34
42
|
})) : value.length > 0 ? value.map(
|
|
@@ -54,7 +62,7 @@ function AddButtons(props) {
|
|
|
54
62
|
tone: "primary",
|
|
55
63
|
mode: "ghost",
|
|
56
64
|
fontSize: 1,
|
|
57
|
-
disabled: readOnly || !!value
|
|
65
|
+
disabled: readOnly || !!(value != null && value.find((item) => item._key === language.id)),
|
|
58
66
|
text: language.id.toUpperCase(),
|
|
59
67
|
icon: languages.length > MAX_COLUMNS ? void 0 : AddIcon,
|
|
60
68
|
value: language.id,
|
|
@@ -76,7 +84,7 @@ function DocumentAddButtons(props) {
|
|
|
76
84
|
return;
|
|
77
85
|
}
|
|
78
86
|
const alreadyTranslated = documentsToTranslation.filter(
|
|
79
|
-
(translation) => translation
|
|
87
|
+
(translation) => (translation == null ? void 0 : translation._key) === languageId
|
|
80
88
|
), removeDuplicates = documentsToTranslation.reduce((filteredTranslations, translation) => alreadyTranslated.filter(
|
|
81
89
|
(alreadyTranslation) => alreadyTranslation.pathString === translation.pathString
|
|
82
90
|
).length > 0 || filteredTranslations.filter(
|
|
@@ -128,20 +136,28 @@ const getSelectedValue = (select, document) => {
|
|
|
128
136
|
for (const [key, path] of Object.entries(selection)) {
|
|
129
137
|
let value = get(document, path);
|
|
130
138
|
Array.isArray(value) && (value = value.filter(
|
|
131
|
-
(item) => typeof item == "object" ? item
|
|
139
|
+
(item) => typeof item == "object" ? (item == null ? void 0 : item._type) === "reference" && "_ref" in item : !0
|
|
132
140
|
)), selectedValue[key] = value;
|
|
133
141
|
}
|
|
134
142
|
return selectedValue;
|
|
135
|
-
}
|
|
136
|
-
|
|
143
|
+
};
|
|
144
|
+
var __defProp$8 = Object.defineProperty, __defProps$7 = Object.defineProperties, __getOwnPropDescs$7 = Object.getOwnPropertyDescriptors, __getOwnPropSymbols$8 = Object.getOwnPropertySymbols, __hasOwnProp$8 = Object.prototype.hasOwnProperty, __propIsEnum$8 = Object.prototype.propertyIsEnumerable, __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$8 = (a, b) => {
|
|
145
|
+
for (var prop in b || (b = {}))
|
|
146
|
+
__hasOwnProp$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
147
|
+
if (__getOwnPropSymbols$8)
|
|
148
|
+
for (var prop of __getOwnPropSymbols$8(b))
|
|
149
|
+
__propIsEnum$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
150
|
+
return a;
|
|
151
|
+
}, __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
|
|
152
|
+
const InternationalizedArrayContext = createContext(__spreadProps$7(__spreadValues$8({}, CONFIG_DEFAULT), {
|
|
137
153
|
languages: [],
|
|
138
154
|
filteredLanguages: []
|
|
139
|
-
});
|
|
155
|
+
}));
|
|
140
156
|
function useInternationalizedArrayContext() {
|
|
141
157
|
return useContext(InternationalizedArrayContext);
|
|
142
158
|
}
|
|
143
159
|
function InternationalizedArrayProvider(props) {
|
|
144
|
-
const { internationalizedArray: internationalizedArray2 } = props, client = useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = useWorkspace(), { formState } = useDocumentPane(), deferredDocument = useDeferredValue(formState
|
|
160
|
+
const { internationalizedArray: internationalizedArray2 } = props, client = useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = useWorkspace(), { formState } = useDocumentPane(), deferredDocument = useDeferredValue(formState == null ? void 0 : formState.value), selectedValue = useMemo(
|
|
145
161
|
() => getSelectedValue(internationalizedArray2.select, deferredDocument),
|
|
146
162
|
[internationalizedArray2.select, deferredDocument]
|
|
147
163
|
), languages = Array.isArray(internationalizedArray2.languages) ? internationalizedArray2.languages : suspend$1(
|
|
@@ -155,7 +171,7 @@ function InternationalizedArrayProvider(props) {
|
|
|
155
171
|
(language) => selectedLanguageIds.includes(language.id)
|
|
156
172
|
) : languages;
|
|
157
173
|
}, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document"), context = useMemo(
|
|
158
|
-
() => ({
|
|
174
|
+
() => __spreadProps$7(__spreadValues$8({}, internationalizedArray2), { languages, filteredLanguages }),
|
|
159
175
|
[filteredLanguages, internationalizedArray2, languages]
|
|
160
176
|
);
|
|
161
177
|
return /* @__PURE__ */ jsx(InternationalizedArrayContext.Provider, { value: context, children: showDocumentButtons ? /* @__PURE__ */ jsxs(Stack, { space: 5, children: [
|
|
@@ -174,11 +190,19 @@ function checkAllLanguagesArePresent(languages, value) {
|
|
|
174
190
|
return languagesInUseIds.length === filteredLanguageIds.length && languagesInUseIds.every((l) => filteredLanguageIds.includes(l));
|
|
175
191
|
}
|
|
176
192
|
function createAddAllTitle(value, languages) {
|
|
177
|
-
return value
|
|
193
|
+
return value != null && value.length ? `Add missing ${languages.length - value.length === 1 ? "language" : "languages"}` : languages.length === 1 ? `Add ${languages[0].title} Field` : "Add all languages";
|
|
178
194
|
}
|
|
179
195
|
function createValueSchemaTypeName(schemaType) {
|
|
180
196
|
return `${schemaType.name}Value`;
|
|
181
197
|
}
|
|
198
|
+
var __defProp$7 = Object.defineProperty, __defProps$6 = Object.defineProperties, __getOwnPropDescs$6 = Object.getOwnPropertyDescriptors, __getOwnPropSymbols$7 = Object.getOwnPropertySymbols, __hasOwnProp$7 = Object.prototype.hasOwnProperty, __propIsEnum$7 = Object.prototype.propertyIsEnumerable, __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$7 = (a, b) => {
|
|
199
|
+
for (var prop in b || (b = {}))
|
|
200
|
+
__hasOwnProp$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
201
|
+
if (__getOwnPropSymbols$7)
|
|
202
|
+
for (var prop of __getOwnPropSymbols$7(b))
|
|
203
|
+
__propIsEnum$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
204
|
+
return a;
|
|
205
|
+
}, __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
|
|
182
206
|
function createAddLanguagePatches(config) {
|
|
183
207
|
const {
|
|
184
208
|
addLanguageKeys,
|
|
@@ -187,15 +211,13 @@ function createAddLanguagePatches(config) {
|
|
|
187
211
|
filteredLanguages,
|
|
188
212
|
value,
|
|
189
213
|
path = []
|
|
190
|
-
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => ({
|
|
191
|
-
...itemBase,
|
|
214
|
+
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => __spreadProps$6(__spreadValues$7({}, itemBase), {
|
|
192
215
|
_key: id
|
|
193
216
|
})) : filteredLanguages.filter(
|
|
194
|
-
(language) => value
|
|
195
|
-
).map((language) => ({
|
|
196
|
-
...itemBase,
|
|
217
|
+
(language) => value != null && value.length ? !value.find((v) => v._key === language.id) : !0
|
|
218
|
+
).map((language) => __spreadProps$6(__spreadValues$7({}, itemBase), {
|
|
197
219
|
_key: language.id
|
|
198
|
-
})), languagesInUse = value
|
|
220
|
+
})), languagesInUse = value != null && value.length ? value.map((v) => v) : [];
|
|
199
221
|
return newItems.map((item) => {
|
|
200
222
|
const languageIndex = languages.findIndex((l) => item._key === l.id), remainingLanguages = languages.slice(languageIndex + 1), nextLanguageIndex = languagesInUse.findIndex(
|
|
201
223
|
(l) => (
|
|
@@ -213,7 +235,7 @@ function createAddLanguagePatches(config) {
|
|
|
213
235
|
});
|
|
214
236
|
}
|
|
215
237
|
const createTranslateFieldActions = (fieldActionProps, { languages, filteredLanguages }) => languages.map((language) => {
|
|
216
|
-
const value = useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!value
|
|
238
|
+
const value = useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!(value != null && value.find((item) => item._key === language.id)) : !1, hidden = !filteredLanguages.some((f) => f.id === language.id), { onChange } = useDocumentPane(), onAction = useCallback(() => {
|
|
217
239
|
const { schemaType, path } = fieldActionProps, addLanguageKeys = [language.id], patches = createAddLanguagePatches({
|
|
218
240
|
addLanguageKeys,
|
|
219
241
|
schemaType,
|
|
@@ -255,7 +277,8 @@ const createTranslateFieldActions = (fieldActionProps, { languages, filteredLang
|
|
|
255
277
|
}, internationalizedArrayFieldAction = defineDocumentFieldAction({
|
|
256
278
|
name: "internationalizedArray",
|
|
257
279
|
useAction(fieldActionProps) {
|
|
258
|
-
|
|
280
|
+
var _a, _b;
|
|
281
|
+
const isInternationalizedArrayField = (_b = (_a = fieldActionProps == null ? void 0 : fieldActionProps.schemaType) == null ? void 0 : _a.type) == null ? void 0 : _b.name.startsWith(
|
|
259
282
|
"internationalizedArray"
|
|
260
283
|
), { languages, filteredLanguages } = useInternationalizedArrayContext(), translateFieldActions = createTranslateFieldActions(
|
|
261
284
|
fieldActionProps,
|
|
@@ -311,6 +334,14 @@ function Feedback() {
|
|
|
311
334
|
/* @__PURE__ */ jsx(Card, { padding: 2, border: !0, radius: 2, children: /* @__PURE__ */ jsx(Code, { size: 1, language: "javascript", children: JSON.stringify(schemaExample, null, 2) }) })
|
|
312
335
|
] }) });
|
|
313
336
|
}
|
|
337
|
+
var __defProp$6 = Object.defineProperty, __defProps$5 = Object.defineProperties, __getOwnPropDescs$5 = Object.getOwnPropertyDescriptors, __getOwnPropSymbols$6 = Object.getOwnPropertySymbols, __hasOwnProp$6 = Object.prototype.hasOwnProperty, __propIsEnum$6 = Object.prototype.propertyIsEnumerable, __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$6 = (a, b) => {
|
|
338
|
+
for (var prop in b || (b = {}))
|
|
339
|
+
__hasOwnProp$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
340
|
+
if (__getOwnPropSymbols$6)
|
|
341
|
+
for (var prop of __getOwnPropSymbols$6(b))
|
|
342
|
+
__propIsEnum$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
343
|
+
return a;
|
|
344
|
+
}, __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
|
|
314
345
|
function InternationalizedArray(props) {
|
|
315
346
|
const { members, value, schemaType, onChange } = props, readOnly = typeof schemaType.readOnly == "boolean" ? schemaType.readOnly : !1, toast = useToast(), {
|
|
316
347
|
languages,
|
|
@@ -332,9 +363,10 @@ function InternationalizedArray(props) {
|
|
|
332
363
|
[languageFilterEnabled, members, languageFilterOptions, selectedLanguageIds]
|
|
333
364
|
), handleAddLanguage = useCallback(
|
|
334
365
|
async (param) => {
|
|
335
|
-
|
|
366
|
+
var _a;
|
|
367
|
+
if (!(filteredLanguages != null && filteredLanguages.length))
|
|
336
368
|
return;
|
|
337
|
-
const addLanguageKeys = Array.isArray(param) ? param : [param
|
|
369
|
+
const addLanguageKeys = Array.isArray(param) ? param : [(_a = param == null ? void 0 : param.currentTarget) == null ? void 0 : _a.value].filter(Boolean), patches = createAddLanguagePatches({
|
|
338
370
|
addLanguageKeys,
|
|
339
371
|
schemaType,
|
|
340
372
|
languages,
|
|
@@ -356,23 +388,23 @@ function InternationalizedArray(props) {
|
|
|
356
388
|
defaultLanguages
|
|
357
389
|
]);
|
|
358
390
|
const handleRestoreOrder = useCallback(() => {
|
|
359
|
-
if (!value
|
|
391
|
+
if (!(value != null && value.length) || !(languages != null && languages.length))
|
|
360
392
|
return;
|
|
361
393
|
const updatedValue = value.reduce((acc, v) => {
|
|
362
|
-
const newIndex = languages.findIndex((l) => l.id === v
|
|
394
|
+
const newIndex = languages.findIndex((l) => l.id === (v == null ? void 0 : v._key));
|
|
363
395
|
return newIndex > -1 && (acc[newIndex] = v), acc;
|
|
364
396
|
}, []).filter(Boolean);
|
|
365
|
-
value
|
|
397
|
+
(value == null ? void 0 : value.length) !== updatedValue.length && toast.push({
|
|
366
398
|
title: "There was an error reordering languages",
|
|
367
399
|
status: "warning"
|
|
368
400
|
}), onChange(set(updatedValue));
|
|
369
|
-
}, [toast, languages, onChange, value]), allKeysAreLanguages = useMemo(() => !value
|
|
370
|
-
() => languages && languages.length > 1 ? languages.filter((l) => value
|
|
401
|
+
}, [toast, languages, onChange, value]), allKeysAreLanguages = useMemo(() => !(value != null && value.length) || !(languages != null && languages.length) ? !0 : value == null ? void 0 : value.every((v) => languages.find((l) => (l == null ? void 0 : l.id) === (v == null ? void 0 : v._key))), [value, languages]), languagesInUse = useMemo(
|
|
402
|
+
() => languages && languages.length > 1 ? languages.filter((l) => value == null ? void 0 : value.find((v) => v._key === l.id)) : [],
|
|
371
403
|
[languages, value]
|
|
372
|
-
), languagesOutOfOrder = useMemo(() => !value
|
|
404
|
+
), languagesOutOfOrder = useMemo(() => !(value != null && value.length) || !languagesInUse.length ? [] : value.map(
|
|
373
405
|
(v, vIndex) => vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v
|
|
374
406
|
).filter(Boolean), [value, languagesInUse]), languagesAreValid = useMemo(
|
|
375
|
-
() => !languages
|
|
407
|
+
() => !(languages != null && languages.length) || (languages == null ? void 0 : languages.length) && languages.every((item) => item.id && item.title),
|
|
376
408
|
[languages]
|
|
377
409
|
);
|
|
378
410
|
useEffect(() => {
|
|
@@ -387,17 +419,16 @@ function InternationalizedArray(props) {
|
|
|
387
419
|
const addButtonsAreVisible = (
|
|
388
420
|
// Plugin was configured to display buttons here (default!)
|
|
389
421
|
buttonLocations.includes("field") && // There's at least one language visible
|
|
390
|
-
filteredLanguages
|
|
422
|
+
(filteredLanguages == null ? void 0 : filteredLanguages.length) > 0 && // Not every language has a value yet
|
|
391
423
|
!allLanguagesArePresent
|
|
392
|
-
), fieldHasMembers = members
|
|
424
|
+
), fieldHasMembers = (members == null ? void 0 : members.length) > 0;
|
|
393
425
|
return /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
394
426
|
fieldHasMembers ? /* @__PURE__ */ jsx(Fragment, { children: filteredMembers.map((member) => member.kind === "item" ? /* @__PURE__ */ createElement(
|
|
395
427
|
ArrayOfObjectsItem,
|
|
396
|
-
{
|
|
397
|
-
...props,
|
|
428
|
+
__spreadProps$5(__spreadValues$6({}, props), {
|
|
398
429
|
key: member.key,
|
|
399
430
|
member
|
|
400
|
-
}
|
|
431
|
+
})
|
|
401
432
|
) : /* @__PURE__ */ jsx(MemberItemError, { member }, member.key)) }) : null,
|
|
402
433
|
!addButtonsAreVisible && !fieldHasMembers ? /* @__PURE__ */ jsx(Card, { border: !0, tone: "transparent", padding: 3, radius: 2, children: /* @__PURE__ */ jsx(Text, { size: 1, children: "This internationalized field currently has no translations." }) }) : null,
|
|
403
434
|
addButtonsAreVisible ? /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
@@ -425,9 +456,17 @@ function InternationalizedArray(props) {
|
|
|
425
456
|
] });
|
|
426
457
|
}
|
|
427
458
|
function getLanguagesFieldOption(schemaType) {
|
|
428
|
-
|
|
459
|
+
var _a;
|
|
460
|
+
return schemaType ? ((_a = schemaType.options) == null ? void 0 : _a.languages) || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
429
461
|
}
|
|
430
|
-
var
|
|
462
|
+
var __defProp$5 = Object.defineProperty, __defProps$4 = Object.defineProperties, __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors, __getOwnPropSymbols$5 = Object.getOwnPropertySymbols, __hasOwnProp$5 = Object.prototype.hasOwnProperty, __propIsEnum$5 = Object.prototype.propertyIsEnumerable, __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$5 = (a, b) => {
|
|
463
|
+
for (var prop in b || (b = {}))
|
|
464
|
+
__hasOwnProp$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
465
|
+
if (__getOwnPropSymbols$5)
|
|
466
|
+
for (var prop of __getOwnPropSymbols$5(b))
|
|
467
|
+
__propIsEnum$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
468
|
+
return a;
|
|
469
|
+
}, __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b)), array = (config) => {
|
|
431
470
|
const { apiVersion, select, languages, type } = config, typeName = typeof type == "string" ? type : type.name, arrayName = createFieldName(typeName), objectName = createFieldName(typeName, !0);
|
|
432
471
|
return defineField({
|
|
433
472
|
name: arrayName,
|
|
@@ -443,11 +482,10 @@ var array = (config) => {
|
|
|
443
482
|
languages
|
|
444
483
|
},
|
|
445
484
|
of: [
|
|
446
|
-
defineField({
|
|
447
|
-
...typeof type == "string" ? {} : type,
|
|
485
|
+
defineField(__spreadProps$4(__spreadValues$5({}, typeof type == "string" ? {} : type), {
|
|
448
486
|
name: objectName,
|
|
449
487
|
type: objectName
|
|
450
|
-
})
|
|
488
|
+
}))
|
|
451
489
|
],
|
|
452
490
|
// @ts-expect-error - fix typings
|
|
453
491
|
validation: (rule) => rule.custom(async (value, context) => {
|
|
@@ -455,10 +493,10 @@ var array = (config) => {
|
|
|
455
493
|
return !0;
|
|
456
494
|
const selectedValue = getSelectedValue(select, context.document), client = context.getClient({ apiVersion });
|
|
457
495
|
let contextLanguages = [];
|
|
458
|
-
const languagesFieldOption = getLanguagesFieldOption(context
|
|
496
|
+
const languagesFieldOption = getLanguagesFieldOption(context == null ? void 0 : context.type);
|
|
459
497
|
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)
|
|
460
498
|
return `Cannot be more than ${contextLanguages.length === 1 ? "1 item" : `${contextLanguages.length} items`}`;
|
|
461
|
-
const nonLanguageKeys = value
|
|
499
|
+
const nonLanguageKeys = value != null && value.length ? value.filter(
|
|
462
500
|
(item) => !contextLanguages.find((language) => item._key === language.id)
|
|
463
501
|
) : [];
|
|
464
502
|
if (nonLanguageKeys.length)
|
|
@@ -466,26 +504,31 @@ var array = (config) => {
|
|
|
466
504
|
message: "Array item keys must be valid languages registered to the field type",
|
|
467
505
|
paths: nonLanguageKeys.map((item) => [{ _key: item._key }])
|
|
468
506
|
};
|
|
469
|
-
const valuesByLanguage = value
|
|
470
|
-
...acc,
|
|
507
|
+
const valuesByLanguage = value != null && value.length ? value.filter((item) => !!(item != null && item._key)).reduce((acc, cur) => acc[cur._key] ? __spreadProps$4(__spreadValues$5({}, acc), { [cur._key]: [...acc[cur._key], cur] }) : __spreadProps$4(__spreadValues$5({}, acc), {
|
|
471
508
|
[cur._key]: [cur]
|
|
472
|
-
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => item
|
|
509
|
+
}), {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => (item == null ? void 0 : item.length) > 1).flat();
|
|
473
510
|
return duplicateValues.length ? {
|
|
474
511
|
message: "There can only be one field per language",
|
|
475
512
|
paths: duplicateValues.map((item) => [{ _key: item._key }])
|
|
476
513
|
} : !0;
|
|
477
514
|
})
|
|
478
515
|
});
|
|
479
|
-
}
|
|
516
|
+
}, __defProp$4 = Object.defineProperty, __defProps$3 = Object.defineProperties, __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors, __getOwnPropSymbols$4 = Object.getOwnPropertySymbols, __hasOwnProp$4 = Object.prototype.hasOwnProperty, __propIsEnum$4 = Object.prototype.propertyIsEnumerable, __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$4 = (a, b) => {
|
|
517
|
+
for (var prop in b || (b = {}))
|
|
518
|
+
__hasOwnProp$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
519
|
+
if (__getOwnPropSymbols$4)
|
|
520
|
+
for (var prop of __getOwnPropSymbols$4(b))
|
|
521
|
+
__propIsEnum$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
522
|
+
return a;
|
|
523
|
+
}, __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
480
524
|
function InternationalizedField(props) {
|
|
481
|
-
return props.schemaType.name === "reference" && props.value ? props.renderDefault({
|
|
482
|
-
...props,
|
|
525
|
+
return props.schemaType.name === "reference" && props.value ? props.renderDefault(__spreadProps$3(__spreadValues$4({}, props), {
|
|
483
526
|
title: "",
|
|
484
527
|
level: 0
|
|
485
|
-
}) : props.children;
|
|
528
|
+
})) : props.children;
|
|
486
529
|
}
|
|
487
530
|
function getToneFromValidation(validations) {
|
|
488
|
-
if (!validations
|
|
531
|
+
if (!(validations != null && validations.length))
|
|
489
532
|
return;
|
|
490
533
|
const validationLevels = validations.map((v) => v.level);
|
|
491
534
|
if (validationLevels.includes("error"))
|
|
@@ -493,11 +536,18 @@ function getToneFromValidation(validations) {
|
|
|
493
536
|
if (validationLevels.includes("warning"))
|
|
494
537
|
return "caution";
|
|
495
538
|
}
|
|
539
|
+
var __defProp$3 = Object.defineProperty, __defProps$2 = Object.defineProperties, __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors, __getOwnPropSymbols$3 = Object.getOwnPropertySymbols, __hasOwnProp$3 = Object.prototype.hasOwnProperty, __propIsEnum$3 = Object.prototype.propertyIsEnumerable, __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$3 = (a, b) => {
|
|
540
|
+
for (var prop in b || (b = {}))
|
|
541
|
+
__hasOwnProp$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
542
|
+
if (__getOwnPropSymbols$3)
|
|
543
|
+
for (var prop of __getOwnPropSymbols$3(b))
|
|
544
|
+
__propIsEnum$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
545
|
+
return a;
|
|
546
|
+
}, __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
496
547
|
function InternationalizedInput(props) {
|
|
497
548
|
const parentValue = useFormValue(
|
|
498
549
|
props.path.slice(0, -1)
|
|
499
|
-
), inlineProps = {
|
|
500
|
-
...props.inputProps,
|
|
550
|
+
), inlineProps = __spreadProps$2(__spreadValues$3({}, props.inputProps), {
|
|
501
551
|
// This is the magic that makes inline editing work?
|
|
502
552
|
members: props.inputProps.members.filter(
|
|
503
553
|
(m) => m.kind === "field" && m.name === "value"
|
|
@@ -505,13 +555,17 @@ function InternationalizedInput(props) {
|
|
|
505
555
|
// This just overrides the type
|
|
506
556
|
// TODO: Remove this as it shouldn't be necessary?
|
|
507
557
|
value: props.value
|
|
508
|
-
}, { validation, value, onChange, readOnly } = inlineProps, { languages } = useInternationalizedArrayContext(), languageKeysInUse = useMemo(
|
|
509
|
-
() =>
|
|
558
|
+
}), { validation, value, onChange, readOnly } = inlineProps, { languages } = useInternationalizedArrayContext(), languageKeysInUse = useMemo(
|
|
559
|
+
() => {
|
|
560
|
+
var _a;
|
|
561
|
+
return (_a = parentValue == null ? void 0 : parentValue.map((v) => v._key)) != null ? _a : [];
|
|
562
|
+
},
|
|
510
563
|
[parentValue]
|
|
511
|
-
), keyIsValid = languages
|
|
564
|
+
), keyIsValid = languages != null && languages.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = useCallback(
|
|
512
565
|
(event) => {
|
|
513
|
-
|
|
514
|
-
|
|
566
|
+
var _a;
|
|
567
|
+
const languageId = (_a = event == null ? void 0 : event.currentTarget) == null ? void 0 : _a.value;
|
|
568
|
+
!value || !(languages != null && languages.length) || !languages.find((l) => l.id === languageId) || onChange([set(languageId, ["_key"])]);
|
|
515
569
|
},
|
|
516
570
|
[onChange, value, languages]
|
|
517
571
|
), handleUnset = useCallback(() => {
|
|
@@ -552,7 +606,14 @@ function InternationalizedInput(props) {
|
|
|
552
606
|
] })
|
|
553
607
|
] }) }) : /* @__PURE__ */ jsx(Spinner, {});
|
|
554
608
|
}
|
|
555
|
-
var
|
|
609
|
+
var __defProp$2 = Object.defineProperty, __defProps$1 = Object.defineProperties, __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors, __getOwnPropSymbols$2 = Object.getOwnPropertySymbols, __hasOwnProp$2 = Object.prototype.hasOwnProperty, __propIsEnum$2 = Object.prototype.propertyIsEnumerable, __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$2 = (a, b) => {
|
|
610
|
+
for (var prop in b || (b = {}))
|
|
611
|
+
__hasOwnProp$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
612
|
+
if (__getOwnPropSymbols$2)
|
|
613
|
+
for (var prop of __getOwnPropSymbols$2(b))
|
|
614
|
+
__propIsEnum$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
615
|
+
return a;
|
|
616
|
+
}, __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b)), object = (config) => {
|
|
556
617
|
const { type } = config, typeName = typeof type == "string" ? type : type.name, objectName = createFieldName(typeName, !0);
|
|
557
618
|
return defineField({
|
|
558
619
|
name: objectName,
|
|
@@ -574,13 +635,12 @@ var object = (config) => {
|
|
|
574
635
|
})
|
|
575
636
|
) : (
|
|
576
637
|
// Pass in the configured options, but overwrite the name
|
|
577
|
-
{
|
|
578
|
-
...type,
|
|
638
|
+
__spreadProps$1(__spreadValues$2({}, type), {
|
|
579
639
|
name: "value",
|
|
580
640
|
components: {
|
|
581
641
|
field: InternationalizedField
|
|
582
642
|
}
|
|
583
|
-
}
|
|
643
|
+
})
|
|
584
644
|
)
|
|
585
645
|
],
|
|
586
646
|
preview: {
|
|
@@ -590,13 +650,20 @@ var object = (config) => {
|
|
|
590
650
|
}
|
|
591
651
|
}
|
|
592
652
|
});
|
|
653
|
+
}, __defProp$1 = Object.defineProperty, __getOwnPropSymbols$1 = Object.getOwnPropertySymbols, __hasOwnProp$1 = Object.prototype.hasOwnProperty, __propIsEnum$1 = Object.prototype.propertyIsEnumerable, __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues$1 = (a, b) => {
|
|
654
|
+
for (var prop in b || (b = {}))
|
|
655
|
+
__hasOwnProp$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
656
|
+
if (__getOwnPropSymbols$1)
|
|
657
|
+
for (var prop of __getOwnPropSymbols$1(b))
|
|
658
|
+
__propIsEnum$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
659
|
+
return a;
|
|
593
660
|
};
|
|
594
661
|
function flattenSchemaType(schemaType) {
|
|
595
662
|
return isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 3) : (console.error("Schema type is not a document"), []);
|
|
596
663
|
}
|
|
597
664
|
function extractInnerFields(fields, path, maxDepth) {
|
|
598
665
|
return path.length >= maxDepth ? [] : fields.reduce((acc, field) => {
|
|
599
|
-
const thisFieldWithPath = { path: [...path, field.name],
|
|
666
|
+
const thisFieldWithPath = __spreadValues$1({ path: [...path, field.name] }, field);
|
|
600
667
|
if (field.type.jsonType === "object") {
|
|
601
668
|
const innerFields = extractInnerFields(
|
|
602
669
|
field.type.fields,
|
|
@@ -605,19 +672,29 @@ function extractInnerFields(fields, path, maxDepth) {
|
|
|
605
672
|
);
|
|
606
673
|
return [...acc, thisFieldWithPath, ...innerFields];
|
|
607
674
|
} else if (field.type.jsonType === "array" && field.type.of.length && field.type.of.some((item) => "fields" in item)) {
|
|
608
|
-
const innerFields =
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
675
|
+
const innerFields = field.type.of.flatMap(
|
|
676
|
+
(innerField) => extractInnerFields(
|
|
677
|
+
// @ts-expect-error - Fix TS assertion for array fields
|
|
678
|
+
innerField.fields,
|
|
679
|
+
[...path, field.name],
|
|
680
|
+
maxDepth
|
|
681
|
+
)
|
|
613
682
|
);
|
|
614
683
|
return [...acc, thisFieldWithPath, ...innerFields];
|
|
615
684
|
}
|
|
616
685
|
return [...acc, thisFieldWithPath];
|
|
617
686
|
}, []);
|
|
618
687
|
}
|
|
688
|
+
var __defProp = Object.defineProperty, __defProps = Object.defineProperties, __getOwnPropDescs = Object.getOwnPropertyDescriptors, __getOwnPropSymbols = Object.getOwnPropertySymbols, __hasOwnProp = Object.prototype.hasOwnProperty, __propIsEnum = Object.prototype.propertyIsEnumerable, __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, __spreadValues = (a, b) => {
|
|
689
|
+
for (var prop in b || (b = {}))
|
|
690
|
+
__hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
691
|
+
if (__getOwnPropSymbols)
|
|
692
|
+
for (var prop of __getOwnPropSymbols(b))
|
|
693
|
+
__propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
694
|
+
return a;
|
|
695
|
+
}, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
619
696
|
const internationalizedArray = definePlugin((config) => {
|
|
620
|
-
const pluginConfig = {
|
|
697
|
+
const pluginConfig = __spreadValues(__spreadValues({}, CONFIG_DEFAULT), config), {
|
|
621
698
|
apiVersion = "2022-11-27",
|
|
622
699
|
select,
|
|
623
700
|
languages,
|
|
@@ -649,10 +726,9 @@ const internationalizedArray = definePlugin((config) => {
|
|
|
649
726
|
(name) => name.startsWith("internationalizedArray")
|
|
650
727
|
) ? props.renderDefault(props) : /* @__PURE__ */ jsx(
|
|
651
728
|
InternationalizedArrayProvider,
|
|
652
|
-
{
|
|
653
|
-
...props,
|
|
729
|
+
__spreadProps(__spreadValues({}, props), {
|
|
654
730
|
internationalizedArray: pluginConfig
|
|
655
|
-
}
|
|
731
|
+
})
|
|
656
732
|
)
|
|
657
733
|
}
|
|
658
734
|
},
|