@payload-enchants/translator 1.2.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +4 -0
- package/dist/client/api/index.js.map +1 -1
- package/dist/client/components/CustomButton/CustomButtonWithTranslator.d.ts +3 -4
- package/dist/client/components/CustomButton/CustomButtonWithTranslator.d.ts.map +1 -1
- package/dist/client/components/CustomButton/CustomButtonWithTranslator.js +6 -5
- package/dist/client/components/CustomButton/CustomButtonWithTranslator.js.map +1 -1
- package/dist/client/components/CustomButton/index.d.ts +2 -1
- package/dist/client/components/CustomButton/index.d.ts.map +1 -1
- package/dist/client/components/CustomButton/index.js +5 -8
- package/dist/client/components/CustomButton/index.js.map +1 -1
- package/dist/client/components/LocaleLabel/LocaleLabel.js.map +1 -1
- package/dist/client/components/ResolverButton/ResolverButton.js +0 -1
- package/dist/client/components/ResolverButton/ResolverButton.js.map +1 -1
- package/dist/client/components/TranslatorModal/TranslatorModal.d.ts.map +1 -1
- package/dist/client/components/TranslatorModal/TranslatorModal.js +2 -3
- package/dist/client/components/TranslatorModal/TranslatorModal.js.map +1 -1
- package/dist/client/providers/Translator/TranslatorProvider.d.ts.map +1 -1
- package/dist/client/providers/Translator/TranslatorProvider.js +25 -19
- package/dist/client/providers/Translator/TranslatorProvider.js.map +1 -1
- package/dist/exports/client.d.ts +2 -0
- package/dist/exports/client.d.ts.map +1 -0
- package/dist/exports/client.js +3 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/i18n-translations.d.ts +18 -0
- package/dist/i18n-translations.d.ts.map +1 -1
- package/dist/i18n-translations.js +18 -0
- package/dist/i18n-translations.js.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -2
- package/dist/index.js.map +1 -1
- package/dist/resolvers/google.js +1 -1
- package/dist/resolvers/google.js.map +1 -1
- package/dist/resolvers/libreTranslate.d.ts +16 -0
- package/dist/resolvers/libreTranslate.d.ts.map +1 -0
- package/dist/resolvers/libreTranslate.js +48 -0
- package/dist/resolvers/libreTranslate.js.map +1 -0
- package/dist/resolvers/openAI.d.ts +2 -2
- package/dist/resolvers/openAI.d.ts.map +1 -1
- package/dist/resolvers/openAI.js +10 -10
- package/dist/resolvers/openAI.js.map +1 -1
- package/dist/translate/findEntityWithConfig.d.ts.map +1 -1
- package/dist/translate/findEntityWithConfig.js +4 -4
- package/dist/translate/findEntityWithConfig.js.map +1 -1
- package/dist/translate/operation.d.ts.map +1 -1
- package/dist/translate/operation.js +3 -1
- package/dist/translate/operation.js.map +1 -1
- package/dist/translate/traverseFields.d.ts.map +1 -1
- package/dist/translate/traverseFields.js +94 -86
- package/dist/translate/traverseFields.js.map +1 -1
- package/dist/translate/traverseRichText.js +3 -3
- package/dist/translate/traverseRichText.js.map +1 -1
- package/dist/translate/updateEntity.d.ts.map +1 -1
- package/dist/translate/updateEntity.js +2 -2
- package/dist/translate/updateEntity.js.map +1 -1
- package/package.json +17 -13
@@ -0,0 +1,16 @@
|
|
1
|
+
import type { TranslateResolver } from './types';
|
2
|
+
export type LibreResolverConfig = {
|
3
|
+
apiKey: string;
|
4
|
+
/**
|
5
|
+
* How many texts to include into 1 request
|
6
|
+
* @default 100
|
7
|
+
*/
|
8
|
+
chunkLength?: number;
|
9
|
+
/**
|
10
|
+
* Custom url for the libre translate instance
|
11
|
+
* @default "https://libretranslate.com/translate"
|
12
|
+
*/
|
13
|
+
url?: string;
|
14
|
+
};
|
15
|
+
export declare const libreResolver: ({ apiKey, chunkLength, url, }: LibreResolverConfig) => TranslateResolver;
|
16
|
+
//# sourceMappingURL=libreTranslate.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"libreTranslate.d.ts","sourceRoot":"","sources":["../../src/resolvers/libreTranslate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAkBjD,MAAM,MAAM,mBAAmB,GAAG;IAChC,MAAM,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,eAAO,MAAM,aAAa,kCAIvB,mBAAmB,KAAG,iBAqDxB,CAAC"}
|
@@ -0,0 +1,48 @@
|
|
1
|
+
import { chunkArray } from '../utils/chunkArray';
|
2
|
+
const localeToCountryCodeMapper = {
|
3
|
+
ua: 'uk'
|
4
|
+
};
|
5
|
+
const mapLocale = (incoming)=>incoming in localeToCountryCodeMapper ? localeToCountryCodeMapper[incoming] : incoming;
|
6
|
+
export const libreResolver = ({ apiKey, chunkLength = 100, url = 'https://libretranslate.com/translate' })=>{
|
7
|
+
return {
|
8
|
+
key: 'libre',
|
9
|
+
resolve: async (args)=>{
|
10
|
+
const { localeFrom, localeTo, req, texts } = args;
|
11
|
+
const apiUrl = url;
|
12
|
+
const responses = await Promise.all(chunkArray(texts, chunkLength).map((q)=>fetch(apiUrl, {
|
13
|
+
body: JSON.stringify({
|
14
|
+
api_key: apiKey,
|
15
|
+
q,
|
16
|
+
source: mapLocale(localeFrom),
|
17
|
+
target: mapLocale(localeTo)
|
18
|
+
}),
|
19
|
+
headers: {
|
20
|
+
'Content-Type': 'application/json'
|
21
|
+
},
|
22
|
+
method: 'POST'
|
23
|
+
}).then(async (res)=>{
|
24
|
+
const data = await res.json();
|
25
|
+
if (!res.ok) req.payload.logger.info({
|
26
|
+
libreResponse: data,
|
27
|
+
message: 'An error occurred when trying to translate the data using LibreTranslate API'
|
28
|
+
});
|
29
|
+
return {
|
30
|
+
data,
|
31
|
+
success: res.ok
|
32
|
+
};
|
33
|
+
})));
|
34
|
+
if (responses.some((res)=>!res.success)) {
|
35
|
+
return {
|
36
|
+
success: false
|
37
|
+
};
|
38
|
+
}
|
39
|
+
const translatedTexts = responses.flatMap((chunk)=>chunk.data.translatedText);
|
40
|
+
return {
|
41
|
+
success: true,
|
42
|
+
translatedTexts
|
43
|
+
};
|
44
|
+
}
|
45
|
+
};
|
46
|
+
};
|
47
|
+
|
48
|
+
//# sourceMappingURL=libreTranslate.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/resolvers/libreTranslate.ts"],"sourcesContent":["import { chunkArray } from '../utils/chunkArray';\nimport type { TranslateResolver } from './types';\n\ntype LibreResponse = {\n data: {\n translatedText: string[];\n };\n success: boolean;\n};\n\nconst localeToCountryCodeMapper = {\n ua: 'uk',\n};\n\nconst mapLocale = (incoming: string) =>\n incoming in localeToCountryCodeMapper\n ? localeToCountryCodeMapper[incoming as keyof typeof localeToCountryCodeMapper]\n : incoming;\n\nexport type LibreResolverConfig = {\n apiKey: string;\n /**\n * How many texts to include into 1 request\n * @default 100\n */\n chunkLength?: number;\n /**\n * Custom url for the libre translate instance\n * @default \"https://libretranslate.com/translate\"\n */\n url?: string;\n};\n\nexport const libreResolver = ({\n apiKey,\n chunkLength = 100,\n url = 'https://libretranslate.com/translate',\n}: LibreResolverConfig): TranslateResolver => {\n return {\n key: 'libre',\n resolve: async (args) => {\n const { localeFrom, localeTo, req, texts } = args;\n\n const apiUrl = url;\n\n const responses: LibreResponse[] = await Promise.all(\n chunkArray(texts, chunkLength).map((q) =>\n fetch(apiUrl, {\n body: JSON.stringify({\n api_key: apiKey,\n q,\n source: mapLocale(localeFrom),\n target: mapLocale(localeTo),\n }),\n headers: {\n 'Content-Type': 'application/json',\n },\n method: 'POST',\n }).then(async (res) => {\n const data = await res.json();\n\n if (!res.ok)\n req.payload.logger.info({\n libreResponse: data,\n message:\n 'An error occurred when trying to translate the data using LibreTranslate API',\n });\n\n return {\n data,\n success: res.ok,\n };\n }),\n ),\n );\n\n if (responses.some((res) => !res.success)) {\n return {\n success: false,\n };\n }\n\n const translatedTexts = responses.flatMap((chunk) => chunk.data.translatedText);\n\n return {\n success: true,\n translatedTexts,\n };\n },\n };\n};\n"],"names":["chunkArray","localeToCountryCodeMapper","ua","mapLocale","incoming","libreResolver","apiKey","chunkLength","url","key","resolve","args","localeFrom","localeTo","req","texts","apiUrl","responses","Promise","all","map","q","fetch","body","JSON","stringify","api_key","source","target","headers","method","then","res","data","json","ok","payload","logger","info","libreResponse","message","success","some","translatedTexts","flatMap","chunk","translatedText"],"mappings":"AAAA,SAASA,UAAU,QAAQ,sBAAsB;AAUjD,MAAMC,4BAA4B;IAChCC,IAAI;AACN;AAEA,MAAMC,YAAY,CAACC,WACjBA,YAAYH,4BACRA,yBAAyB,CAACG,SAAmD,GAC7EA;AAgBN,OAAO,MAAMC,gBAAgB,CAAC,EAC5BC,MAAM,EACNC,cAAc,GAAG,EACjBC,MAAM,sCAAsC,EACxB;IACpB,OAAO;QACLC,KAAK;QACLC,SAAS,OAAOC;YACd,MAAM,EAAEC,UAAU,EAAEC,QAAQ,EAAEC,GAAG,EAAEC,KAAK,EAAE,GAAGJ;YAE7C,MAAMK,SAASR;YAEf,MAAMS,YAA6B,MAAMC,QAAQC,GAAG,CAClDnB,WAAWe,OAAOR,aAAaa,GAAG,CAAC,CAACC,IAClCC,MAAMN,QAAQ;oBACZO,MAAMC,KAAKC,SAAS,CAAC;wBACnBC,SAASpB;wBACTe;wBACAM,QAAQxB,UAAUS;wBAClBgB,QAAQzB,UAAUU;oBACpB;oBACAgB,SAAS;wBACP,gBAAgB;oBAClB;oBACAC,QAAQ;gBACV,GAAGC,IAAI,CAAC,OAAOC;oBACb,MAAMC,OAAO,MAAMD,IAAIE,IAAI;oBAE3B,IAAI,CAACF,IAAIG,EAAE,EACTrB,IAAIsB,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC;wBACtBC,eAAeN;wBACfO,SACE;oBACJ;oBAEF,OAAO;wBACLP;wBACAQ,SAAST,IAAIG,EAAE;oBACjB;gBACF;YAIJ,IAAIlB,UAAUyB,IAAI,CAAC,CAACV,MAAQ,CAACA,IAAIS,OAAO,GAAG;gBACzC,OAAO;oBACLA,SAAS;gBACX;YACF;YAEA,MAAME,kBAAkB1B,UAAU2B,OAAO,CAAC,CAACC,QAAUA,MAAMZ,IAAI,CAACa,cAAc;YAE9E,OAAO;gBACLL,SAAS;gBACTE;YACF;QACF;IACF;AACF,EAAE"}
|
@@ -16,7 +16,7 @@ export type OpenAIResolverConfig = {
|
|
16
16
|
* @default "gpt-3.5-turbo"
|
17
17
|
*/
|
18
18
|
model?: string;
|
19
|
-
|
19
|
+
prompt?: OpenAIPrompt;
|
20
20
|
};
|
21
|
-
export declare const openAIResolver: ({ apiKey, baseUrl, chunkLength, model,
|
21
|
+
export declare const openAIResolver: ({ apiKey, baseUrl, chunkLength, model, prompt, }: OpenAIResolverConfig) => TranslateResolver;
|
22
22
|
//# sourceMappingURL=openAI.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"openAI.d.ts","sourceRoot":"","sources":["../../src/resolvers/openAI.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,KAAK,MAAM,CAAC;AAEb,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,
|
1
|
+
{"version":3,"file":"openAI.d.ts","sourceRoot":"","sources":["../../src/resolvers/openAI.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE;IAChC,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB,KAAK,MAAM,CAAC;AAEb,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,YAAY,CAAC;CACvB,CAAC;AAcF,eAAO,MAAM,cAAc,qDAMxB,oBAAoB,KAAG,iBAkHzB,CAAC"}
|
package/dist/resolvers/openAI.js
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
import { chunkArray } from '../utils/chunkArray';
|
2
|
-
const
|
2
|
+
const defaultPrompt = ({ localeFrom, localeTo, texts })=>{
|
3
3
|
return `Translate me the following array: ${JSON.stringify(texts)} in locale=${localeFrom} to locale ${localeTo}, respond me with the same array structure`;
|
4
4
|
};
|
5
|
-
export const openAIResolver = ({ apiKey, baseUrl, chunkLength = 100, model = 'gpt-3.5-turbo',
|
5
|
+
export const openAIResolver = ({ apiKey, baseUrl, chunkLength = 100, model = 'gpt-3.5-turbo', prompt = defaultPrompt })=>{
|
6
6
|
return {
|
7
7
|
key: 'openai',
|
8
8
|
resolve: async ({ localeFrom, localeTo, req, texts })=>{
|
9
9
|
const apiUrl = `${baseUrl || 'https://api.openai.com'}/v1/chat/completions`;
|
10
10
|
try {
|
11
|
-
const
|
11
|
+
const response = await Promise.all(chunkArray(texts, chunkLength).map((texts)=>{
|
12
12
|
return fetch(apiUrl, {
|
13
13
|
body: JSON.stringify({
|
14
14
|
messages: [
|
15
15
|
{
|
16
|
-
content:
|
16
|
+
content: prompt({
|
17
17
|
localeFrom,
|
18
18
|
localeTo,
|
19
19
|
texts
|
@@ -31,7 +31,7 @@ export const openAIResolver = ({ apiKey, baseUrl, chunkLength = 100, model = 'gp
|
|
31
31
|
}).then(async (res)=>{
|
32
32
|
const data = await res.json();
|
33
33
|
if (!res.ok) req.payload.logger.info({
|
34
|
-
message:
|
34
|
+
message: 'An error occurred when trying to translate the data using OpenAI API',
|
35
35
|
openAIresponse: data
|
36
36
|
});
|
37
37
|
return {
|
@@ -41,13 +41,13 @@ export const openAIResolver = ({ apiKey, baseUrl, chunkLength = 100, model = 'gp
|
|
41
41
|
});
|
42
42
|
}));
|
43
43
|
const translated = [];
|
44
|
-
for (const { data, success } of
|
44
|
+
for (const { data, success } of response){
|
45
45
|
if (!success) return {
|
46
46
|
success: false
|
47
47
|
};
|
48
48
|
const content = data?.choices?.[0]?.message?.content;
|
49
49
|
if (!content) {
|
50
|
-
req.payload.logger.error(
|
50
|
+
req.payload.logger.error('An error occurred when trying to translate the data using OpenAI API - missing content in the response');
|
51
51
|
return {
|
52
52
|
success: false
|
53
53
|
};
|
@@ -57,7 +57,7 @@ export const openAIResolver = ({ apiKey, baseUrl, chunkLength = 100, model = 'gp
|
|
57
57
|
req.payload.logger.error({
|
58
58
|
data: translatedChunk,
|
59
59
|
fullContent: content,
|
60
|
-
message:
|
60
|
+
message: 'An error occurred when trying to translate the data using OpenAI API - parsed content is not an array'
|
61
61
|
});
|
62
62
|
return {
|
63
63
|
success: false
|
@@ -69,7 +69,7 @@ export const openAIResolver = ({ apiKey, baseUrl, chunkLength = 100, model = 'gp
|
|
69
69
|
chunkData: translatedChunk,
|
70
70
|
data: text,
|
71
71
|
fullContent: content,
|
72
|
-
message:
|
72
|
+
message: 'An error occurred when trying to translate the data using OpenAI API - parsed content is not a string'
|
73
73
|
});
|
74
74
|
return {
|
75
75
|
success: false
|
@@ -85,7 +85,7 @@ export const openAIResolver = ({ apiKey, baseUrl, chunkLength = 100, model = 'gp
|
|
85
85
|
} catch (e) {
|
86
86
|
if (e instanceof Error) {
|
87
87
|
req.payload.logger.info({
|
88
|
-
message:
|
88
|
+
message: 'An error occurred when trying to translate the data using OpenAI API',
|
89
89
|
originalErr: e.message
|
90
90
|
});
|
91
91
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/resolvers/openAI.ts"],"sourcesContent":["import { chunkArray } from '../utils/chunkArray';\nimport type { TranslateResolver } from './types';\n\nexport type OpenAIPrompt = (args: {\n localeFrom: string;\n localeTo: string;\n texts: string[];\n}) => string;\n\nexport type OpenAIResolverConfig = {\n apiKey: string;\n baseUrl?: string;\n /**\n * How many texts to include into 1 request\n * @default 100\n */\n chunkLength?: number;\n /**\n * @default \"gpt-3.5-turbo\"\n */\n model?: string;\n
|
1
|
+
{"version":3,"sources":["../../src/resolvers/openAI.ts"],"sourcesContent":["import { chunkArray } from '../utils/chunkArray';\nimport type { TranslateResolver } from './types';\n\nexport type OpenAIPrompt = (args: {\n localeFrom: string;\n localeTo: string;\n texts: string[];\n}) => string;\n\nexport type OpenAIResolverConfig = {\n apiKey: string;\n baseUrl?: string;\n /**\n * How many texts to include into 1 request\n * @default 100\n */\n chunkLength?: number;\n /**\n * @default \"gpt-3.5-turbo\"\n */\n model?: string;\n prompt?: OpenAIPrompt;\n};\n\ntype OpenAIResponse = {\n choices: {\n message: {\n content: string;\n };\n }[];\n};\n\nconst defaultPrompt: OpenAIPrompt = ({ localeFrom, localeTo, texts }) => {\n return `Translate me the following array: ${JSON.stringify(texts)} in locale=${localeFrom} to locale ${localeTo}, respond me with the same array structure`;\n};\n\nexport const openAIResolver = ({\n apiKey,\n baseUrl,\n chunkLength = 100,\n model = 'gpt-3.5-turbo',\n prompt = defaultPrompt,\n}: OpenAIResolverConfig): TranslateResolver => {\n return {\n key: 'openai',\n resolve: async ({ localeFrom, localeTo, req, texts }) => {\n const apiUrl = `${baseUrl || 'https://api.openai.com'}/v1/chat/completions`;\n\n try {\n const response: {\n data: OpenAIResponse;\n success: boolean;\n }[] = await Promise.all(\n chunkArray(texts, chunkLength).map((texts) => {\n return fetch(apiUrl, {\n body: JSON.stringify({\n messages: [\n {\n content: prompt({ localeFrom, localeTo, texts }),\n role: 'user',\n },\n ],\n model,\n }),\n headers: {\n Authorization: `Bearer ${apiKey}`,\n 'Content-Type': 'application/json',\n },\n method: 'post',\n }).then(async (res) => {\n const data = await res.json();\n\n if (!res.ok)\n req.payload.logger.info({\n message: 'An error occurred when trying to translate the data using OpenAI API',\n openAIresponse: data,\n });\n\n return {\n data,\n success: res.ok,\n };\n });\n }),\n );\n\n const translated: string[] = [];\n\n for (const { data, success } of response) {\n if (!success)\n return {\n success: false as const,\n };\n\n const content = data?.choices?.[0]?.message?.content;\n\n if (!content) {\n req.payload.logger.error(\n 'An error occurred when trying to translate the data using OpenAI API - missing content in the response',\n );\n\n return {\n success: false as const,\n };\n }\n\n const translatedChunk: string[] = JSON.parse(content);\n\n if (!Array.isArray(translatedChunk)) {\n req.payload.logger.error({\n data: translatedChunk,\n fullContent: content,\n message:\n 'An error occurred when trying to translate the data using OpenAI API - parsed content is not an array',\n });\n\n return {\n success: false as const,\n };\n }\n\n for (const text of translatedChunk) {\n if (text && typeof text !== 'string') {\n req.payload.logger.error({\n chunkData: translatedChunk,\n data: text,\n fullContent: content,\n message:\n 'An error occurred when trying to translate the data using OpenAI API - parsed content is not a string',\n });\n\n return {\n success: false as const,\n };\n }\n\n translated.push(text);\n }\n }\n\n return {\n success: true as const,\n translatedTexts: translated,\n };\n } catch (e) {\n if (e instanceof Error) {\n req.payload.logger.info({\n message: 'An error occurred when trying to translate the data using OpenAI API',\n originalErr: e.message,\n });\n }\n\n return { success: false as const };\n }\n },\n };\n};\n"],"names":["chunkArray","defaultPrompt","localeFrom","localeTo","texts","JSON","stringify","openAIResolver","apiKey","baseUrl","chunkLength","model","prompt","key","resolve","req","apiUrl","response","Promise","all","map","fetch","body","messages","content","role","headers","Authorization","method","then","res","data","json","ok","payload","logger","info","message","openAIresponse","success","translated","choices","error","translatedChunk","parse","Array","isArray","fullContent","text","chunkData","push","translatedTexts","e","Error","originalErr"],"mappings":"AAAA,SAASA,UAAU,QAAQ,sBAAsB;AAgCjD,MAAMC,gBAA8B,CAAC,EAAEC,UAAU,EAAEC,QAAQ,EAAEC,KAAK,EAAE;IAClE,OAAO,CAAC,kCAA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IAAI,CAACA,SAAS;wBACZT,IAAImB,OAAO,CAACC,MAAM,CAACO,KAAK,CACtB;wBAGF,OAAO;4BACLH,SAAS;wBACX;oBACF;oBAEA,MAAMI,kBAA4BtC,KAAKuC,KAAK,CAACpB;oBAE7C,IAAI,CAACqB,MAAMC,OAAO,CAACH,kBAAkB;wBACnC5B,IAAImB,OAAO,CAACC,MAAM,CAACO,KAAK,CAAC;4BACvBX,MAAMY;4BACNI,aAAavB;4BACba,SACE;wBACJ;wBAEA,OAAO;4BACLE,SAAS;wBACX;oBACF;oBAEA,KAAK,MAAMS,QAAQL,gBAAiB;wBAClC,IAAIK,QAAQ,OAAOA,SAAS,UAAU;4BACpCjC,IAAImB,OAAO,CAACC,MAAM,CAACO,KAAK,CAAC;gCACvBO,WAAWN;gCACXZ,MAAMiB;gCACND,aAAavB;gCACba,SACE;4BACJ;4BAEA,OAAO;gCACLE,SAAS;4BACX;wBACF;wBAEAC,WAAWU,IAAI,CAACF;oBAClB;gBACF;gBAEA,OAAO;oBACLT,SAAS;oBACTY,iBAAiBX;gBACnB;YACF,EAAE,OAAOY,GAAG;gBACV,IAAIA,aAAaC,OAAO;oBACtBtC,IAAImB,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC;wBACtBC,SAAS;wBACTiB,aAAaF,EAAEf,OAAO;oBACxB;gBACF;gBAEA,OAAO;oBAAEE,SAAS;gBAAe;YACnC;QACF;IACF;AACF,EAAE"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"findEntityWithConfig.d.ts","sourceRoot":"","sources":["../../src/translate/findEntityWithConfig.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
1
|
+
{"version":3,"file":"findEntityWithConfig.d.ts","sourceRoot":"","sources":["../../src/translate/findEntityWithConfig.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,cAAc,EACd,yBAAyB,EACzB,qBAAqB,EACrB,UAAU,EACX,MAAM,SAAS,CAAC;AAGjB,KAAK,IAAI,GAAG;IACV,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,GAAG,EAAE,cAAc,CAAC;CACrB,CAAC;AAOF,eAAO,MAAM,oBAAoB,SACzB,IAAI,KACT,OAAO,CAAC;IACT,MAAM,EAAE,yBAAyB,GAAG,qBAAqB,CAAC;IAC1D,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,UAAU,CAAC;CAC3C,CA0CA,CAAC"}
|
@@ -11,17 +11,17 @@ export const findEntityWithConfig = async (args)=>{
|
|
11
11
|
if (!entityConfig) throw new APIError('Bad Request', 400);
|
12
12
|
const docPromise = isGlobal ? payload.findGlobal({
|
13
13
|
depth: 0,
|
14
|
-
fallbackLocale:
|
15
|
-
locale,
|
14
|
+
fallbackLocale: undefined,
|
15
|
+
locale: locale,
|
16
16
|
overrideAccess,
|
17
17
|
req,
|
18
18
|
slug: args.globalSlug
|
19
19
|
}) : payload.findByID({
|
20
20
|
collection: collectionSlug,
|
21
21
|
depth: 0,
|
22
|
-
fallbackLocale:
|
22
|
+
fallbackLocale: undefined,
|
23
23
|
id: id,
|
24
|
-
locale,
|
24
|
+
locale: locale,
|
25
25
|
overrideAccess,
|
26
26
|
req
|
27
27
|
});
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/translate/findEntityWithConfig.ts"],"sourcesContent":["import type {\n PayloadRequest,\n SanitizedCollectionConfig,\n SanitizedGlobalConfig,\n TypeWithID,\n} from 'payload';\nimport { APIError } from 'payload';\n\ntype Args = {\n collectionSlug?: string;\n globalSlug?: string;\n id?: number | string;\n locale: string;\n overrideAccess?: boolean;\n req: PayloadRequest;\n};\n\nconst findConfigBySlug = (\n slug: string,\n enities: SanitizedCollectionConfig[] | SanitizedGlobalConfig[],\n) => enities.find((entity) => entity.slug === slug);\n\nexport const findEntityWithConfig = async (\n args: Args,\n): Promise<{\n config: SanitizedCollectionConfig | SanitizedGlobalConfig;\n doc: Record<string, unknown> & TypeWithID;\n}> => {\n const { collectionSlug, globalSlug, id, locale, overrideAccess, req } = args;\n\n if (!collectionSlug && !globalSlug) throw new APIError('Bad Request', 400);\n\n const { payload } = req;\n\n const { config } = payload;\n\n const isGlobal = !!globalSlug;\n\n if (!isGlobal && !id) throw new APIError('Bad Request', 400);\n\n const entityConfig = isGlobal\n ? findConfigBySlug(globalSlug, config.globals)\n : findConfigBySlug(collectionSlug as string, config.collections);\n\n if (!entityConfig) throw new APIError('Bad Request', 400);\n\n const docPromise = isGlobal\n ? payload.findGlobal({\n depth: 0,\n fallbackLocale:
|
1
|
+
{"version":3,"sources":["../../src/translate/findEntityWithConfig.ts"],"sourcesContent":["import type {\n CollectionSlug,\n GlobalSlug,\n PayloadRequest,\n SanitizedCollectionConfig,\n SanitizedGlobalConfig,\n TypeWithID,\n} from 'payload';\nimport { APIError } from 'payload';\n\ntype Args = {\n collectionSlug?: string;\n globalSlug?: string;\n id?: number | string;\n locale: string;\n overrideAccess?: boolean;\n req: PayloadRequest;\n};\n\nconst findConfigBySlug = (\n slug: string,\n enities: SanitizedCollectionConfig[] | SanitizedGlobalConfig[],\n) => enities.find((entity) => entity.slug === slug);\n\nexport const findEntityWithConfig = async (\n args: Args,\n): Promise<{\n config: SanitizedCollectionConfig | SanitizedGlobalConfig;\n doc: Record<string, unknown> & TypeWithID;\n}> => {\n const { collectionSlug, globalSlug, id, locale, overrideAccess, req } = args;\n\n if (!collectionSlug && !globalSlug) throw new APIError('Bad Request', 400);\n\n const { payload } = req;\n\n const { config } = payload;\n\n const isGlobal = !!globalSlug;\n\n if (!isGlobal && !id) throw new APIError('Bad Request', 400);\n\n const entityConfig = isGlobal\n ? findConfigBySlug(globalSlug, config.globals)\n : findConfigBySlug(collectionSlug as string, config.collections);\n\n if (!entityConfig) throw new APIError('Bad Request', 400);\n\n const docPromise = isGlobal\n ? payload.findGlobal({\n depth: 0,\n fallbackLocale: undefined,\n locale: locale as any,\n overrideAccess,\n req,\n slug: args.globalSlug as GlobalSlug,\n })\n : payload.findByID({\n collection: collectionSlug as CollectionSlug,\n depth: 0,\n fallbackLocale: undefined,\n id: id as number | string,\n locale: locale as any,\n overrideAccess,\n req,\n });\n\n return {\n config: entityConfig,\n doc: (await docPromise) as any,\n };\n};\n"],"names":["APIError","findConfigBySlug","slug","enities","find","entity","findEntityWithConfig","args","collectionSlug","globalSlug","id","locale","overrideAccess","req","payload","config","isGlobal","entityConfig","globals","collections","docPromise","findGlobal","depth","fallbackLocale","undefined","findByID","collection","doc"],"mappings":"AAQA,SAASA,QAAQ,QAAQ,UAAU;AAWnC,MAAMC,mBAAmB,CACvBC,MACAC,UACGA,QAAQC,IAAI,CAAC,CAACC,SAAWA,OAAOH,IAAI,KAAKA;AAE9C,OAAO,MAAMI,uBAAuB,OAClCC;IAKA,MAAM,EAAEC,cAAc,EAAEC,UAAU,EAAEC,EAAE,EAAEC,MAAM,EAAEC,cAAc,EAAEC,GAAG,EAAE,GAAGN;IAExE,IAAI,CAACC,kBAAkB,CAACC,YAAY,MAAM,IAAIT,SAAS,eAAe;IAEtE,MAAM,EAAEc,OAAO,EAAE,GAAGD;IAEpB,MAAM,EAAEE,MAAM,EAAE,GAAGD;IAEnB,MAAME,WAAW,CAAC,CAACP;IAEnB,IAAI,CAACO,YAAY,CAACN,IAAI,MAAM,IAAIV,SAAS,eAAe;IAExD,MAAMiB,eAAeD,WACjBf,iBAAiBQ,YAAYM,OAAOG,OAAO,IAC3CjB,iBAAiBO,gBAA0BO,OAAOI,WAAW;IAEjE,IAAI,CAACF,cAAc,MAAM,IAAIjB,SAAS,eAAe;IAErD,MAAMoB,aAAaJ,WACfF,QAAQO,UAAU,CAAC;QACjBC,OAAO;QACPC,gBAAgBC;QAChBb,QAAQA;QACRC;QACAC;QACAX,MAAMK,KAAKE,UAAU;IACvB,KACAK,QAAQW,QAAQ,CAAC;QACfC,YAAYlB;QACZc,OAAO;QACPC,gBAAgBC;QAChBd,IAAIA;QACJC,QAAQA;QACRC;QACAC;IACF;IAEJ,OAAO;QACLE,QAAQE;QACRU,KAAM,MAAMP;IACd;AACF,EAAE"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"operation.d.ts","sourceRoot":"","sources":["../../src/translate/operation.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"operation.d.ts","sourceRoot":"","sources":["../../src/translate/operation.ts"],"names":[],"mappings":"AACA,OAAO,EAAY,KAAK,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,SAAS,CAAC;AAKtE,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAoB,MAAM,SAAS,CAAC;AAGhF,MAAM,MAAM,sBAAsB,GAAG,CACjC;IACE,OAAO,EAAE,OAAO,CAAC;CAClB,GACD;IACE,GAAG,EAAE,cAAc,CAAC;CACrB,CACJ,GACC,aAAa,CAAC;AAEhB,eAAO,MAAM,kBAAkB,SAAgB,sBAAsB,6BAyFpE,CAAC"}
|
@@ -1,3 +1,4 @@
|
|
1
|
+
import he from 'he';
|
1
2
|
import { APIError } from 'payload';
|
2
3
|
import { findEntityWithConfig } from './findEntityWithConfig';
|
3
4
|
import { traverseFields } from './traverseFields';
|
@@ -49,7 +50,8 @@ export const translateOperation = async (args)=>{
|
|
49
50
|
};
|
50
51
|
} else {
|
51
52
|
resolveResult.translatedTexts.forEach((translated, index)=>{
|
52
|
-
|
53
|
+
const formattedValue = he.decode(translated);
|
54
|
+
valuesToTranslate[index].onTranslate(formattedValue);
|
53
55
|
});
|
54
56
|
if (args.update) {
|
55
57
|
await updateEntity({
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/translate/operation.ts"],"sourcesContent":["import { APIError, type Payload, type PayloadRequest } from 'payload';\n\nimport type { TranslateResolver } from '../resolvers/types';\nimport { findEntityWithConfig } from './findEntityWithConfig';\nimport { traverseFields } from './traverseFields';\nimport type { TranslateArgs, TranslateResult, ValueToTranslate } from './types';\nimport { updateEntity } from './updateEntity';\n\nexport type TranslateOperationArgs = (\n | {\n payload: Payload;\n }\n | {\n req: PayloadRequest;\n }\n) &\n TranslateArgs;\n\nexport const translateOperation = async (args: TranslateOperationArgs) => {\n const req: PayloadRequest =\n 'req' in args\n ? args.req\n : ({\n payload: args.payload,\n } as PayloadRequest);\n\n const { collectionSlug, globalSlug, id, locale, localeFrom, overrideAccess } = args;\n\n const { config, doc: dataFrom } = await findEntityWithConfig({\n collectionSlug,\n globalSlug,\n id,\n locale: localeFrom,\n req,\n });\n\n const resolver = (\n (req.payload.config.custom?.translator?.resolvers as TranslateResolver[]) ?? []\n ).find((each) => each.key === args.resolver);\n\n if (!resolver) throw new APIError(`Resolver with the key ${args.resolver} was not found`);\n\n const valuesToTranslate: ValueToTranslate[] = [];\n\n let translatedData = args.data;\n\n if (!translatedData) {\n const { doc } = await findEntityWithConfig({\n collectionSlug,\n globalSlug,\n id,\n locale,\n overrideAccess,\n req,\n });\n\n translatedData = doc;\n }\n\n traverseFields({\n dataFrom,\n emptyOnly: args.emptyOnly,\n fields: config.fields,\n translatedData,\n valuesToTranslate,\n });\n\n const resolveResult = await resolver.resolve({\n localeFrom: args.localeFrom,\n localeTo: args.locale,\n req,\n texts: valuesToTranslate.map((each) => each.value),\n });\n\n let result: TranslateResult;\n\n if (!resolveResult.success) {\n result = {\n success: false,\n };\n } else {\n resolveResult.translatedTexts.forEach((translated, index) => {\n valuesToTranslate[index].onTranslate(
|
1
|
+
{"version":3,"sources":["../../src/translate/operation.ts"],"sourcesContent":["import he from 'he';\nimport { APIError, type Payload, type PayloadRequest } from 'payload';\n\nimport type { TranslateResolver } from '../resolvers/types';\nimport { findEntityWithConfig } from './findEntityWithConfig';\nimport { traverseFields } from './traverseFields';\nimport type { TranslateArgs, TranslateResult, ValueToTranslate } from './types';\nimport { updateEntity } from './updateEntity';\n\nexport type TranslateOperationArgs = (\n | {\n payload: Payload;\n }\n | {\n req: PayloadRequest;\n }\n) &\n TranslateArgs;\n\nexport const translateOperation = async (args: TranslateOperationArgs) => {\n const req: PayloadRequest =\n 'req' in args\n ? args.req\n : ({\n payload: args.payload,\n } as PayloadRequest);\n\n const { collectionSlug, globalSlug, id, locale, localeFrom, overrideAccess } = args;\n\n const { config, doc: dataFrom } = await findEntityWithConfig({\n collectionSlug,\n globalSlug,\n id,\n locale: localeFrom,\n req,\n });\n\n const resolver = (\n (req.payload.config.custom?.translator?.resolvers as TranslateResolver[]) ?? []\n ).find((each) => each.key === args.resolver);\n\n if (!resolver) throw new APIError(`Resolver with the key ${args.resolver} was not found`);\n\n const valuesToTranslate: ValueToTranslate[] = [];\n\n let translatedData = args.data;\n\n if (!translatedData) {\n const { doc } = await findEntityWithConfig({\n collectionSlug,\n globalSlug,\n id,\n locale,\n overrideAccess,\n req,\n });\n\n translatedData = doc;\n }\n\n traverseFields({\n dataFrom,\n emptyOnly: args.emptyOnly,\n fields: config.fields,\n translatedData,\n valuesToTranslate,\n });\n\n const resolveResult = await resolver.resolve({\n localeFrom: args.localeFrom,\n localeTo: args.locale,\n req,\n texts: valuesToTranslate.map((each) => each.value),\n });\n\n let result: TranslateResult;\n\n if (!resolveResult.success) {\n result = {\n success: false,\n };\n } else {\n resolveResult.translatedTexts.forEach((translated, index) => {\n const formattedValue = he.decode(translated);\n\n valuesToTranslate[index].onTranslate(formattedValue);\n });\n\n if (args.update) {\n await updateEntity({\n collectionSlug,\n data: translatedData,\n depth: 0,\n globalSlug,\n id,\n locale,\n overrideAccess,\n req,\n });\n }\n\n result = {\n success: true,\n translatedData,\n };\n }\n\n return result;\n};\n"],"names":["he","APIError","findEntityWithConfig","traverseFields","updateEntity","translateOperation","args","req","payload","collectionSlug","globalSlug","id","locale","localeFrom","overrideAccess","config","doc","dataFrom","resolver","custom","translator","resolvers","find","each","key","valuesToTranslate","translatedData","data","emptyOnly","fields","resolveResult","resolve","localeTo","texts","map","value","result","success","translatedTexts","forEach","translated","index","formattedValue","decode","onTranslate","update","depth"],"mappings":"AAAA,OAAOA,QAAQ,KAAK;AACpB,SAASC,QAAQ,QAA2C,UAAU;AAGtE,SAASC,oBAAoB,QAAQ,yBAAyB;AAC9D,SAASC,cAAc,QAAQ,mBAAmB;AAElD,SAASC,YAAY,QAAQ,iBAAiB;AAY9C,OAAO,MAAMC,qBAAqB,OAAOC;IACvC,MAAMC,MACJ,SAASD,OACLA,KAAKC,GAAG,GACP;QACCC,SAASF,KAAKE,OAAO;IACvB;IAEN,MAAM,EAAEC,cAAc,EAAEC,UAAU,EAAEC,EAAE,EAAEC,MAAM,EAAEC,UAAU,EAAEC,cAAc,EAAE,GAAGR;IAE/E,MAAM,EAAES,MAAM,EAAEC,KAAKC,QAAQ,EAAE,GAAG,MAAMf,qBAAqB;QAC3DO;QACAC;QACAC;QACAC,QAAQC;QACRN;IACF;IAEA,MAAMW,WAAW,AACf,CAAA,AAACX,IAAIC,OAAO,CAACO,MAAM,CAACI,MAAM,EAAEC,YAAYC,aAAqC,EAAE,AAAD,EAC9EC,IAAI,CAAC,CAACC,OAASA,KAAKC,GAAG,KAAKlB,KAAKY,QAAQ;IAE3C,IAAI,CAACA,UAAU,MAAM,IAAIjB,SAAS,CAAC,sBAAsB,EAAEK,KAAKY,QAAQ,CAAC,cAAc,CAAC;IAExF,MAAMO,oBAAwC,EAAE;IAEhD,IAAIC,iBAAiBpB,KAAKqB,IAAI;IAE9B,IAAI,CAACD,gBAAgB;QACnB,MAAM,EAAEV,GAAG,EAAE,GAAG,MAAMd,qBAAqB;YACzCO;YACAC;YACAC;YACAC;YACAE;YACAP;QACF;QAEAmB,iBAAiBV;IACnB;IAEAb,eAAe;QACbc;QACAW,WAAWtB,KAAKsB,SAAS;QACzBC,QAAQd,OAAOc,MAAM;QACrBH;QACAD;IACF;IAEA,MAAMK,gBAAgB,MAAMZ,SAASa,OAAO,CAAC;QAC3ClB,YAAYP,KAAKO,UAAU;QAC3BmB,UAAU1B,KAAKM,MAAM;QACrBL;QACA0B,OAAOR,kBAAkBS,GAAG,CAAC,CAACX,OAASA,KAAKY,KAAK;IACnD;IAEA,IAAIC;IAEJ,IAAI,CAACN,cAAcO,OAAO,EAAE;QAC1BD,SAAS;YACPC,SAAS;QACX;IACF,OAAO;QACLP,cAAcQ,eAAe,CAACC,OAAO,CAAC,CAACC,YAAYC;YACjD,MAAMC,iBAAiB1C,GAAG2C,MAAM,CAACH;YAEjCf,iBAAiB,CAACgB,MAAM,CAACG,WAAW,CAACF;QACvC;QAEA,IAAIpC,KAAKuC,MAAM,EAAE;YACf,MAAMzC,aAAa;gBACjBK;gBACAkB,MAAMD;gBACNoB,OAAO;gBACPpC;gBACAC;gBACAC;gBACAE;gBACAP;YACF;QACF;QAEA6B,SAAS;YACPC,SAAS;YACTX;QACF;IACF;IAEA,OAAOU;AACT,EAAE"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"traverseFields.d.ts","sourceRoot":"","sources":["../../src/translate/traverseFields.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAKrC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,eAAO,MAAM,cAAc,iIASxB;IACD,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1C,qBAAqB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChD,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACxC,iBAAiB,EAAE,gBAAgB,EAAE,CAAC;CACvC,
|
1
|
+
{"version":3,"file":"traverseFields.d.ts","sourceRoot":"","sources":["../../src/translate/traverseFields.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAKrC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAEhD,eAAO,MAAM,cAAc,iIASxB;IACD,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,EAAE,KAAK,EAAE,CAAC;IAChB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1C,qBAAqB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChD,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACxC,iBAAiB,EAAE,gBAAgB,EAAE,CAAC;CACvC,SAgPA,CAAC"}
|
@@ -5,10 +5,10 @@ import { traverseRichText } from './traverseRichText';
|
|
5
5
|
export const traverseFields = ({ dataFrom, emptyOnly, fields, localizedParent, siblingDataFrom, siblingDataTranslated, translatedData, valuesToTranslate })=>{
|
6
6
|
siblingDataFrom = siblingDataFrom ?? dataFrom;
|
7
7
|
siblingDataTranslated = siblingDataTranslated ?? translatedData;
|
8
|
-
|
8
|
+
for (const field of fields){
|
9
9
|
switch(field.type){
|
10
10
|
case 'tabs':
|
11
|
-
field.tabs
|
11
|
+
for (const tab of field.tabs){
|
12
12
|
const hasName = tabHasName(tab);
|
13
13
|
const tabDataFrom = hasName ? siblingDataFrom[tab.name] : siblingDataFrom;
|
14
14
|
if (!tabDataFrom) return;
|
@@ -23,74 +23,80 @@ export const traverseFields = ({ dataFrom, emptyOnly, fields, localizedParent, s
|
|
23
23
|
translatedData,
|
24
24
|
valuesToTranslate
|
25
25
|
});
|
26
|
-
}
|
26
|
+
}
|
27
27
|
break;
|
28
28
|
case 'group':
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
dataFrom,
|
34
|
-
emptyOnly,
|
35
|
-
fields: field.fields,
|
36
|
-
localizedParent: field.localized,
|
37
|
-
siblingDataFrom: groupDataFrom,
|
38
|
-
siblingDataTranslated: groupDataTranslated,
|
39
|
-
translatedData,
|
40
|
-
valuesToTranslate
|
41
|
-
});
|
42
|
-
break;
|
43
|
-
case 'array':
|
44
|
-
const arrayDataFrom = siblingDataFrom[field.name];
|
45
|
-
if (isEmpty(arrayDataFrom)) break;
|
46
|
-
let arrayDataTranslated = siblingDataTranslated[field.name] ?? [];
|
47
|
-
if (field.localized || localizedParent) {
|
48
|
-
if (arrayDataTranslated.length > 0 && emptyOnly) break;
|
49
|
-
arrayDataTranslated = arrayDataFrom.map(()=>({
|
50
|
-
id: ObjectID().toHexString()
|
51
|
-
}));
|
52
|
-
}
|
53
|
-
arrayDataTranslated.forEach((item, index)=>{
|
29
|
+
{
|
30
|
+
const groupDataFrom = siblingDataFrom[field.name];
|
31
|
+
if (!groupDataFrom) break;
|
32
|
+
const groupDataTranslated = siblingDataTranslated[field.name] ?? {};
|
54
33
|
traverseFields({
|
55
34
|
dataFrom,
|
56
35
|
emptyOnly,
|
57
36
|
fields: field.fields,
|
58
|
-
localizedParent:
|
59
|
-
siblingDataFrom:
|
60
|
-
siblingDataTranslated:
|
37
|
+
localizedParent: field.localized,
|
38
|
+
siblingDataFrom: groupDataFrom,
|
39
|
+
siblingDataTranslated: groupDataTranslated,
|
61
40
|
translatedData,
|
62
41
|
valuesToTranslate
|
63
42
|
});
|
64
|
-
|
65
|
-
siblingDataTranslated[field.name] = arrayDataTranslated;
|
66
|
-
break;
|
67
|
-
case 'blocks':
|
68
|
-
const blocksDataFrom = siblingDataFrom[field.name];
|
69
|
-
if (isEmpty(blocksDataFrom)) break;
|
70
|
-
let blocksDataTranslated = siblingDataTranslated[field.name] ?? [];
|
71
|
-
if (field.localized || localizedParent) {
|
72
|
-
if (blocksDataTranslated.length > 0 && emptyOnly) break;
|
73
|
-
blocksDataTranslated = blocksDataFrom.map(({ blockType })=>({
|
74
|
-
blockType,
|
75
|
-
id: ObjectID().toHexString()
|
76
|
-
}));
|
43
|
+
break;
|
77
44
|
}
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
45
|
+
case 'array':
|
46
|
+
{
|
47
|
+
const arrayDataFrom = siblingDataFrom[field.name];
|
48
|
+
if (isEmpty(arrayDataFrom)) break;
|
49
|
+
let arrayDataTranslated = siblingDataTranslated[field.name] ?? [];
|
50
|
+
if (field.localized || localizedParent) {
|
51
|
+
if (arrayDataTranslated.length > 0 && emptyOnly) break;
|
52
|
+
arrayDataTranslated = arrayDataFrom.map(()=>({
|
53
|
+
id: ObjectID().toHexString()
|
54
|
+
}));
|
55
|
+
}
|
56
|
+
arrayDataTranslated.forEach((item, index)=>{
|
57
|
+
traverseFields({
|
58
|
+
dataFrom,
|
59
|
+
emptyOnly,
|
60
|
+
fields: field.fields,
|
61
|
+
localizedParent: localizedParent ?? field.localized,
|
62
|
+
siblingDataFrom: arrayDataFrom[index],
|
63
|
+
siblingDataTranslated: item,
|
64
|
+
translatedData,
|
65
|
+
valuesToTranslate
|
66
|
+
});
|
90
67
|
});
|
91
|
-
|
92
|
-
|
93
|
-
|
68
|
+
siblingDataTranslated[field.name] = arrayDataTranslated;
|
69
|
+
break;
|
70
|
+
}
|
71
|
+
case 'blocks':
|
72
|
+
{
|
73
|
+
const blocksDataFrom = siblingDataFrom[field.name];
|
74
|
+
if (isEmpty(blocksDataFrom)) break;
|
75
|
+
let blocksDataTranslated = siblingDataTranslated[field.name] ?? [];
|
76
|
+
if (field.localized || localizedParent) {
|
77
|
+
if (blocksDataTranslated.length > 0 && emptyOnly) break;
|
78
|
+
blocksDataTranslated = blocksDataFrom.map(({ blockType })=>({
|
79
|
+
blockType,
|
80
|
+
id: ObjectID().toHexString()
|
81
|
+
}));
|
82
|
+
}
|
83
|
+
blocksDataTranslated.forEach((item, index)=>{
|
84
|
+
const block = field.blocks.find((each)=>each.slug === item.blockType);
|
85
|
+
if (!block) return;
|
86
|
+
traverseFields({
|
87
|
+
dataFrom,
|
88
|
+
emptyOnly,
|
89
|
+
fields: block.fields,
|
90
|
+
localizedParent: localizedParent ?? field.localized,
|
91
|
+
siblingDataFrom: blocksDataFrom[index],
|
92
|
+
siblingDataTranslated: item,
|
93
|
+
translatedData,
|
94
|
+
valuesToTranslate
|
95
|
+
});
|
96
|
+
});
|
97
|
+
siblingDataTranslated[field.name] = blocksDataTranslated;
|
98
|
+
break;
|
99
|
+
}
|
94
100
|
case 'collapsible':
|
95
101
|
case 'row':
|
96
102
|
traverseFields({
|
@@ -135,30 +141,18 @@ export const traverseFields = ({ dataFrom, emptyOnly, fields, localizedParent, s
|
|
135
141
|
});
|
136
142
|
break;
|
137
143
|
case 'richText':
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
valuesToTranslate.push({
|
151
|
-
onTranslate: (translated)=>{
|
152
|
-
siblingData.text = translated;
|
153
|
-
},
|
154
|
-
value: siblingData.text
|
155
|
-
});
|
156
|
-
},
|
157
|
-
root
|
158
|
-
});
|
159
|
-
} else {
|
160
|
-
for (const root of siblingDataTranslated[field.name]){
|
161
|
-
traverseRichText({
|
144
|
+
{
|
145
|
+
if (!(field.localized || localizedParent) || isEmpty(siblingDataFrom[field.name])) break;
|
146
|
+
if (emptyOnly && siblingDataTranslated[field.name]) break;
|
147
|
+
const richTextDataFrom = siblingDataFrom[field.name];
|
148
|
+
siblingDataTranslated[field.name] = richTextDataFrom;
|
149
|
+
if (!richTextDataFrom) break;
|
150
|
+
const isSlate = Array.isArray(richTextDataFrom);
|
151
|
+
const isLexical = 'root' in richTextDataFrom;
|
152
|
+
if (!isSlate && !isLexical) break;
|
153
|
+
if (isLexical) {
|
154
|
+
const root = siblingDataTranslated[field.name]?.root;
|
155
|
+
if (root) traverseRichText({
|
162
156
|
onText: (siblingData)=>{
|
163
157
|
valuesToTranslate.push({
|
164
158
|
onTranslate: (translated)=>{
|
@@ -167,15 +161,29 @@ export const traverseFields = ({ dataFrom, emptyOnly, fields, localizedParent, s
|
|
167
161
|
value: siblingData.text
|
168
162
|
});
|
169
163
|
},
|
170
|
-
root
|
164
|
+
root
|
171
165
|
});
|
166
|
+
} else {
|
167
|
+
for (const root of siblingDataTranslated[field.name]){
|
168
|
+
traverseRichText({
|
169
|
+
onText: (siblingData)=>{
|
170
|
+
valuesToTranslate.push({
|
171
|
+
onTranslate: (translated)=>{
|
172
|
+
siblingData.text = translated;
|
173
|
+
},
|
174
|
+
value: siblingData.text
|
175
|
+
});
|
176
|
+
},
|
177
|
+
root: root
|
178
|
+
});
|
179
|
+
}
|
172
180
|
}
|
181
|
+
break;
|
173
182
|
}
|
174
|
-
break;
|
175
183
|
default:
|
176
184
|
break;
|
177
185
|
}
|
178
|
-
}
|
186
|
+
}
|
179
187
|
};
|
180
188
|
|
181
189
|
//# sourceMappingURL=traverseFields.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/translate/traverseFields.ts"],"sourcesContent":["import ObjectID from 'bson-objectid';\nimport type { Field } from 'payload';\nimport { tabHasName } from 'payload/shared';\n\nimport { isEmpty } from '../utils/isEmpty';\nimport { traverseRichText } from './traverseRichText';\nimport type { ValueToTranslate } from './types';\n\nexport const traverseFields = ({\n dataFrom,\n emptyOnly,\n fields,\n localizedParent,\n siblingDataFrom,\n siblingDataTranslated,\n translatedData,\n valuesToTranslate,\n}: {\n dataFrom: Record<string, unknown>;\n emptyOnly?: boolean;\n fields: Field[];\n localizedParent?: boolean;\n siblingDataFrom?: Record<string, unknown>;\n siblingDataTranslated?: Record<string, unknown>;\n translatedData: Record<string, unknown>;\n valuesToTranslate: ValueToTranslate[];\n}) => {\n siblingDataFrom = siblingDataFrom ?? dataFrom;\n siblingDataTranslated = siblingDataTranslated ?? translatedData;\n\n fields.forEach((field) => {\n switch (field.type) {\n case 'tabs':\n field.tabs.forEach((tab) => {\n const hasName = tabHasName(tab);\n\n const tabDataFrom = hasName\n ? (siblingDataFrom[tab.name] as Record<string, unknown>)\n : siblingDataFrom;\n\n if (!tabDataFrom) return;\n\n const tabDataTranslated = hasName\n ? (siblingDataTranslated[tab.name] as Record<string, unknown>) ?? {}\n : siblingDataTranslated;\n\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: tab.fields,\n localizedParent: tab.localized,\n siblingDataFrom: tabDataFrom,\n siblingDataTranslated: tabDataTranslated,\n translatedData,\n valuesToTranslate,\n });\n });\n\n break;\n\n case 'group':\n const groupDataFrom = siblingDataFrom[field.name] as Record<string, unknown>;\n\n if (!groupDataFrom) break;\n\n const groupDataTranslated =\n (siblingDataTranslated[field.name] as Record<string, unknown>) ?? {};\n\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: field.fields,\n localizedParent: field.localized,\n siblingDataFrom: groupDataFrom,\n siblingDataTranslated: groupDataTranslated,\n translatedData,\n valuesToTranslate,\n });\n\n break;\n\n case 'array':\n const arrayDataFrom = siblingDataFrom[field.name] as {\n id: string;\n }[];\n\n if (isEmpty(arrayDataFrom)) break;\n\n let arrayDataTranslated =\n (siblingDataTranslated[field.name] as { id: string }[] | undefined) ?? [];\n\n if (field.localized || localizedParent) {\n if (arrayDataTranslated.length > 0 && emptyOnly) break;\n\n arrayDataTranslated = arrayDataFrom.map(() => ({\n id: ObjectID().toHexString(),\n }));\n }\n\n arrayDataTranslated.forEach((item, index) => {\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: field.fields,\n localizedParent: localizedParent ?? field.localized,\n siblingDataFrom: arrayDataFrom[index],\n siblingDataTranslated: item,\n translatedData,\n valuesToTranslate,\n });\n });\n\n siblingDataTranslated[field.name] = arrayDataTranslated;\n\n break;\n\n case 'blocks':\n const blocksDataFrom = siblingDataFrom[field.name] as { blockType: string; id: string }[];\n\n if (isEmpty(blocksDataFrom)) break;\n\n let blocksDataTranslated =\n (siblingDataTranslated[field.name] as { blockType: string; id: string }[] | undefined) ??\n [];\n\n if (field.localized || localizedParent) {\n if (blocksDataTranslated.length > 0 && emptyOnly) break;\n\n blocksDataTranslated = blocksDataFrom.map(({ blockType }) => ({\n blockType,\n id: ObjectID().toHexString(),\n }));\n }\n\n blocksDataTranslated.forEach((item, index) => {\n const block = field.blocks.find((each) => each.slug === item.blockType);\n\n if (!block) return;\n\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: block.fields,\n localizedParent: localizedParent ?? field.localized,\n siblingDataFrom: blocksDataFrom[index],\n siblingDataTranslated: item,\n translatedData,\n valuesToTranslate,\n });\n });\n\n siblingDataTranslated[field.name] = blocksDataTranslated;\n\n break;\n\n case 'collapsible':\n case 'row':\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: field.fields,\n localizedParent,\n siblingDataFrom,\n siblingDataTranslated,\n translatedData,\n valuesToTranslate,\n });\n break;\n\n // long ass cases here we have\n case 'date':\n case 'checkbox':\n case 'json':\n case 'code':\n case 'email':\n case 'number':\n case 'point':\n case 'radio':\n case 'relationship':\n case 'select':\n case 'upload':\n siblingDataTranslated[field.name] = siblingDataFrom[field.name];\n\n break;\n\n case 'text':\n case 'textarea':\n if (field.custom && typeof field.custom === 'object' && field.custom.translatorSkip) break;\n\n if (!(field.localized || localizedParent) || isEmpty(siblingDataFrom[field.name])) break;\n if (emptyOnly && siblingDataTranslated[field.name]) break;\n\n // do not translate the block ID or admin-facing label\n if (field.name === 'blockName' || field.name === 'id') {\n break;\n }\n\n valuesToTranslate.push({\n onTranslate: (translated: string) => {\n siblingDataTranslated[field.name] = translated;\n },\n value: siblingDataFrom[field.name],\n });\n break;\n\n case 'richText':\n if (!(field.localized || localizedParent) || isEmpty(siblingDataFrom[field.name])) break;\n if (emptyOnly && siblingDataTranslated[field.name]) break;\n\n const richTextDataFrom = siblingDataFrom[field.name] as object;\n\n siblingDataTranslated[field.name] = richTextDataFrom;\n\n if (!richTextDataFrom) break;\n\n const isSlate = Array.isArray(richTextDataFrom);\n\n const isLexical = 'root' in richTextDataFrom;\n\n if (!isSlate && !isLexical) break;\n\n if (isLexical) {\n const root = (siblingDataTranslated[field.name] as Record<string, unknown>)\n ?.root as Record<string, unknown>;\n\n if (root)\n traverseRichText({\n onText: (siblingData) => {\n valuesToTranslate.push({\n onTranslate: (translated: string) => {\n siblingData.text = translated;\n },\n value: siblingData.text,\n });\n },\n root,\n });\n } else {\n for (const root of siblingDataTranslated[field.name] as unknown[]) {\n traverseRichText({\n onText: (siblingData) => {\n valuesToTranslate.push({\n onTranslate: (translated: string) => {\n siblingData.text = translated;\n },\n value: siblingData.text,\n });\n },\n root: root as Record<string, unknown>,\n });\n }\n }\n\n break;\n\n default:\n break;\n }\n });\n};\n"],"names":["ObjectID","tabHasName","isEmpty","traverseRichText","traverseFields","dataFrom","emptyOnly","fields","localizedParent","siblingDataFrom","siblingDataTranslated","translatedData","valuesToTranslate","forEach","field","type","tabs","tab","hasName","tabDataFrom","name","tabDataTranslated","localized","groupDataFrom","groupDataTranslated","arrayDataFrom","arrayDataTranslated","length","map","id","toHexString","item","index","blocksDataFrom","blocksDataTranslated","blockType","block","blocks","find","each","slug","custom","translatorSkip","push","onTranslate","translated","value","richTextDataFrom","isSlate","Array","isArray","isLexical","root","onText","siblingData","text"],"mappings":"AAAA,OAAOA,cAAc,gBAAgB;AAErC,SAASC,UAAU,QAAQ,iBAAiB;AAE5C,SAASC,OAAO,QAAQ,mBAAmB;AAC3C,SAASC,gBAAgB,QAAQ,qBAAqB;AAGtD,OAAO,MAAMC,iBAAiB,CAAC,EAC7BC,QAAQ,EACRC,SAAS,EACTC,MAAM,EACNC,eAAe,EACfC,eAAe,EACfC,qBAAqB,EACrBC,cAAc,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IAAI,CAACC,WAAW,CAACG,WAAW;gBAE5B,IAAIA,WAAW;oBACb,MAAMC,OAAQ1C,qBAAqB,CAACI,MAAMM,IAAI,CAAC,EAC3CgC;oBAEJ,IAAIA,MACFjD,iBAAiB;wBACfkD,QAAQ,CAACC;4BACP1C,kBAAkB+B,IAAI,CAAC;gCACrBC,aAAa,CAACC;oCACZS,YAAYC,IAAI,GAAGV;gCACrB;gCACAC,OAAOQ,YAAYC,IAAI;4BACzB;wBACF;wBACAH;oBACF;gBACJ,OAAO;oBACL,KAAK,MAAMA,QAAQ1C,qBAAqB,CAACI,MAAMM,IAAI,CAAC,CAAe;wBACjEjB,iBAAiB;4BACfkD,QAAQ,CAACC;gCACP1C,kBAAkB+B,IAAI,CAAC;oCACrBC,aAAa,CAACC;wCACZS,YAAYC,IAAI,GAAGV;oCACrB;oCACAC,OAAOQ,YAAYC,IAAI;gCACzB;4BACF;4BACAH,MAAMA;wBACR;oBACF;gBACF;gBAEA;YAEF;gBACE;QACJ;IACF;AACF,EAAE"}
|
1
|
+
{"version":3,"sources":["../../src/translate/traverseFields.ts"],"sourcesContent":["import ObjectID from 'bson-objectid';\nimport type { Field } from 'payload';\nimport { tabHasName } from 'payload/shared';\n\nimport { isEmpty } from '../utils/isEmpty';\nimport { traverseRichText } from './traverseRichText';\nimport type { ValueToTranslate } from './types';\n\nexport const traverseFields = ({\n dataFrom,\n emptyOnly,\n fields,\n localizedParent,\n siblingDataFrom,\n siblingDataTranslated,\n translatedData,\n valuesToTranslate,\n}: {\n dataFrom: Record<string, unknown>;\n emptyOnly?: boolean;\n fields: Field[];\n localizedParent?: boolean;\n siblingDataFrom?: Record<string, unknown>;\n siblingDataTranslated?: Record<string, unknown>;\n translatedData: Record<string, unknown>;\n valuesToTranslate: ValueToTranslate[];\n}) => {\n siblingDataFrom = siblingDataFrom ?? dataFrom;\n siblingDataTranslated = siblingDataTranslated ?? translatedData;\n\n for (const field of fields) {\n switch (field.type) {\n case 'tabs':\n for (const tab of field.tabs) {\n const hasName = tabHasName(tab);\n\n const tabDataFrom = hasName\n ? (siblingDataFrom[tab.name] as Record<string, unknown>)\n : siblingDataFrom;\n\n if (!tabDataFrom) return;\n\n const tabDataTranslated = hasName\n ? ((siblingDataTranslated[tab.name] as Record<string, unknown>) ?? {})\n : siblingDataTranslated;\n\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: tab.fields,\n localizedParent: tab.localized,\n siblingDataFrom: tabDataFrom,\n siblingDataTranslated: tabDataTranslated,\n translatedData,\n valuesToTranslate,\n });\n }\n\n break;\n\n case 'group': {\n const groupDataFrom = siblingDataFrom[field.name] as Record<string, unknown>;\n\n if (!groupDataFrom) break;\n\n const groupDataTranslated =\n (siblingDataTranslated[field.name] as Record<string, unknown>) ?? {};\n\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: field.fields,\n localizedParent: field.localized,\n siblingDataFrom: groupDataFrom,\n siblingDataTranslated: groupDataTranslated,\n translatedData,\n valuesToTranslate,\n });\n\n break;\n }\n\n case 'array': {\n const arrayDataFrom = siblingDataFrom[field.name] as {\n id: string;\n }[];\n\n if (isEmpty(arrayDataFrom)) break;\n\n let arrayDataTranslated =\n (siblingDataTranslated[field.name] as { id: string }[] | undefined) ?? [];\n\n if (field.localized || localizedParent) {\n if (arrayDataTranslated.length > 0 && emptyOnly) break;\n\n arrayDataTranslated = arrayDataFrom.map(() => ({\n id: ObjectID().toHexString(),\n }));\n }\n\n arrayDataTranslated.forEach((item, index) => {\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: field.fields,\n localizedParent: localizedParent ?? field.localized,\n siblingDataFrom: arrayDataFrom[index],\n siblingDataTranslated: item,\n translatedData,\n valuesToTranslate,\n });\n });\n\n siblingDataTranslated[field.name] = arrayDataTranslated;\n\n break;\n }\n\n case 'blocks': {\n const blocksDataFrom = siblingDataFrom[field.name] as {\n blockType: string;\n id: string;\n }[];\n\n if (isEmpty(blocksDataFrom)) break;\n\n let blocksDataTranslated =\n (siblingDataTranslated[field.name] as { blockType: string; id: string }[] | undefined) ??\n [];\n\n if (field.localized || localizedParent) {\n if (blocksDataTranslated.length > 0 && emptyOnly) break;\n\n blocksDataTranslated = blocksDataFrom.map(({ blockType }) => ({\n blockType,\n id: ObjectID().toHexString(),\n }));\n }\n\n blocksDataTranslated.forEach((item, index) => {\n const block = field.blocks.find((each) => each.slug === item.blockType);\n\n if (!block) return;\n\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: block.fields,\n localizedParent: localizedParent ?? field.localized,\n siblingDataFrom: blocksDataFrom[index],\n siblingDataTranslated: item,\n translatedData,\n valuesToTranslate,\n });\n });\n\n siblingDataTranslated[field.name] = blocksDataTranslated;\n\n break;\n }\n\n case 'collapsible':\n case 'row':\n traverseFields({\n dataFrom,\n emptyOnly,\n fields: field.fields,\n localizedParent,\n siblingDataFrom,\n siblingDataTranslated,\n translatedData,\n valuesToTranslate,\n });\n break;\n\n // long ass cases here we have\n case 'date':\n case 'checkbox':\n case 'json':\n case 'code':\n case 'email':\n case 'number':\n case 'point':\n case 'radio':\n case 'relationship':\n case 'select':\n case 'upload':\n siblingDataTranslated[field.name] = siblingDataFrom[field.name];\n\n break;\n\n case 'text':\n case 'textarea':\n if (field.custom && typeof field.custom === 'object' && field.custom.translatorSkip) break;\n\n if (!(field.localized || localizedParent) || isEmpty(siblingDataFrom[field.name])) break;\n if (emptyOnly && siblingDataTranslated[field.name]) break;\n\n // do not translate the block ID or admin-facing label\n if (field.name === 'blockName' || field.name === 'id') {\n break;\n }\n\n valuesToTranslate.push({\n onTranslate: (translated: string) => {\n siblingDataTranslated[field.name] = translated;\n },\n value: siblingDataFrom[field.name],\n });\n break;\n\n case 'richText': {\n if (!(field.localized || localizedParent) || isEmpty(siblingDataFrom[field.name])) break;\n if (emptyOnly && siblingDataTranslated[field.name]) break;\n\n const richTextDataFrom = siblingDataFrom[field.name] as object;\n\n siblingDataTranslated[field.name] = richTextDataFrom;\n\n if (!richTextDataFrom) break;\n\n const isSlate = Array.isArray(richTextDataFrom);\n\n const isLexical = 'root' in richTextDataFrom;\n\n if (!isSlate && !isLexical) break;\n\n if (isLexical) {\n const root = (siblingDataTranslated[field.name] as Record<string, unknown>)\n ?.root as Record<string, unknown>;\n\n if (root)\n traverseRichText({\n onText: (siblingData) => {\n valuesToTranslate.push({\n onTranslate: (translated: string) => {\n siblingData.text = translated;\n },\n value: siblingData.text,\n });\n },\n root,\n });\n } else {\n for (const root of siblingDataTranslated[field.name] as unknown[]) {\n traverseRichText({\n onText: (siblingData) => {\n valuesToTranslate.push({\n onTranslate: (translated: string) => {\n siblingData.text = translated;\n },\n value: siblingData.text,\n });\n },\n root: root as Record<string, unknown>,\n });\n }\n }\n\n break;\n }\n\n default:\n break;\n }\n }\n};\n"],"names":["ObjectID","tabHasName","isEmpty","traverseRichText","traverseFields","dataFrom","emptyOnly","fields","localizedParent","siblingDataFrom","siblingDataTranslated","translatedData","valuesToTranslate","field","type","tab","tabs","hasName","tabDataFrom","name","tabDataTranslated","localized","groupDataFrom","groupDataTranslated","arrayDataFrom","arrayDataTranslated","length","map","id","toHexString","forEach","item","index","blocksDataFrom","blocksDataTranslated","blockType","block","blocks","find","each","slug","custom","translatorSkip","push","onTranslate","translated","value","richTextDataFrom","isSlate","Array","isArray","isLexical","root","onText","siblingData","text"],"mappings":"AAAA,OAAOA,cAAc,gBAAgB;AAErC,SAASC,UAAU,QAAQ,iBAAiB;AAE5C,SAASC,OAAO,QAAQ,mBAAmB;AAC3C,SAASC,gBAAgB,QAAQ,qBAAqB;AAGtD,OAAO,MAAMC,iBAAiB,CAAC,EAC7BC,QAAQ,EACRC,SAAS,EACTC,MAAM,EACNC,eAAe,EACfC,eAAe,EACfC,qBAAqB,EACrBC,cAAc,EACdC,iBAAiB,EAUlB;IACCH,kBAAkBA,mBAAmBJ;IACrCK,wBAAwBA,yBAAyBC;IAEjD,KAAK,MAAME,SAASN,OAAQ;QAC1B,OAAQM,MAAMC,IAAI;YAChB,KAAK;gBACH,KAAK,MAA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