react-intlayer 2.0.0 → 2.0.2
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/dist/cjs/client/ContentEditionLayout.cjs.map +1 -1
- package/dist/cjs/client/{IntlayerClientProvider.cjs → IntlayerProvider.cjs} +14 -11
- package/dist/cjs/client/IntlayerProvider.cjs.map +1 -0
- package/dist/cjs/client/{IntlayerClientProvider.d.ts → IntlayerProvider.d.ts} +5 -3
- package/dist/cjs/client/getBrowserLocale.cjs +7 -14
- package/dist/cjs/client/getBrowserLocale.cjs.map +1 -1
- package/dist/cjs/client/index.cjs +6 -6
- package/dist/cjs/client/index.cjs.map +1 -1
- package/dist/cjs/client/index.d.ts +3 -2
- package/dist/cjs/client/useContent.d.ts +1 -1
- package/dist/cjs/client/useIntlayer.cjs +2 -2
- package/dist/cjs/client/useIntlayer.cjs.map +1 -1
- package/dist/cjs/client/useIntlayer.d.ts +3 -1
- package/dist/cjs/client/useLocale.cjs +2 -2
- package/dist/cjs/client/useLocale.cjs.map +1 -1
- package/dist/cjs/client/useLocaleBase.cjs +2 -2
- package/dist/cjs/client/useLocaleBase.cjs.map +1 -1
- package/dist/cjs/client/useLocaleCookie.cjs.map +1 -1
- package/dist/cjs/client/useTraduction.cjs +2 -2
- package/dist/cjs/client/useTraduction.cjs.map +1 -1
- package/dist/cjs/index.cjs +4 -4
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +3 -2
- package/dist/cjs/processDictionary/contentDictionary.cjs.map +1 -1
- package/dist/cjs/processDictionary/contentDictionary.d.ts +9 -9
- package/dist/cjs/processDictionary/index.cjs +69 -53
- package/dist/cjs/processDictionary/index.cjs.map +1 -1
- package/dist/cjs/processDictionary/index.d.ts +4 -4
- package/dist/cjs/server/IntlayerServerProvider.cjs +3 -4
- package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -1
- package/dist/cjs/server/IntlayerServerProvider.d.ts +6 -7
- package/dist/cjs/server/index.d.ts +1 -0
- package/dist/cjs/server/serverContext.cjs +1 -2
- package/dist/cjs/server/serverContext.cjs.map +1 -1
- package/dist/cjs/server/useIntlayer.d.ts +3 -1
- package/dist/cjs/useIntlayerBase.cjs +26 -1
- package/dist/cjs/useIntlayerBase.cjs.map +1 -1
- package/dist/cjs/useIntlayerBase.d.ts +62 -4
- package/dist/esm/{chunk-ZNCO4QRF.mjs → chunk-ZD7AOCMD.mjs} +1 -1
- package/dist/esm/client/ContentEditionLayout.mjs +1 -1
- package/dist/esm/client/ContentEditionLayout.mjs.map +1 -1
- package/dist/esm/client/{IntlayerClientProvider.d.mts → IntlayerProvider.d.mts} +5 -3
- package/dist/esm/client/{IntlayerClientProvider.mjs → IntlayerProvider.mjs} +11 -8
- package/dist/esm/client/IntlayerProvider.mjs.map +1 -0
- package/dist/esm/client/getBrowserLocale.mjs +8 -15
- package/dist/esm/client/getBrowserLocale.mjs.map +1 -1
- package/dist/esm/client/index.d.mts +3 -2
- package/dist/esm/client/index.mjs +5 -5
- package/dist/esm/client/index.mjs.map +1 -1
- package/dist/esm/client/useContent.d.mts +1 -1
- package/dist/esm/client/useContent.mjs +1 -1
- package/dist/esm/client/useIntlayer.d.mts +3 -1
- package/dist/esm/client/useIntlayer.mjs +2 -2
- package/dist/esm/client/useIntlayer.mjs.map +1 -1
- package/dist/esm/client/useLocale.mjs +2 -2
- package/dist/esm/client/useLocale.mjs.map +1 -1
- package/dist/esm/client/useLocaleBase.mjs +2 -2
- package/dist/esm/client/useLocaleBase.mjs.map +1 -1
- package/dist/esm/client/useLocaleCookie.mjs +1 -1
- package/dist/esm/client/useLocaleCookie.mjs.map +1 -1
- package/dist/esm/client/useTraduction.mjs +2 -2
- package/dist/esm/client/useTraduction.mjs.map +1 -1
- package/dist/esm/craco/craco.config.mjs +1 -1
- package/dist/esm/craco/intlayerCracoPlugin.mjs +1 -1
- package/dist/esm/getEnumeration.mjs +1 -1
- package/dist/esm/getTranslation.mjs +1 -1
- package/dist/esm/index.d.mts +3 -2
- package/dist/esm/index.mjs +5 -5
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/processDictionary/contentDictionary.d.mts +9 -9
- package/dist/esm/processDictionary/index.d.mts +4 -4
- package/dist/esm/processDictionary/index.mjs +70 -54
- package/dist/esm/processDictionary/index.mjs.map +1 -1
- package/dist/esm/server/IntlayerServerProvider.d.mts +6 -7
- package/dist/esm/server/IntlayerServerProvider.mjs +4 -5
- package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -1
- package/dist/esm/server/getLocaleTranslation.mjs +1 -1
- package/dist/esm/server/index.d.mts +1 -0
- package/dist/esm/server/index.mjs +1 -1
- package/dist/esm/server/serverContext.mjs +2 -3
- package/dist/esm/server/serverContext.mjs.map +1 -1
- package/dist/esm/server/useIntlayer.d.mts +3 -1
- package/dist/esm/server/useIntlayer.mjs +1 -1
- package/dist/esm/server/useTraduction.mjs +1 -1
- package/dist/esm/useIntlayerBase.d.mts +62 -4
- package/dist/esm/useIntlayerBase.mjs +26 -2
- package/dist/esm/useIntlayerBase.mjs.map +1 -1
- package/dist/esm/vite/intlayerPlugin.mjs +1 -1
- package/package.json +18 -19
- package/src/index.ts +4 -3
- package/dist/cjs/IntlayerProvider.cjs +0 -37
- package/dist/cjs/IntlayerProvider.cjs.map +0 -1
- package/dist/cjs/IntlayerProvider.d.ts +0 -9
- package/dist/cjs/client/IntlayerClientProvider.cjs.map +0 -1
- package/dist/esm/IntlayerProvider.d.mts +0 -9
- package/dist/esm/IntlayerProvider.mjs +0 -18
- package/dist/esm/IntlayerProvider.mjs.map +0 -1
- package/dist/esm/client/IntlayerClientProvider.mjs.map +0 -1
- package/src/IntlayerProvider.tsx +0 -24
- package/src/cli/react-intlayer.ts +0 -60
- package/src/client/ContentEditionLayout.tsx +0 -15
- package/src/client/IntlayerClientProvider.tsx +0 -83
- package/src/client/getBrowserLocale.tsx +0 -192
- package/src/client/index.ts +0 -16
- package/src/client/useContent.ts +0 -20
- package/src/client/useIntlayer.ts +0 -25
- package/src/client/useLocale.ts +0 -23
- package/src/client/useLocaleBase.ts +0 -24
- package/src/client/useLocaleCookie.ts +0 -32
- package/src/client/useTraduction.ts +0 -33
- package/src/craco/craco.config.ts +0 -11
- package/src/craco/intlayerCracoPlugin.ts +0 -89
- package/src/getEnumeration.ts +0 -41
- package/src/getTranslation.ts +0 -37
- package/src/processDictionary/contentDictionary.ts +0 -28
- package/src/processDictionary/index.ts +0 -231
- package/src/server/IntlayerServerProvider.tsx +0 -36
- package/src/server/getLocaleTranslation.ts +0 -22
- package/src/server/index.ts +0 -8
- package/src/server/serverContext.ts +0 -85
- package/src/server/useIntlayer.ts +0 -23
- package/src/server/useTraduction.ts +0 -34
- package/src/useIntlayerBase.ts +0 -38
- package/src/vite/intlayerPlugin.ts +0 -59
- /package/dist/esm/{chunk-ZNCO4QRF.mjs.map → chunk-ZD7AOCMD.mjs.map} +0 -0
|
@@ -24,7 +24,6 @@ __export(processDictionary_exports, {
|
|
|
24
24
|
module.exports = __toCommonJS(processDictionary_exports);
|
|
25
25
|
var import_client = require("@intlayer/config/client");
|
|
26
26
|
var import_core = require("@intlayer/core");
|
|
27
|
-
var import_client2 = require("intlayer-editor/client");
|
|
28
27
|
var import_react = require("react");
|
|
29
28
|
var import_getEnumeration = require('../getEnumeration.cjs');
|
|
30
29
|
var import_getTranslation = require('../getTranslation.cjs');
|
|
@@ -48,35 +47,33 @@ const processTranslation = (languageContent, locale, dictionaryId, dictionaryPat
|
|
|
48
47
|
locale
|
|
49
48
|
);
|
|
50
49
|
};
|
|
51
|
-
const processEnumeration = (enumerationContent, locale, dictionaryId, dictionaryPath, keyPath = []) => {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
);
|
|
72
|
-
};
|
|
50
|
+
const processEnumeration = (enumerationContent, locale, dictionaryId, dictionaryPath, keyPath = []) => (quantity) => {
|
|
51
|
+
const enumerationResult = (0, import_getEnumeration.getEnumeration)(
|
|
52
|
+
enumerationContent,
|
|
53
|
+
quantity
|
|
54
|
+
);
|
|
55
|
+
const matchingCondition = (0, import_core.findMatchingCondition)(
|
|
56
|
+
enumerationContent,
|
|
57
|
+
quantity
|
|
58
|
+
);
|
|
59
|
+
const resultKeyPath = [
|
|
60
|
+
...keyPath,
|
|
61
|
+
{ type: import_core.NodeType.Enumeration, key: matchingCondition.toString() }
|
|
62
|
+
];
|
|
63
|
+
return processDictionary(
|
|
64
|
+
enumerationResult,
|
|
65
|
+
dictionaryId,
|
|
66
|
+
dictionaryPath,
|
|
67
|
+
resultKeyPath,
|
|
68
|
+
locale
|
|
69
|
+
);
|
|
73
70
|
};
|
|
74
71
|
const isReactNode = (node) => typeof node?.key !== "undefined" && typeof node?.props !== "undefined";
|
|
75
72
|
const processNode = (field, locale, dictionaryId, dictionaryPath, keyPath = []) => {
|
|
76
73
|
if (typeof field === "object") {
|
|
77
74
|
if (field.nodeType === import_core.NodeType.Translation) {
|
|
78
75
|
return processTranslation(
|
|
79
|
-
field,
|
|
76
|
+
field[import_core.NodeType.Translation],
|
|
80
77
|
locale,
|
|
81
78
|
dictionaryId,
|
|
82
79
|
dictionaryPath,
|
|
@@ -85,7 +82,7 @@ const processNode = (field, locale, dictionaryId, dictionaryPath, keyPath = [])
|
|
|
85
82
|
}
|
|
86
83
|
if (field.nodeType === import_core.NodeType.Enumeration) {
|
|
87
84
|
return processEnumeration(
|
|
88
|
-
field,
|
|
85
|
+
field[import_core.NodeType.Enumeration],
|
|
89
86
|
locale,
|
|
90
87
|
dictionaryId,
|
|
91
88
|
dictionaryPath,
|
|
@@ -126,7 +123,7 @@ const createReactElement = (element) => {
|
|
|
126
123
|
const { type, props } = fixedElement;
|
|
127
124
|
return (0, import_react.createElement)(type ?? "div", props, ...props.children);
|
|
128
125
|
};
|
|
129
|
-
const traceKeys = ["filePath", "
|
|
126
|
+
const traceKeys = ["filePath", "nodeType"];
|
|
130
127
|
const processDictionary = (content, dictionaryId, dictionaryPath, keyPath = [], locale = defaultLocale) => {
|
|
131
128
|
if (isReactNode(content)) {
|
|
132
129
|
return createReactElement(
|
|
@@ -134,40 +131,59 @@ const processDictionary = (content, dictionaryId, dictionaryPath, keyPath = [],
|
|
|
134
131
|
);
|
|
135
132
|
}
|
|
136
133
|
if (content && typeof content === "object") {
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
result[key] = field;
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
const resultKeyPath = [
|
|
145
|
-
...keyPath,
|
|
146
|
-
{ type: "ObjectExpression", key }
|
|
147
|
-
];
|
|
148
|
-
result[key] = processNode(
|
|
149
|
-
field,
|
|
150
|
-
locale,
|
|
151
|
-
dictionaryId,
|
|
152
|
-
dictionaryPath,
|
|
153
|
-
resultKeyPath
|
|
154
|
-
);
|
|
155
|
-
}
|
|
156
|
-
return result;
|
|
157
|
-
}
|
|
158
|
-
if (typeof content === "string") {
|
|
159
|
-
try {
|
|
160
|
-
return (0, import_client2.renderContentEditor)(
|
|
134
|
+
const isArray = Array.isArray(content);
|
|
135
|
+
let result = {};
|
|
136
|
+
if (typeof content.nodeType !== "undefined") {
|
|
137
|
+
return processNode(
|
|
161
138
|
content,
|
|
139
|
+
locale,
|
|
162
140
|
dictionaryId,
|
|
163
141
|
dictionaryPath,
|
|
164
142
|
keyPath
|
|
165
143
|
);
|
|
166
|
-
}
|
|
167
|
-
|
|
144
|
+
} else if (isArray) {
|
|
145
|
+
result = content.map((field, key) => {
|
|
146
|
+
const resultKeyPath = [
|
|
147
|
+
...keyPath,
|
|
148
|
+
{ type: "ArrayExpression", key }
|
|
149
|
+
];
|
|
150
|
+
return processNode(
|
|
151
|
+
field,
|
|
152
|
+
locale,
|
|
153
|
+
dictionaryId,
|
|
154
|
+
dictionaryPath,
|
|
155
|
+
resultKeyPath
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
} else {
|
|
159
|
+
for (const key of Object.keys(content)) {
|
|
160
|
+
const field = content[key];
|
|
161
|
+
if (traceKeys.includes(key)) {
|
|
162
|
+
result[key] = field;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
const resultKeyPath = [
|
|
166
|
+
...keyPath,
|
|
167
|
+
{ type: "ObjectExpression", key }
|
|
168
|
+
];
|
|
169
|
+
const nodeResult = processNode(
|
|
170
|
+
field,
|
|
171
|
+
locale,
|
|
172
|
+
dictionaryId,
|
|
173
|
+
dictionaryPath,
|
|
174
|
+
resultKeyPath
|
|
175
|
+
);
|
|
176
|
+
result[key] = nodeResult;
|
|
177
|
+
}
|
|
168
178
|
}
|
|
179
|
+
return result;
|
|
169
180
|
}
|
|
170
|
-
return
|
|
181
|
+
return {
|
|
182
|
+
content,
|
|
183
|
+
keyPath,
|
|
184
|
+
dictionaryId,
|
|
185
|
+
dictionaryPath
|
|
186
|
+
};
|
|
171
187
|
};
|
|
172
188
|
// Annotate the CommonJS export names for ESM import in node:
|
|
173
189
|
0 && (module.exports = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/processDictionary/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { type Locales, getConfiguration } from '@intlayer/config/client';\nimport {\n NodeType,\n type QuantityContent,\n type LanguageContent,\n findMatchingCondition,\n type KeyPath,\n} from '@intlayer/core';\nimport { renderContentEditor } from 'intlayer-editor/client';\nimport { type ReactElement, createElement, type ReactNode } from 'react';\nimport { getEnumeration } from '../getEnumeration';\nimport { getTranslation } from '../getTranslation';\nimport type {\n Content,\n ContentValue,\n TransformedContent,\n TransformedContentValue,\n} from './contentDictionary';\n\nconst {\n internationalization: { defaultLocale },\n} = getConfiguration();\n\nconst processTranslation = (\n languageContent: LanguageContent<ContentValue>,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContent => {\n const translationResult: ContentValue = getTranslation<ContentValue>(\n languageContent,\n locale\n );\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: NodeType.Translation, key: locale },\n ];\n\n return processDictionary(\n translationResult as Content,\n dictionaryId,\n dictionaryPath,\n resultKeyPath,\n locale\n );\n};\n\nconst processEnumeration = (\n enumerationContent: QuantityContent<ContentValue>,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContentValue => {\n return (quantity: number): TransformedContentValue => {\n const enumerationResult: ContentValue = getEnumeration<ContentValue>(\n enumerationContent,\n quantity\n );\n\n const matchingCondition = findMatchingCondition(\n enumerationContent,\n quantity\n );\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: NodeType.Enumeration, key: matchingCondition.toString() },\n ];\n\n //\n return processDictionary(\n enumerationResult as Content,\n dictionaryId,\n dictionaryPath,\n resultKeyPath,\n locale\n );\n };\n};\n\nconst isReactNode = (node: Record<string, unknown>): boolean =>\n typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';\n\nexport const processNode = (\n field: ContentValue | undefined,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContentValue => {\n if (typeof field === 'object') {\n if (field.nodeType === NodeType.Translation) {\n return processTranslation(\n field as LanguageContent<ContentValue>,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n );\n }\n\n if (field.nodeType === NodeType.Enumeration) {\n return processEnumeration(\n field satisfies QuantityContent<ContentValue>,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n );\n }\n }\n\n return processDictionary(\n field as Content,\n dictionaryId,\n dictionaryPath,\n keyPath,\n locale\n );\n};\n\n// This function recursively creates React elements from a given JSON-like structure\nconst createReactElement = (element: ReactElement) => {\n if (typeof element === 'string') {\n // If it's a string, simply return it (used for text content)\n return element;\n }\n\n // Destructure the component properties\n\n const convertChildrenAsArray = (element: ReactElement): ReactElement => {\n if (element?.props && typeof element.props.children === 'object') {\n const childrenResult: ReactNode[] = [];\n const { children } = element.props;\n\n // Create the children elements recursively, if any\n Object.keys(children).forEach((key) => {\n childrenResult.push(createReactElement(children[key]));\n });\n\n return {\n ...element,\n props: { ...element.props, children: childrenResult },\n };\n }\n\n return {\n ...element,\n props: { ...element.props, children: element.props.children },\n };\n };\n\n const fixedElement = convertChildrenAsArray(element);\n\n const { type, props } = fixedElement;\n\n // Create and return the React element\n return createElement(type ?? 'div', props, ...props.children);\n};\n\nconst traceKeys: string[] = ['filePath', 'id', 'nodeType'];\n\n/**\n * Function that process a dictionary and return the result to be used in the application.\n */\nexport const processDictionary = (\n content: Content,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = [],\n locale: Locales = defaultLocale\n): TransformedContent => {\n // If it's a React element, render it\n if (isReactNode(content)) {\n return createReactElement(\n content as unknown as ReactElement\n ) as unknown as TransformedContent;\n }\n\n if (content && typeof content === 'object') {\n const result: TransformedContent = {};\n\n // List each key in the content and process it\n for (const key of Object.keys(content)) {\n const field = content[key];\n\n if (traceKeys.includes(key)) {\n result[key] = field as TransformedContentValue;\n continue;\n }\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: 'ObjectExpression', key },\n ];\n\n result[key] = processNode(\n field,\n locale,\n dictionaryId,\n dictionaryPath,\n resultKeyPath\n );\n }\n\n return result;\n }\n\n if (typeof content === 'string') {\n try {\n // renderContentEditor come from intlayer-editor, which is an optional dependency.\n // intlayer-editor should be installed in the project to use the content editor.\n return renderContentEditor(\n content,\n dictionaryId,\n dictionaryPath,\n keyPath\n );\n } catch (e) {\n // If renderContentEditor not available, it will return the content as is.\n return content;\n }\n }\n\n // If it's a string, number, or function, return it\n return content;\n};\n"],"mappings":"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","names":["import_client","element"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/processDictionary/index.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport { type Locales, getConfiguration } from '@intlayer/config/client';\nimport {\n NodeType,\n type QuantityContent,\n type LanguageContent,\n findMatchingCondition,\n type KeyPath,\n type Dictionary,\n type DictionaryValue,\n type TranslationContent,\n type EnumerationContent,\n type TypedNode,\n} from '@intlayer/core';\nimport { type ReactElement, createElement, type ReactNode } from 'react';\nimport { getEnumeration } from '../getEnumeration';\nimport { getTranslation } from '../getTranslation';\nimport type {\n TransformedContent,\n TransformedContentValue,\n} from './contentDictionary';\n\nconst {\n internationalization: { defaultLocale },\n} = getConfiguration();\n\nconst processTranslation = (\n languageContent: LanguageContent<DictionaryValue>,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContent => {\n const translationResult: DictionaryValue = getTranslation<DictionaryValue>(\n languageContent,\n locale\n );\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: NodeType.Translation, key: locale },\n ];\n\n return processDictionary(\n translationResult,\n dictionaryId,\n dictionaryPath,\n resultKeyPath,\n locale\n );\n};\n\nconst processEnumeration =\n (\n enumerationContent: QuantityContent<DictionaryValue>,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n ): TransformedContentValue =>\n (quantity: number): TransformedContentValue => {\n const enumerationResult: DictionaryValue = getEnumeration<DictionaryValue>(\n enumerationContent,\n quantity\n );\n\n const matchingCondition = findMatchingCondition(\n enumerationContent,\n quantity\n );\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: NodeType.Enumeration, key: matchingCondition.toString() },\n ];\n\n return processDictionary(\n enumerationResult,\n dictionaryId,\n dictionaryPath,\n resultKeyPath,\n locale\n ) as TransformedContentValue;\n };\n\nconst isReactNode = (node: Record<string, unknown>): boolean =>\n typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';\n\nexport const processNode = (\n field: DictionaryValue | undefined,\n locale: Locales,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = []\n): TransformedContentValue => {\n if (typeof field === 'object') {\n if (\n (field as TranslationContent<DictionaryValue>).nodeType ===\n NodeType.Translation\n ) {\n return processTranslation(\n field[\n NodeType.Translation as keyof typeof field\n ] as LanguageContent<DictionaryValue>,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n ) as TransformedContentValue;\n }\n\n if (\n (field as EnumerationContent<DictionaryValue>).nodeType ===\n NodeType.Enumeration\n ) {\n return processEnumeration(\n field[\n NodeType.Enumeration as keyof typeof field\n ] as QuantityContent<DictionaryValue>,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n );\n }\n }\n\n return processDictionary(\n field!,\n dictionaryId,\n dictionaryPath,\n keyPath,\n locale\n ) as TransformedContentValue;\n};\n\n// This function recursively creates React elements from a given JSON-like structure\nconst createReactElement = (element: ReactElement) => {\n if (typeof element === 'string') {\n // If it's a string, simply return it (used for text content)\n return element;\n }\n\n const convertChildrenAsArray = (element: ReactElement): ReactElement => {\n if (element?.props && typeof element.props.children === 'object') {\n const childrenResult: ReactNode[] = [];\n const { children } = element.props;\n\n // Create the children elements recursively, if any\n Object.keys(children).forEach((key) => {\n childrenResult.push(createReactElement(children[key]));\n });\n\n return {\n ...element,\n props: { ...element.props, children: childrenResult },\n };\n }\n\n return {\n ...element,\n props: { ...element.props, children: element.props.children },\n };\n };\n\n const fixedElement = convertChildrenAsArray(element);\n\n const { type, props } = fixedElement;\n\n // Create and return the React element\n return createElement(type ?? 'div', props, ...props.children);\n};\n\nconst traceKeys: string[] = ['filePath', 'nodeType'];\n\n/**\n * Function that process a dictionary and return the result to be used in the application.\n */\nexport const processDictionary = (\n content: Dictionary | DictionaryValue,\n dictionaryId: string,\n dictionaryPath: string,\n keyPath: KeyPath[] = [],\n locale: Locales = defaultLocale\n // eslint-disable-next-line sonarjs/cognitive-complexity\n): TransformedContent => {\n // If it's a React element, render it\n if (isReactNode(content as Record<string, unknown>)) {\n return createReactElement(\n content as unknown as ReactElement\n ) as unknown as TransformedContent;\n }\n\n if (content && typeof content === 'object') {\n const isArray = Array.isArray(content);\n\n let result: TransformedContent = {};\n\n if (typeof (content as TypedNode).nodeType !== 'undefined') {\n return processNode(\n content as DictionaryValue,\n locale,\n dictionaryId,\n dictionaryPath,\n keyPath\n ) as TransformedContent;\n } else if (isArray) {\n // Eslint fix because promises are awaited during build stage\n // eslint-disable-next-line @typescript-eslint/no-floating-promises\n result = (content as DictionaryValue[]).map((field, key) => {\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: 'ArrayExpression', key },\n ];\n\n return processNode(\n field,\n locale,\n dictionaryId,\n dictionaryPath,\n resultKeyPath\n );\n }) as TransformedContent;\n } else {\n // List each key in the content and process it\n for (const key of Object.keys(content)) {\n const field = content[key as keyof typeof content];\n\n if (traceKeys.includes(key)) {\n result[key] = field as TransformedContentValue;\n continue;\n }\n\n const resultKeyPath: KeyPath[] = [\n ...keyPath,\n { type: 'ObjectExpression', key },\n ];\n\n const nodeResult = processNode(\n field,\n locale,\n dictionaryId,\n dictionaryPath,\n resultKeyPath\n );\n\n result[key] = nodeResult;\n }\n }\n\n return result;\n }\n\n return {\n content,\n keyPath,\n dictionaryId,\n dictionaryPath,\n };\n};\n"],"mappings":"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","names":["element"]}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { Locales } from '@intlayer/config/client';
|
|
2
|
-
import { KeyPath } from '@intlayer/core';
|
|
3
|
-
import {
|
|
2
|
+
import { DictionaryValue, KeyPath, Dictionary } from '@intlayer/core';
|
|
3
|
+
import { TransformedContentValue, TransformedContent } from './contentDictionary.js';
|
|
4
4
|
import 'react';
|
|
5
5
|
|
|
6
|
-
declare const processNode: (field:
|
|
6
|
+
declare const processNode: (field: DictionaryValue | undefined, locale: Locales, dictionaryId: string, dictionaryPath: string, keyPath?: KeyPath[]) => TransformedContentValue;
|
|
7
7
|
/**
|
|
8
8
|
* Function that process a dictionary and return the result to be used in the application.
|
|
9
9
|
*/
|
|
10
|
-
declare const processDictionary: (content:
|
|
10
|
+
declare const processDictionary: (content: Dictionary | DictionaryValue, dictionaryId: string, dictionaryPath: string, keyPath?: KeyPath[], locale?: Locales) => TransformedContent;
|
|
11
11
|
|
|
12
12
|
export { processDictionary, processNode };
|
|
@@ -27,14 +27,13 @@ module.exports = __toCommonJS(IntlayerServerProvider_exports);
|
|
|
27
27
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
28
28
|
var import_client = require("@intlayer/config/client");
|
|
29
29
|
var import_serverContext = require('./serverContext.cjs');
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
);
|
|
30
|
+
const { defaultLocale } = (0, import_client.getConfiguration)().internationalization;
|
|
31
|
+
const IntlayerServerContext = (0, import_serverContext.createServerContext)(defaultLocale);
|
|
33
32
|
const useIntlayer = () => (0, import_serverContext.getServerContext)(IntlayerServerContext);
|
|
34
33
|
const locale = (0, import_serverContext.getServerContext)(IntlayerServerContext);
|
|
35
34
|
const IntlayerServerProvider = ({
|
|
36
35
|
children,
|
|
37
|
-
locale: locale2
|
|
36
|
+
locale: locale2 = defaultLocale
|
|
38
37
|
}) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(IntlayerServerContext.Provider, { value: locale2, children });
|
|
39
38
|
// Annotate the CommonJS export names for ESM import in node:
|
|
40
39
|
0 && (module.exports = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/IntlayerServerProvider.tsx"],"sourcesContent":["import { type Locales, getConfiguration } from '@intlayer/config/client';\nimport type { FC, PropsWithChildren } from 'react';\nimport { createServerContext, getServerContext } from './serverContext';\n\n/**\n * Context that store the current locale on the server side\n */\nexport const IntlayerServerContext
|
|
1
|
+
{"version":3,"sources":["../../../src/server/IntlayerServerProvider.tsx"],"sourcesContent":["import { type Locales, getConfiguration } from '@intlayer/config/client';\nimport type { FC, PropsWithChildren } from 'react';\nimport { createServerContext, getServerContext } from './serverContext';\n\nconst { defaultLocale } = getConfiguration().internationalization;\n\n/**\n * Context that store the current locale on the server side\n */\nexport const IntlayerServerContext =\n createServerContext<Locales>(defaultLocale);\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayer = () => getServerContext(IntlayerServerContext);\n\n/**\n * Get the current locale\n */\nexport const locale = getServerContext(IntlayerServerContext);\n\nexport type IntlayerServerProviderProps = PropsWithChildren & {\n locale?: Locales;\n};\n\n/**\n * Provider that store the current locale on the server side\n */\nexport const IntlayerServerProvider: FC<IntlayerServerProviderProps> = ({\n children,\n locale = defaultLocale,\n}) => (\n <IntlayerServerContext.Provider value={locale}>\n {children}\n </IntlayerServerContext.Provider>\n);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiCE;AAjCF,oBAA+C;AAE/C,2BAAsD;AAEtD,MAAM,EAAE,cAAc,QAAI,gCAAiB,EAAE;AAKtC,MAAM,4BACX,0CAA6B,aAAa;AAKrC,MAAM,cAAc,UAAM,uCAAiB,qBAAqB;AAKhE,MAAM,aAAS,uCAAiB,qBAAqB;AASrD,MAAM,yBAA0D,CAAC;AAAA,EACtE;AAAA,EACA,QAAAA,UAAS;AACX,MACE,4CAAC,sBAAsB,UAAtB,EAA+B,OAAOA,SACpC,UACH;","names":["locale"]}
|
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import * as react from 'react';
|
|
2
|
-
import { PropsWithChildren, FC } from 'react';
|
|
3
1
|
import { Locales } from '@intlayer/config/client';
|
|
2
|
+
import { PropsWithChildren, FC } from 'react';
|
|
4
3
|
|
|
5
4
|
/**
|
|
6
5
|
* Context that store the current locale on the server side
|
|
7
6
|
*/
|
|
8
7
|
declare const IntlayerServerContext: {
|
|
9
8
|
Provider: ({ children, value, }: {
|
|
10
|
-
children:
|
|
9
|
+
children: React.ReactNode;
|
|
11
10
|
value: Locales;
|
|
12
|
-
}) =>
|
|
11
|
+
}) => React.ReactNode;
|
|
13
12
|
Consumer: ({ children, }: {
|
|
14
|
-
children: (context: Locales | undefined) =>
|
|
15
|
-
}) =>
|
|
13
|
+
children: (context: Locales | undefined) => React.ReactNode;
|
|
14
|
+
}) => React.ReactNode;
|
|
16
15
|
_storage: () => {
|
|
17
16
|
value: Locales | undefined;
|
|
18
17
|
};
|
|
@@ -27,7 +26,7 @@ declare const useIntlayer: () => Locales | undefined;
|
|
|
27
26
|
*/
|
|
28
27
|
declare const locale: Locales | undefined;
|
|
29
28
|
type IntlayerServerProviderProps = PropsWithChildren & {
|
|
30
|
-
locale
|
|
29
|
+
locale?: Locales;
|
|
31
30
|
};
|
|
32
31
|
/**
|
|
33
32
|
* Provider that store the current locale on the server side
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/serverContext.ts"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer.Provider value={locale}>\n * {children}\n * </IntlayerServer.Provider>\n */\n\nimport react from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {\n throwInClient();\n\n const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n const getCache = cache(() => ({\n value: undefined,\n }));\n\n return {\n Provider: ({ children, value }) => {\n getCache().value = value;\n return children;\n },\n Consumer: ({ children }) => {\n const store = getCache();\n return children(store ? store.value : defaultValue);\n },\n _storage: getCache,\n _defaultValue: defaultValue,\n };\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T>({\n _storage,\n _defaultValue,\n}: ServerContext<T>) => {\n // throwInClient();\n const store = _storage();\n if (!store) return _defaultValue;\n return store.value;\n};\n\ntype ServerContext<T> = {\n Provider: ({\n children,\n value,\n }: {\n children: React.ReactNode;\n value: T;\n }) => React.ReactNode;\n Consumer: ({\n children,\n }: {\n children: (context: T | undefined) => React.ReactNode;\n }) => React.ReactNode;\n _storage: () => { value: T | undefined };\n _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): void | never => {\n // If window.document is defined we're in a client component\n if (typeof window !== 'undefined') {\n throw new Error(`createServerContext only works in Server Components`);\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYA,mBAAkB;AAIlB,MAAM,gBAAgB,MAAM,OAAO,EAAE,OAAO,OAAU;AAE/C,MAAM,sBAAsB,CAAI,iBAAuC;AAC5E,gBAAc;AAEd,QAAM,QAAQ,aAAAA,QAAM,SAAuB;AAE3C,QAAM,WAAW,MAAM,OAAO;AAAA,IAC5B,OAAO;AAAA,EACT,EAAE;AAEF,SAAO;AAAA,IACL,UAAU,CAAC,EAAE,UAAU,MAAM,MAAM;AACjC,eAAS,EAAE,QAAQ;AACnB,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,EAAE,SAAS,MAAM;AAC1B,YAAM,QAAQ,SAAS;AACvB,aAAO,SAAS,QAAQ,MAAM,QAAQ,YAAY;AAAA,IACpD;AAAA,IACA,UAAU;AAAA,IACV,eAAe;AAAA,EACjB;AACF;AASO,MAAM,mBAAmB,CAAI;AAAA,EAClC;AAAA,EACA;AACF,MAAwB;AAEtB,QAAM,QAAQ,SAAS;AACvB,MAAI,CAAC
|
|
1
|
+
{"version":3,"sources":["../../../src/server/serverContext.ts"],"sourcesContent":["/**\n * Creates a new datastore for a given server context.\n * Attempts to closely mimic the `createContext` API.\n *\n * @example\n * const IntlayerServer = createServerContext<string | null>(null);\n *\n * <IntlayerServer.Provider value={locale}>\n * {children}\n * </IntlayerServer.Provider>\n */\n\nimport react from 'react';\n\ntype CacheType<T> = () => { value: T | undefined };\n\nconst cacheFallback = () => () => ({ value: undefined });\n\nexport const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {\n throwInClient();\n\n const cache = react.cache<CacheType<T>> ?? cacheFallback;\n\n const getCache = cache(() => ({\n value: undefined,\n }));\n\n return {\n Provider: ({ children, value }) => {\n getCache().value = value;\n return children;\n },\n Consumer: ({ children }) => {\n const store = getCache();\n return children(store ? store.value : defaultValue);\n },\n _storage: getCache,\n _defaultValue: defaultValue,\n };\n};\n\n/**\n * Fetches a value present in a given server context.\n * Attempts to closely mimic the `useContext` API.\n *\n * @example\n * getServerContext(IntlayerServer);\n */\nexport const getServerContext = <T>({\n _storage,\n _defaultValue,\n}: ServerContext<T>) => {\n // throwInClient();\n const store = _storage();\n if (!store) return _defaultValue;\n return store.value;\n};\n\ntype ServerContext<T> = {\n Provider: ({\n children,\n value,\n }: {\n children: React.ReactNode;\n value: T;\n }) => React.ReactNode;\n Consumer: ({\n children,\n }: {\n children: (context: T | undefined) => React.ReactNode;\n }) => React.ReactNode;\n _storage: () => { value: T | undefined };\n _defaultValue: T | undefined;\n};\n\n/**\n * Throws if called within a client component environment.\n * Useful to help prevent mistakes.\n */\nconst throwInClient = (): void | never => {\n // If window.document is defined we're in a client component\n if (typeof window !== 'undefined') {\n throw new Error(`createServerContext only works in Server Components`);\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYA,mBAAkB;AAIlB,MAAM,gBAAgB,MAAM,OAAO,EAAE,OAAO,OAAU;AAE/C,MAAM,sBAAsB,CAAI,iBAAuC;AAC5E,gBAAc;AAEd,QAAM,QAAQ,aAAAA,QAAM,SAAuB;AAE3C,QAAM,WAAW,MAAM,OAAO;AAAA,IAC5B,OAAO;AAAA,EACT,EAAE;AAEF,SAAO;AAAA,IACL,UAAU,CAAC,EAAE,UAAU,MAAM,MAAM;AACjC,eAAS,EAAE,QAAQ;AACnB,aAAO;AAAA,IACT;AAAA,IACA,UAAU,CAAC,EAAE,SAAS,MAAM;AAC1B,YAAM,QAAQ,SAAS;AACvB,aAAO,SAAS,QAAQ,MAAM,QAAQ,YAAY;AAAA,IACpD;AAAA,IACA,UAAU;AAAA,IACV,eAAe;AAAA,EACjB;AACF;AASO,MAAM,mBAAmB,CAAI;AAAA,EAClC;AAAA,EACA;AACF,MAAwB;AAEtB,QAAM,QAAQ,SAAS;AACvB,MAAI,CAAC,MAAO,QAAO;AACnB,SAAO,MAAM;AACf;AAuBA,MAAM,gBAAgB,MAAoB;AAExC,MAAI,OAAO,WAAW,aAAa;AACjC,UAAM,IAAI,MAAM,qDAAqD;AAAA,EACvE;AACF;","names":["react"]}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { UseIntlayer } from '../useIntlayerBase.js';
|
|
2
|
-
import '@intlayer/config
|
|
2
|
+
import '@intlayer/config';
|
|
3
|
+
import '@intlayer/core';
|
|
3
4
|
import 'intlayer';
|
|
5
|
+
import 'react';
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
8
|
* On the server side, Hook that picking one dictionary by its id and return the content
|
|
@@ -28,23 +28,48 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
var useIntlayerBase_exports = {};
|
|
30
30
|
__export(useIntlayerBase_exports, {
|
|
31
|
+
recursiveTransformContent: () => recursiveTransformContent,
|
|
31
32
|
useIntlayerBase: () => useIntlayerBase
|
|
32
33
|
});
|
|
33
34
|
module.exports = __toCommonJS(useIntlayerBase_exports);
|
|
34
35
|
var import_dictionaries_entry = __toESM(require("@intlayer/dictionaries-entry"));
|
|
36
|
+
var import_client = require("intlayer-editor/client");
|
|
37
|
+
var import_react = require("react");
|
|
35
38
|
var import_processDictionary = require('./processDictionary/index.cjs');
|
|
39
|
+
const recursiveTransformContent = (value) => {
|
|
40
|
+
if (typeof value === "function") {
|
|
41
|
+
return (props) => recursiveTransformContent(value(props));
|
|
42
|
+
} else if (typeof value === "object" && typeof value.keyPath !== "undefined" && typeof value.dictionaryId !== "undefined" && typeof value.dictionaryPath !== "undefined") {
|
|
43
|
+
return (0, import_client.renderIntlayerEditor)(value);
|
|
44
|
+
} else if (typeof value === "object" && Array.isArray(value)) {
|
|
45
|
+
return value.map(recursiveTransformContent);
|
|
46
|
+
} else if (typeof value === "object" && (0, import_react.isValidElement)(value)) {
|
|
47
|
+
return value;
|
|
48
|
+
} else if (typeof value === "object") {
|
|
49
|
+
return Object.entries(value).reduce(
|
|
50
|
+
(acc, [key, value2]) => ({
|
|
51
|
+
...acc,
|
|
52
|
+
[key]: recursiveTransformContent(value2)
|
|
53
|
+
}),
|
|
54
|
+
{}
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return value.value;
|
|
58
|
+
};
|
|
36
59
|
const useIntlayerBase = (id, locale) => {
|
|
37
60
|
const dictionary = import_dictionaries_entry.default[id];
|
|
38
|
-
|
|
61
|
+
const result = (0, import_processDictionary.processDictionary)(
|
|
39
62
|
dictionary,
|
|
40
63
|
id,
|
|
41
64
|
dictionary.filePath,
|
|
42
65
|
[],
|
|
43
66
|
locale
|
|
44
67
|
);
|
|
68
|
+
return recursiveTransformContent(result);
|
|
45
69
|
};
|
|
46
70
|
// Annotate the CommonJS export names for ESM import in node:
|
|
47
71
|
0 && (module.exports = {
|
|
72
|
+
recursiveTransformContent,
|
|
48
73
|
useIntlayerBase
|
|
49
74
|
});
|
|
50
75
|
//# sourceMappingURL=useIntlayerBase.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../../src/useIntlayerBase.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { Locales } from '@intlayer/config';\n/**\n * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.\n * Using an external package allow to alias it in the bundle configuration (such as webpack).\n * The alias allow hot reload the app (such as nextjs) on any dictionary change.\n */\nimport type { Dictionary, NodeType } from '@intlayer/core';\nimport dictionaries from '@intlayer/dictionaries-entry';\nimport type { IntlayerDictionaryTypesConnector } from 'intlayer';\nimport { renderIntlayerEditor } from 'intlayer-editor/client';\nimport { isValidElement, type ReactNode } from 'react';\nimport { processDictionary } from './processDictionary/index';\n\n/**\n * Provides a fallback to string type if the generic type T is never,\n * otherwise returns T. This is useful for handling cases where no keys are found.\n * Example: StringFallback<never> -> string; StringFallback<'key'> -> 'key'\n */\nexport type StringFallback<T> = T extends never ? string : T; // If no keys are found, return string to disable error, and accept any string as dictionary key\n\n/**\n * Represents the keys of the IntlayerDictionaryTypesConnector,\n * ensuring they are valid dictionary keys or fallback to string if none exist.\n *\n * Example:\n * ```ts\n * DictionaryKeys -> 'key1' | 'key2'\n * // or if IntlayerDictionaryTypesConnector is not defined,\n * DictionaryKeys -> string\n * ```\n */\nexport type DictionaryKeys = StringFallback<\n keyof IntlayerDictionaryTypesConnector\n>;\n\nexport type IntlayerNode<T = string> = ReactNode & {\n value: T;\n};\n\ntype TransformNodeType<T, L extends Locales> = T extends {\n [NodeType.Enumeration]: { '1': any };\n}\n ? (quantity: number) => DeepTransformContent<T[NodeType.Enumeration]['1'], L>\n : T extends {\n [NodeType.Translation]: object;\n }\n ? L extends keyof T[NodeType.Translation]\n ? DeepTransformContent<T[NodeType.Translation][L], L> // DeepTransformContent<T[L], L>\n : never\n : T;\n\ntype DeepTransformContent<T, L extends Locales> = T extends object // Check if the property is an object\n ? T extends (infer U)[] // If it's an array, infer the type of array elements\n ? DeepTransformContent<U, L>[] // Apply DeepTransformContent recursively to each element of the array\n : T extends {\n nodeType: NodeType | string;\n }\n ? TransformNodeType<T, L>\n : T extends { _owner: any; key: any; props: any; ref: any }\n ? ReactNode\n : {\n [K in keyof T]: DeepTransformContent<T[K], L>;\n }\n : T extends undefined\n ? never\n : IntlayerNode<T>;\n\n/**\n * Excludes the 'id' and 'filePath' keys from the dictionary content,\n * as they are not part of the IntlayerDictionaryTypesConnector type.\n */\ntype ExcludeIntlayerUtilsKeys<T> = Omit<T, 'id' | 'filePath'>;\n\n/**\n * Represents the data type returned by the useIntlayer hook,\n * excluding the 'id' and 'filePath' keys from the dictionary content.\n */\ntype Data<\n T extends DictionaryKeys,\n K extends Locales,\n> = ExcludeIntlayerUtilsKeys<\n DeepTransformContent<IntlayerDictionaryTypesConnector[T], K>\n>;\n\n/**\n * Parcourt the object. If a object has a keyPath, render the intlayer editor if editor enabled.\n */\nexport const recursiveTransformContent = (value: any): object => {\n if (typeof value === 'function') {\n return (props: any) => recursiveTransformContent(value(props));\n } else if (\n typeof value === 'object' &&\n typeof value.keyPath !== 'undefined' &&\n typeof value.dictionaryId !== 'undefined' &&\n typeof value.dictionaryPath !== 'undefined'\n ) {\n return renderIntlayerEditor(value);\n } else if (typeof value === 'object' && Array.isArray(value)) {\n return value.map(recursiveTransformContent);\n } else if (typeof value === 'object' && isValidElement(value)) {\n return value;\n } else if (typeof value === 'object') {\n return Object.entries(value).reduce(\n (acc, [key, value]) => ({\n ...acc,\n [key]: recursiveTransformContent(value),\n }),\n {} as object\n );\n }\n\n return value.value;\n};\n\n/**\n * Type definition for the useIntlayer hook, which takes a dictionary ID and an optional locale,\n * and returns the deeply transformed dictionary content.\n *\n */\nexport type UseIntlayer = <T extends DictionaryKeys, L extends Locales>(\n id: T,\n locale?: L\n) => Data<T, L>;\n\n/**\n * Hook that picks one dictionary by its ID and returns the content,\n * deeply transformed according to the dictionary structure and metadata.\n */\nexport const useIntlayerBase: UseIntlayer = <\n T extends DictionaryKeys,\n L extends Locales,\n>(\n id: T,\n locale?: L\n) => {\n const dictionary: Dictionary = dictionaries[id as keyof typeof dictionaries];\n\n const result = processDictionary(\n dictionary,\n id,\n dictionary.filePath,\n [],\n locale\n ) as object;\n\n return recursiveTransformContent(result) as Data<T, L>;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,gCAAyB;AAEzB,oBAAqC;AACrC,mBAA+C;AAC/C,+BAAkC;AA4E3B,MAAM,4BAA4B,CAAC,UAAuB;AAC/D,MAAI,OAAO,UAAU,YAAY;AAC/B,WAAO,CAAC,UAAe,0BAA0B,MAAM,KAAK,CAAC;AAAA,EAC/D,WACE,OAAO,UAAU,YACjB,OAAO,MAAM,YAAY,eACzB,OAAO,MAAM,iBAAiB,eAC9B,OAAO,MAAM,mBAAmB,aAChC;AACA,eAAO,oCAAqB,KAAK;AAAA,EACnC,WAAW,OAAO,UAAU,YAAY,MAAM,QAAQ,KAAK,GAAG;AAC5D,WAAO,MAAM,IAAI,yBAAyB;AAAA,EAC5C,WAAW,OAAO,UAAU,gBAAY,6BAAe,KAAK,GAAG;AAC7D,WAAO;AAAA,EACT,WAAW,OAAO,UAAU,UAAU;AACpC,WAAO,OAAO,QAAQ,KAAK,EAAE;AAAA,MAC3B,CAAC,KAAK,CAAC,KAAKA,MAAK,OAAO;AAAA,QACtB,GAAG;AAAA,QACH,CAAC,GAAG,GAAG,0BAA0BA,MAAK;AAAA,MACxC;AAAA,MACA,CAAC;AAAA,IACH;AAAA,EACF;AAEA,SAAO,MAAM;AACf;AAgBO,MAAM,kBAA+B,CAI1C,IACA,WACG;AACH,QAAM,aAAyB,0BAAAC,QAAa,EAA+B;AAE3E,QAAM,aAAS;AAAA,IACb;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,CAAC;AAAA,IACD;AAAA,EACF;AAEA,SAAO,0BAA0B,MAAM;AACzC;","names":["value","dictionaries"]}
|
|
@@ -1,12 +1,70 @@
|
|
|
1
|
-
import { Locales } from '@intlayer/config
|
|
1
|
+
import { Locales } from '@intlayer/config';
|
|
2
|
+
import { NodeType } from '@intlayer/core';
|
|
2
3
|
import { IntlayerDictionaryTypesConnector } from 'intlayer';
|
|
4
|
+
import { ReactNode } from 'react';
|
|
3
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Provides a fallback to string type if the generic type T is never,
|
|
8
|
+
* otherwise returns T. This is useful for handling cases where no keys are found.
|
|
9
|
+
* Example: StringFallback<never> -> string; StringFallback<'key'> -> 'key'
|
|
10
|
+
*/
|
|
4
11
|
type StringFallback<T> = T extends never ? string : T;
|
|
12
|
+
/**
|
|
13
|
+
* Represents the keys of the IntlayerDictionaryTypesConnector,
|
|
14
|
+
* ensuring they are valid dictionary keys or fallback to string if none exist.
|
|
15
|
+
*
|
|
16
|
+
* Example:
|
|
17
|
+
* ```ts
|
|
18
|
+
* DictionaryKeys -> 'key1' | 'key2'
|
|
19
|
+
* // or if IntlayerDictionaryTypesConnector is not defined,
|
|
20
|
+
* DictionaryKeys -> string
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
5
23
|
type DictionaryKeys = StringFallback<keyof IntlayerDictionaryTypesConnector>;
|
|
6
|
-
type
|
|
24
|
+
type IntlayerNode<T = string> = ReactNode & {
|
|
25
|
+
value: T;
|
|
26
|
+
};
|
|
27
|
+
type TransformNodeType<T, L extends Locales> = T extends {
|
|
28
|
+
[NodeType.Enumeration]: {
|
|
29
|
+
'1': any;
|
|
30
|
+
};
|
|
31
|
+
} ? (quantity: number) => DeepTransformContent<T[NodeType.Enumeration]['1'], L> : T extends {
|
|
32
|
+
[NodeType.Translation]: object;
|
|
33
|
+
} ? L extends keyof T[NodeType.Translation] ? DeepTransformContent<T[NodeType.Translation][L], L> : never : T;
|
|
34
|
+
type DeepTransformContent<T, L extends Locales> = T extends object ? T extends (infer U)[] ? DeepTransformContent<U, L>[] : T extends {
|
|
35
|
+
nodeType: NodeType | string;
|
|
36
|
+
} ? TransformNodeType<T, L> : T extends {
|
|
37
|
+
_owner: any;
|
|
38
|
+
key: any;
|
|
39
|
+
props: any;
|
|
40
|
+
ref: any;
|
|
41
|
+
} ? ReactNode : {
|
|
42
|
+
[K in keyof T]: DeepTransformContent<T[K], L>;
|
|
43
|
+
} : T extends undefined ? never : IntlayerNode<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Excludes the 'id' and 'filePath' keys from the dictionary content,
|
|
46
|
+
* as they are not part of the IntlayerDictionaryTypesConnector type.
|
|
47
|
+
*/
|
|
48
|
+
type ExcludeIntlayerUtilsKeys<T> = Omit<T, 'id' | 'filePath'>;
|
|
49
|
+
/**
|
|
50
|
+
* Represents the data type returned by the useIntlayer hook,
|
|
51
|
+
* excluding the 'id' and 'filePath' keys from the dictionary content.
|
|
52
|
+
*/
|
|
53
|
+
type Data<T extends DictionaryKeys, K extends Locales> = ExcludeIntlayerUtilsKeys<DeepTransformContent<IntlayerDictionaryTypesConnector[T], K>>;
|
|
54
|
+
/**
|
|
55
|
+
* Parcourt the object. If a object has a keyPath, render the intlayer editor if editor enabled.
|
|
56
|
+
*/
|
|
57
|
+
declare const recursiveTransformContent: (value: any) => object;
|
|
58
|
+
/**
|
|
59
|
+
* Type definition for the useIntlayer hook, which takes a dictionary ID and an optional locale,
|
|
60
|
+
* and returns the deeply transformed dictionary content.
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
type UseIntlayer = <T extends DictionaryKeys, L extends Locales>(id: T, locale?: L) => Data<T, L>;
|
|
7
64
|
/**
|
|
8
|
-
* Hook that
|
|
65
|
+
* Hook that picks one dictionary by its ID and returns the content,
|
|
66
|
+
* deeply transformed according to the dictionary structure and metadata.
|
|
9
67
|
*/
|
|
10
68
|
declare const useIntlayerBase: UseIntlayer;
|
|
11
69
|
|
|
12
|
-
export { type DictionaryKeys, type StringFallback, type UseIntlayer, useIntlayerBase };
|
|
70
|
+
export { type DictionaryKeys, type IntlayerNode, type StringFallback, type UseIntlayer, recursiveTransformContent, useIntlayerBase };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/client/ContentEditionLayout.tsx"],"sourcesContent":["import {\n ContentEditionLayout as ContentEditionLayoutBase,\n type ContentEditionLayoutProps,\n} from 'intlayer-editor/client';\nimport type { FC } from 'react';\n\nconst BlankLayout: FC<ContentEditionLayoutProps> = ({ children }) => (\n <>{children}</>\n);\n\n// intlayer-editor is an optional dependency. If it's not installed, return the blank layout\nexport const ContentEditionLayout: FC<ContentEditionLayoutProps> =\n typeof ContentEditionLayoutBase === 'undefined'\n ? BlankLayout\n :
|
|
1
|
+
{"version":3,"sources":["../../../src/client/ContentEditionLayout.tsx"],"sourcesContent":["import {\n ContentEditionLayout as ContentEditionLayoutBase,\n type ContentEditionLayoutProps,\n} from 'intlayer-editor/client';\nimport type { FC } from 'react';\n\nconst BlankLayout: FC<ContentEditionLayoutProps> = ({ children }) => (\n <>{children}</>\n);\n\n// intlayer-editor is an optional dependency. If it's not installed, return the blank layout\nexport const ContentEditionLayout: FC<ContentEditionLayoutProps> =\n typeof ContentEditionLayoutBase === 'undefined'\n ? BlankLayout\n : ContentEditionLayoutBase;\n"],"mappings":";AAOE;AAPF;AAAA,EACE,wBAAwB;AAAA,OAEnB;AAGP,MAAM,cAA6C,CAAC,EAAE,SAAS,MAC7D,gCAAG,UAAS;AAIP,MAAM,uBACX,OAAO,6BAA6B,cAChC,cACA;","names":[]}
|
|
@@ -14,12 +14,14 @@ declare const IntlayerClientContext: react.Context<IntlayerValue>;
|
|
|
14
14
|
* Hook that provides the current locale
|
|
15
15
|
*/
|
|
16
16
|
declare const useIntlayerContext: () => IntlayerValue;
|
|
17
|
-
type
|
|
17
|
+
type IntlayerProviderProps = PropsWithChildren & {
|
|
18
18
|
locale?: Locales;
|
|
19
|
+
setLocale?: (locale: Locales) => void;
|
|
20
|
+
editorEnabled?: boolean;
|
|
19
21
|
};
|
|
20
22
|
/**
|
|
21
23
|
* Provider that store the current locale on the client side
|
|
22
24
|
*/
|
|
23
|
-
declare const
|
|
25
|
+
declare const IntlayerProvider: FC<IntlayerProviderProps>;
|
|
24
26
|
|
|
25
|
-
export { IntlayerClientContext,
|
|
27
|
+
export { IntlayerClientContext, IntlayerProvider, type IntlayerProviderProps, useIntlayerContext };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import "../chunk-
|
|
2
|
+
import "../chunk-ZD7AOCMD.mjs";
|
|
3
3
|
import { jsx } from "react/jsx-runtime";
|
|
4
4
|
import { getConfiguration } from "@intlayer/config/client";
|
|
5
5
|
import {
|
|
@@ -16,18 +16,19 @@ const IntlayerClientContext = createContext({
|
|
|
16
16
|
setLocale: () => null
|
|
17
17
|
});
|
|
18
18
|
const useIntlayerContext = () => useContext(IntlayerClientContext);
|
|
19
|
-
const
|
|
19
|
+
const IntlayerProvider = ({
|
|
20
20
|
locale,
|
|
21
|
-
children
|
|
21
|
+
children,
|
|
22
|
+
setLocale: setLocaleProp,
|
|
23
|
+
editorEnabled
|
|
22
24
|
}) => {
|
|
23
25
|
const { defaultLocale, locales: availableLocales } = getConfiguration().internationalization;
|
|
24
26
|
const [currentLocale, setCurrentLocale] = useState(
|
|
25
27
|
locale ?? localeCookie ?? defaultLocale
|
|
26
28
|
);
|
|
27
|
-
const
|
|
29
|
+
const setLocaleBase = useCallback(
|
|
28
30
|
(newLocale) => {
|
|
29
|
-
if (currentLocale.toString() === newLocale.toString())
|
|
30
|
-
return;
|
|
31
|
+
if (currentLocale.toString() === newLocale.toString()) return;
|
|
31
32
|
if (!availableLocales.includes(newLocale)) {
|
|
32
33
|
console.error(`Locale ${locale} is not available`);
|
|
33
34
|
return;
|
|
@@ -37,6 +38,7 @@ const IntlayerClientProvider = ({
|
|
|
37
38
|
},
|
|
38
39
|
[availableLocales, currentLocale, locale]
|
|
39
40
|
);
|
|
41
|
+
const setLocale = setLocaleProp ?? setLocaleBase;
|
|
40
42
|
const value = useMemo(
|
|
41
43
|
() => ({ locale: currentLocale, setLocale }),
|
|
42
44
|
[currentLocale, setLocale]
|
|
@@ -47,13 +49,14 @@ const IntlayerClientProvider = ({
|
|
|
47
49
|
locale: currentLocale,
|
|
48
50
|
setLocale,
|
|
49
51
|
localeList: availableLocales,
|
|
52
|
+
editorEnabled,
|
|
50
53
|
children
|
|
51
54
|
}
|
|
52
55
|
) });
|
|
53
56
|
};
|
|
54
57
|
export {
|
|
55
58
|
IntlayerClientContext,
|
|
56
|
-
|
|
59
|
+
IntlayerProvider,
|
|
57
60
|
useIntlayerContext
|
|
58
61
|
};
|
|
59
|
-
//# sourceMappingURL=
|
|
62
|
+
//# sourceMappingURL=IntlayerProvider.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/client/IntlayerProvider.tsx"],"sourcesContent":["'use client';\n\nimport { getConfiguration, type Locales } from '@intlayer/config/client';\nimport {\n type PropsWithChildren,\n createContext,\n useContext,\n useMemo,\n type FC,\n useState,\n useCallback,\n} from 'react';\nimport { ContentEditionLayout } from './ContentEditionLayout';\nimport { localeCookie, setLocaleCookie } from './useLocaleCookie';\n\ntype IntlayerValue = {\n locale: Locales;\n setLocale: (newLocale: Locales) => void;\n};\n\n/**\n * Context that store the current locale on the client side\n */\nexport const IntlayerClientContext = createContext<IntlayerValue>({\n locale: localeCookie ?? getConfiguration().internationalization.defaultLocale,\n setLocale: () => null,\n});\n\n/**\n * Hook that provides the current locale\n */\nexport const useIntlayerContext = () => useContext(IntlayerClientContext);\n\nexport type IntlayerProviderProps = PropsWithChildren & {\n locale?: Locales;\n setLocale?: (locale: Locales) => void;\n editorEnabled?: boolean;\n};\n\n/**\n * Provider that store the current locale on the client side\n */\nexport const IntlayerProvider: FC<IntlayerProviderProps> = ({\n locale,\n children,\n setLocale: setLocaleProp,\n editorEnabled,\n}) => {\n const { defaultLocale, locales: availableLocales } =\n getConfiguration().internationalization;\n\n const [currentLocale, setCurrentLocale] = useState(\n locale ?? localeCookie ?? defaultLocale\n );\n\n const setLocaleBase = useCallback(\n (newLocale: Locales) => {\n if (currentLocale.toString() === newLocale.toString()) return;\n\n if (!availableLocales.includes(newLocale)) {\n console.error(`Locale ${locale} is not available`);\n return;\n }\n\n setCurrentLocale(newLocale); // Update state\n setLocaleCookie(newLocale); // Optionally set cookie for persistence\n },\n [availableLocales, currentLocale, locale]\n );\n\n const setLocale = setLocaleProp ?? setLocaleBase;\n\n const value: IntlayerValue = useMemo<IntlayerValue>(\n () => ({ locale: currentLocale, setLocale: setLocale }),\n [currentLocale, setLocale]\n );\n\n return (\n <IntlayerClientContext.Provider value={value}>\n <ContentEditionLayout\n locale={currentLocale}\n setLocale={setLocale}\n localeList={availableLocales}\n editorEnabled={editorEnabled}\n >\n {children}\n </ContentEditionLayout>\n </IntlayerClientContext.Provider>\n );\n};\n"],"mappings":";;AA+EM;AA7EN,SAAS,wBAAsC;AAC/C;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EACA;AAAA,OACK;AACP,SAAS,4BAA4B;AACrC,SAAS,cAAc,uBAAuB;AAUvC,MAAM,wBAAwB,cAA6B;AAAA,EAChE,QAAQ,gBAAgB,iBAAiB,EAAE,qBAAqB;AAAA,EAChE,WAAW,MAAM;AACnB,CAAC;AAKM,MAAM,qBAAqB,MAAM,WAAW,qBAAqB;AAWjE,MAAM,mBAA8C,CAAC;AAAA,EAC1D;AAAA,EACA;AAAA,EACA,WAAW;AAAA,EACX;AACF,MAAM;AACJ,QAAM,EAAE,eAAe,SAAS,iBAAiB,IAC/C,iBAAiB,EAAE;AAErB,QAAM,CAAC,eAAe,gBAAgB,IAAI;AAAA,IACxC,UAAU,gBAAgB;AAAA,EAC5B;AAEA,QAAM,gBAAgB;AAAA,IACpB,CAAC,cAAuB;AACtB,UAAI,cAAc,SAAS,MAAM,UAAU,SAAS,EAAG;AAEvD,UAAI,CAAC,iBAAiB,SAAS,SAAS,GAAG;AACzC,gBAAQ,MAAM,UAAU,MAAM,mBAAmB;AACjD;AAAA,MACF;AAEA,uBAAiB,SAAS;AAC1B,sBAAgB,SAAS;AAAA,IAC3B;AAAA,IACA,CAAC,kBAAkB,eAAe,MAAM;AAAA,EAC1C;AAEA,QAAM,YAAY,iBAAiB;AAEnC,QAAM,QAAuB;AAAA,IAC3B,OAAO,EAAE,QAAQ,eAAe,UAAqB;AAAA,IACrD,CAAC,eAAe,SAAS;AAAA,EAC3B;AAEA,SACE,oBAAC,sBAAsB,UAAtB,EAA+B,OAC9B;AAAA,IAAC;AAAA;AAAA,MACC,QAAQ;AAAA,MACR;AAAA,MACA,YAAY;AAAA,MACZ;AAAA,MAEC;AAAA;AAAA,EACH,GACF;AAEJ;","names":[]}
|