gt-sanity 0.0.5
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/LICENSE.md +138 -0
- package/README.md +100 -0
- package/dist/index.d.mts +241 -0
- package/dist/index.d.ts +241 -0
- package/dist/index.js +2119 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2099 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +92 -0
- package/sanity.json +8 -0
- package/src/adapter/core.ts +44 -0
- package/src/adapter/createTask.ts +41 -0
- package/src/adapter/getLocales.ts +13 -0
- package/src/adapter/getTranslation.ts +20 -0
- package/src/adapter/getTranslationTask.ts +31 -0
- package/src/adapter/index.ts +13 -0
- package/src/components/LanguageStatus.tsx +65 -0
- package/src/components/NewTask.tsx +249 -0
- package/src/components/ProgressBar.tsx +38 -0
- package/src/components/TaskView.tsx +255 -0
- package/src/components/TranslationContext.tsx +19 -0
- package/src/components/TranslationView.tsx +82 -0
- package/src/components/TranslationsTab.tsx +177 -0
- package/src/configuration/README.md +8 -0
- package/src/configuration/baseDocumentLevelConfig/documentLevelPatch.ts +108 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/createI18nDocAndPatchMetadata.ts +47 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/createTranslationMetadata.ts +43 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/getOrCreateTranslationMetadata.ts +77 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/getTranslationMetadata.ts +15 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/index.ts +5 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/patchI18nDoc.ts +25 -0
- package/src/configuration/baseDocumentLevelConfig/index.ts +129 -0
- package/src/configuration/baseDocumentLevelConfig/legacyDocumentLevelPatch.ts +69 -0
- package/src/configuration/baseFieldLevelConfig.ts +118 -0
- package/src/configuration/index.ts +18 -0
- package/src/configuration/utils/checkSerializationVersion.ts +13 -0
- package/src/configuration/utils/findDocumentAtRevision.ts +22 -0
- package/src/configuration/utils/findLatestDraft.ts +16 -0
- package/src/configuration/utils/index.ts +3 -0
- package/src/hooks/useClient.ts +5 -0
- package/src/hooks/useSecrets.ts +33 -0
- package/src/index.ts +120 -0
- package/src/types.ts +124 -0
- package/v2-incompatible.js +11 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import TranslationsTab from './components/TranslationsTab';
|
|
2
|
+
import {
|
|
3
|
+
Secrets,
|
|
4
|
+
Adapter,
|
|
5
|
+
ExportForTranslation,
|
|
6
|
+
ImportTranslation,
|
|
7
|
+
TranslationFunctionContext,
|
|
8
|
+
TranslationsTabConfigOptions,
|
|
9
|
+
GTFile,
|
|
10
|
+
} from './types';
|
|
11
|
+
import {
|
|
12
|
+
baseDocumentLevelConfig,
|
|
13
|
+
legacyDocumentLevelConfig as baseLegacyDocumentLevelConfig,
|
|
14
|
+
legacyDocumentLevelPatch,
|
|
15
|
+
baseFieldLevelConfig,
|
|
16
|
+
findLatestDraft,
|
|
17
|
+
documentLevelPatch,
|
|
18
|
+
fieldLevelPatch,
|
|
19
|
+
} from './configuration';
|
|
20
|
+
import {
|
|
21
|
+
BaseDocumentSerializer,
|
|
22
|
+
BaseDocumentDeserializer,
|
|
23
|
+
BaseDocumentMerger,
|
|
24
|
+
defaultStopTypes,
|
|
25
|
+
customSerializers,
|
|
26
|
+
SerializedDocument,
|
|
27
|
+
} from 'sanity-naive-html-serializer';
|
|
28
|
+
|
|
29
|
+
export type {
|
|
30
|
+
Secrets,
|
|
31
|
+
Adapter,
|
|
32
|
+
ExportForTranslation,
|
|
33
|
+
ImportTranslation,
|
|
34
|
+
TranslationFunctionContext,
|
|
35
|
+
TranslationsTabConfigOptions,
|
|
36
|
+
SerializedDocument,
|
|
37
|
+
};
|
|
38
|
+
export {
|
|
39
|
+
TranslationsTab,
|
|
40
|
+
//helpers for end developers who may need to customize serialization
|
|
41
|
+
findLatestDraft,
|
|
42
|
+
legacyDocumentLevelPatch,
|
|
43
|
+
documentLevelPatch,
|
|
44
|
+
fieldLevelPatch,
|
|
45
|
+
BaseDocumentSerializer,
|
|
46
|
+
BaseDocumentDeserializer,
|
|
47
|
+
BaseDocumentMerger,
|
|
48
|
+
defaultStopTypes,
|
|
49
|
+
customSerializers,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
import { GTAdapter } from './adapter';
|
|
53
|
+
import { definePlugin } from 'sanity';
|
|
54
|
+
import { gt, gtConfig } from './adapter/core';
|
|
55
|
+
import { GTSerializedDocument } from './types';
|
|
56
|
+
import { libraryDefaultLocale } from 'generaltranslation/internal';
|
|
57
|
+
|
|
58
|
+
interface ConfigOptions {
|
|
59
|
+
adapter: Adapter;
|
|
60
|
+
secretsNamespace: string | null;
|
|
61
|
+
exportForTranslation: (
|
|
62
|
+
docInfo: GTFile,
|
|
63
|
+
context: TranslationFunctionContext
|
|
64
|
+
) => Promise<GTSerializedDocument>;
|
|
65
|
+
importTranslation: (
|
|
66
|
+
docInfo: GTFile,
|
|
67
|
+
localeId: string,
|
|
68
|
+
doc: string,
|
|
69
|
+
context: TranslationFunctionContext
|
|
70
|
+
) => Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const defaultDocumentLevelConfig: ConfigOptions = {
|
|
74
|
+
...baseDocumentLevelConfig,
|
|
75
|
+
adapter: GTAdapter,
|
|
76
|
+
secretsNamespace: 'generaltranslation',
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const legacyDocumentLevelConfig: ConfigOptions = {
|
|
80
|
+
...baseLegacyDocumentLevelConfig,
|
|
81
|
+
adapter: GTAdapter,
|
|
82
|
+
secretsNamespace: 'generaltranslation',
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export const defaultFieldLevelConfig: ConfigOptions = {
|
|
86
|
+
...baseFieldLevelConfig,
|
|
87
|
+
adapter: GTAdapter,
|
|
88
|
+
secretsNamespace: 'generaltranslation',
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { GTAdapter };
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Usage in `sanity.config.ts` (or .js)
|
|
95
|
+
*
|
|
96
|
+
* ```ts
|
|
97
|
+
* import {defineConfig} from 'sanity'
|
|
98
|
+
* import {gtPlugin} from '@generaltranslation/sanity'
|
|
99
|
+
*
|
|
100
|
+
* export default defineConfig({
|
|
101
|
+
* // ...
|
|
102
|
+
* plugins: [gtPlugin()],
|
|
103
|
+
* })
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
export const gtPlugin = definePlugin<
|
|
107
|
+
Omit<Parameters<typeof gt.setConfig>[0], 'locales'> & { locales: string[] }
|
|
108
|
+
>(({ sourceLocale, locales, customMapping, apiKey, projectId }) => {
|
|
109
|
+
gtConfig.setLocales(locales);
|
|
110
|
+
gtConfig.setSourceLocale(sourceLocale || libraryDefaultLocale);
|
|
111
|
+
gt.setConfig({
|
|
112
|
+
sourceLocale: sourceLocale,
|
|
113
|
+
customMapping: customMapping,
|
|
114
|
+
apiKey: apiKey,
|
|
115
|
+
projectId: projectId,
|
|
116
|
+
});
|
|
117
|
+
return {
|
|
118
|
+
name: '@generaltranslation/sanity',
|
|
119
|
+
};
|
|
120
|
+
});
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { SanityClient, Schema, TypedObject } from 'sanity';
|
|
2
|
+
import { SerializedDocument } from 'sanity-naive-html-serializer';
|
|
3
|
+
import { PortableTextTypeComponent } from '@portabletext/to-html';
|
|
4
|
+
import { DeserializerRule } from '@sanity/block-tools';
|
|
5
|
+
|
|
6
|
+
export type TranslationTaskLocaleStatus = {
|
|
7
|
+
localeId: string;
|
|
8
|
+
progress: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type TranslationTask = {
|
|
12
|
+
document: GTFile;
|
|
13
|
+
locales: TranslationTaskLocaleStatus[];
|
|
14
|
+
linkToVendorTask?: string;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type TranslationLocale = {
|
|
18
|
+
localeId: string;
|
|
19
|
+
description: string;
|
|
20
|
+
enabled?: boolean;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//this varies according to provider
|
|
24
|
+
//not every vendor uses every field
|
|
25
|
+
export type Secrets = {
|
|
26
|
+
organization: string;
|
|
27
|
+
project: string;
|
|
28
|
+
token?: string;
|
|
29
|
+
secret?: string;
|
|
30
|
+
username?: string;
|
|
31
|
+
password?: string;
|
|
32
|
+
proxy?: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export type WorkflowIdentifiers = {
|
|
36
|
+
workflowUid: string;
|
|
37
|
+
workflowName: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type GTFile = {
|
|
41
|
+
documentId: string;
|
|
42
|
+
versionId?: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export interface Adapter {
|
|
46
|
+
getLocales: (secrets: Secrets | null) => Promise<TranslationLocale[]>;
|
|
47
|
+
getTranslationTask: (
|
|
48
|
+
document: GTFile,
|
|
49
|
+
secrets: Secrets | null
|
|
50
|
+
) => Promise<TranslationTask>;
|
|
51
|
+
createTask: (
|
|
52
|
+
documentInfo: GTFile,
|
|
53
|
+
serializedDocument: GTSerializedDocument,
|
|
54
|
+
localeIds: string[],
|
|
55
|
+
secrets: Secrets | null,
|
|
56
|
+
workflowUid?: string,
|
|
57
|
+
callbackUrl?: string
|
|
58
|
+
) => Promise<TranslationTask>;
|
|
59
|
+
getTranslation: (
|
|
60
|
+
document: GTFile,
|
|
61
|
+
localeId: string,
|
|
62
|
+
secrets: Secrets | null
|
|
63
|
+
) => Promise<any | null>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface TranslationFunctionContext {
|
|
67
|
+
client: SanityClient;
|
|
68
|
+
schema: Schema;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type GTSerializedDocument = Omit<SerializedDocument, 'name'> & GTFile;
|
|
72
|
+
|
|
73
|
+
export type ExportForTranslation = (
|
|
74
|
+
documentInfo: GTFile,
|
|
75
|
+
context: TranslationFunctionContext,
|
|
76
|
+
serializationOptions?: {
|
|
77
|
+
additionalStopTypes?: string[];
|
|
78
|
+
additionalSerializers?: Record<
|
|
79
|
+
string,
|
|
80
|
+
PortableTextTypeComponent | undefined
|
|
81
|
+
>;
|
|
82
|
+
},
|
|
83
|
+
languageField?: string
|
|
84
|
+
) => Promise<GTSerializedDocument>;
|
|
85
|
+
|
|
86
|
+
export type ImportTranslation = (
|
|
87
|
+
documentInfo: GTFile,
|
|
88
|
+
localeId: string,
|
|
89
|
+
document: string,
|
|
90
|
+
context: TranslationFunctionContext,
|
|
91
|
+
serializationOptions?: {
|
|
92
|
+
additionalDeserializers?: Record<
|
|
93
|
+
string,
|
|
94
|
+
(value: HTMLElement) => TypedObject
|
|
95
|
+
>;
|
|
96
|
+
additionalBlockDeserializers?: DeserializerRule[];
|
|
97
|
+
},
|
|
98
|
+
languageField?: string,
|
|
99
|
+
mergeWithTargetLocale?: boolean
|
|
100
|
+
) => Promise<void>;
|
|
101
|
+
|
|
102
|
+
export type TranslationsTabConfigOptions = {
|
|
103
|
+
adapter: Adapter;
|
|
104
|
+
secretsNamespace: string | null;
|
|
105
|
+
exportForTranslation: ExportForTranslation;
|
|
106
|
+
importTranslation: ImportTranslation;
|
|
107
|
+
serializationOptions?: {
|
|
108
|
+
additionalStopTypes?: string[];
|
|
109
|
+
additionalSerializers?: Record<
|
|
110
|
+
string,
|
|
111
|
+
PortableTextTypeComponent | undefined
|
|
112
|
+
>;
|
|
113
|
+
additionalDeserializers?: Record<
|
|
114
|
+
string,
|
|
115
|
+
(value: HTMLElement) => TypedObject
|
|
116
|
+
>;
|
|
117
|
+
additionalBlockDeserializers?: DeserializerRule[];
|
|
118
|
+
};
|
|
119
|
+
workflowOptions?: WorkflowIdentifiers[];
|
|
120
|
+
localeIdAdapter?: (id: string) => string;
|
|
121
|
+
languageField?: string;
|
|
122
|
+
callbackUrl?: string;
|
|
123
|
+
mergeWithTargetLocale?: boolean;
|
|
124
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const { showIncompatiblePluginDialog } = require('@sanity/incompatible-plugin');
|
|
2
|
+
const { name, version, sanityExchangeUrl } = require('./package.json');
|
|
3
|
+
|
|
4
|
+
export default showIncompatiblePluginDialog({
|
|
5
|
+
name: name,
|
|
6
|
+
versions: {
|
|
7
|
+
v3: version,
|
|
8
|
+
v2: undefined,
|
|
9
|
+
},
|
|
10
|
+
sanityExchangeUrl,
|
|
11
|
+
});
|