sanity-plugin-internationalized-array 3.0.2 → 3.1.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.
- package/README.md +8 -1
- package/lib/index.d.mts +7 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.esm.js +190 -91
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +189 -90
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +190 -91
- package/lib/index.mjs.map +1 -1
- package/package.json +7 -9
- package/src/components/AddButtons.tsx +36 -18
- package/src/components/InternationalizedInput.tsx +14 -7
- package/src/constants.ts +6 -1
- package/src/types.ts +7 -0
- package/src/utils/getLanguageDisplay.ts +13 -0
package/lib/index.js
CHANGED
|
@@ -20,26 +20,39 @@ function _interopNamespaceCompat(e) {
|
|
|
20
20
|
}), n.default = e, Object.freeze(n);
|
|
21
21
|
}
|
|
22
22
|
var suspend__namespace = /* @__PURE__ */ _interopNamespaceCompat(suspend), equal__default = /* @__PURE__ */ _interopDefaultCompat(equal), get__default = /* @__PURE__ */ _interopDefaultCompat(get);
|
|
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 =
|
|
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 = {
|
|
24
|
+
codeOnly: 5,
|
|
25
|
+
titleOnly: 4,
|
|
26
|
+
titleAndCode: 3
|
|
27
|
+
}, CONFIG_DEFAULT = {
|
|
24
28
|
languages: [],
|
|
25
29
|
select: {},
|
|
26
30
|
defaultLanguages: [],
|
|
27
31
|
fieldTypes: [],
|
|
28
32
|
apiVersion: "2022-11-27",
|
|
29
33
|
buttonLocations: ["field"],
|
|
30
|
-
buttonAddAll: !0
|
|
31
|
-
|
|
34
|
+
buttonAddAll: !0,
|
|
35
|
+
languageDisplay: "codeOnly"
|
|
36
|
+
};
|
|
37
|
+
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) => {
|
|
38
|
+
for (var prop in b || (b = {}))
|
|
39
|
+
__hasOwnProp$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
40
|
+
if (__getOwnPropSymbols$9)
|
|
41
|
+
for (var prop of __getOwnPropSymbols$9(b))
|
|
42
|
+
__propIsEnum$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
43
|
+
return a;
|
|
44
|
+
}, __spreadProps$8 = (a, b) => __defProps$8(a, __getOwnPropDescs$8(b));
|
|
45
|
+
const getDocumentsToTranslate = (value, rootPath = []) => {
|
|
32
46
|
if (Array.isArray(value)) {
|
|
33
47
|
const arrayRootPath = [...rootPath], internationalizedValues = value.filter((item) => {
|
|
34
48
|
if (Array.isArray(item)) return !1;
|
|
35
49
|
if (typeof item == "object") {
|
|
36
|
-
const type = item
|
|
37
|
-
return type
|
|
50
|
+
const type = item == null ? void 0 : item._type;
|
|
51
|
+
return (type == null ? void 0 : type.startsWith("internationalizedArray")) && (type == null ? void 0 : type.endsWith("Value"));
|
|
38
52
|
}
|
|
39
53
|
return !1;
|
|
40
54
|
});
|
|
41
|
-
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => ({
|
|
42
|
-
...internationalizedValue,
|
|
55
|
+
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => __spreadProps$8(__spreadValues$9({}, internationalizedValue), {
|
|
43
56
|
path: arrayRootPath,
|
|
44
57
|
pathString: arrayRootPath.join(".")
|
|
45
58
|
})) : value.length > 0 ? value.map(
|
|
@@ -57,22 +70,39 @@ const namespace = "sanity-plugin-internationalized-array", version = "v1", prelo
|
|
|
57
70
|
}
|
|
58
71
|
return [];
|
|
59
72
|
};
|
|
73
|
+
function getLanguageDisplay(languageDisplay, title, code) {
|
|
74
|
+
return languageDisplay === "codeOnly" ? code.toUpperCase() : languageDisplay === "titleOnly" ? title : languageDisplay === "titleAndCode" ? `${title} (${code.toUpperCase()})` : title;
|
|
75
|
+
}
|
|
60
76
|
function AddButtons(props) {
|
|
61
|
-
const { languages, readOnly, value, onClick } = props;
|
|
62
|
-
return languages.length > 0 ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
63
|
-
ui.
|
|
77
|
+
const { languages, readOnly, value, onClick } = props, { languageDisplay } = useInternationalizedArrayContext();
|
|
78
|
+
return languages.length > 0 ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
79
|
+
ui.Grid,
|
|
64
80
|
{
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
81
|
+
columns: Math.min(languages.length, MAX_COLUMNS[languageDisplay]),
|
|
82
|
+
gap: 2,
|
|
83
|
+
children: languages.map((language) => {
|
|
84
|
+
const languageTitle = getLanguageDisplay(
|
|
85
|
+
languageDisplay,
|
|
86
|
+
language.title,
|
|
87
|
+
language.id
|
|
88
|
+
);
|
|
89
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
90
|
+
ui.Button,
|
|
91
|
+
{
|
|
92
|
+
tone: "primary",
|
|
93
|
+
mode: "ghost",
|
|
94
|
+
fontSize: 1,
|
|
95
|
+
disabled: readOnly || !!(value != null && value.find((item) => item._key === language.id)),
|
|
96
|
+
text: languageTitle,
|
|
97
|
+
icon: languages.length > MAX_COLUMNS[languageDisplay] && languageDisplay === "codeOnly" ? void 0 : icons.AddIcon,
|
|
98
|
+
value: language.id,
|
|
99
|
+
onClick
|
|
100
|
+
},
|
|
101
|
+
language.id
|
|
102
|
+
);
|
|
103
|
+
})
|
|
104
|
+
}
|
|
105
|
+
) : null;
|
|
76
106
|
}
|
|
77
107
|
var AddButtons$1 = react.memo(AddButtons);
|
|
78
108
|
function DocumentAddButtons(props) {
|
|
@@ -87,7 +117,7 @@ function DocumentAddButtons(props) {
|
|
|
87
117
|
return;
|
|
88
118
|
}
|
|
89
119
|
const alreadyTranslated = documentsToTranslation.filter(
|
|
90
|
-
(translation) => translation
|
|
120
|
+
(translation) => (translation == null ? void 0 : translation._key) === languageId
|
|
91
121
|
), removeDuplicates = documentsToTranslation.reduce((filteredTranslations, translation) => alreadyTranslated.filter(
|
|
92
122
|
(alreadyTranslation) => alreadyTranslation.pathString === translation.pathString
|
|
93
123
|
).length > 0 || filteredTranslations.filter(
|
|
@@ -139,20 +169,28 @@ const getSelectedValue = (select, document) => {
|
|
|
139
169
|
for (const [key, path] of Object.entries(selection)) {
|
|
140
170
|
let value = get__default.default(document, path);
|
|
141
171
|
Array.isArray(value) && (value = value.filter(
|
|
142
|
-
(item) => typeof item == "object" ? item
|
|
172
|
+
(item) => typeof item == "object" ? (item == null ? void 0 : item._type) === "reference" && "_ref" in item : !0
|
|
143
173
|
)), selectedValue[key] = value;
|
|
144
174
|
}
|
|
145
175
|
return selectedValue;
|
|
146
|
-
}
|
|
147
|
-
|
|
176
|
+
};
|
|
177
|
+
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) => {
|
|
178
|
+
for (var prop in b || (b = {}))
|
|
179
|
+
__hasOwnProp$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
180
|
+
if (__getOwnPropSymbols$8)
|
|
181
|
+
for (var prop of __getOwnPropSymbols$8(b))
|
|
182
|
+
__propIsEnum$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
183
|
+
return a;
|
|
184
|
+
}, __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
|
|
185
|
+
const InternationalizedArrayContext = react.createContext(__spreadProps$7(__spreadValues$8({}, CONFIG_DEFAULT), {
|
|
148
186
|
languages: [],
|
|
149
187
|
filteredLanguages: []
|
|
150
|
-
});
|
|
188
|
+
}));
|
|
151
189
|
function useInternationalizedArrayContext() {
|
|
152
190
|
return react.useContext(InternationalizedArrayContext);
|
|
153
191
|
}
|
|
154
192
|
function InternationalizedArrayProvider(props) {
|
|
155
|
-
const { internationalizedArray: internationalizedArray2 } = props, client = sanity.useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = sanity.useWorkspace(), { formState } = structure.useDocumentPane(), deferredDocument = react.useDeferredValue(formState
|
|
193
|
+
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
194
|
() => getSelectedValue(internationalizedArray2.select, deferredDocument),
|
|
157
195
|
[internationalizedArray2.select, deferredDocument]
|
|
158
196
|
), languages = Array.isArray(internationalizedArray2.languages) ? internationalizedArray2.languages : suspend.suspend(
|
|
@@ -166,7 +204,7 @@ function InternationalizedArrayProvider(props) {
|
|
|
166
204
|
(language) => selectedLanguageIds.includes(language.id)
|
|
167
205
|
) : languages;
|
|
168
206
|
}, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document"), context = react.useMemo(
|
|
169
|
-
() => ({
|
|
207
|
+
() => __spreadProps$7(__spreadValues$8({}, internationalizedArray2), { languages, filteredLanguages }),
|
|
170
208
|
[filteredLanguages, internationalizedArray2, languages]
|
|
171
209
|
);
|
|
172
210
|
return /* @__PURE__ */ jsxRuntime.jsx(InternationalizedArrayContext.Provider, { value: context, children: showDocumentButtons ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 5, children: [
|
|
@@ -185,11 +223,19 @@ function checkAllLanguagesArePresent(languages, value) {
|
|
|
185
223
|
return languagesInUseIds.length === filteredLanguageIds.length && languagesInUseIds.every((l) => filteredLanguageIds.includes(l));
|
|
186
224
|
}
|
|
187
225
|
function createAddAllTitle(value, languages) {
|
|
188
|
-
return value
|
|
226
|
+
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
227
|
}
|
|
190
228
|
function createValueSchemaTypeName(schemaType) {
|
|
191
229
|
return `${schemaType.name}Value`;
|
|
192
230
|
}
|
|
231
|
+
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) => {
|
|
232
|
+
for (var prop in b || (b = {}))
|
|
233
|
+
__hasOwnProp$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
234
|
+
if (__getOwnPropSymbols$7)
|
|
235
|
+
for (var prop of __getOwnPropSymbols$7(b))
|
|
236
|
+
__propIsEnum$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
237
|
+
return a;
|
|
238
|
+
}, __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
|
|
193
239
|
function createAddLanguagePatches(config) {
|
|
194
240
|
const {
|
|
195
241
|
addLanguageKeys,
|
|
@@ -198,15 +244,13 @@ function createAddLanguagePatches(config) {
|
|
|
198
244
|
filteredLanguages,
|
|
199
245
|
value,
|
|
200
246
|
path = []
|
|
201
|
-
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => ({
|
|
202
|
-
...itemBase,
|
|
247
|
+
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => __spreadProps$6(__spreadValues$7({}, itemBase), {
|
|
203
248
|
_key: id
|
|
204
249
|
})) : filteredLanguages.filter(
|
|
205
|
-
(language) => value
|
|
206
|
-
).map((language) => ({
|
|
207
|
-
...itemBase,
|
|
250
|
+
(language) => value != null && value.length ? !value.find((v) => v._key === language.id) : !0
|
|
251
|
+
).map((language) => __spreadProps$6(__spreadValues$7({}, itemBase), {
|
|
208
252
|
_key: language.id
|
|
209
|
-
})), languagesInUse = value
|
|
253
|
+
})), languagesInUse = value != null && value.length ? value.map((v) => v) : [];
|
|
210
254
|
return newItems.map((item) => {
|
|
211
255
|
const languageIndex = languages.findIndex((l) => item._key === l.id), remainingLanguages = languages.slice(languageIndex + 1), nextLanguageIndex = languagesInUse.findIndex(
|
|
212
256
|
(l) => (
|
|
@@ -224,7 +268,7 @@ function createAddLanguagePatches(config) {
|
|
|
224
268
|
});
|
|
225
269
|
}
|
|
226
270
|
const createTranslateFieldActions = (fieldActionProps, { languages, filteredLanguages }) => languages.map((language) => {
|
|
227
|
-
const value = sanity.useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!value
|
|
271
|
+
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
272
|
const { schemaType, path } = fieldActionProps, addLanguageKeys = [language.id], patches = createAddLanguagePatches({
|
|
229
273
|
addLanguageKeys,
|
|
230
274
|
schemaType,
|
|
@@ -266,7 +310,8 @@ const createTranslateFieldActions = (fieldActionProps, { languages, filteredLang
|
|
|
266
310
|
}, internationalizedArrayFieldAction = sanity.defineDocumentFieldAction({
|
|
267
311
|
name: "internationalizedArray",
|
|
268
312
|
useAction(fieldActionProps) {
|
|
269
|
-
|
|
313
|
+
var _a, _b;
|
|
314
|
+
const isInternationalizedArrayField = (_b = (_a = fieldActionProps == null ? void 0 : fieldActionProps.schemaType) == null ? void 0 : _a.type) == null ? void 0 : _b.name.startsWith(
|
|
270
315
|
"internationalizedArray"
|
|
271
316
|
), { languages, filteredLanguages } = useInternationalizedArrayContext(), translateFieldActions = createTranslateFieldActions(
|
|
272
317
|
fieldActionProps,
|
|
@@ -322,6 +367,14 @@ function Feedback() {
|
|
|
322
367
|
/* @__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
368
|
] }) });
|
|
324
369
|
}
|
|
370
|
+
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) => {
|
|
371
|
+
for (var prop in b || (b = {}))
|
|
372
|
+
__hasOwnProp$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
373
|
+
if (__getOwnPropSymbols$6)
|
|
374
|
+
for (var prop of __getOwnPropSymbols$6(b))
|
|
375
|
+
__propIsEnum$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
376
|
+
return a;
|
|
377
|
+
}, __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
|
|
325
378
|
function InternationalizedArray(props) {
|
|
326
379
|
const { members, value, schemaType, onChange } = props, readOnly = typeof schemaType.readOnly == "boolean" ? schemaType.readOnly : !1, toast = ui.useToast(), {
|
|
327
380
|
languages,
|
|
@@ -343,9 +396,10 @@ function InternationalizedArray(props) {
|
|
|
343
396
|
[languageFilterEnabled, members, languageFilterOptions, selectedLanguageIds]
|
|
344
397
|
), handleAddLanguage = react.useCallback(
|
|
345
398
|
async (param) => {
|
|
346
|
-
|
|
399
|
+
var _a;
|
|
400
|
+
if (!(filteredLanguages != null && filteredLanguages.length))
|
|
347
401
|
return;
|
|
348
|
-
const addLanguageKeys = Array.isArray(param) ? param : [param
|
|
402
|
+
const addLanguageKeys = Array.isArray(param) ? param : [(_a = param == null ? void 0 : param.currentTarget) == null ? void 0 : _a.value].filter(Boolean), patches = createAddLanguagePatches({
|
|
349
403
|
addLanguageKeys,
|
|
350
404
|
schemaType,
|
|
351
405
|
languages,
|
|
@@ -367,23 +421,23 @@ function InternationalizedArray(props) {
|
|
|
367
421
|
defaultLanguages
|
|
368
422
|
]);
|
|
369
423
|
const handleRestoreOrder = react.useCallback(() => {
|
|
370
|
-
if (!value
|
|
424
|
+
if (!(value != null && value.length) || !(languages != null && languages.length))
|
|
371
425
|
return;
|
|
372
426
|
const updatedValue = value.reduce((acc, v) => {
|
|
373
|
-
const newIndex = languages.findIndex((l) => l.id === v
|
|
427
|
+
const newIndex = languages.findIndex((l) => l.id === (v == null ? void 0 : v._key));
|
|
374
428
|
return newIndex > -1 && (acc[newIndex] = v), acc;
|
|
375
429
|
}, []).filter(Boolean);
|
|
376
|
-
value
|
|
430
|
+
(value == null ? void 0 : value.length) !== updatedValue.length && toast.push({
|
|
377
431
|
title: "There was an error reordering languages",
|
|
378
432
|
status: "warning"
|
|
379
433
|
}), onChange(sanity.set(updatedValue));
|
|
380
|
-
}, [toast, languages, onChange, value]), allKeysAreLanguages = react.useMemo(() => !value
|
|
381
|
-
() => languages && languages.length > 1 ? languages.filter((l) => value
|
|
434
|
+
}, [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(
|
|
435
|
+
() => languages && languages.length > 1 ? languages.filter((l) => value == null ? void 0 : value.find((v) => v._key === l.id)) : [],
|
|
382
436
|
[languages, value]
|
|
383
|
-
), languagesOutOfOrder = react.useMemo(() => !value
|
|
437
|
+
), languagesOutOfOrder = react.useMemo(() => !(value != null && value.length) || !languagesInUse.length ? [] : value.map(
|
|
384
438
|
(v, vIndex) => vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v
|
|
385
439
|
).filter(Boolean), [value, languagesInUse]), languagesAreValid = react.useMemo(
|
|
386
|
-
() => !languages
|
|
440
|
+
() => !(languages != null && languages.length) || (languages == null ? void 0 : languages.length) && languages.every((item) => item.id && item.title),
|
|
387
441
|
[languages]
|
|
388
442
|
);
|
|
389
443
|
react.useEffect(() => {
|
|
@@ -398,17 +452,16 @@ function InternationalizedArray(props) {
|
|
|
398
452
|
const addButtonsAreVisible = (
|
|
399
453
|
// Plugin was configured to display buttons here (default!)
|
|
400
454
|
buttonLocations.includes("field") && // There's at least one language visible
|
|
401
|
-
filteredLanguages
|
|
455
|
+
(filteredLanguages == null ? void 0 : filteredLanguages.length) > 0 && // Not every language has a value yet
|
|
402
456
|
!allLanguagesArePresent
|
|
403
|
-
), fieldHasMembers = members
|
|
457
|
+
), fieldHasMembers = (members == null ? void 0 : members.length) > 0;
|
|
404
458
|
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
|
|
405
459
|
fieldHasMembers ? /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: filteredMembers.map((member) => member.kind === "item" ? /* @__PURE__ */ react.createElement(
|
|
406
460
|
sanity.ArrayOfObjectsItem,
|
|
407
|
-
{
|
|
408
|
-
...props,
|
|
461
|
+
__spreadProps$5(__spreadValues$6({}, props), {
|
|
409
462
|
key: member.key,
|
|
410
463
|
member
|
|
411
|
-
}
|
|
464
|
+
})
|
|
412
465
|
) : /* @__PURE__ */ jsxRuntime.jsx(sanity.MemberItemError, { member }, member.key)) }) : null,
|
|
413
466
|
!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
467
|
addButtonsAreVisible ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
|
|
@@ -436,9 +489,17 @@ function InternationalizedArray(props) {
|
|
|
436
489
|
] });
|
|
437
490
|
}
|
|
438
491
|
function getLanguagesFieldOption(schemaType) {
|
|
439
|
-
|
|
492
|
+
var _a;
|
|
493
|
+
return schemaType ? ((_a = schemaType.options) == null ? void 0 : _a.languages) || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
440
494
|
}
|
|
441
|
-
var
|
|
495
|
+
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) => {
|
|
496
|
+
for (var prop in b || (b = {}))
|
|
497
|
+
__hasOwnProp$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
498
|
+
if (__getOwnPropSymbols$5)
|
|
499
|
+
for (var prop of __getOwnPropSymbols$5(b))
|
|
500
|
+
__propIsEnum$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
501
|
+
return a;
|
|
502
|
+
}, __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b)), array = (config) => {
|
|
442
503
|
const { apiVersion, select, languages, type } = config, typeName = typeof type == "string" ? type : type.name, arrayName = createFieldName(typeName), objectName = createFieldName(typeName, !0);
|
|
443
504
|
return sanity.defineField({
|
|
444
505
|
name: arrayName,
|
|
@@ -454,11 +515,10 @@ var array = (config) => {
|
|
|
454
515
|
languages
|
|
455
516
|
},
|
|
456
517
|
of: [
|
|
457
|
-
sanity.defineField({
|
|
458
|
-
...typeof type == "string" ? {} : type,
|
|
518
|
+
sanity.defineField(__spreadProps$4(__spreadValues$5({}, typeof type == "string" ? {} : type), {
|
|
459
519
|
name: objectName,
|
|
460
520
|
type: objectName
|
|
461
|
-
})
|
|
521
|
+
}))
|
|
462
522
|
],
|
|
463
523
|
// @ts-expect-error - fix typings
|
|
464
524
|
validation: (rule) => rule.custom(async (value, context) => {
|
|
@@ -466,10 +526,10 @@ var array = (config) => {
|
|
|
466
526
|
return !0;
|
|
467
527
|
const selectedValue = getSelectedValue(select, context.document), client = context.getClient({ apiVersion });
|
|
468
528
|
let contextLanguages = [];
|
|
469
|
-
const languagesFieldOption = getLanguagesFieldOption(context
|
|
529
|
+
const languagesFieldOption = getLanguagesFieldOption(context == null ? void 0 : context.type);
|
|
470
530
|
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
531
|
return `Cannot be more than ${contextLanguages.length === 1 ? "1 item" : `${contextLanguages.length} items`}`;
|
|
472
|
-
const nonLanguageKeys = value
|
|
532
|
+
const nonLanguageKeys = value != null && value.length ? value.filter(
|
|
473
533
|
(item) => !contextLanguages.find((language) => item._key === language.id)
|
|
474
534
|
) : [];
|
|
475
535
|
if (nonLanguageKeys.length)
|
|
@@ -477,26 +537,31 @@ var array = (config) => {
|
|
|
477
537
|
message: "Array item keys must be valid languages registered to the field type",
|
|
478
538
|
paths: nonLanguageKeys.map((item) => [{ _key: item._key }])
|
|
479
539
|
};
|
|
480
|
-
const valuesByLanguage = value
|
|
481
|
-
...acc,
|
|
540
|
+
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
541
|
[cur._key]: [cur]
|
|
483
|
-
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => item
|
|
542
|
+
}), {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => (item == null ? void 0 : item.length) > 1).flat();
|
|
484
543
|
return duplicateValues.length ? {
|
|
485
544
|
message: "There can only be one field per language",
|
|
486
545
|
paths: duplicateValues.map((item) => [{ _key: item._key }])
|
|
487
546
|
} : !0;
|
|
488
547
|
})
|
|
489
548
|
});
|
|
490
|
-
}
|
|
549
|
+
}, __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) => {
|
|
550
|
+
for (var prop in b || (b = {}))
|
|
551
|
+
__hasOwnProp$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
552
|
+
if (__getOwnPropSymbols$4)
|
|
553
|
+
for (var prop of __getOwnPropSymbols$4(b))
|
|
554
|
+
__propIsEnum$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
555
|
+
return a;
|
|
556
|
+
}, __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
491
557
|
function InternationalizedField(props) {
|
|
492
|
-
return props.schemaType.name === "reference" && props.value ? props.renderDefault({
|
|
493
|
-
...props,
|
|
558
|
+
return props.schemaType.name === "reference" && props.value ? props.renderDefault(__spreadProps$3(__spreadValues$4({}, props), {
|
|
494
559
|
title: "",
|
|
495
560
|
level: 0
|
|
496
|
-
}) : props.children;
|
|
561
|
+
})) : props.children;
|
|
497
562
|
}
|
|
498
563
|
function getToneFromValidation(validations) {
|
|
499
|
-
if (!validations
|
|
564
|
+
if (!(validations != null && validations.length))
|
|
500
565
|
return;
|
|
501
566
|
const validationLevels = validations.map((v) => v.level);
|
|
502
567
|
if (validationLevels.includes("error"))
|
|
@@ -504,11 +569,18 @@ function getToneFromValidation(validations) {
|
|
|
504
569
|
if (validationLevels.includes("warning"))
|
|
505
570
|
return "caution";
|
|
506
571
|
}
|
|
572
|
+
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) => {
|
|
573
|
+
for (var prop in b || (b = {}))
|
|
574
|
+
__hasOwnProp$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
575
|
+
if (__getOwnPropSymbols$3)
|
|
576
|
+
for (var prop of __getOwnPropSymbols$3(b))
|
|
577
|
+
__propIsEnum$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
578
|
+
return a;
|
|
579
|
+
}, __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
507
580
|
function InternationalizedInput(props) {
|
|
508
581
|
const parentValue = sanity.useFormValue(
|
|
509
582
|
props.path.slice(0, -1)
|
|
510
|
-
), inlineProps = {
|
|
511
|
-
...props.inputProps,
|
|
583
|
+
), inlineProps = __spreadProps$2(__spreadValues$3({}, props.inputProps), {
|
|
512
584
|
// This is the magic that makes inline editing work?
|
|
513
585
|
members: props.inputProps.members.filter(
|
|
514
586
|
(m) => m.kind === "field" && m.name === "value"
|
|
@@ -516,34 +588,41 @@ function InternationalizedInput(props) {
|
|
|
516
588
|
// This just overrides the type
|
|
517
589
|
// TODO: Remove this as it shouldn't be necessary?
|
|
518
590
|
value: props.value
|
|
519
|
-
}, { validation, value, onChange, readOnly } = inlineProps, { languages } = useInternationalizedArrayContext(), languageKeysInUse = react.useMemo(
|
|
520
|
-
() =>
|
|
591
|
+
}), { validation, value, onChange, readOnly } = inlineProps, { languages, languageDisplay } = useInternationalizedArrayContext(), languageKeysInUse = react.useMemo(
|
|
592
|
+
() => {
|
|
593
|
+
var _a;
|
|
594
|
+
return (_a = parentValue == null ? void 0 : parentValue.map((v) => v._key)) != null ? _a : [];
|
|
595
|
+
},
|
|
521
596
|
[parentValue]
|
|
522
|
-
), keyIsValid = languages
|
|
597
|
+
), keyIsValid = languages != null && languages.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = react.useCallback(
|
|
523
598
|
(event) => {
|
|
524
|
-
|
|
525
|
-
|
|
599
|
+
var _a;
|
|
600
|
+
const languageId = (_a = event == null ? void 0 : event.currentTarget) == null ? void 0 : _a.value;
|
|
601
|
+
!value || !(languages != null && languages.length) || !languages.find((l) => l.id === languageId) || onChange([sanity.set(languageId, ["_key"])]);
|
|
526
602
|
},
|
|
527
603
|
[onChange, value, languages]
|
|
528
604
|
), handleUnset = react.useCallback(() => {
|
|
529
605
|
onChange(sanity.unset());
|
|
530
606
|
}, [onChange]);
|
|
531
|
-
|
|
532
|
-
|
|
607
|
+
if (!languages)
|
|
608
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ui.Spinner, {});
|
|
609
|
+
const language = languages.find((l) => l.id === value._key), languageTitle = keyIsValid && language ? getLanguageDisplay(languageDisplay, language.title, language.id) : "";
|
|
610
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ui.Card, { paddingTop: 2, tone: getToneFromValidation(validation), children: /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
|
|
611
|
+
/* @__PURE__ */ jsxRuntime.jsx(ui.Card, { tone: "inherit", children: keyIsValid ? /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { muted: !0, size: 1, children: languageTitle }) : /* @__PURE__ */ jsxRuntime.jsx(
|
|
533
612
|
ui.MenuButton,
|
|
534
613
|
{
|
|
535
614
|
button: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { fontSize: 1, text: `Change "${value._key}"` }),
|
|
536
615
|
id: `${value._key}-change-key`,
|
|
537
|
-
menu: /* @__PURE__ */ jsxRuntime.jsx(ui.Menu, { children: languages.map((
|
|
616
|
+
menu: /* @__PURE__ */ jsxRuntime.jsx(ui.Menu, { children: languages.map((lang) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
538
617
|
ui.MenuItem,
|
|
539
618
|
{
|
|
540
|
-
disabled: languageKeysInUse.includes(
|
|
619
|
+
disabled: languageKeysInUse.includes(lang.id),
|
|
541
620
|
fontSize: 1,
|
|
542
|
-
text:
|
|
543
|
-
value:
|
|
621
|
+
text: lang.id.toLocaleUpperCase(),
|
|
622
|
+
value: lang.id,
|
|
544
623
|
onClick: handleKeyChange
|
|
545
624
|
},
|
|
546
|
-
|
|
625
|
+
lang.id
|
|
547
626
|
)) }),
|
|
548
627
|
popover: { portal: !0 }
|
|
549
628
|
}
|
|
@@ -561,9 +640,16 @@ function InternationalizedInput(props) {
|
|
|
561
640
|
}
|
|
562
641
|
) })
|
|
563
642
|
] })
|
|
564
|
-
] }) })
|
|
643
|
+
] }) });
|
|
565
644
|
}
|
|
566
|
-
var
|
|
645
|
+
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) => {
|
|
646
|
+
for (var prop in b || (b = {}))
|
|
647
|
+
__hasOwnProp$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
648
|
+
if (__getOwnPropSymbols$2)
|
|
649
|
+
for (var prop of __getOwnPropSymbols$2(b))
|
|
650
|
+
__propIsEnum$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
651
|
+
return a;
|
|
652
|
+
}, __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b)), object = (config) => {
|
|
567
653
|
const { type } = config, typeName = typeof type == "string" ? type : type.name, objectName = createFieldName(typeName, !0);
|
|
568
654
|
return sanity.defineField({
|
|
569
655
|
name: objectName,
|
|
@@ -585,13 +671,12 @@ var object = (config) => {
|
|
|
585
671
|
})
|
|
586
672
|
) : (
|
|
587
673
|
// Pass in the configured options, but overwrite the name
|
|
588
|
-
{
|
|
589
|
-
...type,
|
|
674
|
+
__spreadProps$1(__spreadValues$2({}, type), {
|
|
590
675
|
name: "value",
|
|
591
676
|
components: {
|
|
592
677
|
field: InternationalizedField
|
|
593
678
|
}
|
|
594
|
-
}
|
|
679
|
+
})
|
|
595
680
|
)
|
|
596
681
|
],
|
|
597
682
|
preview: {
|
|
@@ -601,13 +686,20 @@ var object = (config) => {
|
|
|
601
686
|
}
|
|
602
687
|
}
|
|
603
688
|
});
|
|
689
|
+
}, __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) => {
|
|
690
|
+
for (var prop in b || (b = {}))
|
|
691
|
+
__hasOwnProp$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
692
|
+
if (__getOwnPropSymbols$1)
|
|
693
|
+
for (var prop of __getOwnPropSymbols$1(b))
|
|
694
|
+
__propIsEnum$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
695
|
+
return a;
|
|
604
696
|
};
|
|
605
697
|
function flattenSchemaType(schemaType) {
|
|
606
698
|
return sanity.isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 3) : (console.error("Schema type is not a document"), []);
|
|
607
699
|
}
|
|
608
700
|
function extractInnerFields(fields, path, maxDepth) {
|
|
609
701
|
return path.length >= maxDepth ? [] : fields.reduce((acc, field) => {
|
|
610
|
-
const thisFieldWithPath = { path: [...path, field.name],
|
|
702
|
+
const thisFieldWithPath = __spreadValues$1({ path: [...path, field.name] }, field);
|
|
611
703
|
if (field.type.jsonType === "object") {
|
|
612
704
|
const innerFields = extractInnerFields(
|
|
613
705
|
field.type.fields,
|
|
@@ -629,8 +721,16 @@ function extractInnerFields(fields, path, maxDepth) {
|
|
|
629
721
|
return [...acc, thisFieldWithPath];
|
|
630
722
|
}, []);
|
|
631
723
|
}
|
|
724
|
+
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) => {
|
|
725
|
+
for (var prop in b || (b = {}))
|
|
726
|
+
__hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
727
|
+
if (__getOwnPropSymbols)
|
|
728
|
+
for (var prop of __getOwnPropSymbols(b))
|
|
729
|
+
__propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
730
|
+
return a;
|
|
731
|
+
}, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
632
732
|
const internationalizedArray = sanity.definePlugin((config) => {
|
|
633
|
-
const pluginConfig = {
|
|
733
|
+
const pluginConfig = __spreadValues(__spreadValues({}, CONFIG_DEFAULT), config), {
|
|
634
734
|
apiVersion = "2022-11-27",
|
|
635
735
|
select,
|
|
636
736
|
languages,
|
|
@@ -662,10 +762,9 @@ const internationalizedArray = sanity.definePlugin((config) => {
|
|
|
662
762
|
(name) => name.startsWith("internationalizedArray")
|
|
663
763
|
) ? props.renderDefault(props) : /* @__PURE__ */ jsxRuntime.jsx(
|
|
664
764
|
InternationalizedArrayProvider,
|
|
665
|
-
{
|
|
666
|
-
...props,
|
|
765
|
+
__spreadProps(__spreadValues({}, props), {
|
|
667
766
|
internationalizedArray: pluginConfig
|
|
668
|
-
}
|
|
767
|
+
})
|
|
669
768
|
)
|
|
670
769
|
}
|
|
671
770
|
},
|