@sanity/document-internationalization 3.0.1 → 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 +62 -19
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +62 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/DocumentInternationalizationMenu.tsx +7 -1
- package/src/components/LanguageManage.tsx +76 -10
- 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;
|
|
@@ -496,7 +530,16 @@ function DocumentInternationalizationMenu(props) {
|
|
|
496
530
|
supportedLanguages
|
|
497
531
|
), valid;
|
|
498
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: [
|
|
499
|
-
/* @__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
|
+
),
|
|
500
543
|
supportedLanguages.length > 4 ? /* @__PURE__ */ jsx(
|
|
501
544
|
TextInput,
|
|
502
545
|
{
|