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.js CHANGED
@@ -58,26 +58,18 @@ const namespace = "sanity-plugin-internationalized-array", version = "v1", funct
58
58
  buttonLocations: ["field"],
59
59
  buttonAddAll: !0,
60
60
  languageDisplay: "codeOnly"
61
- };
62
- 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) => {
63
- for (var prop in b || (b = {}))
64
- __hasOwnProp$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
65
- if (__getOwnPropSymbols$9)
66
- for (var prop of __getOwnPropSymbols$9(b))
67
- __propIsEnum$9.call(b, prop) && __defNormalProp$9(a, prop, b[prop]);
68
- return a;
69
- }, __spreadProps$8 = (a, b) => __defProps$8(a, __getOwnPropDescs$8(b));
70
- const getDocumentsToTranslate = (value, rootPath = []) => {
61
+ }, getDocumentsToTranslate = (value, rootPath = []) => {
71
62
  if (Array.isArray(value)) {
72
63
  const arrayRootPath = [...rootPath], internationalizedValues = value.filter((item) => {
73
64
  if (Array.isArray(item)) return !1;
74
65
  if (typeof item == "object") {
75
- const type = item == null ? void 0 : item._type;
76
- return (type == null ? void 0 : type.startsWith("internationalizedArray")) && (type == null ? void 0 : type.endsWith("Value"));
66
+ const type = item?._type;
67
+ return type?.startsWith("internationalizedArray") && type?.endsWith("Value");
77
68
  }
78
69
  return !1;
79
70
  });
80
- return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => __spreadProps$8(__spreadValues$9({}, internationalizedValue), {
71
+ return internationalizedValues.length > 0 ? internationalizedValues.map((internationalizedValue) => ({
72
+ ...internationalizedValue,
81
73
  path: arrayRootPath,
82
74
  pathString: arrayRootPath.join(".")
83
75
  })) : value.length > 0 ? value.map(
@@ -98,7 +90,7 @@ const getDocumentsToTranslate = (value, rootPath = []) => {
98
90
  function getLanguageDisplay(languageDisplay, title, code) {
99
91
  return languageDisplay === "codeOnly" ? code.toUpperCase() : languageDisplay === "titleOnly" ? title : languageDisplay === "titleAndCode" ? `${title} (${code.toUpperCase()})` : title;
100
92
  }
101
- function AddButtons(props) {
93
+ function AddButtons$1(props) {
102
94
  const { languages, readOnly, value, onClick } = props, { languageDisplay } = useInternationalizedArrayContext();
103
95
  return languages.length > 0 ? /* @__PURE__ */ jsxRuntime.jsx(
104
96
  ui.Grid,
@@ -117,7 +109,7 @@ function AddButtons(props) {
117
109
  tone: "primary",
118
110
  mode: "ghost",
119
111
  fontSize: 1,
120
- disabled: readOnly || !!(value != null && value.find((item) => item._key === language.id)),
112
+ disabled: readOnly || !!value?.find((item) => item._key === language.id),
121
113
  text: languageTitle,
122
114
  icon: languages.length > MAX_COLUMNS[languageDisplay] && languageDisplay === "codeOnly" ? void 0 : icons.AddIcon,
123
115
  value: language.id,
@@ -129,11 +121,10 @@ function AddButtons(props) {
129
121
  }
130
122
  ) : null;
131
123
  }
132
- var AddButtons$1 = react.memo(AddButtons);
124
+ var AddButtons = react.memo(AddButtons$1);
133
125
  function DocumentAddButtons(props) {
134
126
  const { filteredLanguages } = useInternationalizedArrayContext(), value = sanity.isSanityDocument(props.value) ? props.value : void 0, toast = ui.useToast(), { onChange } = structure.useDocumentPane(), schema = sanity.useSchema(), documentsToTranslation = getDocumentsToTranslate(value, []), getInitialValueForType = react.useCallback(
135
127
  (typeName) => {
136
- var _a;
137
128
  if (!typeName) return;
138
129
  const match = typeName.match(/^internationalizedArray(.+)Value$/);
139
130
  if (!match) return;
@@ -148,13 +139,13 @@ function DocumentAddButtons(props) {
148
139
  try {
149
140
  const schemaType = schema.get(typeName);
150
141
  if (schemaType) {
151
- const valueField = (_a = schemaType == null ? void 0 : schemaType.fields) == null ? void 0 : _a.find(
142
+ const valueField = schemaType?.fields?.find(
152
143
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
153
144
  (f) => f.name === "value"
154
145
  );
155
146
  if (valueField) {
156
147
  const fieldType = valueField.type;
157
- if ((fieldType == null ? void 0 : fieldType.jsonType) === "array" || (fieldType == null ? void 0 : fieldType.name) === "array" || (fieldType == null ? void 0 : fieldType.type) === "array" || (fieldType == null ? void 0 : fieldType.of) !== void 0 || arrayBasedTypes.includes(fieldType == null ? void 0 : fieldType.name))
148
+ if (fieldType?.jsonType === "array" || fieldType?.name === "array" || fieldType?.type === "array" || fieldType?.of !== void 0 || arrayBasedTypes.includes(fieldType?.name))
158
149
  return [];
159
150
  }
160
151
  }
@@ -178,7 +169,7 @@ function DocumentAddButtons(props) {
178
169
  return;
179
170
  }
180
171
  const alreadyTranslated = documentsToTranslation.filter(
181
- (translation) => (translation == null ? void 0 : translation._key) === languageId
172
+ (translation) => translation?._key === languageId
182
173
  ), removeDuplicates = documentsToTranslation.reduce((filteredTranslations, translation) => alreadyTranslated.filter(
183
174
  (alreadyTranslation) => alreadyTranslation.pathString === translation.pathString
184
175
  ).length > 0 || filteredTranslations.filter(
@@ -214,7 +205,7 @@ function DocumentAddButtons(props) {
214
205
  return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 3, children: [
215
206
  /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: 1, weight: "semibold", children: "Add translation to internationalized fields" }) }),
216
207
  /* @__PURE__ */ jsxRuntime.jsx(
217
- AddButtons$1,
208
+ AddButtons,
218
209
  {
219
210
  languages: filteredLanguages,
220
211
  readOnly: !1,
@@ -231,36 +222,28 @@ const getSelectedValue = (select, document) => {
231
222
  for (const [key, path] of Object.entries(selection)) {
232
223
  let value = get__default.default(document, path);
233
224
  Array.isArray(value) && (value = value.filter(
234
- (item) => typeof item == "object" ? (item == null ? void 0 : item._type) === "reference" && "_ref" in item : !0
225
+ (item) => typeof item == "object" ? item?._type === "reference" && "_ref" in item : !0
235
226
  )), selectedValue[key] = value;
236
227
  }
237
228
  return selectedValue;
238
- };
239
- 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) => {
240
- for (var prop in b || (b = {}))
241
- __hasOwnProp$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
242
- if (__getOwnPropSymbols$8)
243
- for (var prop of __getOwnPropSymbols$8(b))
244
- __propIsEnum$8.call(b, prop) && __defNormalProp$8(a, prop, b[prop]);
245
- return a;
246
- }, __spreadProps$7 = (a, b) => __defProps$7(a, __getOwnPropDescs$7(b));
247
- const InternationalizedArrayContext = react.createContext(__spreadProps$7(__spreadValues$8({}, CONFIG_DEFAULT), {
229
+ }, InternationalizedArrayContext = react.createContext({
230
+ ...CONFIG_DEFAULT,
248
231
  languages: [],
249
232
  filteredLanguages: []
250
- }));
233
+ });
251
234
  function useInternationalizedArrayContext() {
252
235
  return react.useContext(InternationalizedArrayContext);
253
236
  }
254
237
  function InternationalizedArrayProvider(props) {
255
- 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(
238
+ const { internationalizedArray: internationalizedArray2 } = props, client = sanity.useClient({ apiVersion: internationalizedArray2.apiVersion }), workspace = sanity.useWorkspace(), { formState } = structure.useDocumentPane(), deferredDocument = react.useDeferredValue(formState?.value), selectedValue = react.useMemo(
256
239
  () => getSelectedValue(internationalizedArray2.select, deferredDocument),
257
240
  [internationalizedArray2.select, deferredDocument]
258
241
  ), workspaceId = react.useMemo(() => {
259
- if (workspace != null && workspace.name)
242
+ if (workspace?.name)
260
243
  return workspace.name;
261
244
  const workspaceKey = {
262
- name: workspace == null ? void 0 : workspace.name,
263
- title: workspace == null ? void 0 : workspace.title
245
+ name: workspace?.name,
246
+ title: workspace?.title
264
247
  // Add other stable properties as needed
265
248
  };
266
249
  return JSON.stringify(workspaceKey);
@@ -292,7 +275,7 @@ function InternationalizedArrayProvider(props) {
292
275
  (language) => selectedLanguageIds.includes(language.id)
293
276
  ) : languages;
294
277
  }, [deferredDocument, languageFilterOptions, languages, selectedLanguageIds]), showDocumentButtons = internationalizedArray2.buttonLocations.includes("document"), context = react.useMemo(
295
- () => __spreadProps$7(__spreadValues$8({}, internationalizedArray2), { languages, filteredLanguages }),
278
+ () => ({ ...internationalizedArray2, languages, filteredLanguages }),
296
279
  [filteredLanguages, internationalizedArray2, languages]
297
280
  );
298
281
  return /* @__PURE__ */ jsxRuntime.jsx(InternationalizedArrayContext.Provider, { value: context, children: showDocumentButtons ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 5, children: [
@@ -300,30 +283,24 @@ function InternationalizedArrayProvider(props) {
300
283
  props.renderDefault(props)
301
284
  ] }) : props.renderDefault(props) });
302
285
  }
303
- 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) => {
304
- for (var prop in b || (b = {}))
305
- __hasOwnProp$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
306
- if (__getOwnPropSymbols$7)
307
- for (var prop of __getOwnPropSymbols$7(b))
308
- __propIsEnum$7.call(b, prop) && __defNormalProp$7(a, prop, b[prop]);
309
- return a;
310
- }, __spreadProps$6 = (a, b) => __defProps$6(a, __getOwnPropDescs$6(b));
311
286
  function InternationalizedField(props) {
312
287
  const { languages } = useInternationalizedArrayContext(), customProps = react.useMemo(() => {
313
- var _a;
314
- 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 = ((_a = props.title) == null ? void 0 : _a.toLowerCase()) === "value" && hasValidLanguageId;
315
- return __spreadProps$6(__spreadValues$7({}, props), {
288
+ 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;
289
+ return {
290
+ ...props,
316
291
  title: shouldHideTitle ? "" : props.title
317
- });
292
+ };
318
293
  }, [props, languages]);
319
- return customProps.schemaType.name.startsWith("internationalizedArray") ? customProps.schemaType.name === "reference" && customProps.value ? customProps.renderDefault(__spreadProps$6(__spreadValues$7({}, customProps), {
294
+ return customProps.schemaType.name.startsWith("internationalizedArray") ? customProps.schemaType.name === "reference" && customProps.value ? customProps.renderDefault({
295
+ ...customProps,
320
296
  title: "",
321
297
  level: 0
322
298
  // Reset the level to avoid nested styling
323
- })) : customProps.schemaType.name === "string" || customProps.schemaType.name === "number" || customProps.schemaType.name === "text" ? customProps.children : customProps.renderDefault(__spreadProps$6(__spreadValues$7({}, customProps), {
299
+ }) : customProps.schemaType.name === "string" || customProps.schemaType.name === "number" || customProps.schemaType.name === "text" ? customProps.children : customProps.renderDefault({
300
+ ...customProps,
324
301
  level: 0
325
302
  // Reset the level to avoid nested styling
326
- })) : customProps.renderDefault(customProps);
303
+ }) : customProps.renderDefault(customProps);
327
304
  }
328
305
  var Preload = react.memo(function(props) {
329
306
  const client = sanity.useClient({ apiVersion: props.apiVersion }), cacheKey = createCacheKey({});
@@ -339,19 +316,11 @@ function checkAllLanguagesArePresent(languages, value) {
339
316
  return languagesInUseIds.length === filteredLanguageIds.length && languagesInUseIds.every((l) => filteredLanguageIds.includes(l));
340
317
  }
341
318
  function createAddAllTitle(value, languages) {
342
- 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";
319
+ return value?.length ? `Add missing ${languages.length - value.length === 1 ? "language" : "languages"}` : languages.length === 1 ? `Add ${languages[0].title} Field` : "Add all languages";
343
320
  }
344
321
  function createValueSchemaTypeName(schemaType) {
345
322
  return `${schemaType.name}Value`;
346
323
  }
347
- 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) => {
348
- for (var prop in b || (b = {}))
349
- __hasOwnProp$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
350
- if (__getOwnPropSymbols$6)
351
- for (var prop of __getOwnPropSymbols$6(b))
352
- __propIsEnum$6.call(b, prop) && __defNormalProp$6(a, prop, b[prop]);
353
- return a;
354
- }, __spreadProps$5 = (a, b) => __defProps$5(a, __getOwnPropDescs$5(b));
355
324
  function createAddLanguagePatches(config) {
356
325
  const {
357
326
  addLanguageKeys,
@@ -360,13 +329,15 @@ function createAddLanguagePatches(config) {
360
329
  filteredLanguages,
361
330
  value,
362
331
  path = []
363
- } = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => __spreadProps$5(__spreadValues$6({}, itemBase), {
332
+ } = config, itemBase = { _type: createValueSchemaTypeName(schemaType) }, newItems = Array.isArray(addLanguageKeys) && addLanguageKeys.length > 0 ? addLanguageKeys.map((id) => ({
333
+ ...itemBase,
364
334
  _key: id
365
335
  })) : filteredLanguages.filter(
366
- (language) => value != null && value.length ? !value.find((v) => v._key === language.id) : !0
367
- ).map((language) => __spreadProps$5(__spreadValues$6({}, itemBase), {
336
+ (language) => value?.length ? !value.find((v) => v._key === language.id) : !0
337
+ ).map((language) => ({
338
+ ...itemBase,
368
339
  _key: language.id
369
- })), languagesInUse = value != null && value.length ? value.map((v) => v) : [];
340
+ })), languagesInUse = value?.length ? value.map((v) => v) : [];
370
341
  return newItems.map((item) => {
371
342
  const languageIndex = languages.findIndex((l) => item._key === l.id), remainingLanguages = languages.slice(languageIndex + 1), nextLanguageIndex = languagesInUse.findIndex(
372
343
  (l) => (
@@ -384,7 +355,7 @@ function createAddLanguagePatches(config) {
384
355
  });
385
356
  }
386
357
  const createTranslateFieldActions = (fieldActionProps, { languages, filteredLanguages }) => languages.map((language) => {
387
- 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(() => {
358
+ const value = sanity.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 } = structure.useDocumentPane(), onAction = react.useCallback(() => {
388
359
  const { schemaType, path } = fieldActionProps, addLanguageKeys = [language.id], patches = createAddLanguagePatches({
389
360
  addLanguageKeys,
390
361
  schemaType,
@@ -426,8 +397,7 @@ const createTranslateFieldActions = (fieldActionProps, { languages, filteredLang
426
397
  }, internationalizedArrayFieldAction = sanity.defineDocumentFieldAction({
427
398
  name: "internationalizedArray",
428
399
  useAction(fieldActionProps) {
429
- var _a, _b;
430
- const isInternationalizedArrayField = (_b = (_a = fieldActionProps == null ? void 0 : fieldActionProps.schemaType) == null ? void 0 : _a.type) == null ? void 0 : _b.name.startsWith(
400
+ const isInternationalizedArrayField = fieldActionProps?.schemaType?.type?.name.startsWith(
431
401
  "internationalizedArray"
432
402
  ), { languages, filteredLanguages } = useInternationalizedArrayContext(), translateFieldActions = createTranslateFieldActions(
433
403
  fieldActionProps,
@@ -483,16 +453,14 @@ function Feedback() {
483
453
  /* @__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) }) })
484
454
  ] }) });
485
455
  }
486
- 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) => {
487
- for (var prop in b || (b = {}))
488
- __hasOwnProp$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
489
- if (__getOwnPropSymbols$5)
490
- for (var prop of __getOwnPropSymbols$5(b))
491
- __propIsEnum$5.call(b, prop) && __defNormalProp$5(a, prop, b[prop]);
492
- return a;
493
- }, __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
494
456
  function InternationalizedArray(props) {
495
- const { members, value, schemaType, onChange } = props, readOnly = typeof schemaType.readOnly == "boolean" ? schemaType.readOnly : !1, toast = ui.useToast(), {
457
+ const {
458
+ members,
459
+ value,
460
+ schemaType,
461
+ onChange,
462
+ readOnly: documentReadOnly
463
+ } = props, readOnly = typeof schemaType.readOnly == "boolean" ? schemaType.readOnly : !1, toast = ui.useToast(), {
496
464
  languages,
497
465
  filteredLanguages,
498
466
  defaultLanguages,
@@ -512,10 +480,9 @@ function InternationalizedArray(props) {
512
480
  [languageFilterEnabled, members, languageFilterOptions, selectedLanguageIds]
513
481
  ), handleAddLanguage = react.useCallback(
514
482
  async (param) => {
515
- var _a;
516
- if (!(filteredLanguages != null && filteredLanguages.length))
483
+ if (!filteredLanguages?.length)
517
484
  return;
518
- const addLanguageKeys = Array.isArray(param) ? param : [(_a = param == null ? void 0 : param.currentTarget) == null ? void 0 : _a.value].filter(Boolean), patches = createAddLanguagePatches({
485
+ const addLanguageKeys = Array.isArray(param) ? param : [param?.currentTarget?.value].filter(Boolean), patches = createAddLanguagePatches({
519
486
  addLanguageKeys,
520
487
  schemaType,
521
488
  languages,
@@ -528,7 +495,9 @@ function InternationalizedArray(props) {
528
495
  ), { isDeleting } = structure.useDocumentPane(), addedLanguages = members.map(({ key }) => key), hasAddedDefaultLanguages = defaultLanguages.filter((language) => languages.find((l) => l.id === language)).every((language) => addedLanguages.includes(language));
529
496
  react.useEffect(() => {
530
497
  if (!isDeleting && !hasAddedDefaultLanguages) {
531
- const languagesToAdd = defaultLanguages.filter((language) => !addedLanguages.includes(language)).filter((language) => languages.find((l) => l.id === language)), timeout = setTimeout(() => handleAddLanguage(languagesToAdd));
498
+ const languagesToAdd = defaultLanguages.filter((language) => !addedLanguages.includes(language)).filter((language) => languages.find((l) => l.id === language)), timeout = setTimeout(() => {
499
+ documentReadOnly || handleAddLanguage(languagesToAdd);
500
+ });
532
501
  return () => clearTimeout(timeout);
533
502
  }
534
503
  }, [
@@ -537,26 +506,27 @@ function InternationalizedArray(props) {
537
506
  handleAddLanguage,
538
507
  defaultLanguages,
539
508
  addedLanguages,
540
- languages
509
+ languages,
510
+ documentReadOnly
541
511
  ]);
542
512
  const handleRestoreOrder = react.useCallback(() => {
543
- if (!(value != null && value.length) || !(languages != null && languages.length))
513
+ if (!value?.length || !languages?.length)
544
514
  return;
545
515
  const updatedValue = value.reduce((acc, v) => {
546
- const newIndex = languages.findIndex((l) => l.id === (v == null ? void 0 : v._key));
516
+ const newIndex = languages.findIndex((l) => l.id === v?._key);
547
517
  return newIndex > -1 && (acc[newIndex] = v), acc;
548
518
  }, []).filter(Boolean);
549
- (value == null ? void 0 : value.length) !== updatedValue.length && toast.push({
519
+ value?.length !== updatedValue.length && toast.push({
550
520
  title: "There was an error reordering languages",
551
521
  status: "warning"
552
522
  }), onChange(sanity.set(updatedValue));
553
- }, [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(
554
- () => languages && languages.length > 1 ? languages.filter((l) => value == null ? void 0 : value.find((v) => v._key === l.id)) : [],
523
+ }, [toast, languages, onChange, value]), allKeysAreLanguages = react.useMemo(() => !value?.length || !languages?.length ? !0 : value?.every((v) => languages.find((l) => l?.id === v?._key)), [value, languages]), languagesInUse = react.useMemo(
524
+ () => languages && languages.length > 1 ? languages.filter((l) => value?.find((v) => v._key === l.id)) : [],
555
525
  [languages, value]
556
- ), languagesOutOfOrder = react.useMemo(() => !(value != null && value.length) || !languagesInUse.length ? [] : value.map(
526
+ ), languagesOutOfOrder = react.useMemo(() => !value?.length || !languagesInUse.length ? [] : value.map(
557
527
  (v, vIndex) => vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v
558
528
  ).filter(Boolean), [value, languagesInUse]), languagesAreValid = react.useMemo(
559
- () => !(languages != null && languages.length) || (languages == null ? void 0 : languages.length) && languages.every((item) => item.id && item.title),
529
+ () => !languages?.length || languages?.length && languages.every((item) => item.id && item.title),
560
530
  [languages]
561
531
  );
562
532
  react.useEffect(() => {
@@ -571,21 +541,22 @@ function InternationalizedArray(props) {
571
541
  const addButtonsAreVisible = (
572
542
  // Plugin was configured to display buttons here (default!)
573
543
  buttonLocations.includes("field") && // There's at least one language visible
574
- (filteredLanguages == null ? void 0 : filteredLanguages.length) > 0 && // Not every language has a value yet
544
+ filteredLanguages?.length > 0 && // Not every language has a value yet
575
545
  !allLanguagesArePresent
576
- ), fieldHasMembers = (members == null ? void 0 : members.length) > 0;
546
+ ), fieldHasMembers = members?.length > 0;
577
547
  return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
578
548
  fieldHasMembers ? /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: filteredMembers.map((member) => member.kind === "item" ? /* @__PURE__ */ react.createElement(
579
549
  sanity.ArrayOfObjectsItem,
580
- __spreadProps$4(__spreadValues$5({}, props), {
550
+ {
551
+ ...props,
581
552
  key: member.key,
582
553
  member
583
- })
554
+ }
584
555
  ) : /* @__PURE__ */ jsxRuntime.jsx(sanity.MemberItemError, { member }, member.key)) }) : null,
585
556
  !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,
586
557
  addButtonsAreVisible ? /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
587
558
  /* @__PURE__ */ jsxRuntime.jsx(
588
- AddButtons$1,
559
+ AddButtons,
589
560
  {
590
561
  languages: filteredLanguages,
591
562
  value,
@@ -608,17 +579,9 @@ function InternationalizedArray(props) {
608
579
  ] });
609
580
  }
610
581
  function getLanguagesFieldOption(schemaType) {
611
- var _a;
612
- return schemaType ? ((_a = schemaType.options) == null ? void 0 : _a.languages) || getLanguagesFieldOption(schemaType.type) : void 0;
582
+ return schemaType ? schemaType.options?.languages || getLanguagesFieldOption(schemaType.type) : void 0;
613
583
  }
614
- var __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) => {
615
- for (var prop in b || (b = {}))
616
- __hasOwnProp$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
617
- if (__getOwnPropSymbols$4)
618
- for (var prop of __getOwnPropSymbols$4(b))
619
- __propIsEnum$4.call(b, prop) && __defNormalProp$4(a, prop, b[prop]);
620
- return a;
621
- }, __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b)), array = (config) => {
584
+ var array = (config) => {
622
585
  const { apiVersion, select, languages, type } = config, typeName = typeof type == "string" ? type : type.name, arrayName = createFieldName(typeName), objectName = createFieldName(typeName, !0);
623
586
  return sanity.defineField({
624
587
  name: arrayName,
@@ -634,19 +597,19 @@ var __defProp$4 = Object.defineProperty, __defProps$3 = Object.defineProperties,
634
597
  languages
635
598
  },
636
599
  of: [
637
- sanity.defineField(__spreadProps$3(__spreadValues$4({}, typeof type == "string" ? {} : type), {
600
+ sanity.defineField({
601
+ ...typeof type == "string" ? {} : type,
638
602
  name: objectName,
639
603
  type: objectName
640
- }))
604
+ })
641
605
  ],
642
606
  // @ts-expect-error - fix typings
643
607
  validation: (rule) => rule.custom(async (value, context) => {
644
- var _a;
645
- if (!value || value.length === 0 || value.length === 1 && !((_a = value[0]) != null && _a._key))
608
+ if (!value || value.length === 0 || value.length === 1 && !value[0]?._key)
646
609
  return !0;
647
610
  const selectedValue = getSelectedValue(select, context.document), client = context.getClient({ apiVersion });
648
611
  let contextLanguages = [];
649
- const languagesFieldOption = getLanguagesFieldOption(context == null ? void 0 : context.type);
612
+ const languagesFieldOption = getLanguagesFieldOption(context?.type);
650
613
  if (Array.isArray(languagesFieldOption))
651
614
  contextLanguages = languagesFieldOption;
652
615
  else if (Array.isArray(peek(selectedValue)))
@@ -673,7 +636,7 @@ var __defProp$4 = Object.defineProperty, __defProps$3 = Object.defineProperties,
673
636
  if (value && value.length > contextLanguages.length)
674
637
  return `Cannot be more than ${contextLanguages.length === 1 ? "1 item" : `${contextLanguages.length} items`}`;
675
638
  const languageIds = new Set(contextLanguages.map((lang) => lang.id)), nonLanguageKeys = value.filter(
676
- (item) => (item == null ? void 0 : item._key) && !languageIds.has(item._key)
639
+ (item) => item?._key && !languageIds.has(item._key)
677
640
  );
678
641
  if (nonLanguageKeys.length)
679
642
  return {
@@ -682,7 +645,7 @@ var __defProp$4 = Object.defineProperty, __defProps$3 = Object.defineProperties,
682
645
  };
683
646
  const seenKeys = /* @__PURE__ */ new Set(), duplicateValues = [];
684
647
  for (const item of value)
685
- item != null && item._key && (seenKeys.has(item._key) ? duplicateValues.push(item) : seenKeys.add(item._key));
648
+ item?._key && (seenKeys.has(item._key) ? duplicateValues.push(item) : seenKeys.add(item._key));
686
649
  return duplicateValues.length ? {
687
650
  message: "There can only be one field per language",
688
651
  paths: duplicateValues.map((item) => [{ _key: item._key }])
@@ -691,7 +654,7 @@ var __defProp$4 = Object.defineProperty, __defProps$3 = Object.defineProperties,
691
654
  });
692
655
  };
693
656
  function getToneFromValidation(validations) {
694
- if (!(validations != null && validations.length))
657
+ if (!validations?.length)
695
658
  return;
696
659
  const validationLevels = validations.map((v) => v.level);
697
660
  if (validationLevels.includes("error"))
@@ -699,31 +662,22 @@ function getToneFromValidation(validations) {
699
662
  if (validationLevels.includes("warning"))
700
663
  return "caution";
701
664
  }
702
- 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) => {
703
- for (var prop in b || (b = {}))
704
- __hasOwnProp$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
705
- if (__getOwnPropSymbols$3)
706
- for (var prop of __getOwnPropSymbols$3(b))
707
- __propIsEnum$3.call(b, prop) && __defNormalProp$3(a, prop, b[prop]);
708
- return a;
709
- }, __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
710
665
  function InternationalizedInput(props) {
711
666
  const parentValue = sanity.useFormValue(
712
667
  props.path.slice(0, -1)
713
668
  ), originalOnChange = props.inputProps.onChange, wrappedOnChange = react.useCallback(
714
669
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
715
670
  (patches) => {
716
- var _a;
717
671
  if (!Array.isArray(patches))
718
672
  return originalOnChange(patches);
719
- const valueField = (_a = props.value) == null ? void 0 : _a.value;
673
+ const valueField = props.value?.value;
720
674
  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)) {
721
675
  const initPatch = valueField === void 0 ? { type: "setIfMissing", path: ["value"], value: [] } : null, fixedPatches = patches.map((patch) => {
722
676
  if (!patch || typeof patch != "object")
723
677
  return patch;
724
678
  if (patch.type === "insert" && patch.path && Array.isArray(patch.path)) {
725
679
  const fixedPath = patch.path[0] === "value" ? patch.path : ["value", ...patch.path];
726
- return __spreadProps$2(__spreadValues$3({}, patch), { path: fixedPath });
680
+ return { ...patch, path: fixedPath };
727
681
  }
728
682
  return patch;
729
683
  }), allPatches = initPatch ? [initPatch, ...fixedPatches] : fixedPatches;
@@ -732,7 +686,8 @@ function InternationalizedInput(props) {
732
686
  return originalOnChange(patches);
733
687
  },
734
688
  [props.value, originalOnChange]
735
- ), inlineProps = __spreadProps$2(__spreadValues$3({}, props.inputProps), {
689
+ ), inlineProps = {
690
+ ...props.inputProps,
736
691
  // This is the magic that makes inline editing work?
737
692
  members: props.inputProps.members.filter(
738
693
  (m) => m.kind === "field" && m.name === "value"
@@ -742,17 +697,13 @@ function InternationalizedInput(props) {
742
697
  value: props.value,
743
698
  // Use our wrapped onChange handler
744
699
  onChange: wrappedOnChange
745
- }), { validation, value, onChange, readOnly } = inlineProps, { languages, languageDisplay, defaultLanguages } = useInternationalizedArrayContext(), languageKeysInUse = react.useMemo(
746
- () => {
747
- var _a;
748
- return (_a = parentValue == null ? void 0 : parentValue.map((v) => v._key)) != null ? _a : [];
749
- },
700
+ }, { validation, value, onChange, readOnly } = inlineProps, { languages, languageDisplay, defaultLanguages } = useInternationalizedArrayContext(), languageKeysInUse = react.useMemo(
701
+ () => parentValue?.map((v) => v._key) ?? [],
750
702
  [parentValue]
751
- ), keyIsValid = languages != null && languages.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = react.useCallback(
703
+ ), keyIsValid = languages?.length ? languages.find((l) => l.id === value._key) : !1, handleKeyChange = react.useCallback(
752
704
  (event) => {
753
- var _a;
754
- const languageId = (_a = event == null ? void 0 : event.currentTarget) == null ? void 0 : _a.value;
755
- !value || !(languages != null && languages.length) || !languages.find((l) => l.id === languageId) || onChange([sanity.set(languageId, ["_key"])]);
705
+ const languageId = event?.currentTarget?.value;
706
+ !value || !languages?.length || !languages.find((l) => l.id === languageId) || onChange([sanity.set(languageId, ["_key"])]);
756
707
  },
757
708
  [onChange, value, languages]
758
709
  ), handleUnset = react.useCallback(() => {
@@ -805,14 +756,7 @@ function InternationalizedInput(props) {
805
756
  ] })
806
757
  ] }) });
807
758
  }
808
- 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) => {
809
- for (var prop in b || (b = {}))
810
- __hasOwnProp$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
811
- if (__getOwnPropSymbols$2)
812
- for (var prop of __getOwnPropSymbols$2(b))
813
- __propIsEnum$2.call(b, prop) && __defNormalProp$2(a, prop, b[prop]);
814
- return a;
815
- }, __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b)), object = (config) => {
759
+ var object = (config) => {
816
760
  const { type } = config, typeName = typeof type == "string" ? type : type.name, objectName = createFieldName(typeName, !0);
817
761
  return sanity.defineField({
818
762
  name: objectName,
@@ -823,9 +767,10 @@ var __defProp$2 = Object.defineProperty, __defProps$1 = Object.defineProperties,
823
767
  item: InternationalizedInput
824
768
  },
825
769
  fields: [
826
- sanity.defineField(__spreadProps$1(__spreadValues$2({}, typeof type == "string" ? { type } : type), {
770
+ sanity.defineField({
771
+ ...typeof type == "string" ? { type } : type,
827
772
  name: "value"
828
- }))
773
+ })
829
774
  ],
830
775
  preview: {
831
776
  select: {
@@ -834,20 +779,13 @@ var __defProp$2 = Object.defineProperty, __defProps$1 = Object.defineProperties,
834
779
  }
835
780
  }
836
781
  });
837
- }, __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) => {
838
- for (var prop in b || (b = {}))
839
- __hasOwnProp$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
840
- if (__getOwnPropSymbols$1)
841
- for (var prop of __getOwnPropSymbols$1(b))
842
- __propIsEnum$1.call(b, prop) && __defNormalProp$1(a, prop, b[prop]);
843
- return a;
844
782
  };
845
783
  function flattenSchemaType(schemaType) {
846
784
  return sanity.isDocumentSchemaType(schemaType) ? extractInnerFields(schemaType.fields, [], 3) : (console.error("Schema type is not a document"), []);
847
785
  }
848
786
  function extractInnerFields(fields, path, maxDepth) {
849
787
  return path.length >= maxDepth ? [] : fields.reduce((acc, field) => {
850
- const thisFieldWithPath = __spreadValues$1({ path: [...path, field.name] }, field);
788
+ const thisFieldWithPath = { path: [...path, field.name], ...field };
851
789
  if (field.type.jsonType === "object") {
852
790
  const innerFields = extractInnerFields(
853
791
  field.type.fields,
@@ -869,21 +807,12 @@ function extractInnerFields(fields, path, maxDepth) {
869
807
  return [...acc, thisFieldWithPath];
870
808
  }, []);
871
809
  }
872
- 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) => {
873
- for (var prop in b || (b = {}))
874
- __hasOwnProp.call(b, prop) && __defNormalProp(a, prop, b[prop]);
875
- if (__getOwnPropSymbols)
876
- for (var prop of __getOwnPropSymbols(b))
877
- __propIsEnum.call(b, prop) && __defNormalProp(a, prop, b[prop]);
878
- return a;
879
- }, __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
880
810
  const internationalizedArray = sanity.definePlugin((config) => {
881
- const pluginConfig = __spreadValues(__spreadValues({}, CONFIG_DEFAULT), config), {
811
+ const pluginConfig = { ...CONFIG_DEFAULT, ...config }, {
882
812
  apiVersion = "2025-10-15",
883
813
  select,
884
814
  languages,
885
815
  fieldTypes,
886
- defaultLanguages,
887
816
  buttonLocations
888
817
  } = pluginConfig;
889
818
  return {
@@ -904,16 +833,17 @@ const internationalizedArray = sanity.definePlugin((config) => {
904
833
  // Wrap document editor with a language provider
905
834
  form: {
906
835
  components: {
907
- field: (props) => /* @__PURE__ */ jsxRuntime.jsx(InternationalizedField, __spreadValues({}, props)),
836
+ field: (props) => /* @__PURE__ */ jsxRuntime.jsx(InternationalizedField, { ...props }),
908
837
  input: (props) => !(props.id === "root" && sanity.isObjectInputProps(props)) || !flattenSchemaType(props.schemaType).map(
909
838
  (field) => field.type.name
910
839
  ).some(
911
840
  (name) => name.startsWith("internationalizedArray")
912
841
  ) ? props.renderDefault(props) : /* @__PURE__ */ jsxRuntime.jsx(
913
842
  InternationalizedArrayProvider,
914
- __spreadProps(__spreadValues({}, props), {
843
+ {
844
+ ...props,
915
845
  internationalizedArray: pluginConfig
916
- })
846
+ }
917
847
  )
918
848
  }
919
849
  },
@@ -921,7 +851,7 @@ const internationalizedArray = sanity.definePlugin((config) => {
921
851
  schema: {
922
852
  types: [
923
853
  ...fieldTypes.map(
924
- (type) => array({ type, apiVersion, select, languages, defaultLanguages })
854
+ (type) => array({ type, apiVersion, select, languages })
925
855
  ),
926
856
  ...fieldTypes.map((type) => object({ type }))
927
857
  ]