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.mjs
CHANGED
|
@@ -3,32 +3,45 @@ import { suspend as suspend$1 } from "suspend-react";
|
|
|
3
3
|
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
4
4
|
import { isSanityDocument, setIfMissing, insert, PatchEvent, useClient, useWorkspace, defineDocumentFieldAction, useFormValue, set, ArrayOfObjectsItem, MemberItemError, defineField, unset, isDocumentSchemaType, definePlugin, isObjectInputProps } from "sanity";
|
|
5
5
|
import { useLanguageFilterStudioContext } from "@sanity/language-filter";
|
|
6
|
-
import { Grid, Button, useToast, Stack, Box, Text, Card, Code, Label, MenuButton, Menu, MenuItem, Flex
|
|
6
|
+
import { Grid, Button, useToast, Stack, Box, Text, Card, Code, Spinner, Label, MenuButton, Menu, MenuItem, Flex } from "@sanity/ui";
|
|
7
7
|
import equal from "fast-deep-equal";
|
|
8
8
|
import { memo, useCallback, createContext, useContext, useDeferredValue, useMemo, useEffect, createElement } from "react";
|
|
9
9
|
import { useDocumentPane } from "sanity/structure";
|
|
10
10
|
import { AddIcon, TranslateIcon, RemoveCircleIcon } from "@sanity/icons";
|
|
11
11
|
import get from "lodash/get.js";
|
|
12
|
-
const namespace = "sanity-plugin-internationalized-array", version = "v1", preload = (fn) => suspend.preload(() => fn(), [version, namespace]), clear = () => suspend.clear([version, namespace]), peek = (selectedValue) => suspend.peek([version, namespace, selectedValue]), MAX_COLUMNS =
|
|
12
|
+
const namespace = "sanity-plugin-internationalized-array", version = "v1", preload = (fn) => suspend.preload(() => fn(), [version, namespace]), clear = () => suspend.clear([version, namespace]), peek = (selectedValue) => suspend.peek([version, namespace, selectedValue]), MAX_COLUMNS = {
|
|
13
|
+
codeOnly: 5,
|
|
14
|
+
titleOnly: 4,
|
|
15
|
+
titleAndCode: 3
|
|
16
|
+
}, CONFIG_DEFAULT = {
|
|
13
17
|
languages: [],
|
|
14
18
|
select: {},
|
|
15
19
|
defaultLanguages: [],
|
|
16
20
|
fieldTypes: [],
|
|
17
21
|
apiVersion: "2022-11-27",
|
|
18
22
|
buttonLocations: ["field"],
|
|
19
|
-
buttonAddAll: !0
|
|
20
|
-
|
|
23
|
+
buttonAddAll: !0,
|
|
24
|
+
languageDisplay: "codeOnly"
|
|
25
|
+
};
|
|
26
|
+
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) => {
|
|
27
|
+
for (var prop in b || (b = {}))
|
|
28
|
+
__hasOwnProp$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
29
|
+
if (__getOwnPropSymbols$9)
|
|
30
|
+
for (var prop of __getOwnPropSymbols$9(b))
|
|
31
|
+
__propIsEnum$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
32
|
+
return a;
|
|
33
|
+
}, __spreadProps$8 = (a, b) => __defProps$8(a, __getOwnPropDescs$8(b));
|
|
34
|
+
const getDocumentsToTranslate = (value, rootPath = []) => {
|
|
21
35
|
if (Array.isArray(value)) {
|
|
22
36
|
const arrayRootPath = [...rootPath], internationalizedValues = value.filter((item) => {
|
|
23
37
|
if (Array.isArray(item)) return !1;
|
|
24
38
|
if (typeof item == "object") {
|
|
25
|
-
const type = item
|
|
26
|
-
return type
|
|
39
|
+
const type = item == null ? void 0 : item._type;
|
|
40
|
+
return (type == null ? void 0 : type.startsWith("internationalizedArray")) && (type == null ? void 0 : type.endsWith("Value"));
|
|
27
41
|
}
|
|
28
42
|
return !1;
|
|
29
43
|
});
|
|
30
|
-
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => ({
|
|
31
|
-
...internationalizedValue,
|
|
44
|
+
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => __spreadProps$8(__spreadValues$9({}, internationalizedValue), {
|
|
32
45
|
path: arrayRootPath,
|
|
33
46
|
pathString: arrayRootPath.join(".")
|
|
34
47
|
})) : value.length > 0 ? value.map(
|
|
@@ -46,22 +59,39 @@ const namespace = "sanity-plugin-internationalized-array", version = "v1", prelo
|
|
|
46
59
|
}
|
|
47
60
|
return [];
|
|
48
61
|
};
|
|
62
|
+
function getLanguageDisplay(languageDisplay, title, code) {
|
|
63
|
+
return languageDisplay === "codeOnly" ? code.toUpperCase() : languageDisplay === "titleOnly" ? title : languageDisplay === "titleAndCode" ? `${title} (${code.toUpperCase()})` : title;
|
|
64
|
+
}
|
|
49
65
|
function AddButtons(props) {
|
|
50
|
-
const { languages, readOnly, value, onClick } = props;
|
|
51
|
-
return languages.length > 0 ? /* @__PURE__ */ jsx(
|
|
52
|
-
|
|
66
|
+
const { languages, readOnly, value, onClick } = props, { languageDisplay } = useInternationalizedArrayContext();
|
|
67
|
+
return languages.length > 0 ? /* @__PURE__ */ jsx(
|
|
68
|
+
Grid,
|
|
53
69
|
{
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
70
|
+
columns: Math.min(languages.length, MAX_COLUMNS[languageDisplay]),
|
|
71
|
+
gap: 2,
|
|
72
|
+
children: languages.map((language) => {
|
|
73
|
+
const languageTitle = getLanguageDisplay(
|
|
74
|
+
languageDisplay,
|
|
75
|
+
language.title,
|
|
76
|
+
language.id
|
|
77
|
+
);
|
|
78
|
+
return /* @__PURE__ */ jsx(
|
|
79
|
+
Button,
|
|
80
|
+
{
|
|
81
|
+
tone: "primary",
|
|
82
|
+
mode: "ghost",
|
|
83
|
+
fontSize: 1,
|
|
84
|
+
disabled: readOnly || !!(value != null && value.find((item) => item._key === language.id)),
|
|
85
|
+
text: languageTitle,
|
|
86
|
+
icon: languages.length > MAX_COLUMNS[languageDisplay] && languageDisplay === "codeOnly" ? void 0 : AddIcon,
|
|
87
|
+
value: language.id,
|
|
88
|
+
onClick
|
|
89
|
+
},
|
|
90
|
+
language.id
|
|
91
|
+
);
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
) : null;
|
|
65
95
|
}
|
|
66
96
|
var AddButtons$1 = memo(AddButtons);
|
|
67
97
|
function DocumentAddButtons(props) {
|
|
@@ -76,7 +106,7 @@ function DocumentAddButtons(props) {
|
|
|
76
106
|
return;
|
|
77
107
|
}
|
|
78
108
|
const alreadyTranslated = documentsToTranslation.filter(
|
|
79
|
-
(translation) => translation
|
|
109
|
+
(translation) => (translation == null ? void 0 : translation._key) === languageId
|
|
80
110
|
), removeDuplicates = documentsToTranslation.reduce((filteredTranslations, translation) => alreadyTranslated.filter(
|
|
81
111
|
(alreadyTranslation) => alreadyTranslation.pathString === translation.pathString
|
|
82
112
|
).length > 0 || filteredTranslations.filter(
|
|
@@ -128,20 +158,28 @@ const getSelectedValue = (select, document) => {
|
|
|
128
158
|
for (const [key, path] of Object.entries(selection)) {
|
|
129
159
|
let value = get(document, path);
|
|
130
160
|
Array.isArray(value) && (value = value.filter(
|
|
131
|
-
(item) => typeof item == "object" ? item
|
|
161
|
+
(item) => typeof item == "object" ? (item == null ? void 0 : item._type) === "reference" && "_ref" in item : !0
|
|
132
162
|
)), selectedValue[key] = value;
|
|
133
163
|
}
|
|
134
164
|
return selectedValue;
|
|
135
|
-
}
|
|
136
|
-
|
|
165
|
+
};
|
|
166
|
+
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) => {
|
|
167
|
+
for (var prop in b || (b = {}))
|
|
168
|
+
__hasOwnProp$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
169
|
+
if (__getOwnPropSymbols$8)
|
|
170
|
+
for (var prop of __getOwnPropSymbols$8(b))
|
|
171
|
+
__propIsEnum$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
172
|
+
return a;
|
|
173
|
+
}, __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
|
|
174
|
+
const InternationalizedArrayContext = createContext(__spreadProps$7(__spreadValues$8({}, CONFIG_DEFAULT), {
|
|
137
175
|
languages: [],
|
|
138
176
|
filteredLanguages: []
|
|
139
|
-
});
|
|
177
|
+
}));
|
|
140
178
|
function useInternationalizedArrayContext() {
|
|
141
179
|
return useContext(InternationalizedArrayContext);
|
|
142
180
|
}
|
|
143
181
|
function InternationalizedArrayProvider(props) {
|
|
144
|
-
const { internationalizedArray: internationalizedArray2 } = props, client = useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = useWorkspace(), { formState } = useDocumentPane(), deferredDocument = useDeferredValue(formState
|
|
182
|
+
const { internationalizedArray: internationalizedArray2 } = props, client = useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = useWorkspace(), { formState } = useDocumentPane(), deferredDocument = useDeferredValue(formState == null ? void 0 : formState.value), selectedValue = useMemo(
|
|
145
183
|
() => getSelectedValue(internationalizedArray2.select, deferredDocument),
|
|
146
184
|
[internationalizedArray2.select, deferredDocument]
|
|
147
185
|
), languages = Array.isArray(internationalizedArray2.languages) ? internationalizedArray2.languages : suspend$1(
|
|
@@ -155,7 +193,7 @@ function InternationalizedArrayProvider(props) {
|
|
|
155
193
|
(language) => selectedLanguageIds.includes(language.id)
|
|
156
194
|
) : languages;
|
|
157
195
|
}, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document"), context = useMemo(
|
|
158
|
-
() => ({
|
|
196
|
+
() => __spreadProps$7(__spreadValues$8({}, internationalizedArray2), { languages, filteredLanguages }),
|
|
159
197
|
[filteredLanguages, internationalizedArray2, languages]
|
|
160
198
|
);
|
|
161
199
|
return /* @__PURE__ */ jsx(InternationalizedArrayContext.Provider, { value: context, children: showDocumentButtons ? /* @__PURE__ */ jsxs(Stack, { space: 5, children: [
|
|
@@ -174,11 +212,19 @@ function checkAllLanguagesArePresent(languages, value) {
|
|
|
174
212
|
return languagesInUseIds.length === filteredLanguageIds.length && languagesInUseIds.every((l) => filteredLanguageIds.includes(l));
|
|
175
213
|
}
|
|
176
214
|
function createAddAllTitle(value, languages) {
|
|
177
|
-
return value
|
|
215
|
+
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
216
|
}
|
|
179
217
|
function createValueSchemaTypeName(schemaType) {
|
|
180
218
|
return `${schemaType.name}Value`;
|
|
181
219
|
}
|
|
220
|
+
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) => {
|
|
221
|
+
for (var prop in b || (b = {}))
|
|
222
|
+
__hasOwnProp$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
223
|
+
if (__getOwnPropSymbols$7)
|
|
224
|
+
for (var prop of __getOwnPropSymbols$7(b))
|
|
225
|
+
__propIsEnum$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
226
|
+
return a;
|
|
227
|
+
}, __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
|
|
182
228
|
function createAddLanguagePatches(config) {
|
|
183
229
|
const {
|
|
184
230
|
addLanguageKeys,
|
|
@@ -187,15 +233,13 @@ function createAddLanguagePatches(config) {
|
|
|
187
233
|
filteredLanguages,
|
|
188
234
|
value,
|
|
189
235
|
path = []
|
|
190
|
-
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => ({
|
|
191
|
-
...itemBase,
|
|
236
|
+
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => __spreadProps$6(__spreadValues$7({}, itemBase), {
|
|
192
237
|
_key: id
|
|
193
238
|
})) : filteredLanguages.filter(
|
|
194
|
-
(language) => value
|
|
195
|
-
).map((language) => ({
|
|
196
|
-
...itemBase,
|
|
239
|
+
(language) => value != null && value.length ? !value.find((v) => v._key === language.id) : !0
|
|
240
|
+
).map((language) => __spreadProps$6(__spreadValues$7({}, itemBase), {
|
|
197
241
|
_key: language.id
|
|
198
|
-
})), languagesInUse = value
|
|
242
|
+
})), languagesInUse = value != null && value.length ? value.map((v) => v) : [];
|
|
199
243
|
return newItems.map((item) => {
|
|
200
244
|
const languageIndex = languages.findIndex((l) => item._key === l.id), remainingLanguages = languages.slice(languageIndex + 1), nextLanguageIndex = languagesInUse.findIndex(
|
|
201
245
|
(l) => (
|
|
@@ -213,7 +257,7 @@ function createAddLanguagePatches(config) {
|
|
|
213
257
|
});
|
|
214
258
|
}
|
|
215
259
|
const createTranslateFieldActions = (fieldActionProps, { languages, filteredLanguages }) => languages.map((language) => {
|
|
216
|
-
const value = useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!value
|
|
260
|
+
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
261
|
const { schemaType, path } = fieldActionProps, addLanguageKeys = [language.id], patches = createAddLanguagePatches({
|
|
218
262
|
addLanguageKeys,
|
|
219
263
|
schemaType,
|
|
@@ -255,7 +299,8 @@ const createTranslateFieldActions = (fieldActionProps, { languages, filteredLang
|
|
|
255
299
|
}, internationalizedArrayFieldAction = defineDocumentFieldAction({
|
|
256
300
|
name: "internationalizedArray",
|
|
257
301
|
useAction(fieldActionProps) {
|
|
258
|
-
|
|
302
|
+
var _a, _b;
|
|
303
|
+
const isInternationalizedArrayField = (_b = (_a = fieldActionProps == null ? void 0 : fieldActionProps.schemaType) == null ? void 0 : _a.type) == null ? void 0 : _b.name.startsWith(
|
|
259
304
|
"internationalizedArray"
|
|
260
305
|
), { languages, filteredLanguages } = useInternationalizedArrayContext(), translateFieldActions = createTranslateFieldActions(
|
|
261
306
|
fieldActionProps,
|
|
@@ -311,6 +356,14 @@ function Feedback() {
|
|
|
311
356
|
/* @__PURE__ */ jsx(Card, { padding: 2, border: !0, radius: 2, children: /* @__PURE__ */ jsx(Code, { size: 1, language: "javascript", children: JSON.stringify(schemaExample, null, 2) }) })
|
|
312
357
|
] }) });
|
|
313
358
|
}
|
|
359
|
+
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) => {
|
|
360
|
+
for (var prop in b || (b = {}))
|
|
361
|
+
__hasOwnProp$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
362
|
+
if (__getOwnPropSymbols$6)
|
|
363
|
+
for (var prop of __getOwnPropSymbols$6(b))
|
|
364
|
+
__propIsEnum$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
365
|
+
return a;
|
|
366
|
+
}, __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
|
|
314
367
|
function InternationalizedArray(props) {
|
|
315
368
|
const { members, value, schemaType, onChange } = props, readOnly = typeof schemaType.readOnly == "boolean" ? schemaType.readOnly : !1, toast = useToast(), {
|
|
316
369
|
languages,
|
|
@@ -332,9 +385,10 @@ function InternationalizedArray(props) {
|
|
|
332
385
|
[languageFilterEnabled, members, languageFilterOptions, selectedLanguageIds]
|
|
333
386
|
), handleAddLanguage = useCallback(
|
|
334
387
|
async (param) => {
|
|
335
|
-
|
|
388
|
+
var _a;
|
|
389
|
+
if (!(filteredLanguages != null && filteredLanguages.length))
|
|
336
390
|
return;
|
|
337
|
-
const addLanguageKeys = Array.isArray(param) ? param : [param
|
|
391
|
+
const addLanguageKeys = Array.isArray(param) ? param : [(_a = param == null ? void 0 : param.currentTarget) == null ? void 0 : _a.value].filter(Boolean), patches = createAddLanguagePatches({
|
|
338
392
|
addLanguageKeys,
|
|
339
393
|
schemaType,
|
|
340
394
|
languages,
|
|
@@ -356,23 +410,23 @@ function InternationalizedArray(props) {
|
|
|
356
410
|
defaultLanguages
|
|
357
411
|
]);
|
|
358
412
|
const handleRestoreOrder = useCallback(() => {
|
|
359
|
-
if (!value
|
|
413
|
+
if (!(value != null && value.length) || !(languages != null && languages.length))
|
|
360
414
|
return;
|
|
361
415
|
const updatedValue = value.reduce((acc, v) => {
|
|
362
|
-
const newIndex = languages.findIndex((l) => l.id === v
|
|
416
|
+
const newIndex = languages.findIndex((l) => l.id === (v == null ? void 0 : v._key));
|
|
363
417
|
return newIndex > -1 && (acc[newIndex] = v), acc;
|
|
364
418
|
}, []).filter(Boolean);
|
|
365
|
-
value
|
|
419
|
+
(value == null ? void 0 : value.length) !== updatedValue.length && toast.push({
|
|
366
420
|
title: "There was an error reordering languages",
|
|
367
421
|
status: "warning"
|
|
368
422
|
}), onChange(set(updatedValue));
|
|
369
|
-
}, [toast, languages, onChange, value]), allKeysAreLanguages = useMemo(() => !value
|
|
370
|
-
() => languages && languages.length > 1 ? languages.filter((l) => value
|
|
423
|
+
}, [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(
|
|
424
|
+
() => languages && languages.length > 1 ? languages.filter((l) => value == null ? void 0 : value.find((v) => v._key === l.id)) : [],
|
|
371
425
|
[languages, value]
|
|
372
|
-
), languagesOutOfOrder = useMemo(() => !value
|
|
426
|
+
), languagesOutOfOrder = useMemo(() => !(value != null && value.length) || !languagesInUse.length ? [] : value.map(
|
|
373
427
|
(v, vIndex) => vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v
|
|
374
428
|
).filter(Boolean), [value, languagesInUse]), languagesAreValid = useMemo(
|
|
375
|
-
() => !languages
|
|
429
|
+
() => !(languages != null && languages.length) || (languages == null ? void 0 : languages.length) && languages.every((item) => item.id && item.title),
|
|
376
430
|
[languages]
|
|
377
431
|
);
|
|
378
432
|
useEffect(() => {
|
|
@@ -387,17 +441,16 @@ function InternationalizedArray(props) {
|
|
|
387
441
|
const addButtonsAreVisible = (
|
|
388
442
|
// Plugin was configured to display buttons here (default!)
|
|
389
443
|
buttonLocations.includes("field") && // There's at least one language visible
|
|
390
|
-
filteredLanguages
|
|
444
|
+
(filteredLanguages == null ? void 0 : filteredLanguages.length) > 0 && // Not every language has a value yet
|
|
391
445
|
!allLanguagesArePresent
|
|
392
|
-
), fieldHasMembers = members
|
|
446
|
+
), fieldHasMembers = (members == null ? void 0 : members.length) > 0;
|
|
393
447
|
return /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
394
448
|
fieldHasMembers ? /* @__PURE__ */ jsx(Fragment, { children: filteredMembers.map((member) => member.kind === "item" ? /* @__PURE__ */ createElement(
|
|
395
449
|
ArrayOfObjectsItem,
|
|
396
|
-
{
|
|
397
|
-
...props,
|
|
450
|
+
__spreadProps$5(__spreadValues$6({}, props), {
|
|
398
451
|
key: member.key,
|
|
399
452
|
member
|
|
400
|
-
}
|
|
453
|
+
})
|
|
401
454
|
) : /* @__PURE__ */ jsx(MemberItemError, { member }, member.key)) }) : null,
|
|
402
455
|
!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
456
|
addButtonsAreVisible ? /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
@@ -425,9 +478,17 @@ function InternationalizedArray(props) {
|
|
|
425
478
|
] });
|
|
426
479
|
}
|
|
427
480
|
function getLanguagesFieldOption(schemaType) {
|
|
428
|
-
|
|
481
|
+
var _a;
|
|
482
|
+
return schemaType ? ((_a = schemaType.options) == null ? void 0 : _a.languages) || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
429
483
|
}
|
|
430
|
-
var
|
|
484
|
+
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) => {
|
|
485
|
+
for (var prop in b || (b = {}))
|
|
486
|
+
__hasOwnProp$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
487
|
+
if (__getOwnPropSymbols$5)
|
|
488
|
+
for (var prop of __getOwnPropSymbols$5(b))
|
|
489
|
+
__propIsEnum$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
490
|
+
return a;
|
|
491
|
+
}, __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b)), array = (config) => {
|
|
431
492
|
const { apiVersion, select, languages, type } = config, typeName = typeof type == "string" ? type : type.name, arrayName = createFieldName(typeName), objectName = createFieldName(typeName, !0);
|
|
432
493
|
return defineField({
|
|
433
494
|
name: arrayName,
|
|
@@ -443,11 +504,10 @@ var array = (config) => {
|
|
|
443
504
|
languages
|
|
444
505
|
},
|
|
445
506
|
of: [
|
|
446
|
-
defineField({
|
|
447
|
-
...typeof type == "string" ? {} : type,
|
|
507
|
+
defineField(__spreadProps$4(__spreadValues$5({}, typeof type == "string" ? {} : type), {
|
|
448
508
|
name: objectName,
|
|
449
509
|
type: objectName
|
|
450
|
-
})
|
|
510
|
+
}))
|
|
451
511
|
],
|
|
452
512
|
// @ts-expect-error - fix typings
|
|
453
513
|
validation: (rule) => rule.custom(async (value, context) => {
|
|
@@ -455,10 +515,10 @@ var array = (config) => {
|
|
|
455
515
|
return !0;
|
|
456
516
|
const selectedValue = getSelectedValue(select, context.document), client = context.getClient({ apiVersion });
|
|
457
517
|
let contextLanguages = [];
|
|
458
|
-
const languagesFieldOption = getLanguagesFieldOption(context
|
|
518
|
+
const languagesFieldOption = getLanguagesFieldOption(context == null ? void 0 : context.type);
|
|
459
519
|
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
520
|
return `Cannot be more than ${contextLanguages.length === 1 ? "1 item" : `${contextLanguages.length} items`}`;
|
|
461
|
-
const nonLanguageKeys = value
|
|
521
|
+
const nonLanguageKeys = value != null && value.length ? value.filter(
|
|
462
522
|
(item) => !contextLanguages.find((language) => item._key === language.id)
|
|
463
523
|
) : [];
|
|
464
524
|
if (nonLanguageKeys.length)
|
|
@@ -466,26 +526,31 @@ var array = (config) => {
|
|
|
466
526
|
message: "Array item keys must be valid languages registered to the field type",
|
|
467
527
|
paths: nonLanguageKeys.map((item) => [{ _key: item._key }])
|
|
468
528
|
};
|
|
469
|
-
const valuesByLanguage = value
|
|
470
|
-
...acc,
|
|
529
|
+
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
530
|
[cur._key]: [cur]
|
|
472
|
-
}, {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => item
|
|
531
|
+
}), {}) : {}, duplicateValues = Object.values(valuesByLanguage).filter((item) => (item == null ? void 0 : item.length) > 1).flat();
|
|
473
532
|
return duplicateValues.length ? {
|
|
474
533
|
message: "There can only be one field per language",
|
|
475
534
|
paths: duplicateValues.map((item) => [{ _key: item._key }])
|
|
476
535
|
} : !0;
|
|
477
536
|
})
|
|
478
537
|
});
|
|
479
|
-
}
|
|
538
|
+
}, __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) => {
|
|
539
|
+
for (var prop in b || (b = {}))
|
|
540
|
+
__hasOwnProp$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
541
|
+
if (__getOwnPropSymbols$4)
|
|
542
|
+
for (var prop of __getOwnPropSymbols$4(b))
|
|
543
|
+
__propIsEnum$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
544
|
+
return a;
|
|
545
|
+
}, __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
|
|
480
546
|
function InternationalizedField(props) {
|
|
481
|
-
return props.schemaType.name === "reference" && props.value ? props.renderDefault({
|
|
482
|
-
...props,
|
|
547
|
+
return props.schemaType.name === "reference" && props.value ? props.renderDefault(__spreadProps$3(__spreadValues$4({}, props), {
|
|
483
548
|
title: "",
|
|
484
549
|
level: 0
|
|
485
|
-
}) : props.children;
|
|
550
|
+
})) : props.children;
|
|
486
551
|
}
|
|
487
552
|
function getToneFromValidation(validations) {
|
|
488
|
-
if (!validations
|
|
553
|
+
if (!(validations != null && validations.length))
|
|
489
554
|
return;
|
|
490
555
|
const validationLevels = validations.map((v) => v.level);
|
|
491
556
|
if (validationLevels.includes("error"))
|
|
@@ -493,11 +558,18 @@ function getToneFromValidation(validations) {
|
|
|
493
558
|
if (validationLevels.includes("warning"))
|
|
494
559
|
return "caution";
|
|
495
560
|
}
|
|
561
|
+
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) => {
|
|
562
|
+
for (var prop in b || (b = {}))
|
|
563
|
+
__hasOwnProp$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
564
|
+
if (__getOwnPropSymbols$3)
|
|
565
|
+
for (var prop of __getOwnPropSymbols$3(b))
|
|
566
|
+
__propIsEnum$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
567
|
+
return a;
|
|
568
|
+
}, __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
496
569
|
function InternationalizedInput(props) {
|
|
497
570
|
const parentValue = useFormValue(
|
|
498
571
|
props.path.slice(0, -1)
|
|
499
|
-
), inlineProps = {
|
|
500
|
-
...props.inputProps,
|
|
572
|
+
), inlineProps = __spreadProps$2(__spreadValues$3({}, props.inputProps), {
|
|
501
573
|
// This is the magic that makes inline editing work?
|
|
502
574
|
members: props.inputProps.members.filter(
|
|
503
575
|
(m) => m.kind === "field" && m.name === "value"
|
|
@@ -505,34 +577,41 @@ function InternationalizedInput(props) {
|
|
|
505
577
|
// This just overrides the type
|
|
506
578
|
// TODO: Remove this as it shouldn't be necessary?
|
|
507
579
|
value: props.value
|
|
508
|
-
}, { validation, value, onChange, readOnly } = inlineProps, { languages } = useInternationalizedArrayContext(), languageKeysInUse = useMemo(
|
|
509
|
-
() =>
|
|
580
|
+
}), { validation, value, onChange, readOnly } = inlineProps, { languages, languageDisplay } = useInternationalizedArrayContext(), languageKeysInUse = useMemo(
|
|
581
|
+
() => {
|
|
582
|
+
var _a;
|
|
583
|
+
return (_a = parentValue == null ? void 0 : parentValue.map((v) => v._key)) != null ? _a : [];
|
|
584
|
+
},
|
|
510
585
|
[parentValue]
|
|
511
|
-
), keyIsValid = languages
|
|
586
|
+
), keyIsValid = languages != null && languages.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = useCallback(
|
|
512
587
|
(event) => {
|
|
513
|
-
|
|
514
|
-
|
|
588
|
+
var _a;
|
|
589
|
+
const languageId = (_a = event == null ? void 0 : event.currentTarget) == null ? void 0 : _a.value;
|
|
590
|
+
!value || !(languages != null && languages.length) || !languages.find((l) => l.id === languageId) || onChange([set(languageId, ["_key"])]);
|
|
515
591
|
},
|
|
516
592
|
[onChange, value, languages]
|
|
517
593
|
), handleUnset = useCallback(() => {
|
|
518
594
|
onChange(unset());
|
|
519
595
|
}, [onChange]);
|
|
520
|
-
|
|
521
|
-
|
|
596
|
+
if (!languages)
|
|
597
|
+
return /* @__PURE__ */ jsx(Spinner, {});
|
|
598
|
+
const language = languages.find((l) => l.id === value._key), languageTitle = keyIsValid && language ? getLanguageDisplay(languageDisplay, language.title, language.id) : "";
|
|
599
|
+
return /* @__PURE__ */ jsx(Card, { paddingTop: 2, tone: getToneFromValidation(validation), children: /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
600
|
+
/* @__PURE__ */ jsx(Card, { tone: "inherit", children: keyIsValid ? /* @__PURE__ */ jsx(Label, { muted: !0, size: 1, children: languageTitle }) : /* @__PURE__ */ jsx(
|
|
522
601
|
MenuButton,
|
|
523
602
|
{
|
|
524
603
|
button: /* @__PURE__ */ jsx(Button, { fontSize: 1, text: `Change "${value._key}"` }),
|
|
525
604
|
id: `${value._key}-change-key`,
|
|
526
|
-
menu: /* @__PURE__ */ jsx(Menu, { children: languages.map((
|
|
605
|
+
menu: /* @__PURE__ */ jsx(Menu, { children: languages.map((lang) => /* @__PURE__ */ jsx(
|
|
527
606
|
MenuItem,
|
|
528
607
|
{
|
|
529
|
-
disabled: languageKeysInUse.includes(
|
|
608
|
+
disabled: languageKeysInUse.includes(lang.id),
|
|
530
609
|
fontSize: 1,
|
|
531
|
-
text:
|
|
532
|
-
value:
|
|
610
|
+
text: lang.id.toLocaleUpperCase(),
|
|
611
|
+
value: lang.id,
|
|
533
612
|
onClick: handleKeyChange
|
|
534
613
|
},
|
|
535
|
-
|
|
614
|
+
lang.id
|
|
536
615
|
)) }),
|
|
537
616
|
popover: { portal: !0 }
|
|
538
617
|
}
|
|
@@ -550,9 +629,16 @@ function InternationalizedInput(props) {
|
|
|
550
629
|
}
|
|
551
630
|
) })
|
|
552
631
|
] })
|
|
553
|
-
] }) })
|
|
632
|
+
] }) });
|
|
554
633
|
}
|
|
555
|
-
var
|
|
634
|
+
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) => {
|
|
635
|
+
for (var prop in b || (b = {}))
|
|
636
|
+
__hasOwnProp$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
637
|
+
if (__getOwnPropSymbols$2)
|
|
638
|
+
for (var prop of __getOwnPropSymbols$2(b))
|
|
639
|
+
__propIsEnum$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
640
|
+
return a;
|
|
641
|
+
}, __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b)), object = (config) => {
|
|
556
642
|
const { type } = config, typeName = typeof type == "string" ? type : type.name, objectName = createFieldName(typeName, !0);
|
|
557
643
|
return defineField({
|
|
558
644
|
name: objectName,
|
|
@@ -574,13 +660,12 @@ var object = (config) => {
|
|
|
574
660
|
})
|
|
575
661
|
) : (
|
|
576
662
|
// Pass in the configured options, but overwrite the name
|
|
577
|
-
{
|
|
578
|
-
...type,
|
|
663
|
+
__spreadProps$1(__spreadValues$2({}, type), {
|
|
579
664
|
name: "value",
|
|
580
665
|
components: {
|
|
581
666
|
field: InternationalizedField
|
|
582
667
|
}
|
|
583
|
-
}
|
|
668
|
+
})
|
|
584
669
|
)
|
|
585
670
|
],
|
|
586
671
|
preview: {
|
|
@@ -590,13 +675,20 @@ var object = (config) => {
|
|
|
590
675
|
}
|
|
591
676
|
}
|
|
592
677
|
});
|
|
678
|
+
}, __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) => {
|
|
679
|
+
for (var prop in b || (b = {}))
|
|
680
|
+
__hasOwnProp$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
681
|
+
if (__getOwnPropSymbols$1)
|
|
682
|
+
for (var prop of __getOwnPropSymbols$1(b))
|
|
683
|
+
__propIsEnum$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
684
|
+
return a;
|
|
593
685
|
};
|
|
594
686
|
function flattenSchemaType(schemaType) {
|
|
595
687
|
return isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 3) : (console.error("Schema type is not a document"), []);
|
|
596
688
|
}
|
|
597
689
|
function extractInnerFields(fields, path, maxDepth) {
|
|
598
690
|
return path.length >= maxDepth ? [] : fields.reduce((acc, field) => {
|
|
599
|
-
const thisFieldWithPath = { path: [...path, field.name],
|
|
691
|
+
const thisFieldWithPath = __spreadValues$1({ path: [...path, field.name] }, field);
|
|
600
692
|
if (field.type.jsonType === "object") {
|
|
601
693
|
const innerFields = extractInnerFields(
|
|
602
694
|
field.type.fields,
|
|
@@ -618,8 +710,16 @@ function extractInnerFields(fields, path, maxDepth) {
|
|
|
618
710
|
return [...acc, thisFieldWithPath];
|
|
619
711
|
}, []);
|
|
620
712
|
}
|
|
713
|
+
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) => {
|
|
714
|
+
for (var prop in b || (b = {}))
|
|
715
|
+
__hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
716
|
+
if (__getOwnPropSymbols)
|
|
717
|
+
for (var prop of __getOwnPropSymbols(b))
|
|
718
|
+
__propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
719
|
+
return a;
|
|
720
|
+
}, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
621
721
|
const internationalizedArray = definePlugin((config) => {
|
|
622
|
-
const pluginConfig = {
|
|
722
|
+
const pluginConfig = __spreadValues(__spreadValues({}, CONFIG_DEFAULT), config), {
|
|
623
723
|
apiVersion = "2022-11-27",
|
|
624
724
|
select,
|
|
625
725
|
languages,
|
|
@@ -651,10 +751,9 @@ const internationalizedArray = definePlugin((config) => {
|
|
|
651
751
|
(name) => name.startsWith("internationalizedArray")
|
|
652
752
|
) ? props.renderDefault(props) : /* @__PURE__ */ jsx(
|
|
653
753
|
InternationalizedArrayProvider,
|
|
654
|
-
{
|
|
655
|
-
...props,
|
|
754
|
+
__spreadProps(__spreadValues({}, props), {
|
|
656
755
|
internationalizedArray: pluginConfig
|
|
657
|
-
}
|
|
756
|
+
})
|
|
658
757
|
)
|
|
659
758
|
}
|
|
660
759
|
},
|