@sanity/document-internationalization 3.2.1 → 3.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 +19 -5
- package/dist/index.d.mts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.esm.js +19 -4
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +19 -4
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +19 -4
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/LanguageOption.tsx +24 -3
- package/src/constants.ts +1 -0
- package/src/types.ts +17 -0
package/README.md
CHANGED
|
@@ -128,21 +128,21 @@ export const defineConfig({
|
|
|
128
128
|
|
|
129
129
|
// Optional
|
|
130
130
|
// Customizes the name of the language field
|
|
131
|
-
languageField: `language
|
|
131
|
+
languageField: `language`, // defauts to "language"
|
|
132
132
|
|
|
133
133
|
// Optional
|
|
134
134
|
// Keep translation.metadata references weak
|
|
135
|
-
weakReferences: true // defaults to false
|
|
135
|
+
weakReferences: true, // defaults to false
|
|
136
136
|
|
|
137
137
|
// Optional
|
|
138
138
|
// Adds UI for publishing all translations at once. Requires access to the Scheduling API
|
|
139
139
|
// https://www.sanity.io/docs/scheduling-api
|
|
140
|
-
bulkPublish: true // defaults to false
|
|
140
|
+
bulkPublish: true, // defaults to false
|
|
141
141
|
|
|
142
142
|
// Optional
|
|
143
143
|
// Adds additional fields to the metadata document
|
|
144
144
|
metadataFields: [
|
|
145
|
-
defineField({ name: 'slug', type: 'slug' })
|
|
145
|
+
defineField({ name: 'slug', type: 'slug' }),
|
|
146
146
|
],
|
|
147
147
|
|
|
148
148
|
// Optional
|
|
@@ -153,7 +153,21 @@ export const defineConfig({
|
|
|
153
153
|
// Optional
|
|
154
154
|
// Enable "manage translations" button without creating a translated version. Helpful if you have
|
|
155
155
|
// pre-existing documents that you need to tie together through the metadata document
|
|
156
|
-
allowCreateMetaDoc: true // defaults to false
|
|
156
|
+
allowCreateMetaDoc: true, // defaults to false
|
|
157
|
+
|
|
158
|
+
// Optional
|
|
159
|
+
// Callback function that runs after a translation document has been created
|
|
160
|
+
// Note: Defaults to null
|
|
161
|
+
callback: ({
|
|
162
|
+
sourceDocument, // The document in the original language
|
|
163
|
+
newDocument, // The newly created translation of the source document
|
|
164
|
+
sourceLanguageId, // The id of the original language
|
|
165
|
+
destinationLanguageId, // The id of the destination language
|
|
166
|
+
metaDocumentId, // The id of the meta document referencing the document translations
|
|
167
|
+
client // Sanity client
|
|
168
|
+
}) {
|
|
169
|
+
// Your function implementation
|
|
170
|
+
}
|
|
157
171
|
})
|
|
158
172
|
]
|
|
159
173
|
})
|
package/dist/index.d.mts
CHANGED
|
@@ -8,6 +8,8 @@ import type {ObjectSchemaType} from 'sanity'
|
|
|
8
8
|
import {Plugin as Plugin_2} from 'sanity'
|
|
9
9
|
import type {Reference} from 'sanity'
|
|
10
10
|
import type {SanityClient} from 'sanity'
|
|
11
|
+
import type {SanityDocument} from 'sanity'
|
|
12
|
+
import type {SanityDocumentLike} from 'sanity'
|
|
11
13
|
|
|
12
14
|
export declare const DeleteTranslationAction: DocumentActionComponent
|
|
13
15
|
|
|
@@ -42,6 +44,20 @@ export declare type Metadata = {
|
|
|
42
44
|
translations: TranslationReference[]
|
|
43
45
|
}
|
|
44
46
|
|
|
47
|
+
export declare type MetadataDocument = SanityDocumentLike & {
|
|
48
|
+
schemaTypes: string[]
|
|
49
|
+
translations: TranslationReference[]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export declare type PluginCallbackArgs = {
|
|
53
|
+
sourceDocument: SanityDocument
|
|
54
|
+
newDocument: SanityDocument
|
|
55
|
+
sourceLanguageId: string
|
|
56
|
+
destinationLanguageId: string
|
|
57
|
+
metaDocumentId: string
|
|
58
|
+
client: SanityClient
|
|
59
|
+
}
|
|
60
|
+
|
|
45
61
|
export declare type PluginConfig = {
|
|
46
62
|
supportedLanguages: SupportedLanguages
|
|
47
63
|
schemaTypes: string[]
|
|
@@ -51,6 +67,7 @@ export declare type PluginConfig = {
|
|
|
51
67
|
metadataFields?: FieldDefinition[]
|
|
52
68
|
apiVersion?: string
|
|
53
69
|
allowCreateMetaDoc?: boolean
|
|
70
|
+
callback?: ((args: PluginCallbackArgs) => Promise<void>) | null
|
|
54
71
|
}
|
|
55
72
|
|
|
56
73
|
export declare type PluginConfigContext = Required<PluginConfig> & {
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,8 @@ import type {ObjectSchemaType} from 'sanity'
|
|
|
8
8
|
import {Plugin as Plugin_2} from 'sanity'
|
|
9
9
|
import type {Reference} from 'sanity'
|
|
10
10
|
import type {SanityClient} from 'sanity'
|
|
11
|
+
import type {SanityDocument} from 'sanity'
|
|
12
|
+
import type {SanityDocumentLike} from 'sanity'
|
|
11
13
|
|
|
12
14
|
export declare const DeleteTranslationAction: DocumentActionComponent
|
|
13
15
|
|
|
@@ -42,6 +44,20 @@ export declare type Metadata = {
|
|
|
42
44
|
translations: TranslationReference[]
|
|
43
45
|
}
|
|
44
46
|
|
|
47
|
+
export declare type MetadataDocument = SanityDocumentLike & {
|
|
48
|
+
schemaTypes: string[]
|
|
49
|
+
translations: TranslationReference[]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export declare type PluginCallbackArgs = {
|
|
53
|
+
sourceDocument: SanityDocument
|
|
54
|
+
newDocument: SanityDocument
|
|
55
|
+
sourceLanguageId: string
|
|
56
|
+
destinationLanguageId: string
|
|
57
|
+
metaDocumentId: string
|
|
58
|
+
client: SanityClient
|
|
59
|
+
}
|
|
60
|
+
|
|
45
61
|
export declare type PluginConfig = {
|
|
46
62
|
supportedLanguages: SupportedLanguages
|
|
47
63
|
schemaTypes: string[]
|
|
@@ -51,6 +67,7 @@ export declare type PluginConfig = {
|
|
|
51
67
|
metadataFields?: FieldDefinition[]
|
|
52
68
|
apiVersion?: string
|
|
53
69
|
allowCreateMetaDoc?: boolean
|
|
70
|
+
callback?: ((args: PluginCallbackArgs) => Promise<void>) | null
|
|
54
71
|
}
|
|
55
72
|
|
|
56
73
|
export declare type PluginConfigContext = Required<PluginConfig> & {
|
package/dist/index.esm.js
CHANGED
|
@@ -34,7 +34,8 @@ const METADATA_SCHEMA_NAME = "translation.metadata", TRANSLATIONS_ARRAY_NAME = "
|
|
|
34
34
|
bulkPublish: !1,
|
|
35
35
|
metadataFields: [],
|
|
36
36
|
apiVersion: API_VERSION,
|
|
37
|
-
allowCreateMetaDoc: !1
|
|
37
|
+
allowCreateMetaDoc: !1,
|
|
38
|
+
callback: null
|
|
38
39
|
};
|
|
39
40
|
function separateReferences(data = []) {
|
|
40
41
|
const translations = [], otherReferences = [];
|
|
@@ -3714,7 +3715,7 @@ function LanguageOption(props) {
|
|
|
3714
3715
|
sourceLanguageId,
|
|
3715
3716
|
metadata: metadata2,
|
|
3716
3717
|
metadataId
|
|
3717
|
-
} = 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]);
|
|
3718
|
+
} = 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, callback } = 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]);
|
|
3718
3719
|
useEffect(() => {
|
|
3719
3720
|
setUserHasClicked(!1);
|
|
3720
3721
|
}, [!!translation]);
|
|
@@ -3756,7 +3757,20 @@ function LanguageOption(props) {
|
|
|
3756
3757
|
const metadataPatch = client.patch(metadataId).setIfMissing({ translations: [sourceReference] }).insert("after", "translations[-1]", [newTranslationReference]);
|
|
3757
3758
|
transaction.patch(metadataPatch), transaction.commit().then(() => {
|
|
3758
3759
|
const metadataExisted = !!(metadata2 != null && metadata2._createdAt);
|
|
3759
|
-
return
|
|
3760
|
+
return callback == null || callback({
|
|
3761
|
+
client,
|
|
3762
|
+
sourceLanguageId,
|
|
3763
|
+
sourceDocument: source,
|
|
3764
|
+
newDocument: newTranslationDocument,
|
|
3765
|
+
destinationLanguageId: language.id,
|
|
3766
|
+
metaDocumentId: metadataId
|
|
3767
|
+
}).catch((err) => {
|
|
3768
|
+
toast.push({
|
|
3769
|
+
status: "error",
|
|
3770
|
+
title: "Callback",
|
|
3771
|
+
description: `Error while running callback - ${err}.`
|
|
3772
|
+
});
|
|
3773
|
+
}), toast.push({
|
|
3760
3774
|
status: "success",
|
|
3761
3775
|
title: `Created "${language.title}" translation`,
|
|
3762
3776
|
description: metadataExisted ? "Updated Translations Metadata" : "Created Translations Metadata"
|
|
@@ -3778,7 +3792,8 @@ function LanguageOption(props) {
|
|
|
3778
3792
|
source,
|
|
3779
3793
|
sourceLanguageId,
|
|
3780
3794
|
toast,
|
|
3781
|
-
weakReferences
|
|
3795
|
+
weakReferences,
|
|
3796
|
+
callback
|
|
3782
3797
|
]);
|
|
3783
3798
|
let message;
|
|
3784
3799
|
return current ? message = "Current document" : translation ? message = `Open ${language.title} translation` : translation || (message = `Create new ${language.title} translation`), /* @__PURE__ */ jsx(
|