sanity-plugin-internationalized-array 1.1.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -9
- package/lib/index.esm.js +2 -0
- package/lib/index.esm.js.map +1 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -0
- package/lib/src/index.d.ts +16 -0
- package/package.json +56 -42
- package/src/components/{InternationalizedArrayInput.tsx → InternationalizedArray.tsx} +41 -125
- package/src/components/InternationalizedInput.tsx +108 -0
- package/src/components/getToneFromValidation.ts +5 -5
- package/src/plugin.tsx +2 -2
- package/src/schema/array.ts +16 -7
- package/src/schema/object.ts +18 -3
- package/src/types.ts +2 -2
- package/lib/cjs/index.js +0 -542
- package/lib/cjs/index.js.map +0 -1
- package/lib/esm/index.js +0 -535
- package/lib/esm/index.js.map +0 -1
- package/lib/types/index.d.ts +0 -12
- package/lib/types/index.d.ts.map +0 -1
package/lib/esm/index.js
DELETED
|
@@ -1,535 +0,0 @@
|
|
|
1
|
-
import {createPlugin as $gGrEF$createPlugin, defineField as $gGrEF$defineField} from "sanity";
|
|
2
|
-
import {jsx as $gGrEF$jsx, jsxs as $gGrEF$jsxs} from "react/jsx-runtime";
|
|
3
|
-
import {useMemo as $gGrEF$useMemo, useCallback as $gGrEF$useCallback} from "react";
|
|
4
|
-
import {insert as $gGrEF$insert, setIfMissing as $gGrEF$setIfMissing, unset as $gGrEF$unset, set as $gGrEF$set, ArrayOfObjectsItem as $gGrEF$ArrayOfObjectsItem} from "sanity/form";
|
|
5
|
-
import {useToast as $gGrEF$useToast, Stack as $gGrEF$Stack, Box as $gGrEF$Box, Label as $gGrEF$Label, MenuButton as $gGrEF$MenuButton, Button as $gGrEF$Button, Menu as $gGrEF$Menu, MenuItem as $gGrEF$MenuItem, Flex as $gGrEF$Flex, Text as $gGrEF$Text, Grid as $gGrEF$Grid, Card as $gGrEF$Card, Code as $gGrEF$Code} from "@sanity/ui";
|
|
6
|
-
import {RemoveIcon as $gGrEF$RemoveIcon, RestoreIcon as $gGrEF$RestoreIcon, AddIcon as $gGrEF$AddIcon} from "@sanity/icons";
|
|
7
|
-
import $gGrEF$styledcomponents, {css as $gGrEF$css} from "styled-components";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
function $5c51df8bc000fd76$export$8a7688a96d852767(string) {
|
|
12
|
-
return string.replace(/-([a-z])/g, (g)=>g[1].toUpperCase());
|
|
13
|
-
}
|
|
14
|
-
function $5c51df8bc000fd76$export$348afa8c9ff47183(string) {
|
|
15
|
-
return string.split(` `).map((word)=>word.charAt(0).toUpperCase() + word.slice(1)).join(` `);
|
|
16
|
-
}
|
|
17
|
-
function $5c51df8bc000fd76$export$26c6f48841fe1a8a(string) {
|
|
18
|
-
return $5c51df8bc000fd76$export$348afa8c9ff47183($5c51df8bc000fd76$export$8a7688a96d852767(string));
|
|
19
|
-
}
|
|
20
|
-
function $5c51df8bc000fd76$export$ab1ce2a474f41f52(name, addValue = false) {
|
|
21
|
-
return addValue ? [
|
|
22
|
-
`internationalizedArray`,
|
|
23
|
-
$5c51df8bc000fd76$export$26c6f48841fe1a8a(name),
|
|
24
|
-
`Value`
|
|
25
|
-
].join(``) : [
|
|
26
|
-
`internationalizedArray`,
|
|
27
|
-
$5c51df8bc000fd76$export$26c6f48841fe1a8a(name)
|
|
28
|
-
].join(``);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
// Wrappers required because of bug with passing down "as" prop
|
|
42
|
-
// https://github.com/styled-components/styled-components/issues/2449
|
|
43
|
-
// Table
|
|
44
|
-
const $32ee6db03f46d8dd$var$TableWrapper = (props = {})=>{
|
|
45
|
-
return /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Box), {
|
|
46
|
-
as: "table",
|
|
47
|
-
...props
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
const $32ee6db03f46d8dd$var$StyledTable = (0, $gGrEF$styledcomponents)($32ee6db03f46d8dd$var$TableWrapper)(()=>(0, $gGrEF$css)`
|
|
51
|
-
display: table;
|
|
52
|
-
width: 100%;
|
|
53
|
-
|
|
54
|
-
&:not([hidden]) {
|
|
55
|
-
display: table;
|
|
56
|
-
}
|
|
57
|
-
`);
|
|
58
|
-
function $32ee6db03f46d8dd$export$54ec01a60f47d33d(props) {
|
|
59
|
-
const { children: children , ...rest } = props;
|
|
60
|
-
return /*#__PURE__*/ (0, $gGrEF$jsx)($32ee6db03f46d8dd$var$StyledTable, {
|
|
61
|
-
...rest,
|
|
62
|
-
children: children
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
// Row
|
|
66
|
-
const $32ee6db03f46d8dd$var$RowWrapper = (props = {})=>{
|
|
67
|
-
return /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Card), {
|
|
68
|
-
as: "tr",
|
|
69
|
-
...props
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
const $32ee6db03f46d8dd$var$StyledRow = (0, $gGrEF$styledcomponents)($32ee6db03f46d8dd$var$RowWrapper)(()=>(0, $gGrEF$css)`
|
|
73
|
-
display: table-row;
|
|
74
|
-
|
|
75
|
-
&:not([hidden]) {
|
|
76
|
-
display: table-row;
|
|
77
|
-
}
|
|
78
|
-
`);
|
|
79
|
-
function $32ee6db03f46d8dd$export$b05581f4e764e162(props) {
|
|
80
|
-
const { children: children , ...rest } = props;
|
|
81
|
-
return /*#__PURE__*/ (0, $gGrEF$jsx)($32ee6db03f46d8dd$var$StyledRow, {
|
|
82
|
-
...rest,
|
|
83
|
-
children: children
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
// Cell
|
|
87
|
-
const $32ee6db03f46d8dd$var$CellWrapper = (props = {})=>{
|
|
88
|
-
return /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Box), {
|
|
89
|
-
as: "td",
|
|
90
|
-
...props
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
const $32ee6db03f46d8dd$var$StyledCell = (0, $gGrEF$styledcomponents)($32ee6db03f46d8dd$var$CellWrapper)(()=>(0, $gGrEF$css)`
|
|
94
|
-
display: table-cell;
|
|
95
|
-
|
|
96
|
-
&:not([hidden]) {
|
|
97
|
-
display: table-cell;
|
|
98
|
-
}
|
|
99
|
-
`);
|
|
100
|
-
function $32ee6db03f46d8dd$export$1e4baea7053fc0e3(props) {
|
|
101
|
-
const { children: children , ...rest } = props;
|
|
102
|
-
return /*#__PURE__*/ (0, $gGrEF$jsx)($32ee6db03f46d8dd$var$StyledCell, {
|
|
103
|
-
...rest,
|
|
104
|
-
children: children
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const $e183d660634d2ba0$var$schemaExample = {
|
|
113
|
-
languages: [
|
|
114
|
-
{
|
|
115
|
-
id: "en",
|
|
116
|
-
title: "English"
|
|
117
|
-
},
|
|
118
|
-
{
|
|
119
|
-
id: "no",
|
|
120
|
-
title: "Norsk"
|
|
121
|
-
},
|
|
122
|
-
]
|
|
123
|
-
};
|
|
124
|
-
function $e183d660634d2ba0$export$2e2bcd8739ae039() {
|
|
125
|
-
return /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Card), {
|
|
126
|
-
tone: "caution",
|
|
127
|
-
border: true,
|
|
128
|
-
radius: 2,
|
|
129
|
-
padding: 3,
|
|
130
|
-
children: /*#__PURE__*/ (0, $gGrEF$jsxs)((0, $gGrEF$Stack), {
|
|
131
|
-
space: 4,
|
|
132
|
-
children: [
|
|
133
|
-
/*#__PURE__*/ (0, $gGrEF$jsxs)((0, $gGrEF$Text), {
|
|
134
|
-
children: [
|
|
135
|
-
"An array of language objects must be passed into the ",
|
|
136
|
-
/*#__PURE__*/ (0, $gGrEF$jsx)("code", {
|
|
137
|
-
children: "internationalizedArray"
|
|
138
|
-
}),
|
|
139
|
-
" ",
|
|
140
|
-
"helper function, each with an ",
|
|
141
|
-
/*#__PURE__*/ (0, $gGrEF$jsx)("code", {
|
|
142
|
-
children: "id"
|
|
143
|
-
}),
|
|
144
|
-
" and ",
|
|
145
|
-
/*#__PURE__*/ (0, $gGrEF$jsx)("code", {
|
|
146
|
-
children: "title"
|
|
147
|
-
}),
|
|
148
|
-
" field. Example:"
|
|
149
|
-
]
|
|
150
|
-
}),
|
|
151
|
-
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Card), {
|
|
152
|
-
padding: 2,
|
|
153
|
-
border: true,
|
|
154
|
-
radius: 2,
|
|
155
|
-
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Code), {
|
|
156
|
-
size: 1,
|
|
157
|
-
language: "javascript",
|
|
158
|
-
children: JSON.stringify($e183d660634d2ba0$var$schemaExample, null, 2)
|
|
159
|
-
})
|
|
160
|
-
})
|
|
161
|
-
]
|
|
162
|
-
})
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
function $85e3448f0a0a82fa$export$16423358d6ebe294(validations) {
|
|
168
|
-
if (!validations.length) return `default`;
|
|
169
|
-
const validationLevels = validations.map((v)=>v.level);
|
|
170
|
-
if (validationLevels.includes("error")) return `critical`;
|
|
171
|
-
else if (validationLevels.includes("warning")) return `caution`;
|
|
172
|
-
return `default`;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
function $e3540aa1271cf9b1$export$2e2bcd8739ae039(props) {
|
|
177
|
-
const { members: members , value: value , schemaType: schemaType , onChange: onChange } = props;
|
|
178
|
-
const readOnly = typeof schemaType.readOnly === "boolean" ? schemaType.readOnly : false;
|
|
179
|
-
const { options: options } = schemaType;
|
|
180
|
-
const toast = (0, $gGrEF$useToast)();
|
|
181
|
-
const languages = (0, $gGrEF$useMemo)(()=>options?.languages ?? [], [
|
|
182
|
-
options
|
|
183
|
-
]);
|
|
184
|
-
const valueLanguageKeys = (0, $gGrEF$useMemo)(()=>value?.map((v)=>v._key).filter((key)=>languages.find((l)=>l.id === key)) ?? [], [
|
|
185
|
-
languages,
|
|
186
|
-
value
|
|
187
|
-
]);
|
|
188
|
-
const handleAddLanguage = (0, $gGrEF$useCallback)((languageId)=>{
|
|
189
|
-
// Create new items
|
|
190
|
-
const newItems = languageId ? [
|
|
191
|
-
{
|
|
192
|
-
_key: languageId
|
|
193
|
-
}
|
|
194
|
-
] : languages.filter((language)=>value?.length ? !value.find((v)=>v._key === language.id) : true).map((language)=>({
|
|
195
|
-
_key: language.id
|
|
196
|
-
}));
|
|
197
|
-
// Insert new items in the correct order
|
|
198
|
-
const languagesInUse = value?.length ? value.map((v)=>v) : [];
|
|
199
|
-
const insertions = newItems.map((item)=>{
|
|
200
|
-
// What's the original index of this language?
|
|
201
|
-
const languageIndex = languages.findIndex((l)=>item._key === l.id);
|
|
202
|
-
// What languages are there beyond that index?
|
|
203
|
-
const remainingLanguages = languages.slice(languageIndex + 1);
|
|
204
|
-
// So what is the index in the current value array of the next language in the language array?
|
|
205
|
-
const nextLanguageIndex = languagesInUse.findIndex((l)=>// eslint-disable-next-line max-nested-callbacks
|
|
206
|
-
remainingLanguages.find((r)=>r.id === l._key));
|
|
207
|
-
// Keep local state up to date incase multiple insertions are being made
|
|
208
|
-
if (nextLanguageIndex < 0) languagesInUse.push(item);
|
|
209
|
-
else languagesInUse.splice(nextLanguageIndex, 0, item);
|
|
210
|
-
return nextLanguageIndex < 0 ? (0, $gGrEF$insert)([
|
|
211
|
-
item
|
|
212
|
-
], "after", [
|
|
213
|
-
nextLanguageIndex
|
|
214
|
-
]) : (0, $gGrEF$insert)([
|
|
215
|
-
item
|
|
216
|
-
], "before", [
|
|
217
|
-
nextLanguageIndex
|
|
218
|
-
]);
|
|
219
|
-
});
|
|
220
|
-
onChange([
|
|
221
|
-
(0, $gGrEF$setIfMissing)([]),
|
|
222
|
-
...insertions
|
|
223
|
-
]);
|
|
224
|
-
}, [
|
|
225
|
-
languages,
|
|
226
|
-
onChange,
|
|
227
|
-
value
|
|
228
|
-
]);
|
|
229
|
-
const handleUnsetByKey = (0, $gGrEF$useCallback)((_key)=>{
|
|
230
|
-
onChange((0, $gGrEF$unset)([
|
|
231
|
-
{
|
|
232
|
-
_key: _key
|
|
233
|
-
}
|
|
234
|
-
]));
|
|
235
|
-
}, [
|
|
236
|
-
onChange
|
|
237
|
-
]);
|
|
238
|
-
// TODO: This is lazy, reordering and re-setting the whole array – it could be surgical
|
|
239
|
-
const handleRestoreOrder = (0, $gGrEF$useCallback)(()=>{
|
|
240
|
-
if (!value?.length) return;
|
|
241
|
-
// Create a new value array in the correct order
|
|
242
|
-
// This would also strip out values that don't have a language as the key
|
|
243
|
-
const updatedValue = value.reduce((acc, v)=>{
|
|
244
|
-
const newIndex = languages.findIndex((l)=>l.id === v?._key);
|
|
245
|
-
if (newIndex > -1) acc[newIndex] = v;
|
|
246
|
-
return acc;
|
|
247
|
-
}, []).filter(Boolean);
|
|
248
|
-
if (value.length !== updatedValue.length) toast.push({
|
|
249
|
-
title: "There was an error reordering languages",
|
|
250
|
-
status: "warning"
|
|
251
|
-
});
|
|
252
|
-
onChange((0, $gGrEF$set)(updatedValue));
|
|
253
|
-
}, [
|
|
254
|
-
toast,
|
|
255
|
-
languages,
|
|
256
|
-
onChange,
|
|
257
|
-
value
|
|
258
|
-
]);
|
|
259
|
-
const allKeysAreLanguages = (0, $gGrEF$useMemo)(()=>{
|
|
260
|
-
return value?.every((v)=>languages.find((l)=>l?.id === v?._key));
|
|
261
|
-
}, [
|
|
262
|
-
value,
|
|
263
|
-
languages
|
|
264
|
-
]);
|
|
265
|
-
// Check languages are in the correct order
|
|
266
|
-
const languagesOutOfOrder = (0, $gGrEF$useMemo)(()=>{
|
|
267
|
-
if (!value?.length) return [];
|
|
268
|
-
const languagesInUse = languages.filter((l)=>value.find((v)=>v._key === l.id));
|
|
269
|
-
return value.map((v, vIndex)=>vIndex === languagesInUse.findIndex((l)=>l.id === v._key) ? null : v).filter(Boolean);
|
|
270
|
-
}, [
|
|
271
|
-
value,
|
|
272
|
-
languages
|
|
273
|
-
]);
|
|
274
|
-
const handleKeyChange = (0, $gGrEF$useCallback)((config)=>{
|
|
275
|
-
if (!value) return;
|
|
276
|
-
const { from: from , to: to , index: index } = config;
|
|
277
|
-
const currentValue = value.find((v)=>v._key === from);
|
|
278
|
-
const newValue = {
|
|
279
|
-
...currentValue,
|
|
280
|
-
_key: to
|
|
281
|
-
};
|
|
282
|
-
// TODO: Make sure this gets the correct language index, currently replaces-in-place
|
|
283
|
-
onChange([
|
|
284
|
-
(0, $gGrEF$insert)([
|
|
285
|
-
newValue
|
|
286
|
-
], "after", [
|
|
287
|
-
index
|
|
288
|
-
]),
|
|
289
|
-
(0, $gGrEF$unset)([
|
|
290
|
-
{
|
|
291
|
-
_key: from
|
|
292
|
-
}
|
|
293
|
-
])
|
|
294
|
-
]);
|
|
295
|
-
}, [
|
|
296
|
-
onChange,
|
|
297
|
-
value
|
|
298
|
-
]);
|
|
299
|
-
const languagesAreValid = (0, $gGrEF$useMemo)(()=>!languages?.length || languages?.length && languages.every((item)=>item.id && item.title), [
|
|
300
|
-
languages
|
|
301
|
-
]);
|
|
302
|
-
if (!languagesAreValid) return /*#__PURE__*/ (0, $gGrEF$jsx)((0, $e183d660634d2ba0$export$2e2bcd8739ae039), {});
|
|
303
|
-
return /*#__PURE__*/ (0, $gGrEF$jsxs)((0, $gGrEF$Stack), {
|
|
304
|
-
space: 2,
|
|
305
|
-
children: [
|
|
306
|
-
members?.length > 0 ? /*#__PURE__*/ (0, $gGrEF$jsx)((0, $32ee6db03f46d8dd$export$54ec01a60f47d33d), {
|
|
307
|
-
children: /*#__PURE__*/ (0, $gGrEF$jsx)("tbody", {
|
|
308
|
-
children: members.map((member, memberIndex)=>member.kind === "item" ? /*#__PURE__*/ (0, $gGrEF$jsxs)((0, $32ee6db03f46d8dd$export$b05581f4e764e162), {
|
|
309
|
-
tone: member?.item?.validation?.length > 0 ? (0, $85e3448f0a0a82fa$export$16423358d6ebe294)(member.item.validation) : undefined,
|
|
310
|
-
children: [
|
|
311
|
-
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $32ee6db03f46d8dd$export$1e4baea7053fc0e3), {
|
|
312
|
-
style: {
|
|
313
|
-
verticalAlign: "bottom"
|
|
314
|
-
},
|
|
315
|
-
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Box), {
|
|
316
|
-
paddingY: 3,
|
|
317
|
-
paddingRight: 2,
|
|
318
|
-
children: valueLanguageKeys.includes(member.key) ? /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Label), {
|
|
319
|
-
muted: true,
|
|
320
|
-
size: 1,
|
|
321
|
-
children: member.key
|
|
322
|
-
}) : /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$MenuButton), {
|
|
323
|
-
button: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Button), {
|
|
324
|
-
fontSize: 1,
|
|
325
|
-
text: `Change "${member.key}"`
|
|
326
|
-
}),
|
|
327
|
-
id: `${member.key}-change-key`,
|
|
328
|
-
menu: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Menu), {
|
|
329
|
-
children: languages.map((language)=>/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$MenuItem), {
|
|
330
|
-
disabled: valueLanguageKeys.includes(language.id),
|
|
331
|
-
fontSize: 1,
|
|
332
|
-
text: language.id.toLocaleUpperCase(),
|
|
333
|
-
onClick: ()=>handleKeyChange({
|
|
334
|
-
from: member.key,
|
|
335
|
-
to: language.id,
|
|
336
|
-
index: memberIndex
|
|
337
|
-
})
|
|
338
|
-
}, language.id))
|
|
339
|
-
}),
|
|
340
|
-
placement: "right",
|
|
341
|
-
popover: {
|
|
342
|
-
portal: true
|
|
343
|
-
}
|
|
344
|
-
})
|
|
345
|
-
})
|
|
346
|
-
}),
|
|
347
|
-
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $32ee6db03f46d8dd$export$1e4baea7053fc0e3), {
|
|
348
|
-
paddingRight: 2,
|
|
349
|
-
style: {
|
|
350
|
-
width: `100%`
|
|
351
|
-
},
|
|
352
|
-
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$ArrayOfObjectsItem), {
|
|
353
|
-
...props,
|
|
354
|
-
member: member
|
|
355
|
-
})
|
|
356
|
-
}),
|
|
357
|
-
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $32ee6db03f46d8dd$export$1e4baea7053fc0e3), {
|
|
358
|
-
style: {
|
|
359
|
-
verticalAlign: "bottom"
|
|
360
|
-
},
|
|
361
|
-
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Flex), {
|
|
362
|
-
align: "center",
|
|
363
|
-
justify: "flex-end",
|
|
364
|
-
gap: 3,
|
|
365
|
-
children: /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Button), {
|
|
366
|
-
mode: "ghost",
|
|
367
|
-
icon: (0, $gGrEF$RemoveIcon),
|
|
368
|
-
tone: "critical",
|
|
369
|
-
disabled: typeof readOnly === "boolean" ? readOnly : false,
|
|
370
|
-
onClick: ()=>handleUnsetByKey(member.key)
|
|
371
|
-
})
|
|
372
|
-
})
|
|
373
|
-
})
|
|
374
|
-
]
|
|
375
|
-
}, member.key) : /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Text), {
|
|
376
|
-
children: "Error"
|
|
377
|
-
}))
|
|
378
|
-
})
|
|
379
|
-
}) : null,
|
|
380
|
-
languagesOutOfOrder.length > 0 && allKeysAreLanguages ? /*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Button), {
|
|
381
|
-
tone: "caution",
|
|
382
|
-
icon: (0, $gGrEF$RestoreIcon),
|
|
383
|
-
onClick: ()=>handleRestoreOrder(),
|
|
384
|
-
text: "Restore order of languages"
|
|
385
|
-
}) : null,
|
|
386
|
-
languages?.length > 0 ? /*#__PURE__*/ (0, $gGrEF$jsxs)((0, $gGrEF$Stack), {
|
|
387
|
-
space: 2,
|
|
388
|
-
children: [
|
|
389
|
-
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Grid), {
|
|
390
|
-
columns: Math.min(languages.length, 5),
|
|
391
|
-
gap: 2,
|
|
392
|
-
children: languages.map((language)=>/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Button), {
|
|
393
|
-
tone: "primary",
|
|
394
|
-
mode: "ghost",
|
|
395
|
-
fontSize: 1,
|
|
396
|
-
disabled: readOnly || Boolean(value?.find((item)=>item._key === language.id)),
|
|
397
|
-
text: language.id.toUpperCase(),
|
|
398
|
-
icon: (0, $gGrEF$AddIcon),
|
|
399
|
-
onClick: ()=>handleAddLanguage(language.id)
|
|
400
|
-
}, language.id))
|
|
401
|
-
}),
|
|
402
|
-
/*#__PURE__*/ (0, $gGrEF$jsx)((0, $gGrEF$Button), {
|
|
403
|
-
tone: "primary",
|
|
404
|
-
mode: "ghost",
|
|
405
|
-
disabled: readOnly || value && value?.length >= languages?.length,
|
|
406
|
-
icon: (0, $gGrEF$AddIcon),
|
|
407
|
-
text: value?.length ? `Add missing languages` : `Add all languages`,
|
|
408
|
-
onClick: ()=>handleAddLanguage()
|
|
409
|
-
})
|
|
410
|
-
]
|
|
411
|
-
}) : null
|
|
412
|
-
]
|
|
413
|
-
});
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
var $581a585556642695$export$2e2bcd8739ae039 = (config)=>{
|
|
418
|
-
const { languages: languages , type: type } = config;
|
|
419
|
-
const typeName = typeof type === `string` ? type : type.name;
|
|
420
|
-
const arrayName = (0, $5c51df8bc000fd76$export$ab1ce2a474f41f52)(typeName);
|
|
421
|
-
const objectName = (0, $5c51df8bc000fd76$export$ab1ce2a474f41f52)(typeName, true);
|
|
422
|
-
return (0, $gGrEF$defineField)({
|
|
423
|
-
name: arrayName,
|
|
424
|
-
title: "Internationalized array",
|
|
425
|
-
type: "array",
|
|
426
|
-
components: {
|
|
427
|
-
input: (0, $e3540aa1271cf9b1$export$2e2bcd8739ae039)
|
|
428
|
-
},
|
|
429
|
-
options: {
|
|
430
|
-
languages: languages
|
|
431
|
-
},
|
|
432
|
-
// TODO: Address this typing issue with the inner object
|
|
433
|
-
// @ts-ignore
|
|
434
|
-
of: [
|
|
435
|
-
(0, $gGrEF$defineField)({
|
|
436
|
-
name: objectName,
|
|
437
|
-
type: objectName
|
|
438
|
-
})
|
|
439
|
-
],
|
|
440
|
-
validation: (rule)=>rule.max(languages?.length).custom((value, context)=>{
|
|
441
|
-
const { languages: contextLanguages } = context?.type?.options ?? {};
|
|
442
|
-
const nonLanguageKeys = value?.length ? value.filter((item)=>!contextLanguages.find((language)=>item._key === language.id)) : [];
|
|
443
|
-
if (nonLanguageKeys.length) return {
|
|
444
|
-
message: `Array item keys must be valid languages registered to the field type`,
|
|
445
|
-
paths: nonLanguageKeys.map((item)=>[
|
|
446
|
-
{
|
|
447
|
-
_key: item._key
|
|
448
|
-
}
|
|
449
|
-
])
|
|
450
|
-
};
|
|
451
|
-
const valuesByLanguage = value?.length ? value.filter((item)=>Boolean(item?._key)).reduce((acc, cur)=>{
|
|
452
|
-
if (acc[cur._key]) return {
|
|
453
|
-
...acc,
|
|
454
|
-
[cur._key]: [
|
|
455
|
-
...acc[cur._key],
|
|
456
|
-
cur
|
|
457
|
-
]
|
|
458
|
-
};
|
|
459
|
-
return {
|
|
460
|
-
...acc,
|
|
461
|
-
[cur._key]: [
|
|
462
|
-
cur
|
|
463
|
-
]
|
|
464
|
-
};
|
|
465
|
-
}, {}) : {};
|
|
466
|
-
const duplicateValues = Object.values(valuesByLanguage).filter((item)=>item?.length > 1).flat();
|
|
467
|
-
if (duplicateValues.length) return {
|
|
468
|
-
message: "There can only be one field per language",
|
|
469
|
-
paths: duplicateValues.map((item)=>[
|
|
470
|
-
{
|
|
471
|
-
_key: item._key
|
|
472
|
-
}
|
|
473
|
-
])
|
|
474
|
-
};
|
|
475
|
-
return true;
|
|
476
|
-
})
|
|
477
|
-
});
|
|
478
|
-
};
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
var $3d48ee33fe42e7bc$export$2e2bcd8739ae039 = (config)=>{
|
|
484
|
-
const { type: type } = config;
|
|
485
|
-
const typeName = typeof type === `string` ? type : type.name;
|
|
486
|
-
const objectName = (0, $5c51df8bc000fd76$export$ab1ce2a474f41f52)(typeName, true);
|
|
487
|
-
return (0, $gGrEF$defineField)({
|
|
488
|
-
name: objectName,
|
|
489
|
-
title: `Internationalized array ${type}`,
|
|
490
|
-
type: "object",
|
|
491
|
-
// TODO: Address this typing issue with the inner object
|
|
492
|
-
// @ts-ignore
|
|
493
|
-
fields: [
|
|
494
|
-
typeof type === `string` ? (0, $gGrEF$defineField)({
|
|
495
|
-
name: "value",
|
|
496
|
-
type: type
|
|
497
|
-
}) : {
|
|
498
|
-
...type,
|
|
499
|
-
name: "value"
|
|
500
|
-
},
|
|
501
|
-
]
|
|
502
|
-
});
|
|
503
|
-
};
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
const $91a08798d183e77b$var$CONFIG_DEFAULT = {
|
|
507
|
-
languages: [],
|
|
508
|
-
fieldTypes: []
|
|
509
|
-
};
|
|
510
|
-
const $91a08798d183e77b$export$bec7eb13daf35f0e = (0, $gGrEF$createPlugin)((config = $91a08798d183e77b$var$CONFIG_DEFAULT)=>{
|
|
511
|
-
const { languages: languages , fieldTypes: fieldTypes } = {
|
|
512
|
-
...$91a08798d183e77b$var$CONFIG_DEFAULT,
|
|
513
|
-
...config
|
|
514
|
-
};
|
|
515
|
-
return {
|
|
516
|
-
name: "sanity-plugin-internationalized-array",
|
|
517
|
-
schema: {
|
|
518
|
-
types: [
|
|
519
|
-
...fieldTypes.map((type)=>(0, $581a585556642695$export$2e2bcd8739ae039)({
|
|
520
|
-
type: type,
|
|
521
|
-
languages: languages
|
|
522
|
-
})),
|
|
523
|
-
...fieldTypes.map((type)=>(0, $3d48ee33fe42e7bc$export$2e2bcd8739ae039)({
|
|
524
|
-
type: type
|
|
525
|
-
})),
|
|
526
|
-
]
|
|
527
|
-
}
|
|
528
|
-
};
|
|
529
|
-
});
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
export {$91a08798d183e77b$export$bec7eb13daf35f0e as internationalizedArray};
|
|
535
|
-
//# sourceMappingURL=index.js.map
|
package/lib/esm/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;ACAA;ACAA;ACAO,SAAS,yCAAS,CAAC,MAAc,EAAU;IAChD,OAAO,MAAM,CAAC,OAAO,cAAc,CAAC,CAAC,GAAK,CAAC,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CAAA;CAC9D;AAEM,SAAS,yCAAS,CAAC,MAAc,EAAU;IAChD,OAAO,MAAM,CACV,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CACV,GAAG,CAAC,CAAC,IAAI,GAAK,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAC3D,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;CACb;AAEM,SAAS,yCAAU,CAAC,MAAc,EAAU;IACjD,OAAO,yCAAS,CAAC,yCAAS,CAAC,MAAM,CAAC,CAAC,CAAA;CACpC;AAEM,SAAS,yCAAe,CAAC,IAAY,EAAE,QAAQ,GAAG,KAAK,EAAU;IACtE,OAAO,QAAQ,GACX;QAAC,CAAC,sBAAsB,CAAC;QAAE,yCAAU,CAAC,IAAI,CAAC;QAAE,CAAC,KAAK,CAAC;KAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,GAC9D;QAA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GAAG,CAAA,GAAA,yCAAe,CAAA,CAAC,QAAQ,EAAE,IAAI,CAAC;IAElD,OAAO,CAAA,GAAA,kBAAW,CAAA,CAAC;QACjB,IAAI,EAAE,UAAU;QAChB,KAAK,EAAE,CAAC,wBAAwB,EAAE,IAAI,CAAC,CAAC;QACxC,IAAI,EAAE,QAAQ;QACd,wDAAwD;QACxD,aAAa;QACb,MAAM,EAAE;YACN,OAAO,IAAI,KAAK,CAAC,MAAM,CAAC,GAEpB,CAAA,GAAA,kBAAW,CAAA,CAAC;gBACV,IAAI,EAAE,OAAO;sBACb,IAAI;aACL,CAAC,GAEF;gBAAC,GAAG,IAAI;gBAAE,IAAI,EAAE,OAAO;aAAC;SAC7B;KACF,CAAC,CAAA;CACH;;;APzBD,MAAM,oCAAc,GAAG;IACrB,SAAS,EAAE,EAAE;IACb,UAAU,EAAE,EAAE;CACf;AAEM,MAAM,yCAAsB,GAAG,CAAA,GAAA,mBAAY,CAAA,CAAe,CAAC,MAAM,GAAG,oCAAc,GAAK;IAC5F,MAAM,aAAC,SAAS,CAAA,cAAE,UAAU,CAAA,EAAC,GAAG;QAAC,GAAG,oCAAc;QAAE,GAAG,MAAM;KAAC;IAE9D,OAAO;QACL,IAAI,EAAE,uCAAuC;QAC7C,MAAM,EAAE;YACN,KAAK,EAAE;mBACF,UAAU,CAAC,GAAG,CAAC,CAAC,IAAI,GAAK,CAAA,GAAA,wCAAK,CAAA,CAAC;8BAAC,IAAI;mCAAE,SAAS;qBAAC,CAAC,CAAC;mBAClD,UAAU,CAAC,GAAG,CAAC,CAAC,IAAI,GAAK,CAAA,GAAA,wCAAM,CAAA,CAAC;8BAAC,IAAI;qBAAC,CAAC,CAAC;aAC5C;SACF;KACF,CAAA;CACF,CAAC;;ADtBF","sources":["src/index.ts","src/plugin.tsx","src/schema/array.ts","src/components/createFieldName.ts","src/components/InternationalizedArrayInput.tsx","src/components/Table.tsx","src/components/Feedback.tsx","src/components/getToneFromValidation.ts","src/schema/object.ts"],"sourcesContent":["export {internationalizedArray} from './plugin'\n","import {createPlugin} from 'sanity'\nimport {PluginConfig} from './types'\nimport array from './schema/array'\nimport object from './schema/object'\n\nconst CONFIG_DEFAULT = {\n languages: [],\n fieldTypes: [],\n}\n\nexport const internationalizedArray = createPlugin<PluginConfig>((config = CONFIG_DEFAULT) => {\n const {languages, fieldTypes} = {...CONFIG_DEFAULT, ...config}\n\n return {\n name: 'sanity-plugin-internationalized-array',\n schema: {\n types: [\n ...fieldTypes.map((type) => array({type, languages})),\n ...fieldTypes.map((type) => object({type})),\n ],\n },\n }\n})\n","import {defineField, Rule, Schema} from 'sanity'\n\nimport {createFieldName} from '../components/createFieldName'\nimport InternationalizedArrayInput from '../components/InternationalizedArrayInput'\nimport {Language, Value} from '../types'\n\ntype ArrayFactoryConfig = {\n languages: Language[]\n type: string | Schema.FieldDefinition\n}\n\nexport default (config: ArrayFactoryConfig): Schema.FieldDefinition<'array'> => {\n const {languages, type} = config\n const typeName = typeof type === `string` ? type : type.name\n const arrayName = createFieldName(typeName)\n const objectName = createFieldName(typeName, true)\n\n return defineField({\n name: arrayName,\n title: 'Internationalized array',\n type: 'array',\n components: {input: InternationalizedArrayInput},\n options: {languages},\n // TODO: Address this typing issue with the inner object\n // @ts-ignore\n of: [defineField({name: objectName, type: objectName})],\n validation: (rule: Rule) =>\n rule.max(languages?.length).custom<Value[]>((value, context) => {\n const {languages: contextLanguages}: {languages: Language[]} = context?.type?.options ?? {}\n const nonLanguageKeys = value?.length\n ? value.filter((item) => !contextLanguages.find((language) => item._key === language.id))\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\n return true\n }),\n })\n}\n","export function camelCase(string: string): string {\n return string.replace(/-([a-z])/g, (g) => g[1].toUpperCase())\n}\n\nexport function titleCase(string: string): string {\n return string\n .split(` `)\n .map((word) => word.charAt(0).toUpperCase() + word.slice(1))\n .join(` `)\n}\n\nexport function pascalCase(string: string): string {\n return titleCase(camelCase(string))\n}\n\nexport function createFieldName(name: string, addValue = false): string {\n return addValue\n ? [`internationalizedArray`, pascalCase(name), `Value`].join(``)\n : [`internationalizedArray`, pascalCase(name)].join(``)\n}\n","import React, {useCallback, useMemo} from 'react'\nimport {\n ArrayOfObjectsInputProps,\n ArrayOfObjectsItem,\n unset,\n insert,\n set,\n setIfMissing,\n} from 'sanity/form'\nimport {\n Text,\n Box,\n Button,\n Flex,\n Grid,\n Label,\n MenuButton,\n Stack,\n useToast,\n Menu,\n MenuItem,\n} from '@sanity/ui'\nimport {AddIcon, RemoveIcon, RestoreIcon} from '@sanity/icons'\n\nimport {Language, Value, ArraySchemaWithLanguageOptions} from '../types'\nimport {Table, TableCell, TableRow} from './Table'\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 const toast = useToast()\n\n const languages: Language[] = useMemo(() => options?.languages ?? [], [options])\n const valueLanguageKeys = useMemo(\n () => value?.map((v) => v._key).filter((key) => languages.find((l) => l.id === key)) ?? [],\n [languages, value]\n )\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 // eslint-disable-next-line max-nested-callbacks\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: string) => {\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 > -1) {\n acc[newIndex] = v\n }\n\n return acc\n }, [] as Value[])\n .filter(Boolean)\n\n if (value.length !== updatedValue.length) {\n toast.push({\n title: 'There was an error reordering languages',\n status: 'warning',\n })\n }\n\n onChange(set(updatedValue))\n }, [toast, 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 handleKeyChange = useCallback(\n (config: {from: string; to: string; index: number}) => {\n if (!value) {\n return\n }\n\n const {from, to, index} = config\n const currentValue = value.find((v) => v._key === from)\n const newValue = {...currentValue, _key: to}\n\n // TODO: Make sure this gets the correct language index, currently replaces-in-place\n onChange([insert([newValue], 'after', [index]), unset([{_key: from}])])\n },\n [onChange, value]\n )\n\n const languagesAreValid = useMemo(\n () =>\n !languages?.length || (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, memberIndex) =>\n member.kind === 'item' ? (\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 {valueLanguageKeys.includes(member.key) ? (\n <Label muted size={1}>\n {member.key}\n </Label>\n ) : (\n <MenuButton\n button={<Button fontSize={1} text={`Change \"${member.key}\"`} />}\n id={`${member.key}-change-key`}\n menu={\n <Menu>\n {languages.map((language) => (\n <MenuItem\n disabled={valueLanguageKeys.includes(language.id)}\n fontSize={1}\n key={language.id}\n text={language.id.toLocaleUpperCase()}\n onClick={() =>\n handleKeyChange({\n from: member.key,\n to: language.id,\n index: memberIndex,\n })\n }\n />\n ))}\n </Menu>\n }\n placement=\"right\"\n popover={{portal: true}}\n />\n )}\n </Box>\n </TableCell>\n <TableCell paddingRight={2} style={{width: `100%`}}>\n {/* This renders the entire field default with title */}\n <ArrayOfObjectsItem {...props} member={member} />\n </TableCell>\n <TableCell style={{verticalAlign: 'bottom'}}>\n <Flex align=\"center\" justify=\"flex-end\" gap={3}>\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 <Text>Error</Text>\n )\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 {languages?.length > 0 ? (\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","import {defineField, Schema} from 'sanity'\n\nimport {createFieldName} from '../components/createFieldName'\n\ntype ObjectFactoryConfig = {\n type: string | Schema.FieldDefinition\n}\n\nexport default (config: ObjectFactoryConfig): Schema.FieldDefinition<'object'> => {\n const {type} = config\n const typeName = typeof type === `string` ? type : type.name\n const objectName = createFieldName(typeName, true)\n\n return defineField({\n name: objectName,\n title: `Internationalized array ${type}`,\n type: 'object',\n // TODO: Address this typing issue with the inner object\n // @ts-ignore\n fields: [\n typeof type === `string`\n ? // Define a basic field if all we have is the string name\n defineField({\n name: 'value',\n type,\n })\n : // Pass in the configured options, but overwrite the name\n {...type, name: 'value'},\n ],\n })\n}\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../"}
|
package/lib/types/index.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { Schema } from "sanity";
|
|
2
|
-
type Language = {
|
|
3
|
-
id: string;
|
|
4
|
-
title: string;
|
|
5
|
-
};
|
|
6
|
-
type PluginConfig = {
|
|
7
|
-
languages: Language[];
|
|
8
|
-
fieldTypes: (string | Schema.FieldDefinition)[];
|
|
9
|
-
};
|
|
10
|
-
export const internationalizedArray: import("sanity").Plugin<PluginConfig>;
|
|
11
|
-
|
|
12
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/types/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";AAEA,gBAAuB;IACrB,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,EAAE,MAAM,CAAA;CACd,CAAA;AAqBD,oBAA2B;IACzB,SAAS,EAAE,QAAQ,EAAE,CAAA;IACrB,UAAU,EAAE,CAAC,MAAM,GAAG,OAAO,eAAe,CAAC,EAAE,CAAA;CAChD,CAAA;AQnBD,OAAO,MAAM,6DAYX,CAAA","sources":["src/src/types.ts","src/src/components/createFieldName.ts","src/src/components/Table.tsx","src/src/components/Feedback.tsx","src/src/components/getToneFromValidation.ts","src/src/components/InternationalizedArrayInput.tsx","src/src/schema/array.ts","src/src/schema/object.ts","src/src/plugin.tsx","src/src/index.ts","src/index.ts"],"sourcesContent":[null,null,null,null,null,null,null,null,null,null,"export {internationalizedArray} from './plugin'\n"],"names":[],"version":3,"file":"index.d.ts.map","sourceRoot":"../../"}
|