sanity-plugin-internationalized-array 0.0.7 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +19 -15
- package/lib/cjs/index.js +468 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/esm/index.js +449 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/types/index.d.ts +19 -0
- package/lib/types/index.d.ts.map +1 -0
- package/package.json +47 -34
- package/sanity.json +7 -6
- package/src/components/Feedback.tsx +27 -0
- package/src/components/InternationalizedArrayInput.tsx +220 -0
- package/src/{LanguageArray → components}/Table.tsx +0 -0
- package/src/components/getToneFromValidation.ts +18 -0
- package/src/index.tsx +51 -0
- package/src/internationalizedArray.ts +80 -53
- package/src/types.ts +17 -9
- package/v2-incompatible.js +11 -0
- package/lib/LanguageArray/Table.js +0 -88
- package/lib/LanguageArray/Table.js.map +0 -1
- package/lib/LanguageArray/ValueInput.js +0 -17
- package/lib/LanguageArray/ValueInput.js.map +0 -1
- package/lib/LanguageArray/index.js +0 -253
- package/lib/LanguageArray/index.js.map +0 -1
- package/lib/hooks/useUnsetInputComponent.js +0 -32
- package/lib/hooks/useUnsetInputComponent.js.map +0 -1
- package/lib/index.js +0 -13
- package/lib/index.js.map +0 -1
- package/lib/internationalizedArray.js +0 -105
- package/lib/internationalizedArray.js.map +0 -1
- package/lib/types.js +0 -2
- package/lib/types.js.map +0 -1
- package/migrations/transformObjectToArray.js +0 -94
- package/src/LanguageArray/ValueInput.tsx +0 -6
- package/src/LanguageArray/index.tsx +0 -311
- package/src/hooks/useUnsetInputComponent.tsx +0 -17
- package/src/index.ts +0 -3
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,45 +1,51 @@
|
|
|
1
1
|
# sanity-plugin-internationalized-array
|
|
2
2
|
|
|
3
|
+
> NOTE This is for the Studio v3 version of the plugin
|
|
4
|
+
|
|
3
5
|
A helper function that renders a custom input component for writing localised fields of content into an array.
|
|
4
6
|
|
|
5
7
|
**This an early proof-of-concept and should not yet be used without thorough testing.**
|
|
6
8
|
|
|
7
|
-

|
|
8
10
|
|
|
9
11
|
## Installation
|
|
10
12
|
|
|
11
13
|
```
|
|
12
|
-
|
|
14
|
+
npm install --save sanity-plugin-internationalized-array@studio-v3
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
or
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
yarn add sanity-plugin-internationalized-array-v3@studio-v3
|
|
13
21
|
```
|
|
14
22
|
|
|
15
23
|
Add an array to your schema by importing the helper function.
|
|
16
24
|
|
|
17
25
|
```js
|
|
18
|
-
import {
|
|
26
|
+
import {internationalizedArray} from 'sanity-plugin-internationalized-array'
|
|
19
27
|
|
|
20
28
|
// ./src/schema/person.js
|
|
21
29
|
export default {
|
|
22
|
-
name:
|
|
23
|
-
title:
|
|
24
|
-
type:
|
|
30
|
+
name: 'person',
|
|
31
|
+
title: 'Person',
|
|
32
|
+
type: 'document',
|
|
25
33
|
fields: [
|
|
26
34
|
// ...all your other fields
|
|
27
35
|
internationalizedArray({
|
|
28
36
|
// Required, the `name` of the outer array
|
|
29
|
-
name:
|
|
37
|
+
name: 'greeting',
|
|
30
38
|
// Required, the `type` of the inner field
|
|
31
39
|
// One of: string | text | number | boolean
|
|
32
|
-
type:
|
|
40
|
+
type: 'string',
|
|
33
41
|
// Required, must be an array of objects
|
|
34
42
|
languages: [
|
|
35
|
-
{
|
|
36
|
-
{
|
|
43
|
+
{id: 'en', title: 'English'},
|
|
44
|
+
{id: 'fr', title: 'French'},
|
|
37
45
|
],
|
|
38
|
-
// Optional: just for debugging
|
|
39
|
-
showNativeInput: false,
|
|
40
46
|
}),
|
|
41
47
|
],
|
|
42
|
-
}
|
|
48
|
+
}
|
|
43
49
|
```
|
|
44
50
|
|
|
45
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:
|
|
@@ -105,7 +111,5 @@ greeting[].value
|
|
|
105
111
|
|
|
106
112
|
By using this plugin you can safely extend the number of languages without adding any additional query paths.
|
|
107
113
|
|
|
108
|
-
## License
|
|
109
|
-
|
|
110
114
|
MIT © Simeon Griggs
|
|
111
115
|
See LICENSE
|
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
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
|
+
});
|
|
22
|
+
|
|
23
|
+
return dest;
|
|
24
|
+
}
|
|
25
|
+
function $parcel$export(e, n, v, s) {
|
|
26
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
27
|
+
}
|
|
28
|
+
function $parcel$interopDefault(a) {
|
|
29
|
+
return a && a.__esModule ? a.default : a;
|
|
30
|
+
}
|
|
31
|
+
var $7594066088192472$exports = {};
|
|
32
|
+
|
|
33
|
+
$parcel$export($7594066088192472$exports, "internationalizedArray", () => $7594066088192472$export$bec7eb13daf35f0e);
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
// Wrappers required because of bug with passing down "as" prop
|
|
44
|
+
// https://github.com/styled-components/styled-components/issues/2449
|
|
45
|
+
// Table
|
|
46
|
+
const $acfbfc6ee2363807$var$TableWrapper = (props = {})=>{
|
|
47
|
+
return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Box), {
|
|
48
|
+
as: "table",
|
|
49
|
+
...props
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
const $acfbfc6ee2363807$var$StyledTable = (0, ($parcel$interopDefault($dyHF6$styledcomponents)))($acfbfc6ee2363807$var$TableWrapper)(()=>(0, $dyHF6$styledcomponents.css)`
|
|
53
|
+
display: table;
|
|
54
|
+
width: 100%;
|
|
55
|
+
|
|
56
|
+
&:not([hidden]) {
|
|
57
|
+
display: table;
|
|
58
|
+
}
|
|
59
|
+
`);
|
|
60
|
+
function $acfbfc6ee2363807$export$54ec01a60f47d33d(props) {
|
|
61
|
+
const { children: children , ...rest } = props;
|
|
62
|
+
return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)($acfbfc6ee2363807$var$StyledTable, {
|
|
63
|
+
...rest,
|
|
64
|
+
children: children
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
// Row
|
|
68
|
+
const $acfbfc6ee2363807$var$RowWrapper = (props = {})=>{
|
|
69
|
+
return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Card), {
|
|
70
|
+
as: "tr",
|
|
71
|
+
...props
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
const $acfbfc6ee2363807$var$StyledRow = (0, ($parcel$interopDefault($dyHF6$styledcomponents)))($acfbfc6ee2363807$var$RowWrapper)(()=>(0, $dyHF6$styledcomponents.css)`
|
|
75
|
+
display: table-row;
|
|
76
|
+
|
|
77
|
+
&:not([hidden]) {
|
|
78
|
+
display: table-row;
|
|
79
|
+
}
|
|
80
|
+
`);
|
|
81
|
+
function $acfbfc6ee2363807$export$b05581f4e764e162(props) {
|
|
82
|
+
const { children: children , ...rest } = props;
|
|
83
|
+
return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)($acfbfc6ee2363807$var$StyledRow, {
|
|
84
|
+
...rest,
|
|
85
|
+
children: children
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
// Cell
|
|
89
|
+
const $acfbfc6ee2363807$var$CellWrapper = (props = {})=>{
|
|
90
|
+
return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Box), {
|
|
91
|
+
as: "td",
|
|
92
|
+
...props
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
const $acfbfc6ee2363807$var$StyledCell = (0, ($parcel$interopDefault($dyHF6$styledcomponents)))($acfbfc6ee2363807$var$CellWrapper)(()=>(0, $dyHF6$styledcomponents.css)`
|
|
96
|
+
display: table-cell;
|
|
97
|
+
|
|
98
|
+
&:not([hidden]) {
|
|
99
|
+
display: table-cell;
|
|
100
|
+
}
|
|
101
|
+
`);
|
|
102
|
+
function $acfbfc6ee2363807$export$1e4baea7053fc0e3(props) {
|
|
103
|
+
const { children: children , ...rest } = props;
|
|
104
|
+
return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)($acfbfc6ee2363807$var$StyledCell, {
|
|
105
|
+
...rest,
|
|
106
|
+
children: children
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
const $aaf4d804384afd00$var$schemaExample = {
|
|
116
|
+
languages: [
|
|
117
|
+
{
|
|
118
|
+
id: "en",
|
|
119
|
+
title: "English"
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
id: "no",
|
|
123
|
+
title: "Norsk"
|
|
124
|
+
},
|
|
125
|
+
]
|
|
126
|
+
};
|
|
127
|
+
function $aaf4d804384afd00$export$2e2bcd8739ae039() {
|
|
128
|
+
return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Card), {
|
|
129
|
+
tone: "caution",
|
|
130
|
+
border: true,
|
|
131
|
+
radius: 2,
|
|
132
|
+
padding: 3,
|
|
133
|
+
children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Stack), {
|
|
134
|
+
space: 4,
|
|
135
|
+
children: [
|
|
136
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Text), {
|
|
137
|
+
children: [
|
|
138
|
+
"An array of language objects must be passed into the ",
|
|
139
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)("code", {
|
|
140
|
+
children: "internationalizedArray"
|
|
141
|
+
}),
|
|
142
|
+
" ",
|
|
143
|
+
"helper function, each with an ",
|
|
144
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)("code", {
|
|
145
|
+
children: "id"
|
|
146
|
+
}),
|
|
147
|
+
" and ",
|
|
148
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)("code", {
|
|
149
|
+
children: "title"
|
|
150
|
+
}),
|
|
151
|
+
" field. Example:"
|
|
152
|
+
]
|
|
153
|
+
}),
|
|
154
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Card), {
|
|
155
|
+
padding: 2,
|
|
156
|
+
border: true,
|
|
157
|
+
radius: 2,
|
|
158
|
+
children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Code), {
|
|
159
|
+
size: 1,
|
|
160
|
+
language: "javascript",
|
|
161
|
+
children: JSON.stringify($aaf4d804384afd00$var$schemaExample, null, 2)
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
]
|
|
165
|
+
})
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
function $6a9b28384eb7074d$export$16423358d6ebe294(validations) {
|
|
171
|
+
if (!validations.length) return `default`;
|
|
172
|
+
const validationLevels = validations.map((v)=>v.level);
|
|
173
|
+
if (validationLevels.includes("error")) return `critical`;
|
|
174
|
+
else if (validationLevels.includes("warning")) return `caution`;
|
|
175
|
+
return `default`;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
function $7e790a73eaf2b445$export$2e2bcd8739ae039(props) {
|
|
180
|
+
const { members: members , value: value , schemaType: schemaType , onChange: onChange } = props;
|
|
181
|
+
const readOnly = typeof schemaType.readOnly === "boolean" ? schemaType.readOnly : false;
|
|
182
|
+
const { options: options } = schemaType;
|
|
183
|
+
const languages = (0, $dyHF6$react.useMemo)(()=>options?.languages ?? [], [
|
|
184
|
+
options
|
|
185
|
+
]);
|
|
186
|
+
const handleAddLanguage = (0, $dyHF6$react.useCallback)((languageId)=>{
|
|
187
|
+
// Create new items
|
|
188
|
+
const newItems = languageId ? [
|
|
189
|
+
{
|
|
190
|
+
_key: languageId
|
|
191
|
+
}
|
|
192
|
+
] : languages.filter((language)=>value?.length ? !value.find((v)=>v._key === language.id) : true).map((language)=>({
|
|
193
|
+
_key: language.id
|
|
194
|
+
}));
|
|
195
|
+
// Insert new items in the correct order
|
|
196
|
+
const languagesInUse = value?.length ? value.map((v)=>v) : [];
|
|
197
|
+
const insertions = newItems.map((item)=>{
|
|
198
|
+
// What's the original index of this language?
|
|
199
|
+
const languageIndex = languages.findIndex((l)=>item._key === l.id);
|
|
200
|
+
// What languages are there beyond that index?
|
|
201
|
+
const remainingLanguages = languages.slice(languageIndex + 1);
|
|
202
|
+
// 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));
|
|
204
|
+
// Keep local state up to date incase multiple insertions are being made
|
|
205
|
+
if (nextLanguageIndex < 0) languagesInUse.push(item);
|
|
206
|
+
else languagesInUse.splice(nextLanguageIndex, 0, item);
|
|
207
|
+
return nextLanguageIndex < 0 ? (0, $dyHF6$sanityform.insert)([
|
|
208
|
+
item
|
|
209
|
+
], "after", [
|
|
210
|
+
nextLanguageIndex
|
|
211
|
+
]) : (0, $dyHF6$sanityform.insert)([
|
|
212
|
+
item
|
|
213
|
+
], "before", [
|
|
214
|
+
nextLanguageIndex
|
|
215
|
+
]);
|
|
216
|
+
});
|
|
217
|
+
onChange([
|
|
218
|
+
(0, $dyHF6$sanityform.setIfMissing)([]),
|
|
219
|
+
...insertions
|
|
220
|
+
]);
|
|
221
|
+
}, [
|
|
222
|
+
languages,
|
|
223
|
+
onChange,
|
|
224
|
+
value
|
|
225
|
+
]);
|
|
226
|
+
const handleUnsetByKey = (0, $dyHF6$react.useCallback)((_key)=>{
|
|
227
|
+
onChange((0, $dyHF6$sanityform.unset)([
|
|
228
|
+
{
|
|
229
|
+
_key: _key
|
|
230
|
+
}
|
|
231
|
+
]));
|
|
232
|
+
}, [
|
|
233
|
+
onChange
|
|
234
|
+
]);
|
|
235
|
+
// TODO: This is lazy, reordering and re-setting the whole array – it could be surgical
|
|
236
|
+
const handleRestoreOrder = (0, $dyHF6$react.useCallback)(()=>{
|
|
237
|
+
if (!value?.length) return;
|
|
238
|
+
// Create a new value array in the correct order
|
|
239
|
+
// This would also strip out values that don't have a language as the key
|
|
240
|
+
const updatedValue = value.reduce((acc, v)=>{
|
|
241
|
+
const newIndex = languages.findIndex((l)=>l.id === v?._key);
|
|
242
|
+
if (newIndex) acc[newIndex] = v;
|
|
243
|
+
return acc;
|
|
244
|
+
}, []).filter(Boolean);
|
|
245
|
+
onChange((0, $dyHF6$sanityform.set)(updatedValue));
|
|
246
|
+
}, [
|
|
247
|
+
languages,
|
|
248
|
+
onChange,
|
|
249
|
+
value
|
|
250
|
+
]);
|
|
251
|
+
const allKeysAreLanguages = (0, $dyHF6$react.useMemo)(()=>{
|
|
252
|
+
return value?.every((v)=>languages.find((l)=>l?.id === v?._key));
|
|
253
|
+
}, [
|
|
254
|
+
value,
|
|
255
|
+
languages
|
|
256
|
+
]);
|
|
257
|
+
// Check languages are in the correct order
|
|
258
|
+
const languagesOutOfOrder = (0, $dyHF6$react.useMemo)(()=>{
|
|
259
|
+
if (!value?.length) return [];
|
|
260
|
+
const languagesInUse = languages.filter((l)=>value.find((v)=>v._key === l.id));
|
|
261
|
+
return value.map((v, vIndex)=>vIndex === languagesInUse.findIndex((l)=>l.id === v._key) ? null : v).filter(Boolean);
|
|
262
|
+
}, [
|
|
263
|
+
value,
|
|
264
|
+
languages
|
|
265
|
+
]);
|
|
266
|
+
const languagesAreValid = (0, $dyHF6$react.useMemo)(()=>languages?.length && languages.every((item)=>item.id && item.title), [
|
|
267
|
+
languages
|
|
268
|
+
]);
|
|
269
|
+
if (!languagesAreValid) return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $aaf4d804384afd00$export$2e2bcd8739ae039), {});
|
|
270
|
+
return /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Stack), {
|
|
271
|
+
space: 2,
|
|
272
|
+
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), {
|
|
276
|
+
tone: member?.item?.validation?.length > 0 ? (0, $6a9b28384eb7074d$export$16423358d6ebe294)(member.item.validation) : undefined,
|
|
277
|
+
children: [
|
|
278
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$1e4baea7053fc0e3), {
|
|
279
|
+
style: {
|
|
280
|
+
verticalAlign: "bottom"
|
|
281
|
+
},
|
|
282
|
+
children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Box), {
|
|
283
|
+
paddingY: 3,
|
|
284
|
+
paddingRight: 2,
|
|
285
|
+
children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Label), {
|
|
286
|
+
muted: true,
|
|
287
|
+
size: 1,
|
|
288
|
+
children: member.key
|
|
289
|
+
})
|
|
290
|
+
})
|
|
291
|
+
}),
|
|
292
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$1e4baea7053fc0e3), {
|
|
293
|
+
paddingRight: 2,
|
|
294
|
+
style: {
|
|
295
|
+
width: `100%`
|
|
296
|
+
},
|
|
297
|
+
children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityform.MemberItem), {
|
|
298
|
+
...props,
|
|
299
|
+
member: member
|
|
300
|
+
})
|
|
301
|
+
}),
|
|
302
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $acfbfc6ee2363807$export$1e4baea7053fc0e3), {
|
|
303
|
+
style: {
|
|
304
|
+
verticalAlign: "bottom"
|
|
305
|
+
},
|
|
306
|
+
children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Flex), {
|
|
307
|
+
align: "center",
|
|
308
|
+
justify: "flex-end",
|
|
309
|
+
gap: 3,
|
|
310
|
+
children: [
|
|
311
|
+
member.item.validation.length > 0 ? /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Box), {
|
|
312
|
+
paddingLeft: 2,
|
|
313
|
+
children: /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityform.FormFieldValidationStatus), {
|
|
314
|
+
validation: member.item.validation
|
|
315
|
+
})
|
|
316
|
+
}) : null,
|
|
317
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Button), {
|
|
318
|
+
mode: "ghost",
|
|
319
|
+
icon: (0, $dyHF6$sanityicons.RemoveIcon),
|
|
320
|
+
tone: "critical",
|
|
321
|
+
disabled: typeof readOnly === "boolean" ? readOnly : false,
|
|
322
|
+
onClick: ()=>handleUnsetByKey(member.key)
|
|
323
|
+
})
|
|
324
|
+
]
|
|
325
|
+
})
|
|
326
|
+
})
|
|
327
|
+
]
|
|
328
|
+
}, member.key))
|
|
329
|
+
})
|
|
330
|
+
}) : null,
|
|
331
|
+
languagesOutOfOrder.length > 0 && allKeysAreLanguages ? /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Button), {
|
|
332
|
+
tone: "caution",
|
|
333
|
+
icon: (0, $dyHF6$sanityicons.RestoreIcon),
|
|
334
|
+
onClick: ()=>handleRestoreOrder(),
|
|
335
|
+
text: "Restore order of languages"
|
|
336
|
+
}) : null,
|
|
337
|
+
value && value.length < languages.length ? /*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsxs)((0, $dyHF6$sanityui.Stack), {
|
|
338
|
+
space: 2,
|
|
339
|
+
children: [
|
|
340
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Grid), {
|
|
341
|
+
columns: Math.min(languages.length, 5),
|
|
342
|
+
gap: 2,
|
|
343
|
+
children: languages.map((language)=>/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Button), {
|
|
344
|
+
tone: "primary",
|
|
345
|
+
mode: "ghost",
|
|
346
|
+
fontSize: 1,
|
|
347
|
+
disabled: readOnly || Boolean(value?.find((item)=>item._key === language.id)),
|
|
348
|
+
text: language.id.toUpperCase(),
|
|
349
|
+
icon: (0, $dyHF6$sanityicons.AddIcon),
|
|
350
|
+
onClick: ()=>handleAddLanguage(language.id)
|
|
351
|
+
}, language.id))
|
|
352
|
+
}),
|
|
353
|
+
/*#__PURE__*/ (0, $dyHF6$reactjsxruntime.jsx)((0, $dyHF6$sanityui.Button), {
|
|
354
|
+
tone: "primary",
|
|
355
|
+
mode: "ghost",
|
|
356
|
+
disabled: readOnly || value && value?.length >= languages?.length,
|
|
357
|
+
icon: (0, $dyHF6$sanityicons.AddIcon),
|
|
358
|
+
text: value?.length ? `Add missing languages` : `Add all languages`,
|
|
359
|
+
onClick: ()=>handleAddLanguage()
|
|
360
|
+
})
|
|
361
|
+
]
|
|
362
|
+
}) : null
|
|
363
|
+
]
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
|
|
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,
|
|
384
|
+
type: "array",
|
|
385
|
+
components: {
|
|
386
|
+
input: (0, $7e790a73eaf2b445$export$2e2bcd8739ae039)
|
|
387
|
+
},
|
|
388
|
+
options: {
|
|
389
|
+
languages: languages
|
|
390
|
+
},
|
|
391
|
+
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
|
+
},
|
|
414
|
+
],
|
|
415
|
+
// @ts-ignore
|
|
416
|
+
validation: (rule)=>{
|
|
417
|
+
const rules = [];
|
|
418
|
+
rules.push(rule.custom((value, context)=>{
|
|
419
|
+
const { languages: contextLanguages } = context?.type?.options ?? {};
|
|
420
|
+
const nonLanguageKeys = value?.length ? value.filter((item)=>!contextLanguages.find((language)=>item._key === language.id)) : [];
|
|
421
|
+
if (nonLanguageKeys.length) return {
|
|
422
|
+
message: `Array item keys must be valid languages registered to the field type`,
|
|
423
|
+
paths: nonLanguageKeys.map((item)=>[
|
|
424
|
+
{
|
|
425
|
+
_key: item._key
|
|
426
|
+
}
|
|
427
|
+
])
|
|
428
|
+
};
|
|
429
|
+
const valuesByLanguage = value?.length ? value.filter((item)=>Boolean(item?._key)).reduce((acc, cur)=>{
|
|
430
|
+
if (acc[cur._key]) return {
|
|
431
|
+
...acc,
|
|
432
|
+
[cur._key]: [
|
|
433
|
+
...acc[cur._key],
|
|
434
|
+
cur
|
|
435
|
+
]
|
|
436
|
+
};
|
|
437
|
+
return {
|
|
438
|
+
...acc,
|
|
439
|
+
[cur._key]: [
|
|
440
|
+
cur
|
|
441
|
+
]
|
|
442
|
+
};
|
|
443
|
+
}, {}) : {};
|
|
444
|
+
const duplicateValues = Object.values(valuesByLanguage).filter((item)=>item?.length > 1).flat();
|
|
445
|
+
if (duplicateValues.length) return {
|
|
446
|
+
message: "There can only be one field per language",
|
|
447
|
+
paths: duplicateValues.map((item)=>[
|
|
448
|
+
{
|
|
449
|
+
_key: item._key
|
|
450
|
+
}
|
|
451
|
+
])
|
|
452
|
+
};
|
|
453
|
+
return true;
|
|
454
|
+
}));
|
|
455
|
+
if (languages?.length) rules.push(rule.max(languages.length));
|
|
456
|
+
return [
|
|
457
|
+
...rules,
|
|
458
|
+
...configValidation
|
|
459
|
+
].filter(Boolean);
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
$parcel$exportWildcard(module.exports, $7594066088192472$exports);
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACAA;ACAA;;;;ACAA;;;;AAIA,+DAA+D;AAC/D,qEAAqE;AAErE,QAAQ;AACR,MAAM,kCAAY,GAAG,CAAC,KAAK,GAAG,EAAE,GAAK;IACnC,qBAAO,gCAAC,CAAA,GAAA,mBAAG,CAAA;QAAC,EAAE,EAAC,OAAO;QAAE,GAAG,KAAK;MAAI,CAAA;CACrC;AAED,MAAM,iCAAW,GAAG,CAAA,GAAA,iDAAM,CAAA,CAAC,kCAAY,CAAC,CACtC,IACE,CAAA,GAAA,2BAAG,CAAA,CAAC;;;;;;;IAOJ,CAAC,CACJ;AAOM,SAAS,yCAAK,CAAC,KAAiB,EAAE;IACvC,MAAM,YAAC,QAAQ,CAAA,EAAE,GAAG,IAAI,EAAC,GAAG,KAAK;IAEjC,qBAAO,gCAAC,iCAAW;QAAE,GAAG,IAAI;kBAAG,QAAQ;MAAe,CAAA;CACvD;AAED,MAAM;AACN,MAAM,gCAAU,GAAG,CAAC,KAAK,GAAG,EAAE,GAAK;IACjC,qBAAO,gCAAC,CAAA,GAAA,oBAAI,CAAA;QAAC,EAAE,EAAC,IAAI;QAAE,GAAG,KAAK;MAAI,CAAA;CACnC;AAED,MAAM,+BAAS,GAAG,CAAA,GAAA,iDAAM,CAAA,CAAC,gCAAU,CAAC,CAClC,IACE,CAAA,GAAA,2BAAG,CAAA,CAAC;;;;;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AAK;mBAAK,gBAAgB;aAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;SACvD;KACF,CAAC,CAAA;CACH;;AD9GD","sources":["src/index.tsx","src/internationalizedArray.ts","src/components/InternationalizedArrayInput.tsx","src/components/Table.tsx","src/components/Feedback.tsx","src/components/getToneFromValidation.ts"],"sourcesContent":["export * from './internationalizedArray'\n\n/**\n * Because of the complexity of the field, there's no utility currently to using the plugin framework\n * We need to register:\n * - an array field which\n * - only has a single object field with\n * - a single inner field and\n * - an array of languages\n * ...this is easier with a helper function\n */\n\n// import React from 'react'\n// import {createPlugin} from 'sanity'\n\n// import InternationalizedArrayInput from './components/InternationalizedArrayInput'\n// import {PluginConfig} from './types'\n\n// const CONFIG_DEFAULT = {languages: []}\n\n// export const internationalizedArray = createPlugin<PluginConfig>((config = CONFIG_DEFAULT) => {\n// return {\n// name: 'sanity-plugin-internationalized-array',\n// form: {\n// renderInput: (inputProps: unknown, next: unknown) => {\n// if (\n// config.languages.length &&\n// inputProps?.schemaType?.jsonType === 'array' &&\n// inputProps?.schemaType?.options.i18n === true\n// ) {\n// if (inputProps.schemaType.of.length > 1) {\n// return <div>Cannot have more than one field type in the array</div>\n// }\n\n// if (inputProps.schemaType.of[0].jsonType !== 'object') {\n// return <div>Single Field in the Array must be an object</div>\n// }\n\n// if (inputProps.schemaType.of[0].fields[0].name !== 'value') {\n// return <div>Single Field in the Object must be named `value`</div>\n// }\n\n// console.log({inputProps})\n// return <InternationalizedArrayInput inputProps={inputProps} {...config} />\n// }\n\n// return null\n// },\n// },\n// }\n// })\n","import {\n CustomValidatorResult,\n defineField,\n FieldDefinition,\n Rule,\n SchemaType,\n ValidationError,\n} from 'sanity'\n\nimport InternationalizedArrayInput from './components/InternationalizedArrayInput'\nimport {AllowedType, ArrayConfig, Language, Value} from './types'\n\nconst CONFIG_DEFAULT = {name: `title`, type: `string` as AllowedType, languages: []}\n\nexport function internationalizedArray(config: ArrayConfig = CONFIG_DEFAULT): FieldDefinition {\n const {name, type, languages} = config\n\n const configValidation = Array.isArray(config?.validation)\n ? config.validation\n : [config?.validation]\n\n return defineField({\n name,\n title: config?.title ?? undefined,\n group: config?.group ?? undefined,\n hidden: config?.hidden ?? undefined,\n readOnly: config?.readOnly ?? undefined,\n type: 'array',\n components: {input: InternationalizedArrayInput},\n options: {languages},\n of: [\n {\n type: 'object',\n fields: [\n {\n name: 'value',\n type,\n },\n ],\n preview: {\n select: {title: 'value', key: '_key'},\n prepare(select) {\n const {title, key} = select as Record<string, string>\n\n return {\n title,\n subtitle: key.toUpperCase(),\n }\n },\n },\n },\n ],\n // @ts-ignore\n validation: (rule: Rule) => {\n const rules = [] as Rule[]\n\n rules.push(\n rule.custom<Value[]>((value, context) => {\n const {languages: contextLanguages}: {languages: Language[]} =\n context?.type?.options ?? {}\n const nonLanguageKeys = value?.length\n ? value.filter(\n (item) => !contextLanguages.find((language) => item._key === language.id)\n )\n : []\n if (nonLanguageKeys.length) {\n return {\n message: `Array item keys must be valid languages registered to the field type`,\n paths: nonLanguageKeys.map((item) => [{_key: item._key}]),\n }\n }\n\n // Ensure there's no duplicate `language` fields\n type KeyedValues = {\n [key: string]: Value[]\n }\n\n const valuesByLanguage = value?.length\n ? value\n .filter((item) => Boolean(item?._key))\n .reduce((acc, cur) => {\n if (acc[cur._key]) {\n return {...acc, [cur._key]: [...acc[cur._key], cur]}\n }\n return {\n ...acc,\n [cur._key]: [cur],\n }\n }, {} as KeyedValues)\n : {}\n const duplicateValues = Object.values(valuesByLanguage)\n .filter((item) => item?.length > 1)\n .flat()\n if (duplicateValues.length) {\n return {\n message: 'There can only be one field per language',\n paths: duplicateValues.map((item) => [{_key: item._key}]),\n }\n }\n return true\n })\n )\n\n if (languages?.length) {\n rules.push(rule.max(languages.length))\n }\n\n return [...rules, ...configValidation].filter(Boolean)\n },\n })\n}\n","import React, {useCallback, useMemo} from 'react'\nimport {\n PatchEvent,\n ArrayOfObjectsInputProps,\n MemberItem,\n unset,\n insert,\n set,\n setIfMissing,\n FormFieldValidationStatus,\n} from 'sanity/form'\nimport {Box, Button, Flex, Grid, Label, Stack} from '@sanity/ui'\n\nimport {Language, Value, ArraySchemaWithLanguageOptions} from '../types'\nimport {Table, TableCell, TableRow} from './Table'\nimport {AddIcon, RemoveIcon, RestoreIcon} from '@sanity/icons'\nimport Feedback from './Feedback'\nimport {getToneFromValidation} from './getToneFromValidation'\n\nexport type InternationalizedArrayInputProps = ArrayOfObjectsInputProps<\n Value,\n ArraySchemaWithLanguageOptions\n>\n\nexport default function InternationalizedArrayInput(props: InternationalizedArrayInputProps) {\n const {members, value, schemaType, onChange} = props\n const readOnly = typeof schemaType.readOnly === 'boolean' ? schemaType.readOnly : false\n const {options} = schemaType\n\n const languages: Language[] = useMemo(() => options?.languages ?? [], [options])\n\n const handleAddLanguage = useCallback(\n (languageId?: string) => {\n // Create new items\n const newItems = languageId\n ? // Just one for this language\n [{_key: languageId}]\n : // Or one for every missing language\n languages\n .filter((language) =>\n value?.length ? !value.find((v) => v._key === language.id) : true\n )\n .map((language) => ({_key: language.id}))\n\n // Insert new items in the correct order\n const languagesInUse = value?.length ? value.map((v) => v) : []\n\n const insertions = newItems.map((item) => {\n // What's the original index of this language?\n const languageIndex = languages.findIndex((l) => item._key === l.id)\n\n // What languages are there beyond that index?\n const remainingLanguages = languages.slice(languageIndex + 1)\n\n // So what is the index in the current value array of the next language in the language array?\n const nextLanguageIndex = languagesInUse.findIndex((l) =>\n remainingLanguages.find((r) => r.id === l._key)\n )\n\n // Keep local state up to date incase multiple insertions are being made\n if (nextLanguageIndex < 0) {\n languagesInUse.push(item)\n } else {\n languagesInUse.splice(nextLanguageIndex, 0, item)\n }\n\n return nextLanguageIndex < 0\n ? // No next language (-1), add to end of array\n insert([item], 'after', [nextLanguageIndex])\n : // Next language found, insert before that\n insert([item], 'before', [nextLanguageIndex])\n })\n\n onChange([setIfMissing([]), ...insertions])\n },\n [languages, onChange, value]\n )\n\n const handleUnsetByKey = useCallback(\n (_key) => {\n onChange(unset([{_key}]))\n },\n [onChange]\n )\n\n // TODO: This is lazy, reordering and re-setting the whole array – it could be surgical\n const handleRestoreOrder = useCallback(() => {\n if (!value?.length) {\n return\n }\n\n // Create a new value array in the correct order\n // This would also strip out values that don't have a language as the key\n const updatedValue = value\n .reduce((acc, v) => {\n const newIndex = languages.findIndex((l) => l.id === v?._key)\n\n if (newIndex) {\n acc[newIndex] = v\n }\n\n return acc\n }, [] as Value[])\n .filter(Boolean)\n\n onChange(set(updatedValue))\n }, [languages, onChange, value])\n\n const allKeysAreLanguages = useMemo(() => {\n return value?.every((v) => languages.find((l) => l?.id === v?._key))\n }, [value, languages])\n\n // Check languages are in the correct order\n const languagesOutOfOrder = useMemo(() => {\n if (!value?.length) {\n return []\n }\n\n const languagesInUse = languages.filter((l) => value.find((v) => v._key === l.id))\n\n return value\n .map((v, vIndex) => (vIndex === languagesInUse.findIndex((l) => l.id === v._key) ? null : v))\n .filter(Boolean)\n }, [value, languages])\n\n const languagesAreValid = useMemo(\n () => languages?.length && languages.every((item) => item.id && item.title),\n [languages]\n )\n\n if (!languagesAreValid) {\n return <Feedback />\n }\n\n return (\n <Stack space={2}>\n {members?.length > 0 ? (\n <Table>\n <tbody>\n {members.map((member) => (\n <TableRow\n key={member.key}\n tone={\n member?.item?.validation?.length > 0\n ? getToneFromValidation(member.item.validation)\n : undefined\n }\n >\n <TableCell style={{verticalAlign: 'bottom'}}>\n <Box paddingY={3} paddingRight={2}>\n <Label muted size={1}>\n {member.key}\n </Label>\n </Box>\n </TableCell>\n <TableCell paddingRight={2} style={{width: `100%`}}>\n <MemberItem {...props} member={member} />\n </TableCell>\n <TableCell style={{verticalAlign: 'bottom'}}>\n <Flex align=\"center\" justify=\"flex-end\" gap={3}>\n {/* Possibly unncessary, validation shows up in <MemberItem /> */}\n {member.item.validation.length > 0 ? (\n <Box paddingLeft={2}>\n <FormFieldValidationStatus validation={member.item.validation} />\n </Box>\n ) : null}\n <Button\n mode=\"ghost\"\n icon={RemoveIcon}\n tone=\"critical\"\n disabled={typeof readOnly === 'boolean' ? readOnly : false}\n onClick={() => handleUnsetByKey(member.key)}\n />\n </Flex>\n </TableCell>\n </TableRow>\n ))}\n </tbody>\n </Table>\n ) : null}\n\n {languagesOutOfOrder.length > 0 && allKeysAreLanguages ? (\n <Button\n tone=\"caution\"\n icon={RestoreIcon}\n onClick={() => handleRestoreOrder()}\n text=\"Restore order of languages\"\n />\n ) : null}\n\n {value && value.length < languages.length ? (\n <Stack space={2}>\n {/* No more than 5 columns */}\n <Grid columns={Math.min(languages.length, 5)} gap={2}>\n {languages.map((language) => (\n <Button\n key={language.id}\n tone=\"primary\"\n mode=\"ghost\"\n fontSize={1}\n disabled={readOnly || Boolean(value?.find((item) => item._key === language.id))}\n text={language.id.toUpperCase()}\n icon={AddIcon}\n onClick={() => handleAddLanguage(language.id)}\n />\n ))}\n </Grid>\n <Button\n tone=\"primary\"\n mode=\"ghost\"\n disabled={readOnly || (value && value?.length >= languages?.length)}\n icon={AddIcon}\n text={value?.length ? `Add missing languages` : `Add all languages`}\n onClick={() => handleAddLanguage()}\n />\n </Stack>\n ) : null}\n </Stack>\n )\n}\n","import React from 'react'\nimport styled, {css} from 'styled-components'\nimport {Box, BoxProps, Card, CardProps} from '@sanity/ui'\n\n// Wrappers required because of bug with passing down \"as\" prop\n// https://github.com/styled-components/styled-components/issues/2449\n\n// Table\nconst TableWrapper = (props = {}) => {\n return <Box as=\"table\" {...props} />\n}\n\nconst StyledTable = styled(TableWrapper)(\n () =>\n css`\n display: table;\n width: 100%;\n\n &:not([hidden]) {\n display: table;\n }\n `\n)\n\ntype TableProps = BoxProps & {\n children: React.ReactNode\n style?: React.CSSProperties\n}\n\nexport function Table(props: TableProps) {\n const {children, ...rest} = props\n\n return <StyledTable {...rest}>{children}</StyledTable>\n}\n\n// Row\nconst RowWrapper = (props = {}) => {\n return <Card as=\"tr\" {...props} />\n}\n\nconst StyledRow = styled(RowWrapper)(\n () =>\n css`\n display: table-row;\n\n &:not([hidden]) {\n display: table-row;\n }\n `\n)\n\ntype TableRowProps = CardProps & {\n children: React.ReactNode\n style?: React.CSSProperties\n}\n\nexport function TableRow(props: TableRowProps) {\n const {children, ...rest} = props\n\n return <StyledRow {...rest}>{children}</StyledRow>\n}\n\n// Cell\nconst CellWrapper = (props = {}) => {\n return <Box as=\"td\" {...props} />\n}\n\nconst StyledCell = styled(CellWrapper)(\n () =>\n css`\n display: table-cell;\n\n &:not([hidden]) {\n display: table-cell;\n }\n `\n)\n\ntype TableCellProps = BoxProps & {\n children: React.ReactNode\n style?: React.CSSProperties\n}\n\nexport function TableCell(props: TableCellProps) {\n const {children, ...rest} = props\n\n return <StyledCell {...rest}>{children}</StyledCell>\n}\n","import {Text, Card, Stack, Code} from '@sanity/ui'\nimport React from 'react'\n\nconst schemaExample = {\n languages: [\n {id: 'en', title: 'English'},\n {id: 'no', title: 'Norsk'},\n ],\n}\n\nexport default function Feedback() {\n return (\n <Card tone=\"caution\" border radius={2} padding={3}>\n <Stack space={4}>\n <Text>\n An array of language objects must be passed into the <code>internationalizedArray</code>{' '}\n helper function, each with an <code>id</code> and <code>title</code> field. Example:\n </Text>\n <Card padding={2} border radius={2}>\n <Code size={1} language=\"javascript\">\n {JSON.stringify(schemaExample, null, 2)}\n </Code>\n </Card>\n </Stack>\n </Card>\n )\n}\n","import {NodeValidation} from 'sanity/form'\nimport {CardTone} from '@sanity/ui'\n\nexport function getToneFromValidation(validations: NodeValidation[]): CardTone {\n if (!validations.length) {\n return `default`\n }\n\n const validationLevels = validations.map((v) => v.level)\n\n if (validationLevels.includes('error')) {\n return `critical`\n } else if (validationLevels.includes('warning')) {\n return `caution`\n }\n\n return `default`\n}\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../"}
|