@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/package.json
CHANGED
|
@@ -15,7 +15,12 @@ import {type ObjectSchemaType, type SanityDocument, useClient} from 'sanity'
|
|
|
15
15
|
|
|
16
16
|
import {METADATA_SCHEMA_NAME} from '../constants'
|
|
17
17
|
import {useOpenInNewPane} from '../hooks/useOpenInNewPane'
|
|
18
|
-
import type {
|
|
18
|
+
import type {
|
|
19
|
+
Language,
|
|
20
|
+
Metadata,
|
|
21
|
+
MetadataDocument,
|
|
22
|
+
TranslationReference,
|
|
23
|
+
} from '../types'
|
|
19
24
|
import {createReference} from '../utils/createReference'
|
|
20
25
|
import {removeExcludedPaths} from '../utils/excludePaths'
|
|
21
26
|
import {useDocumentInternationalizationContext} from './DocumentInternationalizationContext'
|
|
@@ -58,7 +63,7 @@ export default function LanguageOption(props: LanguageOptionProps) {
|
|
|
58
63
|
.length
|
|
59
64
|
? metadata.translations.find((t) => t._key === language.id)
|
|
60
65
|
: undefined
|
|
61
|
-
const {apiVersion, languageField, weakReferences} =
|
|
66
|
+
const {apiVersion, languageField, weakReferences, callback} =
|
|
62
67
|
useDocumentInternationalizationContext()
|
|
63
68
|
const client = useClient({apiVersion})
|
|
64
69
|
const toast = useToast()
|
|
@@ -121,7 +126,7 @@ export default function LanguageOption(props: LanguageOptionProps) {
|
|
|
121
126
|
schemaType.name,
|
|
122
127
|
!weakReferences
|
|
123
128
|
)
|
|
124
|
-
const newMetadataDocument = {
|
|
129
|
+
const newMetadataDocument: MetadataDocument = {
|
|
125
130
|
_id: metadataId,
|
|
126
131
|
_type: METADATA_SCHEMA_NAME,
|
|
127
132
|
schemaTypes: [schemaType.name],
|
|
@@ -146,6 +151,21 @@ export default function LanguageOption(props: LanguageOptionProps) {
|
|
|
146
151
|
.then(() => {
|
|
147
152
|
const metadataExisted = Boolean(metadata?._createdAt)
|
|
148
153
|
|
|
154
|
+
callback?.({
|
|
155
|
+
client,
|
|
156
|
+
sourceLanguageId,
|
|
157
|
+
sourceDocument: source,
|
|
158
|
+
newDocument: newTranslationDocument,
|
|
159
|
+
destinationLanguageId: language.id,
|
|
160
|
+
metaDocumentId: metadataId,
|
|
161
|
+
}).catch((err) => {
|
|
162
|
+
toast.push({
|
|
163
|
+
status: 'error',
|
|
164
|
+
title: `Callback`,
|
|
165
|
+
description: `Error while running callback - ${err}.`,
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
|
|
149
169
|
return toast.push({
|
|
150
170
|
status: 'success',
|
|
151
171
|
title: `Created "${language.title}" translation`,
|
|
@@ -179,6 +199,7 @@ export default function LanguageOption(props: LanguageOptionProps) {
|
|
|
179
199
|
sourceLanguageId,
|
|
180
200
|
toast,
|
|
181
201
|
weakReferences,
|
|
202
|
+
callback,
|
|
182
203
|
])
|
|
183
204
|
|
|
184
205
|
let message
|
package/src/constants.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -6,6 +6,8 @@ import type {
|
|
|
6
6
|
ObjectSchemaType,
|
|
7
7
|
Reference,
|
|
8
8
|
SanityClient,
|
|
9
|
+
SanityDocument,
|
|
10
|
+
SanityDocumentLike,
|
|
9
11
|
} from 'sanity'
|
|
10
12
|
|
|
11
13
|
export type Language = {
|
|
@@ -17,6 +19,15 @@ export type SupportedLanguages =
|
|
|
17
19
|
| Language[]
|
|
18
20
|
| ((client: SanityClient) => Promise<Language[]>)
|
|
19
21
|
|
|
22
|
+
export type PluginCallbackArgs = {
|
|
23
|
+
sourceDocument: SanityDocument
|
|
24
|
+
newDocument: SanityDocument
|
|
25
|
+
sourceLanguageId: string
|
|
26
|
+
destinationLanguageId: string
|
|
27
|
+
metaDocumentId: string
|
|
28
|
+
client: SanityClient
|
|
29
|
+
}
|
|
30
|
+
|
|
20
31
|
export type PluginConfig = {
|
|
21
32
|
supportedLanguages: SupportedLanguages
|
|
22
33
|
schemaTypes: string[]
|
|
@@ -26,6 +37,7 @@ export type PluginConfig = {
|
|
|
26
37
|
metadataFields?: FieldDefinition[]
|
|
27
38
|
apiVersion?: string
|
|
28
39
|
allowCreateMetaDoc?: boolean
|
|
40
|
+
callback?: ((args: PluginCallbackArgs) => Promise<void>) | null
|
|
29
41
|
}
|
|
30
42
|
|
|
31
43
|
// Context version of config
|
|
@@ -46,6 +58,11 @@ export type Metadata = {
|
|
|
46
58
|
translations: TranslationReference[]
|
|
47
59
|
}
|
|
48
60
|
|
|
61
|
+
export type MetadataDocument = SanityDocumentLike & {
|
|
62
|
+
schemaTypes: string[]
|
|
63
|
+
translations: TranslationReference[]
|
|
64
|
+
}
|
|
65
|
+
|
|
49
66
|
export type DocumentInternationalizationMenuProps = {
|
|
50
67
|
schemaType: ObjectSchemaType
|
|
51
68
|
documentId: string
|