fix: i18nInit doesn't have to be async, richtext i18n types (#5176)

This commit is contained in:
Alessio Gravili
2024-02-26 15:25:28 -05:00
committed by GitHub
parent 37177f1226
commit 75c4b4f234
11 changed files with 59 additions and 69 deletions

View File

@@ -69,7 +69,7 @@ export const createPayloadRequest = async ({
headers: request.headers,
})
const i18n = await initI18n({
const i18n = initI18n({
config: config.i18n,
context: 'api',
language,

View File

@@ -14,7 +14,7 @@ export const getNextT = async ({
config: SanitizedConfig
language?: string
}): Promise<TFunction> => {
const i18n = await initI18n({
const i18n = initI18n({
config: config.i18n,
context: 'client',
language: language || getRequestLanguage({ cookies: cookies(), headers: headers() }),

View File

@@ -68,7 +68,7 @@ export const initPage = async ({
config,
})
const i18n = await initI18n({
const i18n = initI18n({
config: config.i18n,
context: 'client',
language,

View File

@@ -33,7 +33,6 @@
"bson-objectid": "2.0.4",
"classnames": "^2.3.2",
"deep-equal": "2.2.3",
"i18next": "22.5.1",
"json-schema": "^0.4.0",
"lexical": "0.13.1",
"lodash": "4.17.21",

View File

@@ -1,4 +1,4 @@
import type { i18n } from 'i18next'
import type { I18n } from '@payloadcms/translations'
import type { SanitizedConfig } from 'payload/config'
import type { Field } from 'payload/types'
@@ -46,7 +46,7 @@ export type LinkFeatureProps = ExclusiveLinkCollectionsProps & {
* displayed in the link editor drawer.
*/
fields?:
| ((args: { config: SanitizedConfig; defaultFields: Field[]; i18n: i18n }) => Field[])
| ((args: { config: SanitizedConfig; defaultFields: Field[]; i18n: I18n }) => Field[])
| Field[]
}
@@ -59,7 +59,7 @@ export const LinkFeature = (props: LinkFeatureProps): FeatureProvider => {
FeaturesSectionWithEntries([
{
ChildComponent: () =>
// @ts-ignore-next-line
// @ts-expect-error-next-line
import('../../lexical/ui/icons/Link').then((module) => module.LinkIcon),
isActive: ({ selection }) => {
if ($isRangeSelection(selection)) {
@@ -99,6 +99,7 @@ export const LinkFeature = (props: LinkFeatureProps): FeatureProvider => {
},
nodes: [
{
type: LinkNode.getType(),
converters: {
html: {
converter: async ({ converters, node, parent }) => {
@@ -125,10 +126,10 @@ export const LinkFeature = (props: LinkFeatureProps): FeatureProvider => {
},
node: LinkNode,
populationPromises: [linkPopulationPromiseHOC(props)],
type: LinkNode.getType(),
// TODO: Add validation similar to upload for internal links and fields
},
{
type: AutoLinkNode.getType(),
converters: {
html: {
converter: async ({ converters, node, parent }) => {
@@ -158,31 +159,30 @@ export const LinkFeature = (props: LinkFeatureProps): FeatureProvider => {
},
node: AutoLinkNode,
populationPromises: [linkPopulationPromiseHOC(props)],
type: AutoLinkNode.getType(),
},
],
plugins: [
{
Component: () =>
// @ts-ignore-next-line
// @ts-expect-error-next-line
import('./plugins/link').then((module) => module.LinkPlugin),
position: 'normal',
},
{
Component: () =>
// @ts-ignore-next-line
// @ts-expect-error-next-line
import('./plugins/autoLink').then((module) => module.AutoLinkPlugin),
position: 'normal',
},
{
Component: () =>
// @ts-ignore-next-line
// @ts-expect-error-next-line
import('./plugins/clickableLink').then((module) => module.ClickableLinkPlugin),
position: 'normal',
},
{
Component: () =>
// @ts-ignore-next-line
// @ts-expect-error-next-line
import('./plugins/floatingLinkEditor').then((module) => {
const floatingLinkEditorPlugin = module.FloatingLinkEditorPlugin
return import('@payloadcms/ui').then((module) =>

View File

@@ -1,4 +1,4 @@
import type { i18n } from 'i18next'
import type { I18n } from '@payloadcms/translations'
import type { SanitizedConfig } from 'payload/config'
import type { Field, GroupField } from 'payload/types'
@@ -9,10 +9,10 @@ import { getBaseFields } from '../../drawer/baseFields'
*/
export function transformExtraFields(
customFieldSchema:
| ((args: { config: SanitizedConfig; defaultFields: Field[]; i18n: i18n }) => Field[])
| ((args: { config: SanitizedConfig; defaultFields: Field[]; i18n: I18n }) => Field[])
| Field[],
config: SanitizedConfig,
i18n: i18n,
i18n: I18n,
enabledCollections?: false | string[],
disabledCollections?: false | string[],
): Field[] {

View File

@@ -1,4 +1,4 @@
import type { i18n } from 'i18next'
import type { I18n } from '@payloadcms/translations'
import type { SanitizedConfig } from 'payload/config'
import type { Field } from 'payload/types'
import type { Editor } from 'slate'
@@ -16,9 +16,9 @@ export const wrapLink = (editor: Editor): void => {
const isCollapsed = selection && Range.isCollapsed(selection)
const link = {
type: 'link',
children: isCollapsed ? [{ text: '' }] : [],
newTab: false,
type: 'link',
url: undefined,
}
@@ -35,10 +35,10 @@ export const wrapLink = (editor: Editor): void => {
*/
export function transformExtraFields(
customFieldSchema:
| ((args: { config: SanitizedConfig; defaultFields: Field[]; i18n: i18n }) => Field[])
| ((args: { config: SanitizedConfig; defaultFields: Field[]; i18n: I18n }) => Field[])
| Field[],
config: SanitizedConfig,
i18n: i18n,
i18n: I18n,
): Field[] {
const baseFields: Field[] = getBaseFields(config)
@@ -68,6 +68,7 @@ export function transformExtraFields(
if (Array.isArray(customFieldSchema) || fields.length > 0) {
fields.push({
name: 'fields',
type: 'group',
admin: {
style: {
borderBottom: 0,
@@ -79,7 +80,6 @@ export function transformExtraFields(
fields: Array.isArray(customFieldSchema)
? customFieldSchema.concat(extraFields)
: extraFields,
type: 'group',
})
}
return fields

View File

@@ -1,4 +1,4 @@
import type { i18n as Ii18n } from 'i18next'
import type { I18n } from '@payloadcms/translations'
import type { SanitizedConfig } from 'payload/config'
import type { Field, RichTextFieldProps } from 'payload/types'
import type { Editor } from 'slate'
@@ -59,7 +59,7 @@ export type AdapterArguments = {
leaves?: RichTextLeaf[]
link?: {
fields?:
| ((args: { config: SanitizedConfig; defaultFields: Field[]; i18n: Ii18n }) => Field[])
| ((args: { config: SanitizedConfig; defaultFields: Field[]; i18n: I18n }) => Field[])
| Field[]
}
placeholder?: Record<string, string> | string

View File

@@ -48,4 +48,4 @@ export type InitI18n = (args: {
language?: string
translations: Translations
context: 'api' | 'client'
}) => Promise<I18n>
}) => I18n

View File

@@ -207,11 +207,11 @@ const initTFunction: InitTFunction = (args) => (key, vars) => {
function memoize<T>(fn: Function, keys: string[]): T {
const cacheMap = new Map()
return <T>async function (args) {
return <T>function (args) {
const cacheKey = keys.reduce((acc, key) => acc + args[key], '')
if (!cacheMap.has(cacheKey)) {
const result = await fn(args)
const result = fn(args)
cacheMap.set(cacheKey, result)
}
@@ -220,7 +220,7 @@ function memoize<T>(fn: Function, keys: string[]): T {
}
export const initI18n: InitI18n = memoize(
<InitI18n>(async ({ config, language = 'en', translations, context }) => {
<InitI18n>(({ config, language = 'en', translations, context }) => {
const i18n = {
fallbackLanguage: config.fallbackLanguage,
language: language || config.fallbackLanguage,

75
pnpm-lock.yaml generated
View File

@@ -920,7 +920,7 @@ importers:
version: 18.2.0
ts-node:
specifier: 10.9.1
version: 10.9.1(@swc/core@1.4.2)(@types/node@20.6.2)(typescript@5.2.2)
version: 10.9.1(@swc/core@1.4.2)(@types/node@16.18.83)(typescript@5.2.2)
packages/plugin-nested-docs:
devDependencies:
@@ -1141,9 +1141,6 @@ importers:
deep-equal:
specifier: 2.2.3
version: 2.2.3
i18next:
specifier: 22.5.1
version: 22.5.1
json-schema:
specifier: ^0.4.0
version: 0.4.0
@@ -9971,12 +9968,6 @@ packages:
hasBin: true
dev: true
/i18next@22.5.1:
resolution: {integrity: sha512-8TGPgM3pAD+VRsMtUMNknRz3kzqwp/gPALrWMsDnmC1mKqJwpWyooQRLMcbTwq8z8YwSmuj+ZYvc+xCuEpkssA==}
dependencies:
'@babel/runtime': 7.23.9
dev: false
/iconv-lite@0.4.24:
resolution: {integrity: sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==}
engines: {node: '>=0.10.0'}
@@ -15299,6 +15290,38 @@ packages:
yargs-parser: 21.1.1
dev: true
/ts-node@10.9.1(@swc/core@1.4.2)(@types/node@16.18.83)(typescript@5.2.2):
resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==}
hasBin: true
peerDependencies:
'@swc/core': '>=1.2.50'
'@swc/wasm': '>=1.2.50'
'@types/node': '*'
typescript: '>=2.7'
peerDependenciesMeta:
'@swc/core':
optional: true
'@swc/wasm':
optional: true
dependencies:
'@cspotcode/source-map-support': 0.8.1
'@swc/core': 1.4.2
'@tsconfig/node10': 1.0.9
'@tsconfig/node12': 1.0.11
'@tsconfig/node14': 1.0.3
'@tsconfig/node16': 1.0.4
'@types/node': 16.18.83
acorn: 8.11.3
acorn-walk: 8.3.2
arg: 4.1.3
create-require: 1.1.1
diff: 4.0.2
make-error: 1.3.6
typescript: 5.2.2
v8-compile-cache-lib: 3.0.1
yn: 3.1.1
dev: true
/ts-node@10.9.1(@swc/core@1.4.2)(@types/node@20.5.7)(typescript@5.2.2):
resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==}
hasBin: true
@@ -15330,38 +15353,6 @@ packages:
v8-compile-cache-lib: 3.0.1
yn: 3.1.1
/ts-node@10.9.1(@swc/core@1.4.2)(@types/node@20.6.2)(typescript@5.2.2):
resolution: {integrity: sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==}
hasBin: true
peerDependencies:
'@swc/core': '>=1.2.50'
'@swc/wasm': '>=1.2.50'
'@types/node': '*'
typescript: '>=2.7'
peerDependenciesMeta:
'@swc/core':
optional: true
'@swc/wasm':
optional: true
dependencies:
'@cspotcode/source-map-support': 0.8.1
'@swc/core': 1.4.2
'@tsconfig/node10': 1.0.9
'@tsconfig/node12': 1.0.11
'@tsconfig/node14': 1.0.3
'@tsconfig/node16': 1.0.4
'@types/node': 20.6.2
acorn: 8.11.3
acorn-walk: 8.3.2
arg: 4.1.3
create-require: 1.1.1
diff: 4.0.2
make-error: 1.3.6
typescript: 5.2.2
v8-compile-cache-lib: 3.0.1
yn: 3.1.1
dev: true
/ts-node@9.1.1(typescript@5.2.2):
resolution: {integrity: sha512-hPlt7ZACERQGf03M253ytLY3dHbGNGrAq9qIHWUY9XHYl1z7wYngSr3OQ5xmui8o2AaxsONxIzjafLUiWBo1Fg==}
engines: {node: '>=10.0.0'}