sanity-plugin-internationalized-array 1.0.0 → 1.1.1

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 CHANGED
@@ -2,9 +2,7 @@
2
2
 
3
3
  > NOTE This is for the Studio v3 version of the plugin
4
4
 
5
- A helper function that renders a custom input component for writing localised fields of content into an array.
6
-
7
- **This an early proof-of-concept and should not yet be used without thorough testing.**
5
+ A helper function that renders a custom input component for writing localized fields of content into an array.
8
6
 
9
7
  ![2022-07-27 12 26 25](https://user-images.githubusercontent.com/9684022/181235876-45a6a4c5-e6d3-48a2-a6a0-523ee7196184.gif)
10
8
 
@@ -20,35 +18,81 @@ or
20
18
  yarn add sanity-plugin-internationalized-array-v3@studio-v3
21
19
  ```
22
20
 
23
- Add an array to your schema by importing the helper function.
21
+ ## Usage (simple)
24
22
 
25
- ```js
23
+ Add it as a plugin in sanity.config.ts (or .js):
24
+
25
+ ```ts
26
+ import {createConfig} from 'sanity'
26
27
  import {internationalizedArray} from 'sanity-plugin-internationalized-array'
27
28
 
28
- // ./src/schema/person.js
29
- export default {
30
- name: 'person',
31
- title: 'Person',
32
- type: 'document',
33
- fields: [
34
- // ...all your other fields
29
+ export const createConfig({
30
+ // ...
31
+ plugins: [
35
32
  internationalizedArray({
36
- // Required, the `name` of the outer array
37
- name: 'greeting',
38
- // Required, the `type` of the inner field
39
- // One of: string | text | number | boolean
40
- type: 'string',
41
- // Required, must be an array of objects
42
33
  languages: [
43
34
  {id: 'en', title: 'English'},
44
- {id: 'fr', title: 'French'},
35
+ {id: 'fr', title: 'French'}
45
36
  ],
46
- }),
47
- ],
48
- }
37
+ fieldTypes: ['string'],
38
+ })
39
+ ]
40
+ })
49
41
  ```
50
42
 
51
- This will create an Array field where `string` fields can be added with the name `title`. The custom input contains buttons which will add new array items with the language as the `_key` value. Data returned from this array will look like this:
43
+ This will register two new fields to the schema, based on the settings passed into `fieldTypes`:
44
+
45
+ - `internationalizedArrayString` an array field of:
46
+ - `internationalizedArrayStringValue` an object field, with a single `string` field inside called `value`
47
+
48
+ You can pass in more registered schema type names to generate more internationalized arrays. Use them in your schema like this:
49
+
50
+ ```ts
51
+ defineField({
52
+ name: 'greeting',
53
+ type: 'internationalizedArrayString',
54
+ }),
55
+ ```
56
+
57
+ ## Usage (advanced)
58
+
59
+ For more control over the `value` field, you can pass a schema definition into the `fieldTypes` array.
60
+
61
+ ```ts
62
+ import {createConfig} from 'sanity'
63
+ import {internationalizedArray} from 'sanity-plugin-internationalized-array'
64
+
65
+ export const createConfig({
66
+ // ...
67
+ plugins: [
68
+ internationalizedArray({
69
+ languages: [
70
+ {id: 'en', title: 'English'},
71
+ {id: 'fr', title: 'French'}
72
+ ],
73
+ fieldTypes: [
74
+ defineField({
75
+ name: 'featuredProduct',
76
+ type: 'reference',
77
+ to: [{type: 'product'}]
78
+ hidden: (({document}) => !document?.title)
79
+ })
80
+ ],
81
+ })
82
+ ]
83
+ })
84
+ ```
85
+
86
+ This would also create two new fields in your schema.
87
+
88
+ - `internationalizedArrayFeaturedProduct` an array field of:
89
+ - `internationalizedArrayFeaturedProductValue` an object field, with a single `string` field inside called `value`
90
+
91
+ Note that the `name` key in the field gets rewritten to `value` and is instead used to name the object field.
92
+
93
+ ## Shape of stored data
94
+
95
+ The custom input contains buttons which will add new array items with the language as the `_key` value. Data returned from this array will look like this:
52
96
 
53
97
  ```json
54
98
  "greeting": [
@@ -57,6 +101,8 @@ This will create an Array field where `string` fields can be added with the name
57
101
  ]
58
102
  ```
59
103
 
104
+ ## Querying data
105
+
60
106
  Using GROQ filters you can query for a specific language key like so:
61
107
 
62
108
  ```js
@@ -73,7 +119,7 @@ Follow the instructions inside the script and set the `_type` and field name you
73
119
 
74
120
  Please take a backup first!
75
121
 
76
- ### Why store localised field data like this?
122
+ ### Why store localized field data like this?
77
123
 
78
124
  The most popular way to store field-level translated content is in an object using the method prescribed in [@sanity/language-filter](https://www.npmjs.com/package/@sanity/language-filter). This works well and creates tidy object structures, but also create a unique field path for every unique field name, multiplied by the number of languages in your dataset.
79
125
 
package/lib/cjs/index.js CHANGED
@@ -1,36 +1,41 @@
1
- var $dyHF6$sanity = require("sanity");
2
- var $dyHF6$reactjsxruntime = require("react/jsx-runtime");
3
- var $dyHF6$react = require("react");
4
- var $dyHF6$sanityform = require("sanity/form");
5
- var $dyHF6$sanityui = require("@sanity/ui");
6
- var $dyHF6$sanityicons = require("@sanity/icons");
7
- var $dyHF6$styledcomponents = require("styled-components");
8
-
9
- function $parcel$exportWildcard(dest, source) {
10
- Object.keys(source).forEach(function(key) {
11
- if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
12
- return;
13
- }
14
-
15
- Object.defineProperty(dest, key, {
16
- enumerable: true,
17
- get: function get() {
18
- return source[key];
19
- }
20
- });
21
- });
1
+ var $k7rGe$sanity = require("sanity");
2
+ var $k7rGe$reactjsxruntime = require("react/jsx-runtime");
3
+ var $k7rGe$react = require("react");
4
+ var $k7rGe$sanityform = require("sanity/form");
5
+ var $k7rGe$sanityui = require("@sanity/ui");
6
+ var $k7rGe$sanityicons = require("@sanity/icons");
7
+ var $k7rGe$styledcomponents = require("styled-components");
22
8
 
23
- return dest;
24
- }
25
9
  function $parcel$export(e, n, v, s) {
26
10
  Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
27
11
  }
28
12
  function $parcel$interopDefault(a) {
29
13
  return a && a.__esModule ? a.default : a;
30
14
  }
31
- var $7594066088192472$exports = {};
32
15
 
33
- $parcel$export($7594066088192472$exports, "internationalizedArray", () => $7594066088192472$export$bec7eb13daf35f0e);
16
+ $parcel$export(module.exports, "internationalizedArray", () => $45e3093e7de8dca2$export$bec7eb13daf35f0e);
17
+
18
+
19
+ function $a0eca5cb1c058e2c$export$8a7688a96d852767(string) {
20
+ return string.replace(/-([a-z])/g, (g)=>g[1].toUpperCase());
21
+ }
22
+ function $a0eca5cb1c058e2c$export$348afa8c9ff47183(string) {
23
+ return string.split(` `).map((word)=>word.charAt(0).toUpperCase() + word.slice(1)).join(` `);
24
+ }
25
+ function $a0eca5cb1c058e2c$export$26c6f48841fe1a8a(string) {
26
+ return $a0eca5cb1c058e2c$export$348afa8c9ff47183($a0eca5cb1c058e2c$export$8a7688a96d852767(string));
27
+ }
28
+ function $a0eca5cb1c058e2c$export$ab1ce2a474f41f52(name, addValue = false) {
29
+ return addValue ? [
30
+ `internationalizedArray`,
31
+ $a0eca5cb1c058e2c$export$26c6f48841fe1a8a(name),
32
+ `Value`
33
+ ].join(``) : [
34
+ `internationalizedArray`,
35
+ $a0eca5cb1c058e2c$export$26c6f48841fe1a8a(name)
36
+ ].join(``);
37
+ }
38
+
34
39
 
35
40
 
36
41
 
@@ -44,12 +49,12 @@ $parcel$export($7594066088192472$exports, "internationalizedArray", () => $75940
44
49
  // https://github.com/styled-components/styled-components/issues/2449
45
50
  // Table
46
51
  const $acfbfc6ee2363807$var$TableWrapper = (props = {})=>{
47
- return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Box), {
52
+ return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Box), {
48
53
  as: "table",
49
54
  ...props
50
55
  });
51
56
  };
52
- const $acfbfc6ee2363807$var$StyledTable = (0, ($parcel$interopDefault($dyHF6$styledcomponents)))($acfbfc6ee2363807$var$TableWrapper)(()=>(0, $dyHF6$styledcomponents.css)`
57
+ const $acfbfc6ee2363807$var$StyledTable = (0, ($parcel$interopDefault($k7rGe$styledcomponents)))($acfbfc6ee2363807$var$TableWrapper)(()=>(0, $k7rGe$styledcomponents.css)`
53
58
  display: table;
54
59
  width: 100%;
55
60
 
@@ -59,19 +64,19 @@ const $acfbfc6ee2363807$var$StyledTable = (0, ($parcel$interopDefault($dyHF6$sty
59
64
  `);
60
65
  function $acfbfc6ee2363807$export$54ec01a60f47d33d(props) {
61
66
  const { children: children , ...rest } = props;
62
- return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)($acfbfc6ee2363807$var$StyledTable, {
67
+ return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)($acfbfc6ee2363807$var$StyledTable, {
63
68
  ...rest,
64
69
  children: children
65
70
  });
66
71
  }
67
72
  // Row
68
73
  const $acfbfc6ee2363807$var$RowWrapper = (props = {})=>{
69
- return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Card), {
74
+ return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Card), {
70
75
  as: "tr",
71
76
  ...props
72
77
  });
73
78
  };
74
- const $acfbfc6ee2363807$var$StyledRow = (0, ($parcel$interopDefault($dyHF6$styledcomponents)))($acfbfc6ee2363807$var$RowWrapper)(()=>(0, $dyHF6$styledcomponents.css)`
79
+ const $acfbfc6ee2363807$var$StyledRow = (0, ($parcel$interopDefault($k7rGe$styledcomponents)))($acfbfc6ee2363807$var$RowWrapper)(()=>(0, $k7rGe$styledcomponents.css)`
75
80
  display: table-row;
76
81
 
77
82
  &:not([hidden]) {
@@ -80,19 +85,19 @@ const $acfbfc6ee2363807$var$StyledRow = (0, ($parcel$interopDefault($dyHF6$style
80
85
  `);
81
86
  function $acfbfc6ee2363807$export$b05581f4e764e162(props) {
82
87
  const { children: children , ...rest } = props;
83
- return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)($acfbfc6ee2363807$var$StyledRow, {
88
+ return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)($acfbfc6ee2363807$var$StyledRow, {
84
89
  ...rest,
85
90
  children: children
86
91
  });
87
92
  }
88
93
  // Cell
89
94
  const $acfbfc6ee2363807$var$CellWrapper = (props = {})=>{
90
- return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Box), {
95
+ return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Box), {
91
96
  as: "td",
92
97
  ...props
93
98
  });
94
99
  };
95
- const $acfbfc6ee2363807$var$StyledCell = (0, ($parcel$interopDefault($dyHF6$styledcomponents)))($acfbfc6ee2363807$var$CellWrapper)(()=>(0, $dyHF6$styledcomponents.css)`
100
+ const $acfbfc6ee2363807$var$StyledCell = (0, ($parcel$interopDefault($k7rGe$styledcomponents)))($acfbfc6ee2363807$var$CellWrapper)(()=>(0, $k7rGe$styledcomponents.css)`
96
101
  display: table-cell;
97
102
 
98
103
  &:not([hidden]) {
@@ -101,7 +106,7 @@ const $acfbfc6ee2363807$var$StyledCell = (0, ($parcel$interopDefault($dyHF6$styl
101
106
  `);
102
107
  function $acfbfc6ee2363807$export$1e4baea7053fc0e3(props) {
103
108
  const { children: children , ...rest } = props;
104
- return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)($acfbfc6ee2363807$var$StyledCell, {
109
+ return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)($acfbfc6ee2363807$var$StyledCell, {
105
110
  ...rest,
106
111
  children: children
107
112
  });
@@ -125,37 +130,37 @@ const $aaf4d804384afd00$var$schemaExample = {
125
130
  ]
126
131
  };
127
132
  function $aaf4d804384afd00$export$2e2bcd8739ae039() {
128
- return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Card), {
133
+ return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Card), {
129
134
  tone: "caution",
130
135
  border: true,
131
136
  radius: 2,
132
137
  padding: 3,
133
- children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Stack), {
138
+ children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $k7rGe$sanityui.Stack), {
134
139
  space: 4,
135
140
  children: [
136
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Text), {
141
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $k7rGe$sanityui.Text), {
137
142
  children: [
138
143
  "An array of language objects must be passed into the ",
139
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)("code", {
144
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)("code", {
140
145
  children: "internationalizedArray"
141
146
  }),
142
147
  " ",
143
148
  "helper function, each with an ",
144
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)("code", {
149
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)("code", {
145
150
  children: "id"
146
151
  }),
147
152
  " and ",
148
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)("code", {
153
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)("code", {
149
154
  children: "title"
150
155
  }),
151
156
  " field. Example:"
152
157
  ]
153
158
  }),
154
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Card), {
159
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Card), {
155
160
  padding: 2,
156
161
  border: true,
157
162
  radius: 2,
158
- children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Code), {
163
+ children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Code), {
159
164
  size: 1,
160
165
  language: "javascript",
161
166
  children: JSON.stringify($aaf4d804384afd00$var$schemaExample, null, 2)
@@ -180,10 +185,11 @@ function $7e790a73eaf2b445$export$2e2bcd8739ae039(props) {
180
185
  const { members: members , value: value , schemaType: schemaType , onChange: onChange } = props;
181
186
  const readOnly = typeof schemaType.readOnly === "boolean" ? schemaType.readOnly : false;
182
187
  const { options: options } = schemaType;
183
- const languages = (0, $dyHF6$react.useMemo)(()=>options?.languages ?? [], [
188
+ const toast = (0, $k7rGe$sanityui.useToast)();
189
+ const languages = (0, $k7rGe$react.useMemo)(()=>options?.languages ?? [], [
184
190
  options
185
191
  ]);
186
- const handleAddLanguage = (0, $dyHF6$react.useCallback)((languageId)=>{
192
+ const handleAddLanguage = (0, $k7rGe$react.useCallback)((languageId)=>{
187
193
  // Create new items
188
194
  const newItems = languageId ? [
189
195
  {
@@ -200,22 +206,23 @@ function $7e790a73eaf2b445$export$2e2bcd8739ae039(props) {
200
206
  // What languages are there beyond that index?
201
207
  const remainingLanguages = languages.slice(languageIndex + 1);
202
208
  // So what is the index in the current value array of the next language in the language array?
203
- const nextLanguageIndex = languagesInUse.findIndex((l)=>remainingLanguages.find((r)=>r.id === l._key));
209
+ const nextLanguageIndex = languagesInUse.findIndex((l)=>// eslint-disable-next-line max-nested-callbacks
210
+ remainingLanguages.find((r)=>r.id === l._key));
204
211
  // Keep local state up to date incase multiple insertions are being made
205
212
  if (nextLanguageIndex < 0) languagesInUse.push(item);
206
213
  else languagesInUse.splice(nextLanguageIndex, 0, item);
207
- return nextLanguageIndex < 0 ? (0, $dyHF6$sanityform.insert)([
214
+ return nextLanguageIndex < 0 ? (0, $k7rGe$sanityform.insert)([
208
215
  item
209
216
  ], "after", [
210
217
  nextLanguageIndex
211
- ]) : (0, $dyHF6$sanityform.insert)([
218
+ ]) : (0, $k7rGe$sanityform.insert)([
212
219
  item
213
220
  ], "before", [
214
221
  nextLanguageIndex
215
222
  ]);
216
223
  });
217
224
  onChange([
218
- (0, $dyHF6$sanityform.setIfMissing)([]),
225
+ (0, $k7rGe$sanityform.setIfMissing)([]),
219
226
  ...insertions
220
227
  ]);
221
228
  }, [
@@ -223,8 +230,8 @@ function $7e790a73eaf2b445$export$2e2bcd8739ae039(props) {
223
230
  onChange,
224
231
  value
225
232
  ]);
226
- const handleUnsetByKey = (0, $dyHF6$react.useCallback)((_key)=>{
227
- onChange((0, $dyHF6$sanityform.unset)([
233
+ const handleUnsetByKey = (0, $k7rGe$react.useCallback)((_key)=>{
234
+ onChange((0, $k7rGe$sanityform.unset)([
228
235
  {
229
236
  _key: _key
230
237
  }
@@ -233,29 +240,35 @@ function $7e790a73eaf2b445$export$2e2bcd8739ae039(props) {
233
240
  onChange
234
241
  ]);
235
242
  // TODO: This is lazy, reordering and re-setting the whole array – it could be surgical
236
- const handleRestoreOrder = (0, $dyHF6$react.useCallback)(()=>{
243
+ const handleRestoreOrder = (0, $k7rGe$react.useCallback)(()=>{
237
244
  if (!value?.length) return;
245
+ console.log(value);
238
246
  // Create a new value array in the correct order
239
247
  // This would also strip out values that don't have a language as the key
240
248
  const updatedValue = value.reduce((acc, v)=>{
241
249
  const newIndex = languages.findIndex((l)=>l.id === v?._key);
242
- if (newIndex) acc[newIndex] = v;
250
+ if (newIndex > -1) acc[newIndex] = v;
243
251
  return acc;
244
252
  }, []).filter(Boolean);
245
- onChange((0, $dyHF6$sanityform.set)(updatedValue));
253
+ if (value.length !== updatedValue.length) toast.push({
254
+ title: "There was an error reordering languages",
255
+ status: "warning"
256
+ });
257
+ onChange((0, $k7rGe$sanityform.set)(updatedValue));
246
258
  }, [
259
+ toast,
247
260
  languages,
248
261
  onChange,
249
262
  value
250
263
  ]);
251
- const allKeysAreLanguages = (0, $dyHF6$react.useMemo)(()=>{
264
+ const allKeysAreLanguages = (0, $k7rGe$react.useMemo)(()=>{
252
265
  return value?.every((v)=>languages.find((l)=>l?.id === v?._key));
253
266
  }, [
254
267
  value,
255
268
  languages
256
269
  ]);
257
270
  // Check languages are in the correct order
258
- const languagesOutOfOrder = (0, $dyHF6$react.useMemo)(()=>{
271
+ const languagesOutOfOrder = (0, $k7rGe$react.useMemo)(()=>{
259
272
  if (!value?.length) return [];
260
273
  const languagesInUse = languages.filter((l)=>value.find((v)=>v._key === l.id));
261
274
  return value.map((v, vIndex)=>vIndex === languagesInUse.findIndex((l)=>l.id === v._key) ? null : v).filter(Boolean);
@@ -263,60 +276,60 @@ function $7e790a73eaf2b445$export$2e2bcd8739ae039(props) {
263
276
  value,
264
277
  languages
265
278
  ]);
266
- const languagesAreValid = (0, $dyHF6$react.useMemo)(()=>languages?.length && languages.every((item)=>item.id && item.title), [
279
+ const languagesAreValid = (0, $k7rGe$react.useMemo)(()=>!languages?.length || languages?.length && languages.every((item)=>item.id && item.title), [
267
280
  languages
268
281
  ]);
269
- if (!languagesAreValid) return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $aaf4d804384afd00$export$2e2bcd8739ae039), {});
270
- return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Stack), {
282
+ if (!languagesAreValid) return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $aaf4d804384afd00$export$2e2bcd8739ae039), {});
283
+ return /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $k7rGe$sanityui.Stack), {
271
284
  space: 2,
272
285
  children: [
273
- members?.length > 0 ? /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$54ec01a60f47d33d), {
274
- children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)("tbody", {
275
- children: members.map((member)=>/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $acfbfc6ee2363807$export$b05581f4e764e162), {
286
+ members?.length > 0 ? /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$54ec01a60f47d33d), {
287
+ children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)("tbody", {
288
+ children: members.map((member)=>/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $acfbfc6ee2363807$export$b05581f4e764e162), {
276
289
  tone: member?.item?.validation?.length > 0 ? (0, $6a9b28384eb7074d$export$16423358d6ebe294)(member.item.validation) : undefined,
277
290
  children: [
278
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$1e4baea7053fc0e3), {
291
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$1e4baea7053fc0e3), {
279
292
  style: {
280
293
  verticalAlign: "bottom"
281
294
  },
282
- children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Box), {
295
+ children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Box), {
283
296
  paddingY: 3,
284
297
  paddingRight: 2,
285
- children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Label), {
298
+ children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Label), {
286
299
  muted: true,
287
300
  size: 1,
288
301
  children: member.key
289
302
  })
290
303
  })
291
304
  }),
292
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$1e4baea7053fc0e3), {
305
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$1e4baea7053fc0e3), {
293
306
  paddingRight: 2,
294
307
  style: {
295
308
  width: `100%`
296
309
  },
297
- children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityform.MemberItem), {
310
+ children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityform.MemberItem), {
298
311
  ...props,
299
312
  member: member
300
313
  })
301
314
  }),
302
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$1e4baea7053fc0e3), {
315
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$1e4baea7053fc0e3), {
303
316
  style: {
304
317
  verticalAlign: "bottom"
305
318
  },
306
- children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Flex), {
319
+ children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $k7rGe$sanityui.Flex), {
307
320
  align: "center",
308
321
  justify: "flex-end",
309
322
  gap: 3,
310
323
  children: [
311
- member.item.validation.length > 0 ? /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Box), {
324
+ member?.item?.validation?.length > 0 ? /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Box), {
312
325
  paddingLeft: 2,
313
- children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityform.FormFieldValidationStatus), {
326
+ children: /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityform.FormFieldValidationStatus), {
314
327
  validation: member.item.validation
315
328
  })
316
329
  }) : null,
317
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Button), {
330
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Button), {
318
331
  mode: "ghost",
319
- icon: (0, $dyHF6$sanityicons.RemoveIcon),
332
+ icon: (0, $k7rGe$sanityicons.RemoveIcon),
320
333
  tone: "critical",
321
334
  disabled: typeof readOnly === "boolean" ? readOnly : false,
322
335
  onClick: ()=>handleUnsetByKey(member.key)
@@ -328,33 +341,33 @@ function $7e790a73eaf2b445$export$2e2bcd8739ae039(props) {
328
341
  }, member.key))
329
342
  })
330
343
  }) : null,
331
- languagesOutOfOrder.length > 0 && allKeysAreLanguages ? /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Button), {
344
+ languagesOutOfOrder.length > 0 && allKeysAreLanguages ? /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Button), {
332
345
  tone: "caution",
333
- icon: (0, $dyHF6$sanityicons.RestoreIcon),
346
+ icon: (0, $k7rGe$sanityicons.RestoreIcon),
334
347
  onClick: ()=>handleRestoreOrder(),
335
348
  text: "Restore order of languages"
336
349
  }) : null,
337
- value && value.length < languages.length ? /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Stack), {
350
+ languages?.length > 0 ? /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsxs)((0, $k7rGe$sanityui.Stack), {
338
351
  space: 2,
339
352
  children: [
340
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Grid), {
353
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Grid), {
341
354
  columns: Math.min(languages.length, 5),
342
355
  gap: 2,
343
- children: languages.map((language)=>/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Button), {
356
+ children: languages.map((language)=>/*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Button), {
344
357
  tone: "primary",
345
358
  mode: "ghost",
346
359
  fontSize: 1,
347
360
  disabled: readOnly || Boolean(value?.find((item)=>item._key === language.id)),
348
361
  text: language.id.toUpperCase(),
349
- icon: (0, $dyHF6$sanityicons.AddIcon),
362
+ icon: (0, $k7rGe$sanityicons.AddIcon),
350
363
  onClick: ()=>handleAddLanguage(language.id)
351
364
  }, language.id))
352
365
  }),
353
- /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Button), {
366
+ /*#__PURE__*/ (0, $k7rGe$reactjsxruntime.jsx)((0, $k7rGe$sanityui.Button), {
354
367
  tone: "primary",
355
368
  mode: "ghost",
356
369
  disabled: readOnly || value && value?.length >= languages?.length,
357
- icon: (0, $dyHF6$sanityicons.AddIcon),
370
+ icon: (0, $k7rGe$sanityicons.AddIcon),
358
371
  text: value?.length ? `Add missing languages` : `Add all languages`,
359
372
  onClick: ()=>handleAddLanguage()
360
373
  })
@@ -365,22 +378,14 @@ function $7e790a73eaf2b445$export$2e2bcd8739ae039(props) {
365
378
  }
366
379
 
367
380
 
368
- const $7594066088192472$var$CONFIG_DEFAULT = {
369
- name: `title`,
370
- type: `string`,
371
- languages: []
372
- };
373
- function $7594066088192472$export$bec7eb13daf35f0e(config = $7594066088192472$var$CONFIG_DEFAULT) {
374
- const { name: name , type: type , languages: languages } = config;
375
- const configValidation = Array.isArray(config?.validation) ? config.validation : [
376
- config?.validation
377
- ];
378
- return (0, $dyHF6$sanity.defineField)({
379
- name: name,
380
- title: config?.title ?? undefined,
381
- group: config?.group ?? undefined,
382
- hidden: config?.hidden ?? undefined,
383
- readOnly: config?.readOnly ?? undefined,
381
+ var $0437df2e2e149362$export$2e2bcd8739ae039 = (config)=>{
382
+ const { languages: languages , type: type } = config;
383
+ const typeName = typeof type === `string` ? type : type.name;
384
+ const arrayName = (0, $a0eca5cb1c058e2c$export$ab1ce2a474f41f52)(typeName);
385
+ const objectName = (0, $a0eca5cb1c058e2c$export$ab1ce2a474f41f52)(typeName, true);
386
+ return (0, $k7rGe$sanity.defineField)({
387
+ name: arrayName,
388
+ title: "Internationalized array",
384
389
  type: "array",
385
390
  components: {
386
391
  input: (0, $7e790a73eaf2b445$export$2e2bcd8739ae039)
@@ -389,33 +394,12 @@ function $7594066088192472$export$bec7eb13daf35f0e(config = $7594066088192472$va
389
394
  languages: languages
390
395
  },
391
396
  of: [
392
- {
393
- type: "object",
394
- fields: [
395
- {
396
- name: "value",
397
- type: type
398
- },
399
- ],
400
- preview: {
401
- select: {
402
- title: "value",
403
- key: "_key"
404
- },
405
- prepare (select) {
406
- const { title: title , key: key } = select;
407
- return {
408
- title: title,
409
- subtitle: key.toUpperCase()
410
- };
411
- }
412
- }
413
- },
397
+ (0, $k7rGe$sanity.defineField)({
398
+ name: objectName,
399
+ type: objectName
400
+ })
414
401
  ],
415
- // @ts-ignore
416
- validation: (rule)=>{
417
- const rules = [];
418
- rules.push(rule.custom((value, context)=>{
402
+ validation: (rule)=>rule.max(languages?.length).custom((value, context)=>{
419
403
  const { languages: contextLanguages } = context?.type?.options ?? {};
420
404
  const nonLanguageKeys = value?.length ? value.filter((item)=>!contextLanguages.find((language)=>item._key === language.id)) : [];
421
405
  if (nonLanguageKeys.length) return {
@@ -451,18 +435,60 @@ function $7594066088192472$export$bec7eb13daf35f0e(config = $7594066088192472$va
451
435
  ])
452
436
  };
453
437
  return true;
454
- }));
455
- if (languages?.length) rules.push(rule.max(languages.length));
456
- return [
457
- ...rules,
458
- ...configValidation
459
- ].filter(Boolean);
460
- }
438
+ })
461
439
  });
462
- }
440
+ };
441
+
442
+
443
+
444
+
445
+ var $f727fb16d5d00f8f$export$2e2bcd8739ae039 = (config)=>{
446
+ const { type: type } = config;
447
+ const typeName = typeof type === `string` ? type : type.name;
448
+ const objectName = (0, $a0eca5cb1c058e2c$export$ab1ce2a474f41f52)(typeName, true);
449
+ return (0, $k7rGe$sanity.defineField)({
450
+ name: objectName,
451
+ title: `Internationalized array ${type}`,
452
+ type: "object",
453
+ fields: [
454
+ typeof type === `string` ? (0, $k7rGe$sanity.defineField)({
455
+ name: "value",
456
+ type: type
457
+ }) : {
458
+ ...type,
459
+ name: "value"
460
+ },
461
+ ]
462
+ });
463
+ };
464
+
465
+
466
+ const $45e3093e7de8dca2$var$CONFIG_DEFAULT = {
467
+ languages: [],
468
+ fieldTypes: []
469
+ };
470
+ const $45e3093e7de8dca2$export$bec7eb13daf35f0e = (0, $k7rGe$sanity.createPlugin)((config = $45e3093e7de8dca2$var$CONFIG_DEFAULT)=>{
471
+ const { languages: languages , fieldTypes: fieldTypes } = {
472
+ ...$45e3093e7de8dca2$var$CONFIG_DEFAULT,
473
+ ...config
474
+ };
475
+ return {
476
+ name: "sanity-plugin-internationalized-array",
477
+ schema: {
478
+ types: [
479
+ ...fieldTypes.map((type)=>(0, $0437df2e2e149362$export$2e2bcd8739ae039)({
480
+ type: type,
481
+ languages: languages
482
+ })),
483
+ ...fieldTypes.map((type)=>(0, $f727fb16d5d00f8f$export$2e2bcd8739ae039)({
484
+ type: type
485
+ })),
486
+ ]
487
+ }
488
+ };
489
+ });
463
490
 
464
491
 
465
- $parcel$exportWildcard(module.exports, $7594066088192472$exports);
466
492
 
467
493
 
468
494
  //# sourceMappingURL=index.js.map