@sanity/document-internationalization 3.0.0 → 3.1.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 +8 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +69 -21
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +69 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +69 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
- package/src/components/DocumentInternationalizationMenu.tsx +7 -1
- package/src/components/LanguageManage.tsx +76 -10
- package/src/components/LanguageOption.tsx +24 -2
- package/src/constants.ts +1 -0
- package/src/hooks/useOpenInNewPane.tsx +1 -1
- package/src/types.ts +1 -0
package/README.md
CHANGED
|
@@ -11,6 +11,7 @@ All new rewrite exclusively for Sanity Studio v3
|
|
|
11
11
|
- [Basic configuration](#basic-configuration)
|
|
12
12
|
- [Advanced configuration](#advanced-configuration)
|
|
13
13
|
- [Language field](#language-field)
|
|
14
|
+
- [Excluding fields](#excluding-fields)
|
|
14
15
|
- [Querying translations](#querying-translations)
|
|
15
16
|
- [Querying with GROQ](#querying-with-groq)
|
|
16
17
|
- [Querying with GraphQL](#querying-with-graphql)
|
|
@@ -118,6 +119,7 @@ export const createConfig({
|
|
|
118
119
|
],
|
|
119
120
|
// ...or a function that takes the client and returns a promise of an array of supported languages
|
|
120
121
|
// MUST return an "id" and "title" as strings
|
|
122
|
+
// Note: Async language configuration cannot create templates for new documents
|
|
121
123
|
// supportedLanguages: (client) => client.fetch(`*[_type == "language"]{id, title}`),
|
|
122
124
|
|
|
123
125
|
// Required
|
|
@@ -146,7 +148,12 @@ export const createConfig({
|
|
|
146
148
|
// Optional
|
|
147
149
|
// Define API Version for all queries
|
|
148
150
|
// https://www.sanity.io/docs/api-versioning
|
|
149
|
-
apiVersion: '2023-05-22'
|
|
151
|
+
apiVersion: '2023-05-22',
|
|
152
|
+
|
|
153
|
+
// Optional
|
|
154
|
+
// Enable "manage translations" button without creating a translated version. Helpful if you have
|
|
155
|
+
// pre-existing documents that you need to tie together through the metadata document
|
|
156
|
+
allowCreateMetaDoc: true // defaults to false
|
|
150
157
|
})
|
|
151
158
|
]
|
|
152
159
|
})
|
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.esm.js
CHANGED
|
@@ -21,7 +21,8 @@ const METADATA_SCHEMA_NAME = "translation.metadata", TRANSLATIONS_ARRAY_NAME = "
|
|
|
21
21
|
weakReferences: !1,
|
|
22
22
|
bulkPublish: !1,
|
|
23
23
|
metadataFields: [],
|
|
24
|
-
apiVersion: API_VERSION
|
|
24
|
+
apiVersion: API_VERSION,
|
|
25
|
+
allowCreateMetaDoc: !1
|
|
25
26
|
};
|
|
26
27
|
function separateReferences(data = []) {
|
|
27
28
|
const translations = [], otherReferences = [];
|
|
@@ -243,42 +244,75 @@ function useOpenInNewPane(id, type) {
|
|
|
243
244
|
routerContext.navigateUrl({ path: href });
|
|
244
245
|
}, [id, type, routerContext, routerPanesState, groupIndex]);
|
|
245
246
|
}
|
|
247
|
+
function createReference(key, ref, type, strengthenOnPublish = !0) {
|
|
248
|
+
return {
|
|
249
|
+
_key: key,
|
|
250
|
+
_type: "internationalizedArrayReferenceValue",
|
|
251
|
+
value: {
|
|
252
|
+
_type: "reference",
|
|
253
|
+
_ref: ref,
|
|
254
|
+
_weak: !0,
|
|
255
|
+
// If the user has configured weakReferences, we won't want to strengthen them
|
|
256
|
+
...strengthenOnPublish ? { _strengthenOnPublish: { type } } : {}
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
}
|
|
246
260
|
function LanguageManage(props) {
|
|
247
|
-
const { id } = props, open = useOpenInNewPane(id, METADATA_SCHEMA_NAME)
|
|
261
|
+
const { id, metadataId, schemaType, documentId, sourceLanguageId } = props, open = useOpenInNewPane(id, METADATA_SCHEMA_NAME), openCreated = useOpenInNewPane(metadataId, METADATA_SCHEMA_NAME), { allowCreateMetaDoc, apiVersion, weakReferences } = useDocumentInternationalizationContext(), client = useClient({ apiVersion }), [userHasClicked, setUserHasClicked] = useState(!1), canCreate = !id && !!metadataId && allowCreateMetaDoc, handleClick = useCallback(() => {
|
|
262
|
+
if (!id && metadataId && sourceLanguageId) {
|
|
263
|
+
setUserHasClicked(!0);
|
|
264
|
+
const transaction = client.transaction(), sourceReference = createReference(
|
|
265
|
+
sourceLanguageId,
|
|
266
|
+
documentId,
|
|
267
|
+
schemaType.name,
|
|
268
|
+
!weakReferences
|
|
269
|
+
), newMetadataDocument = {
|
|
270
|
+
_id: metadataId,
|
|
271
|
+
_type: METADATA_SCHEMA_NAME,
|
|
272
|
+
schemaTypes: [schemaType.name],
|
|
273
|
+
translations: [sourceReference]
|
|
274
|
+
};
|
|
275
|
+
transaction.createIfNotExists(newMetadataDocument), transaction.commit().then(() => {
|
|
276
|
+
setUserHasClicked(!1), openCreated();
|
|
277
|
+
}).catch((err) => {
|
|
278
|
+
console.error(err), setUserHasClicked(!1);
|
|
279
|
+
});
|
|
280
|
+
} else
|
|
281
|
+
open();
|
|
282
|
+
}, [
|
|
283
|
+
id,
|
|
284
|
+
metadataId,
|
|
285
|
+
sourceLanguageId,
|
|
286
|
+
client,
|
|
287
|
+
documentId,
|
|
288
|
+
schemaType.name,
|
|
289
|
+
weakReferences,
|
|
290
|
+
openCreated,
|
|
291
|
+
open
|
|
292
|
+
]);
|
|
248
293
|
return /* @__PURE__ */ jsx(
|
|
249
294
|
Tooltip,
|
|
250
295
|
{
|
|
251
296
|
animate: !0,
|
|
252
|
-
content:
|
|
297
|
+
content: /* @__PURE__ */ jsx(Box, { padding: 2, children: /* @__PURE__ */ jsx(Text, { muted: !0, size: 1, children: "Document has no other translations" }) }),
|
|
253
298
|
fallbackPlacements: ["right", "left"],
|
|
254
299
|
placement: "top",
|
|
255
300
|
portal: !0,
|
|
301
|
+
disabled: !!id || canCreate,
|
|
256
302
|
children: /* @__PURE__ */ jsx(Stack, { children: /* @__PURE__ */ jsx(
|
|
257
303
|
Button,
|
|
258
304
|
{
|
|
259
|
-
disabled: !id,
|
|
305
|
+
disabled: !id && !canCreate || canCreate && !sourceLanguageId || userHasClicked,
|
|
260
306
|
mode: "ghost",
|
|
261
307
|
text: "Manage Translations",
|
|
262
308
|
icon: CogIcon,
|
|
263
|
-
|
|
309
|
+
loading: userHasClicked,
|
|
310
|
+
onClick: handleClick
|
|
264
311
|
}
|
|
265
312
|
) })
|
|
266
313
|
}
|
|
267
314
|
);
|
|
268
315
|
}
|
|
269
|
-
function createReference(key, ref, type, strengthenOnPublish = !0) {
|
|
270
|
-
return {
|
|
271
|
-
_key: key,
|
|
272
|
-
_type: "internationalizedArrayReferenceValue",
|
|
273
|
-
value: {
|
|
274
|
-
_type: "reference",
|
|
275
|
-
_ref: ref,
|
|
276
|
-
_weak: !0,
|
|
277
|
-
// If the user has configured weakReferences, we won't want to strengthen them
|
|
278
|
-
...strengthenOnPublish ? { _strengthenOnPublish: { type } } : {}
|
|
279
|
-
}
|
|
280
|
-
};
|
|
281
|
-
}
|
|
282
316
|
function removeExcludedPaths(doc, schemaType) {
|
|
283
317
|
if (!isDocumentSchemaType(schemaType) || !doc)
|
|
284
318
|
return doc;
|
|
@@ -351,13 +385,18 @@ function LanguageOption(props) {
|
|
|
351
385
|
sourceLanguageId,
|
|
352
386
|
metadata: metadata2,
|
|
353
387
|
metadataId
|
|
354
|
-
} = props, disabled = props.disabled || current || !source || !sourceLanguageId || !metadataId, translation = metadata2 != null && metadata2.translations.length ? metadata2.translations.find((t) => t._key === language.id) : void 0, { apiVersion, languageField, weakReferences } = useDocumentInternationalizationContext(), client = useClient({ apiVersion }), toast = useToast(), open = useOpenInNewPane((_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref, schemaType.name), handleOpen = useCallback(() => open(), [open])
|
|
388
|
+
} = props, [userHasClicked, setUserHasClicked] = useState(!1), disabled = props.disabled || userHasClicked || current || !source || !sourceLanguageId || !metadataId, translation = metadata2 != null && metadata2.translations.length ? metadata2.translations.find((t) => t._key === language.id) : void 0, { apiVersion, languageField, weakReferences } = useDocumentInternationalizationContext(), client = useClient({ apiVersion }), toast = useToast(), open = useOpenInNewPane((_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref, schemaType.name), handleOpen = useCallback(() => open(), [open]);
|
|
389
|
+
useEffect(() => {
|
|
390
|
+
setUserHasClicked(!1);
|
|
391
|
+
}, [!!translation]);
|
|
392
|
+
const handleCreate = useCallback(async () => {
|
|
355
393
|
if (!source)
|
|
356
394
|
throw new Error("Cannot create translation without source document");
|
|
357
395
|
if (!sourceLanguageId)
|
|
358
396
|
throw new Error("Cannot create translation without source language ID");
|
|
359
397
|
if (!metadataId)
|
|
360
398
|
throw new Error("Cannot create translation without a metadata ID");
|
|
399
|
+
setUserHasClicked(!0);
|
|
361
400
|
const transaction = client.transaction(), newTranslationDocumentId = uuid();
|
|
362
401
|
let newTranslationDocument = {
|
|
363
402
|
...source,
|
|
@@ -394,7 +433,7 @@ function LanguageOption(props) {
|
|
|
394
433
|
title: `Created "${language.title}" translation`,
|
|
395
434
|
description: metadataExisted ? "Updated Translations Metadata" : "Created Translations Metadata"
|
|
396
435
|
});
|
|
397
|
-
}).catch((err) => (console.error(err), toast.push({
|
|
436
|
+
}).catch((err) => (console.error(err), setUserHasClicked(!1), toast.push({
|
|
398
437
|
status: "error",
|
|
399
438
|
title: "Error creating translation",
|
|
400
439
|
description: err.message
|
|
@@ -491,7 +530,16 @@ function DocumentInternationalizationMenu(props) {
|
|
|
491
530
|
supportedLanguages
|
|
492
531
|
), valid;
|
|
493
532
|
}, [supportedLanguages]), content = /* @__PURE__ */ jsx(Box, { padding: 1, children: error ? /* @__PURE__ */ jsx(Card, { tone: "critical", padding: 1, children: /* @__PURE__ */ jsx(Text, { children: "There was an error returning translations metadata" }) }) : /* @__PURE__ */ jsxs(Stack, { space: 1, children: [
|
|
494
|
-
/* @__PURE__ */ jsx(
|
|
533
|
+
/* @__PURE__ */ jsx(
|
|
534
|
+
LanguageManage,
|
|
535
|
+
{
|
|
536
|
+
id: metadata2 == null ? void 0 : metadata2._id,
|
|
537
|
+
documentId,
|
|
538
|
+
metadataId,
|
|
539
|
+
schemaType,
|
|
540
|
+
sourceLanguageId
|
|
541
|
+
}
|
|
542
|
+
),
|
|
495
543
|
supportedLanguages.length > 4 ? /* @__PURE__ */ jsx(
|
|
496
544
|
TextInput,
|
|
497
545
|
{
|