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.js
CHANGED
|
@@ -28,18 +28,26 @@ const namespace = "sanity-plugin-internationalized-array", version = "v1", prelo
|
|
|
28
28
|
apiVersion: "2022-11-27",
|
|
29
29
|
buttonLocations: ["field"],
|
|
30
30
|
buttonAddAll: !0
|
|
31
|
-
}
|
|
31
|
+
};
|
|
32
|
+
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) => {
|
|
33
|
+
for (var prop in b || (b = {}))
|
|
34
|
+
__hasOwnProp$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
35
|
+
if (__getOwnPropSymbols$9)
|
|
36
|
+
for (var prop of __getOwnPropSymbols$9(b))
|
|
37
|
+
__propIsEnum$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
38
|
+
return a;
|
|
39
|
+
}, __spreadProps$8 = (a, b) => __defProps$8(a, __getOwnPropDescs$8(b));
|
|
40
|
+
const getDocumentsToTranslate = (value, rootPath = []) => {
|
|
32
41
|
if (Array.isArray(value)) {
|
|
33
42
|
const arrayRootPath = [...rootPath], internationalizedValues = value.filter((item) => {
|
|
34
43
|
if (Array.isArray(item)) return !1;
|
|
35
44
|
if (typeof item == "object") {
|
|
36
|
-
const type = item
|
|
37
|
-
return type
|
|
45
|
+
const type = item == null ? void 0 : item._type;
|
|
46
|
+
return (type == null ? void 0 : type.startsWith("internationalizedArray")) && (type == null ? void 0 : type.endsWith("Value"));
|
|
38
47
|
}
|
|
39
48
|
return !1;
|
|
40
49
|
});
|
|
41
|
-
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => ({
|
|
42
|
-
...internationalizedValue,
|
|
50
|
+
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => __spreadProps$8(__spreadValues$9({}, internationalizedValue), {
|
|
43
51
|
path: arrayRootPath,
|
|
44
52
|
pathString: arrayRootPath.join(".")
|
|
45
53
|
})) : value.length > 0 ? value.map(
|
|
@@ -65,7 +73,7 @@ function AddButtons(props) {
|
|
|
65
73
|
tone: "primary",
|
|
66
74
|
mode: "ghost",
|
|
67
75
|
fontSize: 1,
|
|
68
|
-
disabled: readOnly || !!value
|
|
76
|
+
disabled: readOnly || !!(value != null && value.find((item) => item._key === language.id)),
|
|
69
77
|
text: language.id.toUpperCase(),
|
|
70
78
|
icon: languages.length > MAX_COLUMNS ? void 0 : icons.AddIcon,
|
|
71
79
|
value: language.id,
|
|
@@ -87,7 +95,7 @@ function DocumentAddButtons(props) {
|
|
|
87
95
|
return;
|
|
88
96
|
}
|
|
89
97
|
const alreadyTranslated = documentsToTranslation.filter(
|
|
90
|
-
(translation) => translation
|
|
98
|
+
(translation) => (translation == null ? void 0 : translation._key) === languageId
|
|
91
99
|
), removeDuplicates = documentsToTranslation.reduce((filteredTranslations, translation) => alreadyTranslated.filter(
|
|
92
100
|
(alreadyTranslation) => alreadyTranslation.pathString === translation.pathString
|
|
93
101
|
).length > 0 || filteredTranslations.filter(
|
|
@@ -139,20 +147,28 @@ const getSelectedValue = (select, document) => {
|
|
|
139
147
|
for (const [key, path] of Object.entries(selection)) {
|
|
140
148
|
let value = get__default.default(document, path);
|
|
141
149
|
Array.isArray(value) && (value = value.filter(
|
|
142
|
-
(item) => typeof item == "object" ? item
|
|
150
|
+
(item) => typeof item == "object" ? (item == null ? void 0 : item._type) === "reference" && "_ref" in item : !0
|
|
143
151
|
)), selectedValue[key] = value;
|
|
144
152
|
}
|
|
145
153
|
return selectedValue;
|
|
146
|
-
}
|
|
147
|
-
|
|
154
|
+
};
|
|
155
|
+
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) => {
|
|
156
|
+
for (var prop in b || (b = {}))
|
|
157
|
+
__hasOwnProp$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
158
|
+
if (__getOwnPropSymbols$8)
|
|
159
|
+
for (var prop of __getOwnPropSymbols$8(b))
|
|
160
|
+
__propIsEnum$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
161
|
+
return a;
|
|
162
|
+
}, __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
|
|
163
|
+
const InternationalizedArrayContext = react.createContext(__spreadProps$7(__spreadValues$8({}, CONFIG_DEFAULT), {
|
|
148
164
|
languages: [],
|
|
149
165
|
filteredLanguages: []
|
|
150
|
-
});
|
|
166
|
+
}));
|
|
151
167
|
function useInternationalizedArrayContext() {
|
|
152
168
|
return react.useContext(InternationalizedArrayContext);
|
|
153
169
|
}
|
|
154
170
|
function InternationalizedArrayProvider(props) {
|
|
155
|
-
const { internationalizedArray: internationalizedArray2 } = props, client = sanity.useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = sanity.useWorkspace(), { formState } = structure.useDocumentPane(), deferredDocument = react.useDeferredValue(formState
|
|
171
|
+
const { internationalizedArray: internationalizedArray2 } = props, client = sanity.useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = sanity.useWorkspace(), { formState } = structure.useDocumentPane(), deferredDocument = react.useDeferredValue(formState == null ? void 0 : formState.value), selectedValue = react.useMemo(
|
|
156
172
|
() => getSelectedValue(internationalizedArray2.select, deferredDocument),
|
|
157
173
|
[internationalizedArray2.select, deferredDocument]
|
|
158
174
|
), languages = Array.isArray(internationalizedArray2.languages) ? internationalizedArray2.languages : suspend.suspend(
|
|
@@ -166,7 +182,7 @@ function InternationalizedArrayProvider(props) {
|
|
|
166
182
|
(language) => selectedLanguageIds.includes(language.id)
|
|
167
183
|
) : languages;
|
|
168
184
|
}, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document"), context = react.useMemo(
|
|
169
|
-
() => ({
|
|
185
|
+
() => __spreadProps$7(__spreadValues$8({}, internationalizedArray2), { languages, filteredLanguages }),
|
|
170
186
|
[filteredLanguages, internationalizedArray2, languages]
|
|
171
187
|
);
|
|
172
188
|
return /* @__PURE__ */ jsxRuntime.jsx(InternationalizedArrayContext.Provider, { value: context, children: showDocumentButtons ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 5, children: [
|
|
@@ -185,11 +201,19 @@ function checkAllLanguagesArePresent(languages, value) {
|
|
|
185
201
|
return languagesInUseIds.length === filteredLanguageIds.length && languagesInUseIds.every((l) => filteredLanguageIds.includes(l));
|
|
186
202
|
}
|
|
187
203
|
function createAddAllTitle(value, languages) {
|
|
188
|
-
return value
|
|
204
|
+
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";
|
|
189
205
|
}
|
|
190
206
|
function createValueSchemaTypeName(schemaType) {
|
|
191
207
|
return `${schemaType.name}Value`;
|
|
192
208
|
}
|
|
209
|
+
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) => {
|
|
210
|
+
for (var prop in b || (b = {}))
|
|
211
|
+
__hasOwnProp$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
212
|
+
if (__getOwnPropSymbols$7)
|
|
213
|
+
for (var prop of __getOwnPropSymbols$7(b))
|
|
214
|
+
__propIsEnum$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
215
|
+
return a;
|
|
216
|
+
}, __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
|
|
193
217
|
function createAddLanguagePatches(config) {
|
|
194
218
|
const {
|
|
195
219
|
addLanguageKeys,
|
|
@@ -198,15 +222,13 @@ function createAddLanguagePatches(config) {
|
|
|
198
222
|
filteredLanguages,
|
|
199
223
|
value,
|
|
200
224
|
path = []
|
|
201
|
-
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => ({
|
|
202
|
-
...itemBase,
|
|
225
|
+
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => __spreadProps$6(__spreadValues$7({}, itemBase), {
|
|
203
226
|
_key: id
|
|
204
227
|
})) : filteredLanguages.filter(
|
|
205
|
-
(language) => value
|
|
206
|
-
).map((language) => ({
|
|
207
|
-
...itemBase,
|
|
228
|
+
(language) => value != null && value.length ? !value.find((v) => v._key === language.id) : !0
|
|
229
|
+
).map((language) => __spreadProps$6(__spreadValues$7({}, itemBase), {
|
|
208
230
|
_key: language.id
|
|
209
|
-
})), languagesInUse = value
|
|
231
|
+
})), languagesInUse = value != null && value.length ? value.map((v) => v) : [];
|
|
210
232
|
return newItems.map((item) => {
|
|
211
233
|
const languageIndex = languages.findIndex((l) => item._key === l.id), remainingLanguages = languages.slice(languageIndex + 1), nextLanguageIndex = languagesInUse.findIndex(
|
|
212
234
|
(l) => (
|
|
@@ -224,7 +246,7 @@ function createAddLanguagePatches(config) {
|
|
|
224
246
|
});
|
|
225
247
|
}
|
|
226
248
|
const createTranslateFieldActions = (fieldActionProps, { languages, filteredLanguages }) => languages.map((language) => {
|
|
227
|
-
const value = sanity.useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!value
|
|
249
|
+
const value = sanity.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 } = structure.useDocumentPane(), onAction = react.useCallback(() => {
|
|
228
250
|
const { schemaType, path } = fieldActionProps, addLanguageKeys = [language.id], patches = createAddLanguagePatches({
|
|
229
251
|
addLanguageKeys,
|
|
230
252
|
schemaType,
|
|
@@ -266,7 +288,8 @@ const createTranslateFieldActions = (fieldActionProps, { languages, filteredLang
|
|
|
266
288
|
}, internationalizedArrayFieldAction = sanity.defineDocumentFieldAction({
|
|
267
289
|
name: "internationalizedArray",
|
|
268
290
|
useAction(fieldActionProps) {
|
|
269
|
-
|
|
291
|
+
var _a, _b;
|
|
292
|
+
const isInternationalizedArrayField = (_b = (_a = fieldActionProps == null ? void 0 : fieldActionProps.schemaType) == null ? void 0 : _a.type) == null ? void 0 : _b.name.startsWith(
|
|
270
293
|
"internationalizedArray"
|
|
271
294
|
), { languages, filteredLanguages } = useInternationalizedArrayContext(), translateFieldActions = createTranslateFieldActions(
|
|
272
295
|
fieldActionProps,
|
|
@@ -322,6 +345,14 @@ function Feedback() {
|
|
|
322
345
|
/* @__PURE__ */ jsxRuntime.jsx(ui.Card, { padding: 2, border: !0, radius: 2, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Code, { size: 1, language: "javascript", children: JSON.stringify(schemaExample, null, 2) }) })
|
|
323
346
|
] }) });
|
|
324
347
|
}
|
|
348
|
+
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) => {
|
|
349
|
+
for (var prop in b || (b = {}))
|
|
350
|
+
__hasOwnProp$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
351
|
+
if (__getOwnPropSymbols$6)
|
|
352
|
+
for (var prop of __getOwnPropSymbols$6(b))
|
|
353
|
+
__propIsEnum$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
354
|
+
return a;
|
|
355
|
+
}, __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
|
|
325
356
|
function InternationalizedArray(props) {
|
|
326
357
|
const { members, value, schemaType, onChange } = props, readOnly = typeof schemaType.readOnly == "boolean" ? schemaType.readOnly : !1, toast = ui.useToast(), {
|
|
327
358
|
languages,
|
|
@@ -343,9 +374,10 @@ function InternationalizedArray(props) {
|
|
|
343
374
|
[languageFilterEnabled, members, languageFilterOptions, selectedLanguageIds]
|
|
344
375
|
), handleAddLanguage = react.useCallback(
|
|
345
376
|
async (param) => {
|
|
346
|
-
|
|
377
|
+
var _a;
|
|
378
|
+
if (!(filteredLanguages != null && filteredLanguages.length))
|
|
347
379
|
return;
|
|
348
|
-
const addLanguageKeys = Array.isArray(param) ? param : [param
|
|
380
|
+
const addLanguageKeys = Array.isArray(param) ? param : [(_a = param == null ? void 0 : param.currentTarget) == null ? void 0 : _a.value].filter(Boolean), patches = createAddLanguagePatches({
|
|
349
381
|
addLanguageKeys,
|
|
350
382
|
schemaType,
|
|
351
383
|
languages,
|
|
@@ -367,23 +399,23 @@ function InternationalizedArray(props) {
|
|
|
367
399
|
defaultLanguages
|
|
368
400
|
]);
|
|
369
401
|
const handleRestoreOrder = react.useCallback(() => {
|
|
370
|
-
if (!value
|
|
402
|
+
if (!(value != null && value.length) || !(languages != null && languages.length))
|
|
371
403
|
return;
|
|
372
404
|
const updatedValue = value.reduce((acc, v) => {
|
|
373
|
-
const newIndex = languages.findIndex((l) => l.id === v
|
|
405
|
+
const newIndex = languages.findIndex((l) => l.id === (v == null ? void 0 : v._key));
|
|
374
406
|
return newIndex > -1 && (acc[newIndex] = v), acc;
|
|
375
407
|
}, []).filter(Boolean);
|
|
376
|
-
value
|
|
408
|
+
(value == null ? void 0 : value.length) !== updatedValue.length && toast.push({
|
|
377
409
|
title: "There was an error reordering languages",
|
|
378
410
|
status: "warning"
|
|
379
411
|
}), onChange(sanity.set(updatedValue));
|
|
380
|
-
}, [toast, languages, onChange, value]), allKeysAreLanguages = react.useMemo(() => !value
|
|
381
|
-
() => languages && languages.length > 1 ? languages.filter((l) => value
|
|
412
|
+
}, [toast, languages, onChange, value]), allKeysAreLanguages = react.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 = react.useMemo(
|
|
413
|
+
() => languages && languages.length > 1 ? languages.filter((l) => value == null ? void 0 : value.find((v) => v._key === l.id)) : [],
|
|
382
414
|
[languages, value]
|
|
383
|
-
), languagesOutOfOrder = react.useMemo(() => !value
|
|
415
|
+
), languagesOutOfOrder = react.useMemo(() => !(value != null && value.length) || !languagesInUse.length ? [] : value.map(
|
|
384
416
|
(v, vIndex) => vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v
|
|
385
417
|
).filter(Boolean), [value, languagesInUse]), languagesAreValid = react.useMemo(
|
|
386
|
-
() => !languages
|
|
418
|
+
() => !(languages != null && languages.length) || (languages == null ? void 0 : languages.length) && languages.every((item) => item.id && item.title),
|
|
387
419
|
[languages]
|
|
388
420
|
);
|
|
389
421
|
react.useEffect(() => {
|
|
@@ -398,17 +430,16 @@ function InternationalizedArray(props) {
|
|
|
398
430
|
const addButtonsAreVisible = (
|
|
399
431
|
// Plugin was configured to display buttons here (default!)
|
|
400
432
|
buttonLocations.includes("field") && // There's at least one language visible
|
|
401
|
-
filteredLanguages
|
|
433
|
+
(filteredLanguages == null ? void 0 : filteredLanguages.length) > 0 && // Not every language has a value yet
|
|
402
434
|
!allLanguagesArePresent
|
|
403
|
-
), fieldHasMembers = members
|
|
435
|
+
), fieldHasMembers = (members == null ? void 0 : members.length) > 0;
|
|
404
436
|
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
|
|
405
437
|
fieldHasMembers ? /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: filteredMembers.map((member) => member.kind === "item" ? /* @__PURE__ */ react.createElement(
|
|
406
438
|
sanity.ArrayOfObjectsItem,
|
|
407
|
-
{
|
|
408
|
-
...props,
|
|
439
|
+
__spreadProps$5(__spreadValues$6({}, props), {
|
|
409
440
|
key: member.key,
|
|
410
441
|
member
|
|
411
|
-
}
|
|
442
|
+
})
|
|
412
443
|
) : /* @__PURE__ */ jsxRuntime.jsx(sanity.MemberItemError, { member }, member.key)) }) : null,
|
|
413
444
|
!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,
|
|
414
445
|
addButtonsAreVisible ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
|
|
@@ -436,9 +467,17 @@ function InternationalizedArray(props) {
|
|
|
436
467
|
] });
|
|
437
468
|
}
|
|
438
469
|
function getLanguagesFieldOption(schemaType) {
|
|
439
|
-
|
|
470
|
+
var _a;
|
|
471
|
+
return schemaType ? ((_a = schemaType.options) == null ? void 0 : _a.languages) || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
440
472
|
}
|
|
441
|
-
var
|
|
473
|
+
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) => {
|
|
474
|
+
for (var prop in b || (b = {}))
|
|
475
|
+
__hasOwnProp$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
476
|
+
if (__getOwnPropSymbols$5)
|
|
477
|
+
for (var prop of __getOwnPropSymbols$5(b))
|
|
478
|
+
__propIsEnum$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
479
|
+
return a;
|
|
480
|
+
}, __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b)), array = (config) => {
|
|
442
481
|
const { apiVersion, select, languages, type } = config, typeName = typeof type == "string" ? type : type.name, arrayName = createFieldName(typeName), objectName = createFieldName(typeName, !0);
|
|
443
482
|
return sanity.defineField({
|
|
444
483
|
name: arrayName,
|
|
@@ -454,11 +493,10 @@ var array = (config) => {
|
|
|
454
493
|
languages
|
|
455
494
|
},
|
|
456
495
|
of: [
|
|
457
|
-
sanity.defineField({
|
|
458
|
-
...typeof type == "string" ? {} : type,
|
|
496
|
+
sanity.defineField(__spreadProps$4(__spreadValues$5({}, typeof type == "string" ? {} : type), {
|
|
459
497
|
name: objectName,
|
|
460
498
|
type: objectName
|
|
461
|
-
})
|
|
499
|
+
}))
|
|
462
500
|
],
|
|
463
501
|
// @ts-expect-error - fix typings
|
|
464
502
|
validation: (rule) => rule.custom(async (value, context) => {
|
|
@@ -466,10 +504,10 @@ var array = (config) => {
|
|
|
466
504
|
return !0;
|
|
467
505
|
const selectedValue = getSelectedValue(select, context.document), client = context.getClient({ apiVersion });
|
|
468
506
|
let contextLanguages = [];
|
|
469
|
-
const languagesFieldOption = getLanguagesFieldOption(context
|
|
507
|
+
const languagesFieldOption = getLanguagesFieldOption(context == null ? void 0 : context.type);
|
|
470
508
|
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)
|
|
471
509
|
return `Cannot be more than ${contextLanguages.length === 1 ? "1 item" : `${contextLanguages.length} items`}`;
|
|
472
|
-
const nonLanguageKeys = value
|
|
510
|
+
const nonLanguageKeys = value != null && value.length ? value.filter(
|
|
473
511
|
(item) => !contextLanguages.find((language) => item._key === language.id)
|
|
474
512
|
) : [];
|
|
475
513
|
if (nonLanguageKeys.length)
|
|
@@ -477,26 +515,31 @@ var array = (config) => {
|
|
|
477
515
|
message: "Array item keys must be valid languages registered to the field type",
|
|
478
516
|
paths: nonLanguageKeys.map((item) => [{ _key: item._key }])
|
|
479
517
|
};
|
|
480
|
-
const valuesByLanguage = value
|
|
481
|
-
...acc,
|
|
518
|
+
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), {
|
|
482
519
|
[cur._key]: [cur]
|
|
483
|
-
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => item
|
|
520
|
+
}), {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => (item == null ? void 0 : item.length) > 1).flat();
|
|
484
521
|
return duplicateValues.length ? {
|
|
485
522
|
message: "There can only be one field per language",
|
|
486
523
|
paths: duplicateValues.map((item) => [{ _key: item._key }])
|
|
487
524
|
} : !0;
|
|
488
525
|
})
|
|
489
526
|
});
|
|
490
|
-
}
|
|
527
|
+
}, __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) => {
|
|
528
|
+
for (var prop in b || (b = {}))
|
|
529
|
+
__hasOwnProp$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
530
|
+
if (__getOwnPropSymbols$4)
|
|
531
|
+
for (var prop of __getOwnPropSymbols$4(b))
|
|
532
|
+
__propIsEnum$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
533
|
+
return a;
|
|
534
|
+
}, __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
491
535
|
function InternationalizedField(props) {
|
|
492
|
-
return props.schemaType.name === "reference" && props.value ? props.renderDefault({
|
|
493
|
-
...props,
|
|
536
|
+
return props.schemaType.name === "reference" && props.value ? props.renderDefault(__spreadProps$3(__spreadValues$4({}, props), {
|
|
494
537
|
title: "",
|
|
495
538
|
level: 0
|
|
496
|
-
}) : props.children;
|
|
539
|
+
})) : props.children;
|
|
497
540
|
}
|
|
498
541
|
function getToneFromValidation(validations) {
|
|
499
|
-
if (!validations
|
|
542
|
+
if (!(validations != null && validations.length))
|
|
500
543
|
return;
|
|
501
544
|
const validationLevels = validations.map((v) => v.level);
|
|
502
545
|
if (validationLevels.includes("error"))
|
|
@@ -504,11 +547,18 @@ function getToneFromValidation(validations) {
|
|
|
504
547
|
if (validationLevels.includes("warning"))
|
|
505
548
|
return "caution";
|
|
506
549
|
}
|
|
550
|
+
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) => {
|
|
551
|
+
for (var prop in b || (b = {}))
|
|
552
|
+
__hasOwnProp$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
553
|
+
if (__getOwnPropSymbols$3)
|
|
554
|
+
for (var prop of __getOwnPropSymbols$3(b))
|
|
555
|
+
__propIsEnum$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
556
|
+
return a;
|
|
557
|
+
}, __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
507
558
|
function InternationalizedInput(props) {
|
|
508
559
|
const parentValue = sanity.useFormValue(
|
|
509
560
|
props.path.slice(0, -1)
|
|
510
|
-
), inlineProps = {
|
|
511
|
-
...props.inputProps,
|
|
561
|
+
), inlineProps = __spreadProps$2(__spreadValues$3({}, props.inputProps), {
|
|
512
562
|
// This is the magic that makes inline editing work?
|
|
513
563
|
members: props.inputProps.members.filter(
|
|
514
564
|
(m) => m.kind === "field" && m.name === "value"
|
|
@@ -516,13 +566,17 @@ function InternationalizedInput(props) {
|
|
|
516
566
|
// This just overrides the type
|
|
517
567
|
// TODO: Remove this as it shouldn't be necessary?
|
|
518
568
|
value: props.value
|
|
519
|
-
}, { validation, value, onChange, readOnly } = inlineProps, { languages } = useInternationalizedArrayContext(), languageKeysInUse = react.useMemo(
|
|
520
|
-
() =>
|
|
569
|
+
}), { validation, value, onChange, readOnly } = inlineProps, { languages } = useInternationalizedArrayContext(), languageKeysInUse = react.useMemo(
|
|
570
|
+
() => {
|
|
571
|
+
var _a;
|
|
572
|
+
return (_a = parentValue == null ? void 0 : parentValue.map((v) => v._key)) != null ? _a : [];
|
|
573
|
+
},
|
|
521
574
|
[parentValue]
|
|
522
|
-
), keyIsValid = languages
|
|
575
|
+
), keyIsValid = languages != null && languages.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = react.useCallback(
|
|
523
576
|
(event) => {
|
|
524
|
-
|
|
525
|
-
|
|
577
|
+
var _a;
|
|
578
|
+
const languageId = (_a = event == null ? void 0 : event.currentTarget) == null ? void 0 : _a.value;
|
|
579
|
+
!value || !(languages != null && languages.length) || !languages.find((l) => l.id === languageId) || onChange([sanity.set(languageId, ["_key"])]);
|
|
526
580
|
},
|
|
527
581
|
[onChange, value, languages]
|
|
528
582
|
), handleUnset = react.useCallback(() => {
|
|
@@ -563,7 +617,14 @@ function InternationalizedInput(props) {
|
|
|
563
617
|
] })
|
|
564
618
|
] }) }) : /* @__PURE__ */ jsxRuntime.jsx(ui.Spinner, {});
|
|
565
619
|
}
|
|
566
|
-
var
|
|
620
|
+
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) => {
|
|
621
|
+
for (var prop in b || (b = {}))
|
|
622
|
+
__hasOwnProp$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
623
|
+
if (__getOwnPropSymbols$2)
|
|
624
|
+
for (var prop of __getOwnPropSymbols$2(b))
|
|
625
|
+
__propIsEnum$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
626
|
+
return a;
|
|
627
|
+
}, __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b)), object = (config) => {
|
|
567
628
|
const { type } = config, typeName = typeof type == "string" ? type : type.name, objectName = createFieldName(typeName, !0);
|
|
568
629
|
return sanity.defineField({
|
|
569
630
|
name: objectName,
|
|
@@ -585,13 +646,12 @@ var object = (config) => {
|
|
|
585
646
|
})
|
|
586
647
|
) : (
|
|
587
648
|
// Pass in the configured options, but overwrite the name
|
|
588
|
-
{
|
|
589
|
-
...type,
|
|
649
|
+
__spreadProps$1(__spreadValues$2({}, type), {
|
|
590
650
|
name: "value",
|
|
591
651
|
components: {
|
|
592
652
|
field: InternationalizedField
|
|
593
653
|
}
|
|
594
|
-
}
|
|
654
|
+
})
|
|
595
655
|
)
|
|
596
656
|
],
|
|
597
657
|
preview: {
|
|
@@ -601,13 +661,20 @@ var object = (config) => {
|
|
|
601
661
|
}
|
|
602
662
|
}
|
|
603
663
|
});
|
|
664
|
+
}, __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) => {
|
|
665
|
+
for (var prop in b || (b = {}))
|
|
666
|
+
__hasOwnProp$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
667
|
+
if (__getOwnPropSymbols$1)
|
|
668
|
+
for (var prop of __getOwnPropSymbols$1(b))
|
|
669
|
+
__propIsEnum$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
670
|
+
return a;
|
|
604
671
|
};
|
|
605
672
|
function flattenSchemaType(schemaType) {
|
|
606
673
|
return sanity.isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 3) : (console.error("Schema type is not a document"), []);
|
|
607
674
|
}
|
|
608
675
|
function extractInnerFields(fields, path, maxDepth) {
|
|
609
676
|
return path.length >= maxDepth ? [] : fields.reduce((acc, field) => {
|
|
610
|
-
const thisFieldWithPath = { path: [...path, field.name],
|
|
677
|
+
const thisFieldWithPath = __spreadValues$1({ path: [...path, field.name] }, field);
|
|
611
678
|
if (field.type.jsonType === "object") {
|
|
612
679
|
const innerFields = extractInnerFields(
|
|
613
680
|
field.type.fields,
|
|
@@ -616,19 +683,29 @@ function extractInnerFields(fields, path, maxDepth) {
|
|
|
616
683
|
);
|
|
617
684
|
return [...acc, thisFieldWithPath, ...innerFields];
|
|
618
685
|
} else if (field.type.jsonType === "array" && field.type.of.length && field.type.of.some((item) => "fields" in item)) {
|
|
619
|
-
const innerFields =
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
686
|
+
const innerFields = field.type.of.flatMap(
|
|
687
|
+
(innerField) => extractInnerFields(
|
|
688
|
+
// @ts-expect-error - Fix TS assertion for array fields
|
|
689
|
+
innerField.fields,
|
|
690
|
+
[...path, field.name],
|
|
691
|
+
maxDepth
|
|
692
|
+
)
|
|
624
693
|
);
|
|
625
694
|
return [...acc, thisFieldWithPath, ...innerFields];
|
|
626
695
|
}
|
|
627
696
|
return [...acc, thisFieldWithPath];
|
|
628
697
|
}, []);
|
|
629
698
|
}
|
|
699
|
+
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) => {
|
|
700
|
+
for (var prop in b || (b = {}))
|
|
701
|
+
__hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
702
|
+
if (__getOwnPropSymbols)
|
|
703
|
+
for (var prop of __getOwnPropSymbols(b))
|
|
704
|
+
__propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
705
|
+
return a;
|
|
706
|
+
}, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
630
707
|
const internationalizedArray = sanity.definePlugin((config) => {
|
|
631
|
-
const pluginConfig = {
|
|
708
|
+
const pluginConfig = __spreadValues(__spreadValues({}, CONFIG_DEFAULT), config), {
|
|
632
709
|
apiVersion = "2022-11-27",
|
|
633
710
|
select,
|
|
634
711
|
languages,
|
|
@@ -660,10 +737,9 @@ const internationalizedArray = sanity.definePlugin((config) => {
|
|
|
660
737
|
(name) => name.startsWith("internationalizedArray")
|
|
661
738
|
) ? props.renderDefault(props) : /* @__PURE__ */ jsxRuntime.jsx(
|
|
662
739
|
InternationalizedArrayProvider,
|
|
663
|
-
{
|
|
664
|
-
...props,
|
|
740
|
+
__spreadProps(__spreadValues({}, props), {
|
|
665
741
|
internationalizedArray: pluginConfig
|
|
666
|
-
}
|
|
742
|
+
})
|
|
667
743
|
)
|
|
668
744
|
}
|
|
669
745
|
},
|