sanity-plugin-internationalized-array 3.2.0 → 3.2.2
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 +97 -167
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +96 -166
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +97 -167
- package/lib/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/components/InternationalizedArray.tsx +11 -2
package/lib/index.esm.js
CHANGED
|
@@ -5,7 +5,7 @@ import { isSanityDocument, useSchema, setIfMissing, insert, PatchEvent, useClien
|
|
|
5
5
|
import { useLanguageFilterStudioContext } from "@sanity/language-filter";
|
|
6
6
|
import { Grid, Button, useToast, Stack, Box, Text, Card, Code, Spinner, Label, MenuButton, Menu, MenuItem, Flex, Tooltip } from "@sanity/ui";
|
|
7
7
|
import equal from "fast-deep-equal";
|
|
8
|
-
import { memo, useCallback,
|
|
8
|
+
import { memo, useCallback, useContext, createContext, 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";
|
|
@@ -47,26 +47,18 @@ const namespace = "sanity-plugin-internationalized-array", version = "v1", funct
|
|
|
47
47
|
buttonLocations: ["field"],
|
|
48
48
|
buttonAddAll: !0,
|
|
49
49
|
languageDisplay: "codeOnly"
|
|
50
|
-
}
|
|
51
|
-
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) => {
|
|
52
|
-
for (var prop in b || (b = {}))
|
|
53
|
-
__hasOwnProp$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
54
|
-
if (__getOwnPropSymbols$9)
|
|
55
|
-
for (var prop of __getOwnPropSymbols$9(b))
|
|
56
|
-
__propIsEnum$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
|
|
57
|
-
return a;
|
|
58
|
-
}, __spreadProps$8 = (a, b) => __defProps$8(a, __getOwnPropDescs$8(b));
|
|
59
|
-
const getDocumentsToTranslate = (value, rootPath = []) => {
|
|
50
|
+
}, getDocumentsToTranslate = (value, rootPath = []) => {
|
|
60
51
|
if (Array.isArray(value)) {
|
|
61
52
|
const arrayRootPath = [...rootPath], internationalizedValues = value.filter((item) => {
|
|
62
53
|
if (Array.isArray(item)) return !1;
|
|
63
54
|
if (typeof item == "object") {
|
|
64
|
-
const type = item
|
|
65
|
-
return
|
|
55
|
+
const type = item?._type;
|
|
56
|
+
return type?.startsWith("internationalizedArray") && type?.endsWith("Value");
|
|
66
57
|
}
|
|
67
58
|
return !1;
|
|
68
59
|
});
|
|
69
|
-
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) =>
|
|
60
|
+
return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => ({
|
|
61
|
+
...internationalizedValue,
|
|
70
62
|
path: arrayRootPath,
|
|
71
63
|
pathString: arrayRootPath.join(".")
|
|
72
64
|
})) : value.length > 0 ? value.map(
|
|
@@ -87,7 +79,7 @@ const getDocumentsToTranslate = (value, rootPath = []) => {
|
|
|
87
79
|
function getLanguageDisplay(languageDisplay, title, code) {
|
|
88
80
|
return languageDisplay === "codeOnly" ? code.toUpperCase() : languageDisplay === "titleOnly" ? title : languageDisplay === "titleAndCode" ? `${title} (${code.toUpperCase()})` : title;
|
|
89
81
|
}
|
|
90
|
-
function AddButtons(props) {
|
|
82
|
+
function AddButtons$1(props) {
|
|
91
83
|
const { languages, readOnly, value, onClick } = props, { languageDisplay } = useInternationalizedArrayContext();
|
|
92
84
|
return languages.length > 0 ? /* @__PURE__ */ jsx(
|
|
93
85
|
Grid,
|
|
@@ -106,7 +98,7 @@ function AddButtons(props) {
|
|
|
106
98
|
tone: "primary",
|
|
107
99
|
mode: "ghost",
|
|
108
100
|
fontSize: 1,
|
|
109
|
-
disabled: readOnly || !!
|
|
101
|
+
disabled: readOnly || !!value?.find((item) => item._key === language.id),
|
|
110
102
|
text: languageTitle,
|
|
111
103
|
icon: languages.length > MAX_COLUMNS[languageDisplay] && languageDisplay === "codeOnly" ? void 0 : AddIcon,
|
|
112
104
|
value: language.id,
|
|
@@ -118,11 +110,10 @@ function AddButtons(props) {
|
|
|
118
110
|
}
|
|
119
111
|
) : null;
|
|
120
112
|
}
|
|
121
|
-
var AddButtons
|
|
113
|
+
var AddButtons = memo(AddButtons$1);
|
|
122
114
|
function DocumentAddButtons(props) {
|
|
123
115
|
const { filteredLanguages } = useInternationalizedArrayContext(), value = isSanityDocument(props.value) ? props.value : void 0, toast = useToast(), { onChange } = useDocumentPane(), schema = useSchema(), documentsToTranslation = getDocumentsToTranslate(value, []), getInitialValueForType = useCallback(
|
|
124
116
|
(typeName) => {
|
|
125
|
-
var _a;
|
|
126
117
|
if (!typeName) return;
|
|
127
118
|
const match = typeName.match(/^internationalizedArray(.+)Value$/);
|
|
128
119
|
if (!match) return;
|
|
@@ -137,13 +128,13 @@ function DocumentAddButtons(props) {
|
|
|
137
128
|
try {
|
|
138
129
|
const schemaType = schema.get(typeName);
|
|
139
130
|
if (schemaType) {
|
|
140
|
-
const valueField =
|
|
131
|
+
const valueField = schemaType?.fields?.find(
|
|
141
132
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
142
133
|
(f) => f.name === "value"
|
|
143
134
|
);
|
|
144
135
|
if (valueField) {
|
|
145
136
|
const fieldType = valueField.type;
|
|
146
|
-
if (
|
|
137
|
+
if (fieldType?.jsonType === "array" || fieldType?.name === "array" || fieldType?.type === "array" || fieldType?.of !== void 0 || arrayBasedTypes.includes(fieldType?.name))
|
|
147
138
|
return [];
|
|
148
139
|
}
|
|
149
140
|
}
|
|
@@ -167,7 +158,7 @@ function DocumentAddButtons(props) {
|
|
|
167
158
|
return;
|
|
168
159
|
}
|
|
169
160
|
const alreadyTranslated = documentsToTranslation.filter(
|
|
170
|
-
(translation) =>
|
|
161
|
+
(translation) => translation?._key === languageId
|
|
171
162
|
), removeDuplicates = documentsToTranslation.reduce((filteredTranslations, translation) => alreadyTranslated.filter(
|
|
172
163
|
(alreadyTranslation) => alreadyTranslation.pathString === translation.pathString
|
|
173
164
|
).length > 0 || filteredTranslations.filter(
|
|
@@ -203,7 +194,7 @@ function DocumentAddButtons(props) {
|
|
|
203
194
|
return /* @__PURE__ */ jsxs(Stack, { space: 3, children: [
|
|
204
195
|
/* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(Text, { size: 1, weight: "semibold", children: "Add translation to internationalized fields" }) }),
|
|
205
196
|
/* @__PURE__ */ jsx(
|
|
206
|
-
AddButtons
|
|
197
|
+
AddButtons,
|
|
207
198
|
{
|
|
208
199
|
languages: filteredLanguages,
|
|
209
200
|
readOnly: !1,
|
|
@@ -220,36 +211,28 @@ const getSelectedValue = (select, document) => {
|
|
|
220
211
|
for (const [key, path] of Object.entries(selection)) {
|
|
221
212
|
let value = get(document, path);
|
|
222
213
|
Array.isArray(value) && (value = value.filter(
|
|
223
|
-
(item) => typeof item == "object" ?
|
|
214
|
+
(item) => typeof item == "object" ? item?._type === "reference" && "_ref" in item : !0
|
|
224
215
|
)), selectedValue[key] = value;
|
|
225
216
|
}
|
|
226
217
|
return selectedValue;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
for (var prop in b || (b = {}))
|
|
230
|
-
__hasOwnProp$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
231
|
-
if (__getOwnPropSymbols$8)
|
|
232
|
-
for (var prop of __getOwnPropSymbols$8(b))
|
|
233
|
-
__propIsEnum$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
|
|
234
|
-
return a;
|
|
235
|
-
}, __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
|
|
236
|
-
const InternationalizedArrayContext = createContext(__spreadProps$7(__spreadValues$8({}, CONFIG_DEFAULT), {
|
|
218
|
+
}, InternationalizedArrayContext = createContext({
|
|
219
|
+
...CONFIG_DEFAULT,
|
|
237
220
|
languages: [],
|
|
238
221
|
filteredLanguages: []
|
|
239
|
-
})
|
|
222
|
+
});
|
|
240
223
|
function useInternationalizedArrayContext() {
|
|
241
224
|
return useContext(InternationalizedArrayContext);
|
|
242
225
|
}
|
|
243
226
|
function InternationalizedArrayProvider(props) {
|
|
244
|
-
const { internationalizedArray: internationalizedArray2 } = props, client = useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = useWorkspace(), { formState } = useDocumentPane(), deferredDocument = useDeferredValue(formState
|
|
227
|
+
const { internationalizedArray: internationalizedArray2 } = props, client = useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = useWorkspace(), { formState } = useDocumentPane(), deferredDocument = useDeferredValue(formState?.value), selectedValue = useMemo(
|
|
245
228
|
() => getSelectedValue(internationalizedArray2.select, deferredDocument),
|
|
246
229
|
[internationalizedArray2.select, deferredDocument]
|
|
247
230
|
), workspaceId = useMemo(() => {
|
|
248
|
-
if (workspace
|
|
231
|
+
if (workspace?.name)
|
|
249
232
|
return workspace.name;
|
|
250
233
|
const workspaceKey = {
|
|
251
|
-
name: workspace
|
|
252
|
-
title: workspace
|
|
234
|
+
name: workspace?.name,
|
|
235
|
+
title: workspace?.title
|
|
253
236
|
// Add other stable properties as needed
|
|
254
237
|
};
|
|
255
238
|
return JSON.stringify(workspaceKey);
|
|
@@ -281,7 +264,7 @@ function InternationalizedArrayProvider(props) {
|
|
|
281
264
|
(language) => selectedLanguageIds.includes(language.id)
|
|
282
265
|
) : languages;
|
|
283
266
|
}, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document"), context = useMemo(
|
|
284
|
-
() =>
|
|
267
|
+
() => ({ ...internationalizedArray2, languages, filteredLanguages }),
|
|
285
268
|
[filteredLanguages, internationalizedArray2, languages]
|
|
286
269
|
);
|
|
287
270
|
return /* @__PURE__ */ jsx(InternationalizedArrayContext.Provider, { value: context, children: showDocumentButtons ? /* @__PURE__ */ jsxs(Stack, { space: 5, children: [
|
|
@@ -289,30 +272,24 @@ function InternationalizedArrayProvider(props) {
|
|
|
289
272
|
props.renderDefault(props)
|
|
290
273
|
] }) : props.renderDefault(props) });
|
|
291
274
|
}
|
|
292
|
-
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) => {
|
|
293
|
-
for (var prop in b || (b = {}))
|
|
294
|
-
__hasOwnProp$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
295
|
-
if (__getOwnPropSymbols$7)
|
|
296
|
-
for (var prop of __getOwnPropSymbols$7(b))
|
|
297
|
-
__propIsEnum$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
|
|
298
|
-
return a;
|
|
299
|
-
}, __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
|
|
300
275
|
function InternationalizedField(props) {
|
|
301
276
|
const { languages } = useInternationalizedArrayContext(), customProps = useMemo(() => {
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
277
|
+
const pathSegment = props.path.slice(0, -1)[1], languageId = typeof pathSegment == "object" && "_key" in pathSegment ? pathSegment._key : void 0, hasValidLanguageId = languageId ? languages.some((l) => l.id === languageId) : !1, shouldHideTitle = props.title?.toLowerCase() === "value" && hasValidLanguageId;
|
|
278
|
+
return {
|
|
279
|
+
...props,
|
|
305
280
|
title: shouldHideTitle ? "" : props.title
|
|
306
|
-
}
|
|
281
|
+
};
|
|
307
282
|
}, [props, languages]);
|
|
308
|
-
return customProps.schemaType.name.startsWith("internationalizedArray") ? customProps.schemaType.name === "reference" && customProps.value ? customProps.renderDefault(
|
|
283
|
+
return customProps.schemaType.name.startsWith("internationalizedArray") ? customProps.schemaType.name === "reference" && customProps.value ? customProps.renderDefault({
|
|
284
|
+
...customProps,
|
|
309
285
|
title: "",
|
|
310
286
|
level: 0
|
|
311
287
|
// Reset the level to avoid nested styling
|
|
312
|
-
})
|
|
288
|
+
}) : customProps.schemaType.name === "string" || customProps.schemaType.name === "number" || customProps.schemaType.name === "text" ? customProps.children : customProps.renderDefault({
|
|
289
|
+
...customProps,
|
|
313
290
|
level: 0
|
|
314
291
|
// Reset the level to avoid nested styling
|
|
315
|
-
})
|
|
292
|
+
}) : customProps.renderDefault(customProps);
|
|
316
293
|
}
|
|
317
294
|
var Preload = memo(function(props) {
|
|
318
295
|
const client = useClient({ apiVersion: props.apiVersion }), cacheKey = createCacheKey({});
|
|
@@ -328,19 +305,11 @@ function checkAllLanguagesArePresent(languages, value) {
|
|
|
328
305
|
return languagesInUseIds.length === filteredLanguageIds.length && languagesInUseIds.every((l) => filteredLanguageIds.includes(l));
|
|
329
306
|
}
|
|
330
307
|
function createAddAllTitle(value, languages) {
|
|
331
|
-
return value
|
|
308
|
+
return value?.length ? `Add missing ${languages.length - value.length === 1 ? "language" : "languages"}` : languages.length === 1 ? `Add ${languages[0].title} Field` : "Add all languages";
|
|
332
309
|
}
|
|
333
310
|
function createValueSchemaTypeName(schemaType) {
|
|
334
311
|
return `${schemaType.name}Value`;
|
|
335
312
|
}
|
|
336
|
-
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) => {
|
|
337
|
-
for (var prop in b || (b = {}))
|
|
338
|
-
__hasOwnProp$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
339
|
-
if (__getOwnPropSymbols$6)
|
|
340
|
-
for (var prop of __getOwnPropSymbols$6(b))
|
|
341
|
-
__propIsEnum$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
|
|
342
|
-
return a;
|
|
343
|
-
}, __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
|
|
344
313
|
function createAddLanguagePatches(config) {
|
|
345
314
|
const {
|
|
346
315
|
addLanguageKeys,
|
|
@@ -349,13 +318,15 @@ function createAddLanguagePatches(config) {
|
|
|
349
318
|
filteredLanguages,
|
|
350
319
|
value,
|
|
351
320
|
path = []
|
|
352
|
-
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) =>
|
|
321
|
+
} = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => ({
|
|
322
|
+
...itemBase,
|
|
353
323
|
_key: id
|
|
354
324
|
})) : filteredLanguages.filter(
|
|
355
|
-
(language) => value
|
|
356
|
-
).map((language) =>
|
|
325
|
+
(language) => value?.length ? !value.find((v) => v._key === language.id) : !0
|
|
326
|
+
).map((language) => ({
|
|
327
|
+
...itemBase,
|
|
357
328
|
_key: language.id
|
|
358
|
-
})), languagesInUse = value
|
|
329
|
+
})), languagesInUse = value?.length ? value.map((v) => v) : [];
|
|
359
330
|
return newItems.map((item) => {
|
|
360
331
|
const languageIndex = languages.findIndex((l) => item._key === l.id), remainingLanguages = languages.slice(languageIndex + 1), nextLanguageIndex = languagesInUse.findIndex(
|
|
361
332
|
(l) => (
|
|
@@ -373,7 +344,7 @@ function createAddLanguagePatches(config) {
|
|
|
373
344
|
});
|
|
374
345
|
}
|
|
375
346
|
const createTranslateFieldActions = (fieldActionProps, { languages, filteredLanguages }) => languages.map((language) => {
|
|
376
|
-
const value = useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!
|
|
347
|
+
const value = useFormValue(fieldActionProps.path), disabled = value && Array.isArray(value) ? !!value?.find((item) => item._key === language.id) : !1, hidden = !filteredLanguages.some((f) => f.id === language.id), { onChange } = useDocumentPane(), onAction = useCallback(() => {
|
|
377
348
|
const { schemaType, path } = fieldActionProps, addLanguageKeys = [language.id], patches = createAddLanguagePatches({
|
|
378
349
|
addLanguageKeys,
|
|
379
350
|
schemaType,
|
|
@@ -415,8 +386,7 @@ const createTranslateFieldActions = (fieldActionProps, { languages, filteredLang
|
|
|
415
386
|
}, internationalizedArrayFieldAction = defineDocumentFieldAction({
|
|
416
387
|
name: "internationalizedArray",
|
|
417
388
|
useAction(fieldActionProps) {
|
|
418
|
-
|
|
419
|
-
const isInternationalizedArrayField = (_b = (_a = fieldActionProps == null ? void 0 : fieldActionProps.schemaType) == null ? void 0 : _a.type) == null ? void 0 : _b.name.startsWith(
|
|
389
|
+
const isInternationalizedArrayField = fieldActionProps?.schemaType?.type?.name.startsWith(
|
|
420
390
|
"internationalizedArray"
|
|
421
391
|
), { languages, filteredLanguages } = useInternationalizedArrayContext(), translateFieldActions = createTranslateFieldActions(
|
|
422
392
|
fieldActionProps,
|
|
@@ -472,16 +442,14 @@ function Feedback() {
|
|
|
472
442
|
/* @__PURE__ */ jsx(Card, { padding: 2, border: !0, radius: 2, children: /* @__PURE__ */ jsx(Code, { size: 1, language: "javascript", children: JSON.stringify(schemaExample, null, 2) }) })
|
|
473
443
|
] }) });
|
|
474
444
|
}
|
|
475
|
-
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) => {
|
|
476
|
-
for (var prop in b || (b = {}))
|
|
477
|
-
__hasOwnProp$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
478
|
-
if (__getOwnPropSymbols$5)
|
|
479
|
-
for (var prop of __getOwnPropSymbols$5(b))
|
|
480
|
-
__propIsEnum$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
|
|
481
|
-
return a;
|
|
482
|
-
}, __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
|
|
483
445
|
function InternationalizedArray(props) {
|
|
484
|
-
const {
|
|
446
|
+
const {
|
|
447
|
+
members,
|
|
448
|
+
value,
|
|
449
|
+
schemaType,
|
|
450
|
+
onChange,
|
|
451
|
+
readOnly: documentReadOnly
|
|
452
|
+
} = props, readOnly = typeof schemaType.readOnly == "boolean" ? schemaType.readOnly : !1, toast = useToast(), {
|
|
485
453
|
languages,
|
|
486
454
|
filteredLanguages,
|
|
487
455
|
defaultLanguages,
|
|
@@ -501,10 +469,9 @@ function InternationalizedArray(props) {
|
|
|
501
469
|
[languageFilterEnabled, members, languageFilterOptions, selectedLanguageIds]
|
|
502
470
|
), handleAddLanguage = useCallback(
|
|
503
471
|
async (param) => {
|
|
504
|
-
|
|
505
|
-
if (!(filteredLanguages != null && filteredLanguages.length))
|
|
472
|
+
if (!filteredLanguages?.length)
|
|
506
473
|
return;
|
|
507
|
-
const addLanguageKeys = Array.isArray(param) ? param : [
|
|
474
|
+
const addLanguageKeys = Array.isArray(param) ? param : [param?.currentTarget?.value].filter(Boolean), patches = createAddLanguagePatches({
|
|
508
475
|
addLanguageKeys,
|
|
509
476
|
schemaType,
|
|
510
477
|
languages,
|
|
@@ -517,7 +484,9 @@ function InternationalizedArray(props) {
|
|
|
517
484
|
), { isDeleting } = useDocumentPane(), addedLanguages = members.map(({ key }) => key), hasAddedDefaultLanguages = defaultLanguages.filter((language) => languages.find((l) => l.id === language)).every((language) => addedLanguages.includes(language));
|
|
518
485
|
useEffect(() => {
|
|
519
486
|
if (!isDeleting && !hasAddedDefaultLanguages) {
|
|
520
|
-
const languagesToAdd = defaultLanguages.filter((language) => !addedLanguages.includes(language)).filter((language) => languages.find((l) => l.id === language)), timeout = setTimeout(() =>
|
|
487
|
+
const languagesToAdd = defaultLanguages.filter((language) => !addedLanguages.includes(language)).filter((language) => languages.find((l) => l.id === language)), timeout = setTimeout(() => {
|
|
488
|
+
documentReadOnly || handleAddLanguage(languagesToAdd);
|
|
489
|
+
});
|
|
521
490
|
return () => clearTimeout(timeout);
|
|
522
491
|
}
|
|
523
492
|
}, [
|
|
@@ -526,26 +495,27 @@ function InternationalizedArray(props) {
|
|
|
526
495
|
handleAddLanguage,
|
|
527
496
|
defaultLanguages,
|
|
528
497
|
addedLanguages,
|
|
529
|
-
languages
|
|
498
|
+
languages,
|
|
499
|
+
documentReadOnly
|
|
530
500
|
]);
|
|
531
501
|
const handleRestoreOrder = useCallback(() => {
|
|
532
|
-
if (!
|
|
502
|
+
if (!value?.length || !languages?.length)
|
|
533
503
|
return;
|
|
534
504
|
const updatedValue = value.reduce((acc, v) => {
|
|
535
|
-
const newIndex = languages.findIndex((l) => l.id ===
|
|
505
|
+
const newIndex = languages.findIndex((l) => l.id === v?._key);
|
|
536
506
|
return newIndex > -1 && (acc[newIndex] = v), acc;
|
|
537
507
|
}, []).filter(Boolean);
|
|
538
|
-
|
|
508
|
+
value?.length !== updatedValue.length && toast.push({
|
|
539
509
|
title: "There was an error reordering languages",
|
|
540
510
|
status: "warning"
|
|
541
511
|
}), onChange(set(updatedValue));
|
|
542
|
-
}, [toast, languages, onChange, value]), allKeysAreLanguages = useMemo(() => !
|
|
543
|
-
() => languages && languages.length > 1 ? languages.filter((l) => value
|
|
512
|
+
}, [toast, languages, onChange, value]), allKeysAreLanguages = useMemo(() => !value?.length || !languages?.length ? !0 : value?.every((v) => languages.find((l) => l?.id === v?._key)), [value, languages]), languagesInUse = useMemo(
|
|
513
|
+
() => languages && languages.length > 1 ? languages.filter((l) => value?.find((v) => v._key === l.id)) : [],
|
|
544
514
|
[languages, value]
|
|
545
|
-
), languagesOutOfOrder = useMemo(() => !
|
|
515
|
+
), languagesOutOfOrder = useMemo(() => !value?.length || !languagesInUse.length ? [] : value.map(
|
|
546
516
|
(v, vIndex) => vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v
|
|
547
517
|
).filter(Boolean), [value, languagesInUse]), languagesAreValid = useMemo(
|
|
548
|
-
() => !
|
|
518
|
+
() => !languages?.length || languages?.length && languages.every((item) => item.id && item.title),
|
|
549
519
|
[languages]
|
|
550
520
|
);
|
|
551
521
|
useEffect(() => {
|
|
@@ -560,21 +530,22 @@ function InternationalizedArray(props) {
|
|
|
560
530
|
const addButtonsAreVisible = (
|
|
561
531
|
// Plugin was configured to display buttons here (default!)
|
|
562
532
|
buttonLocations.includes("field") && // There's at least one language visible
|
|
563
|
-
|
|
533
|
+
filteredLanguages?.length > 0 && // Not every language has a value yet
|
|
564
534
|
!allLanguagesArePresent
|
|
565
|
-
), fieldHasMembers =
|
|
535
|
+
), fieldHasMembers = members?.length > 0;
|
|
566
536
|
return /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
567
537
|
fieldHasMembers ? /* @__PURE__ */ jsx(Fragment, { children: filteredMembers.map((member) => member.kind === "item" ? /* @__PURE__ */ createElement(
|
|
568
538
|
ArrayOfObjectsItem,
|
|
569
|
-
|
|
539
|
+
{
|
|
540
|
+
...props,
|
|
570
541
|
key: member.key,
|
|
571
542
|
member
|
|
572
|
-
}
|
|
543
|
+
}
|
|
573
544
|
) : /* @__PURE__ */ jsx(MemberItemError, { member }, member.key)) }) : null,
|
|
574
545
|
!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,
|
|
575
546
|
addButtonsAreVisible ? /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
576
547
|
/* @__PURE__ */ jsx(
|
|
577
|
-
AddButtons
|
|
548
|
+
AddButtons,
|
|
578
549
|
{
|
|
579
550
|
languages: filteredLanguages,
|
|
580
551
|
value,
|
|
@@ -597,17 +568,9 @@ function InternationalizedArray(props) {
|
|
|
597
568
|
] });
|
|
598
569
|
}
|
|
599
570
|
function getLanguagesFieldOption(schemaType) {
|
|
600
|
-
|
|
601
|
-
return schemaType ? ((_a = schemaType.options) == null ? void 0 : _a.languages) || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
571
|
+
return schemaType ? schemaType.options?.languages || getLanguagesFieldOption(schemaType.type) : void 0;
|
|
602
572
|
}
|
|
603
|
-
var
|
|
604
|
-
for (var prop in b || (b = {}))
|
|
605
|
-
__hasOwnProp$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
606
|
-
if (__getOwnPropSymbols$4)
|
|
607
|
-
for (var prop of __getOwnPropSymbols$4(b))
|
|
608
|
-
__propIsEnum$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
|
|
609
|
-
return a;
|
|
610
|
-
}, __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b)), array = (config) => {
|
|
573
|
+
var array = (config) => {
|
|
611
574
|
const { apiVersion, select, languages, type } = config, typeName = typeof type == "string" ? type : type.name, arrayName = createFieldName(typeName), objectName = createFieldName(typeName, !0);
|
|
612
575
|
return defineField({
|
|
613
576
|
name: arrayName,
|
|
@@ -623,19 +586,19 @@ var __defProp$4 = Object.defineProperty, __defProps$3 = Object.defineProperties,
|
|
|
623
586
|
languages
|
|
624
587
|
},
|
|
625
588
|
of: [
|
|
626
|
-
defineField(
|
|
589
|
+
defineField({
|
|
590
|
+
...typeof type == "string" ? {} : type,
|
|
627
591
|
name: objectName,
|
|
628
592
|
type: objectName
|
|
629
|
-
})
|
|
593
|
+
})
|
|
630
594
|
],
|
|
631
595
|
// @ts-expect-error - fix typings
|
|
632
596
|
validation: (rule) => rule.custom(async (value, context) => {
|
|
633
|
-
|
|
634
|
-
if (!value || value.length === 0 || value.length === 1 && !((_a = value[0]) != null && _a._key))
|
|
597
|
+
if (!value || value.length === 0 || value.length === 1 && !value[0]?._key)
|
|
635
598
|
return !0;
|
|
636
599
|
const selectedValue = getSelectedValue(select, context.document), client = context.getClient({ apiVersion });
|
|
637
600
|
let contextLanguages = [];
|
|
638
|
-
const languagesFieldOption = getLanguagesFieldOption(context
|
|
601
|
+
const languagesFieldOption = getLanguagesFieldOption(context?.type);
|
|
639
602
|
if (Array.isArray(languagesFieldOption))
|
|
640
603
|
contextLanguages = languagesFieldOption;
|
|
641
604
|
else if (Array.isArray(peek(selectedValue)))
|
|
@@ -662,7 +625,7 @@ var __defProp$4 = Object.defineProperty, __defProps$3 = Object.defineProperties,
|
|
|
662
625
|
if (value && value.length > contextLanguages.length)
|
|
663
626
|
return `Cannot be more than ${contextLanguages.length === 1 ? "1 item" : `${contextLanguages.length} items`}`;
|
|
664
627
|
const languageIds = new Set(contextLanguages.map((lang) => lang.id)), nonLanguageKeys = value.filter(
|
|
665
|
-
(item) =>
|
|
628
|
+
(item) => item?._key && !languageIds.has(item._key)
|
|
666
629
|
);
|
|
667
630
|
if (nonLanguageKeys.length)
|
|
668
631
|
return {
|
|
@@ -671,7 +634,7 @@ var __defProp$4 = Object.defineProperty, __defProps$3 = Object.defineProperties,
|
|
|
671
634
|
};
|
|
672
635
|
const seenKeys = /* @__PURE__ */ new Set(), duplicateValues = [];
|
|
673
636
|
for (const item of value)
|
|
674
|
-
item
|
|
637
|
+
item?._key && (seenKeys.has(item._key) ? duplicateValues.push(item) : seenKeys.add(item._key));
|
|
675
638
|
return duplicateValues.length ? {
|
|
676
639
|
message: "There can only be one field per language",
|
|
677
640
|
paths: duplicateValues.map((item) => [{ _key: item._key }])
|
|
@@ -680,7 +643,7 @@ var __defProp$4 = Object.defineProperty, __defProps$3 = Object.defineProperties,
|
|
|
680
643
|
});
|
|
681
644
|
};
|
|
682
645
|
function getToneFromValidation(validations) {
|
|
683
|
-
if (!
|
|
646
|
+
if (!validations?.length)
|
|
684
647
|
return;
|
|
685
648
|
const validationLevels = validations.map((v) => v.level);
|
|
686
649
|
if (validationLevels.includes("error"))
|
|
@@ -688,31 +651,22 @@ function getToneFromValidation(validations) {
|
|
|
688
651
|
if (validationLevels.includes("warning"))
|
|
689
652
|
return "caution";
|
|
690
653
|
}
|
|
691
|
-
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) => {
|
|
692
|
-
for (var prop in b || (b = {}))
|
|
693
|
-
__hasOwnProp$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
694
|
-
if (__getOwnPropSymbols$3)
|
|
695
|
-
for (var prop of __getOwnPropSymbols$3(b))
|
|
696
|
-
__propIsEnum$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
|
|
697
|
-
return a;
|
|
698
|
-
}, __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
|
|
699
654
|
function InternationalizedInput(props) {
|
|
700
655
|
const parentValue = useFormValue(
|
|
701
656
|
props.path.slice(0, -1)
|
|
702
657
|
), originalOnChange = props.inputProps.onChange, wrappedOnChange = useCallback(
|
|
703
658
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
704
659
|
(patches) => {
|
|
705
|
-
var _a;
|
|
706
660
|
if (!Array.isArray(patches))
|
|
707
661
|
return originalOnChange(patches);
|
|
708
|
-
const valueField =
|
|
662
|
+
const valueField = props.value?.value;
|
|
709
663
|
if ((valueField == null || Array.isArray(valueField) && valueField.length === 0) && patches.some((patch) => !patch || typeof patch != "object" ? !1 : patch.type === "insert" && patch.path && Array.isArray(patch.path) && patch.path.length > 0 ? patch.path[0] === "value" || typeof patch.path[0] == "number" : !1)) {
|
|
710
664
|
const initPatch = valueField === void 0 ? { type: "setIfMissing", path: ["value"], value: [] } : null, fixedPatches = patches.map((patch) => {
|
|
711
665
|
if (!patch || typeof patch != "object")
|
|
712
666
|
return patch;
|
|
713
667
|
if (patch.type === "insert" && patch.path && Array.isArray(patch.path)) {
|
|
714
668
|
const fixedPath = patch.path[0] === "value" ? patch.path : ["value", ...patch.path];
|
|
715
|
-
return
|
|
669
|
+
return { ...patch, path: fixedPath };
|
|
716
670
|
}
|
|
717
671
|
return patch;
|
|
718
672
|
}), allPatches = initPatch ? [initPatch, ...fixedPatches] : fixedPatches;
|
|
@@ -721,7 +675,8 @@ function InternationalizedInput(props) {
|
|
|
721
675
|
return originalOnChange(patches);
|
|
722
676
|
},
|
|
723
677
|
[props.value, originalOnChange]
|
|
724
|
-
), inlineProps =
|
|
678
|
+
), inlineProps = {
|
|
679
|
+
...props.inputProps,
|
|
725
680
|
// This is the magic that makes inline editing work?
|
|
726
681
|
members: props.inputProps.members.filter(
|
|
727
682
|
(m) => m.kind === "field" && m.name === "value"
|
|
@@ -731,17 +686,13 @@ function InternationalizedInput(props) {
|
|
|
731
686
|
value: props.value,
|
|
732
687
|
// Use our wrapped onChange handler
|
|
733
688
|
onChange: wrappedOnChange
|
|
734
|
-
}
|
|
735
|
-
() =>
|
|
736
|
-
var _a;
|
|
737
|
-
return (_a = parentValue == null ? void 0 : parentValue.map((v) => v._key)) != null ? _a : [];
|
|
738
|
-
},
|
|
689
|
+
}, { validation, value, onChange, readOnly } = inlineProps, { languages, languageDisplay, defaultLanguages } = useInternationalizedArrayContext(), languageKeysInUse = useMemo(
|
|
690
|
+
() => parentValue?.map((v) => v._key) ?? [],
|
|
739
691
|
[parentValue]
|
|
740
|
-
), keyIsValid = languages
|
|
692
|
+
), keyIsValid = languages?.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = useCallback(
|
|
741
693
|
(event) => {
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
!value || !(languages != null && languages.length) || !languages.find((l) => l.id === languageId) || onChange([set(languageId, ["_key"])]);
|
|
694
|
+
const languageId = event?.currentTarget?.value;
|
|
695
|
+
!value || !languages?.length || !languages.find((l) => l.id === languageId) || onChange([set(languageId, ["_key"])]);
|
|
745
696
|
},
|
|
746
697
|
[onChange, value, languages]
|
|
747
698
|
), handleUnset = useCallback(() => {
|
|
@@ -794,14 +745,7 @@ function InternationalizedInput(props) {
|
|
|
794
745
|
] })
|
|
795
746
|
] }) });
|
|
796
747
|
}
|
|
797
|
-
var
|
|
798
|
-
for (var prop in b || (b = {}))
|
|
799
|
-
__hasOwnProp$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
800
|
-
if (__getOwnPropSymbols$2)
|
|
801
|
-
for (var prop of __getOwnPropSymbols$2(b))
|
|
802
|
-
__propIsEnum$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
|
|
803
|
-
return a;
|
|
804
|
-
}, __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b)), object = (config) => {
|
|
748
|
+
var object = (config) => {
|
|
805
749
|
const { type } = config, typeName = typeof type == "string" ? type : type.name, objectName = createFieldName(typeName, !0);
|
|
806
750
|
return defineField({
|
|
807
751
|
name: objectName,
|
|
@@ -812,9 +756,10 @@ var __defProp$2 = Object.defineProperty, __defProps$1 = Object.defineProperties,
|
|
|
812
756
|
item: InternationalizedInput
|
|
813
757
|
},
|
|
814
758
|
fields: [
|
|
815
|
-
defineField(
|
|
759
|
+
defineField({
|
|
760
|
+
...typeof type == "string" ? { type } : type,
|
|
816
761
|
name: "value"
|
|
817
|
-
})
|
|
762
|
+
})
|
|
818
763
|
],
|
|
819
764
|
preview: {
|
|
820
765
|
select: {
|
|
@@ -823,20 +768,13 @@ var __defProp$2 = Object.defineProperty, __defProps$1 = Object.defineProperties,
|
|
|
823
768
|
}
|
|
824
769
|
}
|
|
825
770
|
});
|
|
826
|
-
}, __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) => {
|
|
827
|
-
for (var prop in b || (b = {}))
|
|
828
|
-
__hasOwnProp$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
829
|
-
if (__getOwnPropSymbols$1)
|
|
830
|
-
for (var prop of __getOwnPropSymbols$1(b))
|
|
831
|
-
__propIsEnum$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
|
|
832
|
-
return a;
|
|
833
771
|
};
|
|
834
772
|
function flattenSchemaType(schemaType) {
|
|
835
773
|
return isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 3) : (console.error("Schema type is not a document"), []);
|
|
836
774
|
}
|
|
837
775
|
function extractInnerFields(fields, path, maxDepth) {
|
|
838
776
|
return path.length >= maxDepth ? [] : fields.reduce((acc, field) => {
|
|
839
|
-
const thisFieldWithPath =
|
|
777
|
+
const thisFieldWithPath = { path: [...path, field.name], ...field };
|
|
840
778
|
if (field.type.jsonType === "object") {
|
|
841
779
|
const innerFields = extractInnerFields(
|
|
842
780
|
field.type.fields,
|
|
@@ -858,21 +796,12 @@ function extractInnerFields(fields, path, maxDepth) {
|
|
|
858
796
|
return [...acc, thisFieldWithPath];
|
|
859
797
|
}, []);
|
|
860
798
|
}
|
|
861
|
-
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) => {
|
|
862
|
-
for (var prop in b || (b = {}))
|
|
863
|
-
__hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
864
|
-
if (__getOwnPropSymbols)
|
|
865
|
-
for (var prop of __getOwnPropSymbols(b))
|
|
866
|
-
__propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
|
|
867
|
-
return a;
|
|
868
|
-
}, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
869
799
|
const internationalizedArray = definePlugin((config) => {
|
|
870
|
-
const pluginConfig =
|
|
800
|
+
const pluginConfig = { ...CONFIG_DEFAULT, ...config }, {
|
|
871
801
|
apiVersion = "2025-10-15",
|
|
872
802
|
select,
|
|
873
803
|
languages,
|
|
874
804
|
fieldTypes,
|
|
875
|
-
defaultLanguages,
|
|
876
805
|
buttonLocations
|
|
877
806
|
} = pluginConfig;
|
|
878
807
|
return {
|
|
@@ -893,16 +822,17 @@ const internationalizedArray = definePlugin((config) => {
|
|
|
893
822
|
// Wrap document editor with a language provider
|
|
894
823
|
form: {
|
|
895
824
|
components: {
|
|
896
|
-
field: (props) => /* @__PURE__ */ jsx(InternationalizedField,
|
|
825
|
+
field: (props) => /* @__PURE__ */ jsx(InternationalizedField, { ...props }),
|
|
897
826
|
input: (props) => !(props.id === "root" && isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
|
|
898
827
|
(field) => field.type.name
|
|
899
828
|
).some(
|
|
900
829
|
(name) => name.startsWith("internationalizedArray")
|
|
901
830
|
) ? props.renderDefault(props) : /* @__PURE__ */ jsx(
|
|
902
831
|
InternationalizedArrayProvider,
|
|
903
|
-
|
|
832
|
+
{
|
|
833
|
+
...props,
|
|
904
834
|
internationalizedArray: pluginConfig
|
|
905
|
-
}
|
|
835
|
+
}
|
|
906
836
|
)
|
|
907
837
|
}
|
|
908
838
|
},
|
|
@@ -910,7 +840,7 @@ const internationalizedArray = definePlugin((config) => {
|
|
|
910
840
|
schema: {
|
|
911
841
|
types: [
|
|
912
842
|
...fieldTypes.map(
|
|
913
|
-
(type) => array({ type, apiVersion, select, languages
|
|
843
|
+
(type) => array({ type, apiVersion, select, languages })
|
|
914
844
|
),
|
|
915
845
|
...fieldTypes.map((type) => object({ type }))
|
|
916
846
|
]
|