Compare commits
38 Commits
v10.0.0-rc
...
v10.0.0-rc
Author | SHA1 | Date | |
---|---|---|---|
7d42ce1c87 | |||
57f6f980cf | |||
8cba3aae2c | |||
01b32f78ed | |||
b6066dfd5f | |||
3ad554ed59 | |||
6aacc6361b | |||
638e4e6410 | |||
aa9b7cccc7 | |||
41739c8528 | |||
89b32dc7fc | |||
44aec23251 | |||
12fd6160c5 | |||
8819abc418 | |||
96b627095c | |||
dba004f924 | |||
5423a07c47 | |||
aba725372e | |||
a61aa9dd5d | |||
74349b20ce | |||
09ab9a1c8f | |||
abfe5789a3 | |||
67ebac496d | |||
60a2bf173b | |||
4e03f07864 | |||
aef1709d7f | |||
2f590f7be2 | |||
d5fa6ca89a | |||
8eaaffb25a | |||
28c5e2bab2 | |||
e212039f2c | |||
99b0b67f77 | |||
6ec9ba3c01 | |||
d7960a7dcf | |||
2a6e9af9c9 | |||
327e4d1f90 | |||
fffadd7b9e | |||
aaaf0d2e77 |
17
.github/workflows/ci.yaml
vendored
17
.github/workflows/ci.yaml
vendored
@ -16,8 +16,8 @@ jobs:
|
|||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- uses: actions/setup-node@v4
|
- uses: actions/setup-node@v4
|
||||||
- uses: bahmutov/npm-install@v1
|
- uses: bahmutov/npm-install@v1
|
||||||
- name: If this step fails run 'yarn format' then commit again.
|
- name: If this step fails run 'npm run format' then commit again.
|
||||||
run: yarn format:check
|
run: npm run format:check
|
||||||
test:
|
test:
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
needs: test_lint
|
needs: test_lint
|
||||||
@ -32,13 +32,12 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
node-version: ${{ matrix.node }}
|
node-version: ${{ matrix.node }}
|
||||||
- uses: bahmutov/npm-install@v1
|
- uses: bahmutov/npm-install@v1
|
||||||
- run: yarn build
|
- run: npm run build
|
||||||
- run: yarn test
|
- run: npm run test
|
||||||
#- run: yarn test:keycloakify-starter
|
|
||||||
|
|
||||||
storybook:
|
storybook:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.event_name == 'push'
|
#if: github.event_name == 'push'
|
||||||
needs: test
|
needs: test
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
@ -46,11 +45,11 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
node-version: '18'
|
node-version: '18'
|
||||||
- uses: bahmutov/npm-install@v1
|
- uses: bahmutov/npm-install@v1
|
||||||
- run: yarn build-storybook -o ./build_storybook
|
- run: npm run build-storybook
|
||||||
- run: git remote set-url origin https://git:${GITHUB_TOKEN}@github.com/${{github.repository}}.git
|
- run: git remote set-url origin https://git:${GITHUB_TOKEN}@github.com/${{github.repository}}.git
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
- run: npx -y -p gh-pages@3.1.0 gh-pages -d ./build_storybook -u "github-actions-bot <actions@github.com>"
|
- run: npx -y -p gh-pages@3.1.0 gh-pages -d ./storybook-static -u "github-actions-bot <actions@github.com>"
|
||||||
|
|
||||||
check_if_version_upgraded:
|
check_if_version_upgraded:
|
||||||
name: Check if version upgrade
|
name: Check if version upgrade
|
||||||
@ -112,7 +111,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
registry-url: https://registry.npmjs.org/
|
registry-url: https://registry.npmjs.org/
|
||||||
- uses: bahmutov/npm-install@v1
|
- uses: bahmutov/npm-install@v1
|
||||||
- run: yarn build
|
- run: npm run build
|
||||||
- run: npx -y -p denoify@1.6.12 enable_short_npm_import_path
|
- run: npx -y -p denoify@1.6.12 enable_short_npm_import_path
|
||||||
env:
|
env:
|
||||||
DRY_RUN: "0"
|
DRY_RUN: "0"
|
||||||
|
15
package.json
15
package.json
@ -1,13 +1,13 @@
|
|||||||
{
|
{
|
||||||
"name": "keycloakify",
|
"name": "keycloakify",
|
||||||
"version": "10.0.0-rc.66",
|
"version": "10.0.0-rc.81",
|
||||||
"description": "Create Keycloak themes using React",
|
"description": "Create Keycloak themes using React",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/keycloakify/keycloakify.git"
|
"url": "git://github.com/keycloakify/keycloakify.git"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare": "patch-package && tsx scripts/generate-i18n-messages.ts",
|
"prepare": "tsx scripts/generate-i18n-messages.ts",
|
||||||
"build": "tsx scripts/build.ts",
|
"build": "tsx scripts/build.ts",
|
||||||
"storybook": "tsx scripts/start-storybook.ts",
|
"storybook": "tsx scripts/start-storybook.ts",
|
||||||
"link-in-starter": "tsx scripts/link-in-starter.ts",
|
"link-in-starter": "tsx scripts/link-in-starter.ts",
|
||||||
@ -41,10 +41,10 @@
|
|||||||
"!dist/bin/",
|
"!dist/bin/",
|
||||||
"dist/bin/main.js",
|
"dist/bin/main.js",
|
||||||
"dist/bin/*.index.js",
|
"dist/bin/*.index.js",
|
||||||
|
"!dist/bin/shared/*.js",
|
||||||
"dist/bin/shared/constants.js",
|
"dist/bin/shared/constants.js",
|
||||||
"dist/bin/shared/constants.d.ts",
|
"dist/bin/shared/*.d.ts",
|
||||||
"dist/bin/shared/constants.js.map",
|
"dist/bin/shared/*.js.map",
|
||||||
"dist/bin/shared/buildContext.d.ts",
|
|
||||||
"!dist/vite-plugin/",
|
"!dist/vite-plugin/",
|
||||||
"dist/vite-plugin/index.d.ts",
|
"dist/vite-plugin/index.d.ts",
|
||||||
"dist/vite-plugin/vite-plugin.d.ts",
|
"dist/vite-plugin/vite-plugin.d.ts",
|
||||||
@ -66,7 +66,7 @@
|
|||||||
"react": "*"
|
"react": "*"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"react-markdown": "^5.0.3",
|
"react-markdown": "^9.0.1",
|
||||||
"tsafe": "^1.6.6"
|
"tsafe": "^1.6.6"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@ -102,7 +102,6 @@
|
|||||||
"lint-staged": "^11.0.0",
|
"lint-staged": "^11.0.0",
|
||||||
"magic-string": "^0.30.7",
|
"magic-string": "^0.30.7",
|
||||||
"make-fetch-happen": "^11.0.3",
|
"make-fetch-happen": "^11.0.3",
|
||||||
"patch-package": "^8.0.0",
|
|
||||||
"powerhooks": "^1.0.10",
|
"powerhooks": "^1.0.10",
|
||||||
"prettier": "^3.2.5",
|
"prettier": "^3.2.5",
|
||||||
"properties-parser": "^0.3.1",
|
"properties-parser": "^0.3.1",
|
||||||
@ -111,7 +110,7 @@
|
|||||||
"recast": "^0.23.3",
|
"recast": "^0.23.3",
|
||||||
"run-exclusive": "^2.2.19",
|
"run-exclusive": "^2.2.19",
|
||||||
"storybook-dark-mode": "^1.1.2",
|
"storybook-dark-mode": "^1.1.2",
|
||||||
"termost": "^0.12.0",
|
"termost": "^v0.12.1",
|
||||||
"tsc-alias": "^1.8.10",
|
"tsc-alias": "^1.8.10",
|
||||||
"tss-react": "^4.9.10",
|
"tss-react": "^4.9.10",
|
||||||
"typescript": "^4.9.1-beta",
|
"typescript": "^4.9.1-beta",
|
||||||
|
File diff suppressed because one or more lines are too long
@ -13,7 +13,7 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
|
|
||||||
const { kcClsx } = getKcClsx({ doUseDefaultCss, classes });
|
const { kcClsx } = getKcClsx({ doUseDefaultCss, classes });
|
||||||
|
|
||||||
const { msg, msgStr, getChangeLocalUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n;
|
const { msg, msgStr, getChangeLocaleUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n;
|
||||||
|
|
||||||
const { locale, url, features, realm, message, referrer } = kcContext;
|
const { locale, url, features, realm, message, referrer } = kcContext;
|
||||||
|
|
||||||
@ -79,7 +79,7 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
<ul>
|
<ul>
|
||||||
{locale.supported.map(({ languageTag }) => (
|
{locale.supported.map(({ languageTag }) => (
|
||||||
<li key={languageTag} className="kc-dropdown-item">
|
<li key={languageTag} className="kc-dropdown-item">
|
||||||
<a href={getChangeLocalUrl(languageTag)}>{labelBySupportedLanguageTag[languageTag]}</a>
|
<a href={getChangeLocaleUrl(languageTag)}>{labelBySupportedLanguageTag[languageTag]}</a>
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
import "keycloakify/tools/Object.fromEntries";
|
import "keycloakify/tools/Object.fromEntries";
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { assert } from "tsafe/assert";
|
import { assert } from "tsafe/assert";
|
||||||
import messages_fallbackLanguage from "./baseMessages/en";
|
import messages_fallbackLanguage from "./baseMessages/en";
|
||||||
import { getMessages } from "./baseMessages";
|
import { getMessages } from "./baseMessages";
|
||||||
import type { KcContext } from "../KcContext";
|
import type { KcContext } from "../KcContext";
|
||||||
import { Reflect } from "tsafe/Reflect";
|
|
||||||
|
|
||||||
export const fallbackLanguageTag = "en";
|
export const fallbackLanguageTag = "en";
|
||||||
|
|
||||||
@ -30,7 +28,7 @@ export type GenericI18n<MessageKey extends string> = {
|
|||||||
* Redirect to this url to change the language.
|
* Redirect to this url to change the language.
|
||||||
* After reload currentLanguageTag === newLanguageTag
|
* After reload currentLanguageTag === newLanguageTag
|
||||||
*/
|
*/
|
||||||
getChangeLocalUrl: (newLanguageTag: string) => string;
|
getChangeLocaleUrl: (newLanguageTag: string) => string;
|
||||||
/**
|
/**
|
||||||
* e.g. "en" => "English", "fr" => "Français", ...
|
* e.g. "en" => "English", "fr" => "Français", ...
|
||||||
*
|
*
|
||||||
@ -88,7 +86,9 @@ export type GenericI18n<MessageKey extends string> = {
|
|||||||
isFetchingTranslations: boolean;
|
isFetchingTranslations: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: { [languageTag: string]: { [key in ExtraMessageKey]: string } }) {
|
export function createGetI18n<ExtraMessageKey extends string = never>(messageBundle: {
|
||||||
|
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||||
|
}) {
|
||||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||||
|
|
||||||
type Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined };
|
type Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined };
|
||||||
@ -108,9 +108,9 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|||||||
return cachedResult;
|
return cachedResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
const partialI18n: Pick<I18n, "currentLanguageTag" | "getChangeLocalUrl" | "labelBySupportedLanguageTag"> = {
|
const partialI18n: Pick<I18n, "currentLanguageTag" | "getChangeLocaleUrl" | "labelBySupportedLanguageTag"> = {
|
||||||
currentLanguageTag: kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag,
|
currentLanguageTag: kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag,
|
||||||
getChangeLocalUrl: newLanguageTag => {
|
getChangeLocaleUrl: newLanguageTag => {
|
||||||
const { locale } = kcContext;
|
const { locale } = kcContext;
|
||||||
|
|
||||||
assert(locale !== undefined, "Internationalization not enabled");
|
assert(locale !== undefined, "Internationalization not enabled");
|
||||||
@ -126,8 +126,8 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|||||||
|
|
||||||
const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
|
const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
|
||||||
messages_fallbackLanguage,
|
messages_fallbackLanguage,
|
||||||
extraMessages_fallbackLanguage: extraMessages[fallbackLanguageTag],
|
messageBundle_fallbackLanguage: messageBundle[fallbackLanguageTag],
|
||||||
extraMessages: extraMessages[partialI18n.currentLanguageTag]
|
messageBundle_currentLanguage: messageBundle[partialI18n.currentLanguageTag]
|
||||||
});
|
});
|
||||||
|
|
||||||
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
|
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
|
||||||
@ -135,17 +135,19 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|||||||
const result: Result = {
|
const result: Result = {
|
||||||
i18n: {
|
i18n: {
|
||||||
...partialI18n,
|
...partialI18n,
|
||||||
...createI18nTranslationFunctions({ messages: undefined }),
|
...createI18nTranslationFunctions({
|
||||||
|
messages_currentLanguage: isCurrentLanguageFallbackLanguage ? messages_fallbackLanguage : undefined
|
||||||
|
}),
|
||||||
isFetchingTranslations: !isCurrentLanguageFallbackLanguage
|
isFetchingTranslations: !isCurrentLanguageFallbackLanguage
|
||||||
},
|
},
|
||||||
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
|
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
|
||||||
? undefined
|
? undefined
|
||||||
: (async () => {
|
: (async () => {
|
||||||
const messages = await getMessages(partialI18n.currentLanguageTag);
|
const messages_currentLanguage = await getMessages(partialI18n.currentLanguageTag);
|
||||||
|
|
||||||
const i18n_currentLanguage: I18n = {
|
const i18n_currentLanguage: I18n = {
|
||||||
...partialI18n,
|
...partialI18n,
|
||||||
...createI18nTranslationFunctions({ messages }),
|
...createI18nTranslationFunctions({ messages_currentLanguage }),
|
||||||
isFetchingTranslations: false
|
isFetchingTranslations: false
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -168,66 +170,30 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|||||||
return { getI18n };
|
return { getI18n };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createUseI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|
||||||
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
|
||||||
}) {
|
|
||||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
|
||||||
|
|
||||||
const { getI18n } = createGetI18n(extraMessages);
|
|
||||||
|
|
||||||
function useI18n(params: { kcContext: KcContextLike }): { i18n: I18n } {
|
|
||||||
const { kcContext } = params;
|
|
||||||
|
|
||||||
const { i18n, prI18n_currentLanguage } = getI18n({ kcContext });
|
|
||||||
|
|
||||||
const [i18n_toReturn, setI18n_toReturn] = useState<I18n>(i18n);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
let isActive = true;
|
|
||||||
|
|
||||||
prI18n_currentLanguage?.then(i18n => {
|
|
||||||
if (!isActive) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setI18n_toReturn(i18n);
|
|
||||||
});
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
isActive = false;
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return { i18n: i18n_toReturn };
|
|
||||||
}
|
|
||||||
|
|
||||||
return { useI18n, ofTypeI18n: Reflect<I18n>() };
|
|
||||||
}
|
|
||||||
|
|
||||||
function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraMessageKey extends string>(params: {
|
function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraMessageKey extends string>(params: {
|
||||||
messages_fallbackLanguage: Record<MessageKey, string>;
|
messages_fallbackLanguage: Record<MessageKey, string>;
|
||||||
extraMessages_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
messageBundle_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||||
extraMessages: Partial<Record<ExtraMessageKey, string>> | undefined;
|
messageBundle_currentLanguage: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||||
}) {
|
}) {
|
||||||
const { extraMessages } = params;
|
const { messageBundle_currentLanguage } = params;
|
||||||
|
|
||||||
const messages_fallbackLanguage = {
|
const messages_fallbackLanguage = {
|
||||||
...params.messages_fallbackLanguage,
|
...params.messages_fallbackLanguage,
|
||||||
...params.extraMessages_fallbackLanguage
|
...params.messageBundle_fallbackLanguage
|
||||||
};
|
};
|
||||||
|
|
||||||
function createI18nTranslationFunctions(params: {
|
function createI18nTranslationFunctions(params: {
|
||||||
messages: Partial<Record<MessageKey, string>> | undefined;
|
messages_currentLanguage: Partial<Record<MessageKey, string>> | undefined;
|
||||||
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
|
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
|
||||||
const messages = {
|
const messages_currentLanguage = {
|
||||||
...params.messages,
|
...params.messages_currentLanguage,
|
||||||
...extraMessages
|
...messageBundle_currentLanguage
|
||||||
};
|
};
|
||||||
|
|
||||||
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
|
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
|
||||||
const { key, args, doRenderAsHtml } = props;
|
const { key, args, doRenderAsHtml } = props;
|
||||||
|
|
||||||
const messageOrUndefined: string | undefined = (messages as any)[key] ?? (messages_fallbackLanguage as any)[key];
|
const messageOrUndefined: string | undefined = (messages_currentLanguage as any)[key] ?? (messages_fallbackLanguage as any)[key];
|
||||||
|
|
||||||
if (messageOrUndefined === undefined) {
|
if (messageOrUndefined === undefined) {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
|
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
|
||||||
export type { MessageKey, KcContextLike };
|
export type { MessageKey, KcContextLike };
|
||||||
export type I18n = GenericI18n<MessageKey>;
|
export type I18n = GenericI18n<MessageKey>;
|
||||||
export { createUseI18n } from "./i18n";
|
export { createUseI18n } from "./useI18n";
|
||||||
export { fallbackLanguageTag } from "./i18n";
|
export { fallbackLanguageTag } from "./i18n";
|
||||||
|
44
src/account/i18n/useI18n.ts
Normal file
44
src/account/i18n/useI18n.ts
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import {
|
||||||
|
createGetI18n,
|
||||||
|
type GenericI18n,
|
||||||
|
type MessageKey,
|
||||||
|
type KcContextLike
|
||||||
|
} from "./i18n";
|
||||||
|
import { Reflect } from "tsafe/Reflect";
|
||||||
|
|
||||||
|
export function createUseI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||||
|
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||||
|
}) {
|
||||||
|
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||||
|
|
||||||
|
const { getI18n } = createGetI18n(extraMessages);
|
||||||
|
|
||||||
|
function useI18n(params: { kcContext: KcContextLike }): { i18n: I18n } {
|
||||||
|
const { kcContext } = params;
|
||||||
|
|
||||||
|
const { i18n, prI18n_currentLanguage } = getI18n({ kcContext });
|
||||||
|
|
||||||
|
const [i18n_toReturn, setI18n_toReturn] = useState<I18n>(i18n);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let isActive = true;
|
||||||
|
|
||||||
|
prI18n_currentLanguage?.then(i18n => {
|
||||||
|
if (!isActive) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setI18n_toReturn(i18n);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
isActive = false;
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { i18n: i18n_toReturn };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { useI18n, ofTypeI18n: Reflect<I18n>() };
|
||||||
|
}
|
@ -190,6 +190,8 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
|
|||||||
|
|
||||||
const userProfileFormFieldComponentName = "UserProfileFormFields";
|
const userProfileFormFieldComponentName = "UserProfileFormFields";
|
||||||
|
|
||||||
|
const componentName = componentBasename.replace(/.tsx$/, "");
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
[
|
[
|
||||||
``,
|
``,
|
||||||
@ -207,10 +209,7 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
|
|||||||
`// ...`,
|
`// ...`,
|
||||||
``,
|
``,
|
||||||
chalk.green(
|
chalk.green(
|
||||||
`+const ${componentBasename.replace(
|
`+const ${componentName} = lazy(() => import("./pages/${componentName}"));`
|
||||||
/.tsx$/,
|
|
||||||
""
|
|
||||||
)} = lazy(() => import("./pages/${componentBasename}"));`
|
|
||||||
),
|
),
|
||||||
...[
|
...[
|
||||||
``,
|
``,
|
||||||
@ -224,7 +223,7 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
|
|||||||
` switch (kcContext.pageId) {`,
|
` switch (kcContext.pageId) {`,
|
||||||
` // ...`,
|
` // ...`,
|
||||||
`+ case "${pageIdOrComponent}": return (`,
|
`+ case "${pageIdOrComponent}": return (`,
|
||||||
`+ <${componentBasename}`,
|
`+ <${componentName}`,
|
||||||
`+ {...{ kcContext, i18n, classes }}`,
|
`+ {...{ kcContext, i18n, classes }}`,
|
||||||
`+ Template={Template}`,
|
`+ Template={Template}`,
|
||||||
`+ doUseDefaultCss={true}`,
|
`+ doUseDefaultCss={true}`,
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import cheerio from "cheerio";
|
import cheerio from "cheerio";
|
||||||
import { replaceImportsInJsCode } from "../replacers/replaceImportsInJsCode";
|
import { replaceImportsInJsCode } from "../replacers/replaceImportsInJsCode";
|
||||||
import { generateCssCodeToDefineGlobals } from "../replacers/replaceImportsInCssCode";
|
import { replaceImportsInCssCode } from "../replacers/replaceImportsInCssCode";
|
||||||
import { replaceImportsInInlineCssCode } from "../replacers/replaceImportsInInlineCssCode";
|
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { join as pathJoin } from "path";
|
import { join as pathJoin } from "path";
|
||||||
import type { BuildContext } from "../../shared/buildContext";
|
import type { BuildContext } from "../../shared/buildContext";
|
||||||
@ -9,8 +8,7 @@ import { assert } from "tsafe/assert";
|
|||||||
import {
|
import {
|
||||||
type ThemeType,
|
type ThemeType,
|
||||||
basenameOfTheKeycloakifyResourcesDir,
|
basenameOfTheKeycloakifyResourcesDir,
|
||||||
resources_common,
|
resources_common
|
||||||
nameOfTheLocalizationRealmOverridesUserProfileProperty
|
|
||||||
} from "../../shared/constants";
|
} from "../../shared/constants";
|
||||||
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
|
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
|
||||||
|
|
||||||
@ -28,7 +26,6 @@ assert<BuildContext extends BuildContextLike ? true : false>();
|
|||||||
export function generateFtlFilesCodeFactory(params: {
|
export function generateFtlFilesCodeFactory(params: {
|
||||||
themeName: string;
|
themeName: string;
|
||||||
indexHtmlCode: string;
|
indexHtmlCode: string;
|
||||||
cssGlobalsToDefine: Record<string, string>;
|
|
||||||
buildContext: BuildContextLike;
|
buildContext: BuildContextLike;
|
||||||
keycloakifyVersion: string;
|
keycloakifyVersion: string;
|
||||||
themeType: ThemeType;
|
themeType: ThemeType;
|
||||||
@ -36,7 +33,6 @@ export function generateFtlFilesCodeFactory(params: {
|
|||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
themeName,
|
themeName,
|
||||||
cssGlobalsToDefine,
|
|
||||||
indexHtmlCode,
|
indexHtmlCode,
|
||||||
buildContext,
|
buildContext,
|
||||||
keycloakifyVersion,
|
keycloakifyVersion,
|
||||||
@ -65,8 +61,9 @@ export function generateFtlFilesCodeFactory(params: {
|
|||||||
|
|
||||||
assert(cssCode !== null);
|
assert(cssCode !== null);
|
||||||
|
|
||||||
const { fixedCssCode } = replaceImportsInInlineCssCode({
|
const { fixedCssCode } = replaceImportsInCssCode({
|
||||||
cssCode,
|
cssCode,
|
||||||
|
cssFileRelativeDirPath: undefined,
|
||||||
buildContext
|
buildContext
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -97,21 +94,6 @@ export function generateFtlFilesCodeFactory(params: {
|
|||||||
);
|
);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
if (Object.keys(cssGlobalsToDefine).length !== 0) {
|
|
||||||
$("head").prepend(
|
|
||||||
[
|
|
||||||
"",
|
|
||||||
"<style>",
|
|
||||||
generateCssCodeToDefineGlobals({
|
|
||||||
cssGlobalsToDefine,
|
|
||||||
buildContext
|
|
||||||
}).cssCodeToPrependInHead,
|
|
||||||
"</style>",
|
|
||||||
""
|
|
||||||
].join("\n")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//FTL is no valid html, we can't insert with cheerio, we put placeholder for injecting later.
|
//FTL is no valid html, we can't insert with cheerio, we put placeholder for injecting later.
|
||||||
@ -136,10 +118,6 @@ export function generateFtlFilesCodeFactory(params: {
|
|||||||
.replace("KEYCLOAKIFY_THEME_TYPE_dExKd3xEdr", themeType)
|
.replace("KEYCLOAKIFY_THEME_TYPE_dExKd3xEdr", themeType)
|
||||||
.replace("KEYCLOAKIFY_THEME_NAME_cXxKd3xEer", themeName)
|
.replace("KEYCLOAKIFY_THEME_NAME_cXxKd3xEer", themeName)
|
||||||
.replace("RESOURCES_COMMON_cLsLsMrtDkpVv", resources_common)
|
.replace("RESOURCES_COMMON_cLsLsMrtDkpVv", resources_common)
|
||||||
.replace(
|
|
||||||
"lOCALIZATION_REALM_OVERRIDES_USER_PROFILE_PROPERTY_KEY_aaGLsPgGIdeeX",
|
|
||||||
nameOfTheLocalizationRealmOverridesUserProfileProperty
|
|
||||||
)
|
|
||||||
.replace(
|
.replace(
|
||||||
"USER_DEFINED_EXCLUSIONS_eKsaY4ZsZ4eMr2",
|
"USER_DEFINED_EXCLUSIONS_eKsaY4ZsZ4eMr2",
|
||||||
buildContext.kcContextExclusionsFtlCode ?? ""
|
buildContext.kcContextExclusionsFtlCode ?? ""
|
||||||
|
@ -33,8 +33,9 @@ kcContext.pageId = "${pageId}";
|
|||||||
if( kcContext.url && kcContext.url.resourcesPath ){
|
if( kcContext.url && kcContext.url.resourcesPath ){
|
||||||
kcContext.url.resourcesCommonPath = kcContext.url.resourcesPath + "/" + "RESOURCES_COMMON_cLsLsMrtDkpVv";
|
kcContext.url.resourcesCommonPath = kcContext.url.resourcesPath + "/" + "RESOURCES_COMMON_cLsLsMrtDkpVv";
|
||||||
}
|
}
|
||||||
|
kcContext["x-keycloakify"] = {};
|
||||||
<#if profile?? && profile.attributes??>
|
<#if profile?? && profile.attributes??>
|
||||||
kcContext.lOCALIZATION_REALM_OVERRIDES_USER_PROFILE_PROPERTY_KEY_aaGLsPgGIdeeX = {
|
kcContext["x-keycloakify"].realmMessageBundleUserProfile = {
|
||||||
<#list profile.attributes as attribute>
|
<#list profile.attributes as attribute>
|
||||||
<#if attribute.annotations?? && attribute.displayName??>
|
<#if attribute.annotations?? && attribute.displayName??>
|
||||||
"${attribute.displayName}": decodeHtmlEntities("${advancedMsg(attribute.displayName)?js_string}"),
|
"${attribute.displayName}": decodeHtmlEntities("${advancedMsg(attribute.displayName)?js_string}"),
|
||||||
@ -61,6 +62,9 @@ if( kcContext.url && kcContext.url.resourcesPath ){
|
|||||||
</#list>
|
</#list>
|
||||||
};
|
};
|
||||||
</#if>
|
</#if>
|
||||||
|
<#if pageId == "terms.ftl" || termsAcceptanceRequired?? && termsAcceptanceRequired>
|
||||||
|
kcContext["x-keycloakify"].realmMessageBundleTermsText= decodeHtmlEntities("${msg("termsText")?js_string}");
|
||||||
|
</#if>
|
||||||
attributes_to_attributesByName: {
|
attributes_to_attributesByName: {
|
||||||
if( !kcContext.profile ){
|
if( !kcContext.profile ){
|
||||||
break attributes_to_attributesByName;
|
break attributes_to_attributesByName;
|
||||||
@ -198,6 +202,9 @@ function decodeHtmlEntities(htmlStr){
|
|||||||
) || (
|
) || (
|
||||||
key == "execution" &&
|
key == "execution" &&
|
||||||
are_same_path(path, [])
|
are_same_path(path, [])
|
||||||
|
) || (
|
||||||
|
key == "entity" &&
|
||||||
|
are_same_path(path, ["user"])
|
||||||
)
|
)
|
||||||
>
|
>
|
||||||
<#-- <#local out_seq += ["/*" + path?join(".") + "." + key + " excluded*/"]> -->
|
<#-- <#local out_seq += ["/*" + path?join(".") + "." + key + " excluded*/"]> -->
|
||||||
|
@ -9,6 +9,8 @@ import * as babelParser from "@babel/parser";
|
|||||||
import babelGenerate from "@babel/generator";
|
import babelGenerate from "@babel/generator";
|
||||||
import * as babelTypes from "@babel/types";
|
import * as babelTypes from "@babel/types";
|
||||||
import { escapeStringForPropertiesFile } from "../../tools/escapeStringForPropertiesFile";
|
import { escapeStringForPropertiesFile } from "../../tools/escapeStringForPropertiesFile";
|
||||||
|
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
|
||||||
|
import * as fs from "fs";
|
||||||
|
|
||||||
export function generateMessageProperties(params: {
|
export function generateMessageProperties(params: {
|
||||||
themeSrcDirPath: string;
|
themeSrcDirPath: string;
|
||||||
@ -39,10 +41,6 @@ export function generateMessageProperties(params: {
|
|||||||
readFileSync(file).toString("utf8").includes("createUseI18n")
|
readFileSync(file).toString("utf8").includes("createUseI18n")
|
||||||
);
|
);
|
||||||
|
|
||||||
if (files.length === 0) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const extraMessages = files
|
const extraMessages = files
|
||||||
.map(file => {
|
.map(file => {
|
||||||
const root = recast.parse(readFileSync(file).toString("utf8"), {
|
const root = recast.parse(readFileSync(file).toString("utf8"), {
|
||||||
@ -99,15 +97,28 @@ export function generateMessageProperties(params: {
|
|||||||
return extraMessages;
|
return extraMessages;
|
||||||
});
|
});
|
||||||
|
|
||||||
const languageTags = extraMessages
|
const languageTags = [
|
||||||
.map(extraMessage => Object.keys(extraMessage))
|
...extraMessages.map(extraMessage => Object.keys(extraMessage)).flat(),
|
||||||
.flat()
|
...fs
|
||||||
.reduce(...removeDuplicates<string>());
|
.readdirSync(
|
||||||
|
pathJoin(
|
||||||
|
getThisCodebaseRootDirPath(),
|
||||||
|
"src",
|
||||||
|
themeType,
|
||||||
|
"i18n",
|
||||||
|
"baseMessages"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.filter(baseName => baseName !== "index.ts")
|
||||||
|
.map(baseName => baseName.replace(/\.ts$/, ""))
|
||||||
|
].reduce(...removeDuplicates<string>());
|
||||||
|
|
||||||
const keyValueMapByLanguageTag: Record<string, Record<string, string>> = {};
|
const keyValueMapByLanguageTag: Record<string, Record<string, string>> = {};
|
||||||
|
|
||||||
for (const languageTag of languageTags) {
|
for (const languageTag of languageTags) {
|
||||||
const keyValueMap: Record<string, string> = {};
|
const keyValueMap: Record<string, string> = {
|
||||||
|
termsText: ""
|
||||||
|
};
|
||||||
|
|
||||||
for (const extraMessage of extraMessages) {
|
for (const extraMessage of extraMessages) {
|
||||||
const keyValueMap_i = extraMessage[languageTag];
|
const keyValueMap_i = extraMessage[languageTag];
|
||||||
@ -117,7 +128,7 @@ export function generateMessageProperties(params: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const [key, value] of Object.entries(keyValueMap_i)) {
|
for (const [key, value] of Object.entries(keyValueMap_i)) {
|
||||||
if (keyValueMap[key] !== undefined) {
|
if (key !== "termsText" && keyValueMap[key] !== undefined) {
|
||||||
console.warn(
|
console.warn(
|
||||||
[
|
[
|
||||||
"WARNING: The following key is defined multiple times:",
|
"WARNING: The following key is defined multiple times:",
|
||||||
@ -152,14 +163,9 @@ export function generateMessageProperties(params: {
|
|||||||
|
|
||||||
out.push({
|
out.push({
|
||||||
languageTag,
|
languageTag,
|
||||||
propertiesFileSource: [
|
propertiesFileSource: ["", "parent=base", "", propertiesFileSource, ""].join(
|
||||||
"# This file was generated by keycloakify",
|
"\n"
|
||||||
"",
|
)
|
||||||
"parent=base",
|
|
||||||
"",
|
|
||||||
propertiesFileSource,
|
|
||||||
""
|
|
||||||
].join("\n")
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
import { transformCodebase } from "../../tools/transformCodebase";
|
import { transformCodebase } from "../../tools/transformCodebase";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import { join as pathJoin, resolve as pathResolve, relative as pathRelative } from "path";
|
import {
|
||||||
|
join as pathJoin,
|
||||||
|
resolve as pathResolve,
|
||||||
|
relative as pathRelative,
|
||||||
|
dirname as pathDirname
|
||||||
|
} from "path";
|
||||||
import { replaceImportsInJsCode } from "../replacers/replaceImportsInJsCode";
|
import { replaceImportsInJsCode } from "../replacers/replaceImportsInJsCode";
|
||||||
import { replaceImportsInCssCode } from "../replacers/replaceImportsInCssCode";
|
import { replaceImportsInCssCode } from "../replacers/replaceImportsInCssCode";
|
||||||
import {
|
import {
|
||||||
@ -64,8 +69,6 @@ export async function generateResourcesForMainTheme(params: {
|
|||||||
return pathJoin(resourcesDirPath, "theme", themeName, themeType);
|
return pathJoin(resourcesDirPath, "theme", themeName, themeType);
|
||||||
};
|
};
|
||||||
|
|
||||||
const cssGlobalsToDefine: Record<string, string> = {};
|
|
||||||
|
|
||||||
for (const themeType of ["login", "account"] as const) {
|
for (const themeType of ["login", "account"] as const) {
|
||||||
if (!buildContext.recordIsImplementedByThemeType[themeType]) {
|
if (!buildContext.recordIsImplementedByThemeType[themeType]) {
|
||||||
continue;
|
continue;
|
||||||
@ -127,21 +130,14 @@ export async function generateResourcesForMainTheme(params: {
|
|||||||
transformCodebase({
|
transformCodebase({
|
||||||
srcDirPath: buildContext.projectBuildDirPath,
|
srcDirPath: buildContext.projectBuildDirPath,
|
||||||
destDirPath,
|
destDirPath,
|
||||||
transformSourceCode: ({ filePath, sourceCode }) => {
|
transformSourceCode: ({ filePath, fileRelativePath, sourceCode }) => {
|
||||||
if (filePath.endsWith(".css")) {
|
if (filePath.endsWith(".css")) {
|
||||||
const {
|
const { fixedCssCode } = replaceImportsInCssCode({
|
||||||
cssGlobalsToDefine: cssGlobalsToDefineForThisFile,
|
cssCode: sourceCode.toString("utf8"),
|
||||||
fixedCssCode
|
cssFileRelativeDirPath: pathDirname(fileRelativePath),
|
||||||
} = replaceImportsInCssCode({
|
buildContext
|
||||||
cssCode: sourceCode.toString("utf8")
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Object.entries(cssGlobalsToDefineForThisFile).forEach(
|
|
||||||
([key, value]) => {
|
|
||||||
cssGlobalsToDefine[key] = value;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
modifiedSourceCode: Buffer.from(fixedCssCode, "utf8")
|
modifiedSourceCode: Buffer.from(fixedCssCode, "utf8")
|
||||||
};
|
};
|
||||||
@ -168,7 +164,6 @@ export async function generateResourcesForMainTheme(params: {
|
|||||||
indexHtmlCode: fs
|
indexHtmlCode: fs
|
||||||
.readFileSync(pathJoin(buildContext.projectBuildDirPath, "index.html"))
|
.readFileSync(pathJoin(buildContext.projectBuildDirPath, "index.html"))
|
||||||
.toString("utf8"),
|
.toString("utf8"),
|
||||||
cssGlobalsToDefine,
|
|
||||||
buildContext,
|
buildContext,
|
||||||
keycloakifyVersion: readThisNpmPackageVersion(),
|
keycloakifyVersion: readThisNpmPackageVersion(),
|
||||||
themeType,
|
themeType,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import * as crypto from "crypto";
|
|
||||||
import type { BuildContext } from "../../shared/buildContext";
|
import type { BuildContext } from "../../shared/buildContext";
|
||||||
import { assert } from "tsafe/assert";
|
|
||||||
import { basenameOfTheKeycloakifyResourcesDir } from "../../shared/constants";
|
import { basenameOfTheKeycloakifyResourcesDir } from "../../shared/constants";
|
||||||
|
import { assert } from "tsafe/assert";
|
||||||
|
import { posix } from "path";
|
||||||
|
|
||||||
export type BuildContextLike = {
|
export type BuildContextLike = {
|
||||||
urlPathname: string | undefined;
|
urlPathname: string | undefined;
|
||||||
@ -9,68 +9,45 @@ export type BuildContextLike = {
|
|||||||
|
|
||||||
assert<BuildContext extends BuildContextLike ? true : false>();
|
assert<BuildContext extends BuildContextLike ? true : false>();
|
||||||
|
|
||||||
export function replaceImportsInCssCode(params: { cssCode: string }): {
|
export function replaceImportsInCssCode(params: {
|
||||||
fixedCssCode: string;
|
cssCode: string;
|
||||||
cssGlobalsToDefine: Record<string, string>;
|
cssFileRelativeDirPath: string | undefined;
|
||||||
} {
|
|
||||||
const { cssCode } = params;
|
|
||||||
|
|
||||||
const cssGlobalsToDefine: Record<string, string> = {};
|
|
||||||
|
|
||||||
new Set(cssCode.match(/url\(["']?\/[^/][^)"']+["']?\)[^;}]*?/g) ?? []).forEach(
|
|
||||||
match =>
|
|
||||||
(cssGlobalsToDefine[
|
|
||||||
"url" +
|
|
||||||
crypto
|
|
||||||
.createHash("sha256")
|
|
||||||
.update(match)
|
|
||||||
.digest("hex")
|
|
||||||
.substring(0, 15)
|
|
||||||
] = match)
|
|
||||||
);
|
|
||||||
|
|
||||||
let fixedCssCode = cssCode;
|
|
||||||
|
|
||||||
Object.keys(cssGlobalsToDefine).forEach(
|
|
||||||
cssVariableName =>
|
|
||||||
//NOTE: split/join pattern ~ replace all
|
|
||||||
(fixedCssCode = fixedCssCode
|
|
||||||
.split(cssGlobalsToDefine[cssVariableName])
|
|
||||||
.join(`var(--${cssVariableName})`))
|
|
||||||
);
|
|
||||||
|
|
||||||
return { fixedCssCode, cssGlobalsToDefine };
|
|
||||||
}
|
|
||||||
|
|
||||||
export function generateCssCodeToDefineGlobals(params: {
|
|
||||||
cssGlobalsToDefine: Record<string, string>;
|
|
||||||
buildContext: BuildContextLike;
|
buildContext: BuildContextLike;
|
||||||
}): {
|
}): {
|
||||||
cssCodeToPrependInHead: string;
|
fixedCssCode: string;
|
||||||
} {
|
} {
|
||||||
const { cssGlobalsToDefine, buildContext } = params;
|
const { cssCode, cssFileRelativeDirPath, buildContext } = params;
|
||||||
|
|
||||||
return {
|
const fixedCssCode = cssCode.replace(
|
||||||
cssCodeToPrependInHead: [
|
/url\(["']?(\/[^/][^)"']+)["']?\)/g,
|
||||||
":root {",
|
(match, assetFileAbsoluteUrlPathname) => {
|
||||||
...Object.keys(cssGlobalsToDefine)
|
if (buildContext.urlPathname !== undefined) {
|
||||||
.map(cssVariableName =>
|
if (!assetFileAbsoluteUrlPathname.startsWith(buildContext.urlPathname)) {
|
||||||
[
|
// NOTE: Should never happen
|
||||||
`--${cssVariableName}:`,
|
return match;
|
||||||
cssGlobalsToDefine[cssVariableName].replace(
|
}
|
||||||
new RegExp(
|
assetFileAbsoluteUrlPathname = assetFileAbsoluteUrlPathname.replace(
|
||||||
`url\\(${(buildContext.urlPathname ?? "/").replace(
|
buildContext.urlPathname,
|
||||||
/\//g,
|
"/"
|
||||||
"\\/"
|
);
|
||||||
)}`,
|
}
|
||||||
"g"
|
|
||||||
),
|
inline_style_in_html: {
|
||||||
`url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/`
|
if (cssFileRelativeDirPath !== undefined) {
|
||||||
)
|
break inline_style_in_html;
|
||||||
].join(" ")
|
}
|
||||||
)
|
|
||||||
.map(line => ` ${line};`),
|
return `url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}${assetFileAbsoluteUrlPathname})`;
|
||||||
"}"
|
}
|
||||||
].join("\n")
|
|
||||||
};
|
const assetFileRelativeUrlPathname = posix.relative(
|
||||||
|
cssFileRelativeDirPath.replace(/\\/g, "/"),
|
||||||
|
assetFileAbsoluteUrlPathname.replace(/^\//, "")
|
||||||
|
);
|
||||||
|
|
||||||
|
return `url(${assetFileRelativeUrlPathname})`;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return { fixedCssCode };
|
||||||
}
|
}
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
import type { BuildContext } from "../../shared/buildContext";
|
|
||||||
import { assert } from "tsafe/assert";
|
|
||||||
import { basenameOfTheKeycloakifyResourcesDir } from "../../shared/constants";
|
|
||||||
|
|
||||||
export type BuildContextLike = {
|
|
||||||
urlPathname: string | undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
assert<BuildContext extends BuildContextLike ? true : false>();
|
|
||||||
|
|
||||||
export function replaceImportsInInlineCssCode(params: {
|
|
||||||
cssCode: string;
|
|
||||||
buildContext: BuildContextLike;
|
|
||||||
}): {
|
|
||||||
fixedCssCode: string;
|
|
||||||
} {
|
|
||||||
const { cssCode, buildContext } = params;
|
|
||||||
|
|
||||||
const fixedCssCode = cssCode.replace(
|
|
||||||
buildContext.urlPathname === undefined
|
|
||||||
? /url\(["']?\/([^/][^)"']+)["']?\)/g
|
|
||||||
: new RegExp(`url\\(["']?${buildContext.urlPathname}([^)"']+)["']?\\)`, "g"),
|
|
||||||
(...[, group]) =>
|
|
||||||
`url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/${group})`
|
|
||||||
);
|
|
||||||
|
|
||||||
return { fixedCssCode };
|
|
||||||
}
|
|
@ -53,6 +53,7 @@ export type BuildContext = {
|
|||||||
|
|
||||||
export type BuildOptions = {
|
export type BuildOptions = {
|
||||||
themeName?: string | string[];
|
themeName?: string | string[];
|
||||||
|
themeVersion?: string;
|
||||||
environmentVariables?: { name: string; default: string }[];
|
environmentVariables?: { name: string; default: string }[];
|
||||||
extraThemeProperties?: string[];
|
extraThemeProperties?: string[];
|
||||||
artifactId?: string;
|
artifactId?: string;
|
||||||
@ -60,6 +61,7 @@ export type BuildOptions = {
|
|||||||
loginThemeResourcesFromKeycloakVersion?: string;
|
loginThemeResourcesFromKeycloakVersion?: string;
|
||||||
keycloakifyBuildDirPath?: string;
|
keycloakifyBuildDirPath?: string;
|
||||||
kcContextExclusionsFtl?: string;
|
kcContextExclusionsFtl?: string;
|
||||||
|
/** https://docs.keycloakify.dev/v/v10/targetting-specific-keycloak-versions */
|
||||||
keycloakVersionTargets?: BuildOptions.KeycloakVersionTargets;
|
keycloakVersionTargets?: BuildOptions.KeycloakVersionTargets;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -136,6 +138,8 @@ export function getBuildContext(params: {
|
|||||||
const parsedPackageJson = (() => {
|
const parsedPackageJson = (() => {
|
||||||
type BuildOptions_packageJson = BuildOptions & {
|
type BuildOptions_packageJson = BuildOptions & {
|
||||||
projectBuildDirPath?: string;
|
projectBuildDirPath?: string;
|
||||||
|
staticDirPathInProjectBuildDirPath?: string;
|
||||||
|
publicDirPath?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type ParsedPackageJson = {
|
type ParsedPackageJson = {
|
||||||
@ -168,6 +172,9 @@ export function getBuildContext(params: {
|
|||||||
)
|
)
|
||||||
.optional(),
|
.optional(),
|
||||||
themeName: z.union([z.string(), z.array(z.string())]).optional(),
|
themeName: z.union([z.string(), z.array(z.string())]).optional(),
|
||||||
|
themeVersion: z.string().optional(),
|
||||||
|
staticDirPathInProjectBuildDirPath: z.string().optional(),
|
||||||
|
publicDirPath: z.string().optional(),
|
||||||
keycloakVersionTargets: id<
|
keycloakVersionTargets: id<
|
||||||
z.ZodType<BuildOptions.KeycloakVersionTargets>
|
z.ZodType<BuildOptions.KeycloakVersionTargets>
|
||||||
>(
|
>(
|
||||||
@ -228,7 +235,7 @@ export function getBuildContext(params: {
|
|||||||
);
|
);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const buildOptions: BuildOptions = {
|
const buildOptions = {
|
||||||
...parsedPackageJson.keycloakify,
|
...parsedPackageJson.keycloakify,
|
||||||
...resolvedViteConfig?.buildOptions
|
...resolvedViteConfig?.buildOptions
|
||||||
};
|
};
|
||||||
@ -308,9 +315,9 @@ export function getBuildContext(params: {
|
|||||||
break webpack;
|
break webpack;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parsedPackageJson.keycloakify?.projectBuildDirPath !== undefined) {
|
if (buildOptions.projectBuildDirPath !== undefined) {
|
||||||
return getAbsoluteAndInOsFormatPath({
|
return getAbsoluteAndInOsFormatPath({
|
||||||
pathIsh: parsedPackageJson.keycloakify.projectBuildDirPath,
|
pathIsh: buildOptions.projectBuildDirPath,
|
||||||
cwd: projectDirPath
|
cwd: projectDirPath
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -330,8 +337,7 @@ export function getBuildContext(params: {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
bundler,
|
bundler,
|
||||||
themeVersion:
|
themeVersion: buildOptions.themeVersion ?? parsedPackageJson.version ?? "0.0.0",
|
||||||
process.env.KEYCLOAKIFY_THEME_VERSION ?? parsedPackageJson.version ?? "0.0.0",
|
|
||||||
themeNames,
|
themeNames,
|
||||||
extraThemeProperties: buildOptions.extraThemeProperties,
|
extraThemeProperties: buildOptions.extraThemeProperties,
|
||||||
groupId: (() => {
|
groupId: (() => {
|
||||||
@ -373,14 +379,21 @@ export function getBuildContext(params: {
|
|||||||
);
|
);
|
||||||
})(),
|
})(),
|
||||||
publicDirPath: (() => {
|
publicDirPath: (() => {
|
||||||
|
if (process.env.PUBLIC_DIR_PATH !== undefined) {
|
||||||
|
return getAbsoluteAndInOsFormatPath({
|
||||||
|
pathIsh: process.env.PUBLIC_DIR_PATH,
|
||||||
|
cwd: projectDirPath
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
webpack: {
|
webpack: {
|
||||||
if (resolvedViteConfig !== undefined) {
|
if (resolvedViteConfig !== undefined) {
|
||||||
break webpack;
|
break webpack;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.env.PUBLIC_DIR_PATH !== undefined) {
|
if (buildOptions.publicDirPath !== undefined) {
|
||||||
return getAbsoluteAndInOsFormatPath({
|
return getAbsoluteAndInOsFormatPath({
|
||||||
pathIsh: process.env.PUBLIC_DIR_PATH,
|
pathIsh: buildOptions.publicDirPath,
|
||||||
cwd: projectDirPath
|
cwd: projectDirPath
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -437,6 +450,13 @@ export function getBuildContext(params: {
|
|||||||
break webpack;
|
break webpack;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (buildOptions.staticDirPathInProjectBuildDirPath !== undefined) {
|
||||||
|
getAbsoluteAndInOsFormatPath({
|
||||||
|
pathIsh: buildOptions.staticDirPathInProjectBuildDirPath,
|
||||||
|
cwd: projectBuildDirPath
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return pathJoin(projectBuildDirPath, "static");
|
return pathJoin(projectBuildDirPath, "static");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -677,6 +697,8 @@ export function getBuildContext(params: {
|
|||||||
null,
|
null,
|
||||||
2
|
2
|
||||||
);
|
);
|
||||||
|
message +=
|
||||||
|
"\nSee: https://docs.keycloakify.dev/v/v10/targetting-specific-keycloak-versions";
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
})()
|
})()
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
export const nameOfTheLocalizationRealmOverridesUserProfileProperty =
|
|
||||||
"__localizationRealmOverridesUserProfile";
|
|
||||||
export const keycloak_resources = "keycloak-resources";
|
export const keycloak_resources = "keycloak-resources";
|
||||||
export const resources_common = "resources-common";
|
export const resources_common = "resources-common";
|
||||||
export const lastKeycloakVersionWithAccountV1 = "21.1.2";
|
export const lastKeycloakVersionWithAccountV1 = "21.1.2";
|
||||||
export const basenameOfTheKeycloakifyResourcesDir = "build";
|
export const basenameOfTheKeycloakifyResourcesDir = "dist";
|
||||||
|
|
||||||
export const themeTypes = ["login", "account"] as const;
|
export const themeTypes = ["login", "account"] as const;
|
||||||
export const accountV1ThemeName = "account-v1";
|
export const accountV1ThemeName = "account-v1";
|
||||||
|
@ -112,6 +112,18 @@ export async function downloadKeycloakDefaultTheme(params: {
|
|||||||
"dist",
|
"dist",
|
||||||
"fonts",
|
"fonts",
|
||||||
"OpenSans-Semibold-webfont.woff2"
|
"OpenSans-Semibold-webfont.woff2"
|
||||||
|
),
|
||||||
|
pathJoin(
|
||||||
|
"patternfly",
|
||||||
|
"dist",
|
||||||
|
"fonts",
|
||||||
|
"PatternFlyIcons-webfont.ttf"
|
||||||
|
),
|
||||||
|
pathJoin(
|
||||||
|
"patternfly",
|
||||||
|
"dist",
|
||||||
|
"fonts",
|
||||||
|
"PatternFlyIcons-webfont.woff"
|
||||||
)
|
)
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@ -183,12 +195,42 @@ export async function downloadKeycloakDefaultTheme(params: {
|
|||||||
"fonts",
|
"fonts",
|
||||||
"OpenSans-Light-webfont.woff2"
|
"OpenSans-Light-webfont.woff2"
|
||||||
),
|
),
|
||||||
|
pathJoin(
|
||||||
|
"patternfly",
|
||||||
|
"dist",
|
||||||
|
"fonts",
|
||||||
|
"OpenSans-Bold-webfont.woff2"
|
||||||
|
),
|
||||||
|
pathJoin(
|
||||||
|
"patternfly",
|
||||||
|
"dist",
|
||||||
|
"fonts",
|
||||||
|
"OpenSans-Bold-webfont.woff"
|
||||||
|
),
|
||||||
|
pathJoin(
|
||||||
|
"patternfly",
|
||||||
|
"dist",
|
||||||
|
"fonts",
|
||||||
|
"OpenSans-Bold-webfont.ttf"
|
||||||
|
),
|
||||||
pathJoin(
|
pathJoin(
|
||||||
"patternfly",
|
"patternfly",
|
||||||
"dist",
|
"dist",
|
||||||
"fonts",
|
"fonts",
|
||||||
"fontawesome-webfont.woff2"
|
"fontawesome-webfont.woff2"
|
||||||
),
|
),
|
||||||
|
pathJoin(
|
||||||
|
"patternfly",
|
||||||
|
"dist",
|
||||||
|
"fonts",
|
||||||
|
"PatternFlyIcons-webfont.ttf"
|
||||||
|
),
|
||||||
|
pathJoin(
|
||||||
|
"patternfly",
|
||||||
|
"dist",
|
||||||
|
"fonts",
|
||||||
|
"PatternFlyIcons-webfont.woff"
|
||||||
|
),
|
||||||
pathJoin("jquery", "dist", "jquery.min.js")
|
pathJoin("jquery", "dist", "jquery.min.js")
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,4 @@
|
|||||||
import type {
|
import type { ThemeType, LoginThemePageId } from "keycloakify/bin/shared/constants";
|
||||||
ThemeType,
|
|
||||||
LoginThemePageId,
|
|
||||||
nameOfTheLocalizationRealmOverridesUserProfileProperty
|
|
||||||
} from "keycloakify/bin/shared/constants";
|
|
||||||
import type { ExtractAfterStartingWith } from "keycloakify/tools/ExtractAfterStartingWith";
|
import type { ExtractAfterStartingWith } from "keycloakify/tools/ExtractAfterStartingWith";
|
||||||
import type { ValueOf } from "keycloakify/tools/ValueOf";
|
import type { ValueOf } from "keycloakify/tools/ValueOf";
|
||||||
import { assert } from "tsafe/assert";
|
import { assert } from "tsafe/assert";
|
||||||
@ -158,7 +154,10 @@ export declare namespace KcContext {
|
|||||||
ssoLoginInOtherTabsUrl: string;
|
ssoLoginInOtherTabsUrl: string;
|
||||||
};
|
};
|
||||||
properties: {};
|
properties: {};
|
||||||
__localizationRealmOverridesUserProfile?: Record<string, string>;
|
"x-keycloakify": {
|
||||||
|
realmMessageBundleUserProfile: Record<string, string> | undefined;
|
||||||
|
realmMessageBundleTermsText: string | undefined;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export type SamlPostForm = Common & {
|
export type SamlPostForm = Common & {
|
||||||
@ -276,6 +275,7 @@ export declare namespace KcContext {
|
|||||||
lastName?: string;
|
lastName?: string;
|
||||||
markedForEviction?: boolean;
|
markedForEviction?: boolean;
|
||||||
};
|
};
|
||||||
|
__localizationRealmOverridesTermsText?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type LoginDeviceVerifyUserCode = Common & {
|
export type LoginDeviceVerifyUserCode = Common & {
|
||||||
@ -772,11 +772,3 @@ export type PasswordPolicies = {
|
|||||||
/** Whether the password can be the email address */
|
/** Whether the password can be the email address */
|
||||||
notEmail?: boolean;
|
notEmail?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
assert<
|
|
||||||
KcContext.Common extends Partial<
|
|
||||||
Record<typeof nameOfTheLocalizationRealmOverridesUserProfileProperty, unknown>
|
|
||||||
>
|
|
||||||
? true
|
|
||||||
: false
|
|
||||||
>();
|
|
||||||
|
@ -161,7 +161,10 @@ export const kcContextCommonMock: KcContext.Common = {
|
|||||||
scripts: [],
|
scripts: [],
|
||||||
isAppInitiatedAction: false,
|
isAppInitiatedAction: false,
|
||||||
properties: {},
|
properties: {},
|
||||||
__localizationRealmOverridesUserProfile: {}
|
"x-keycloakify": {
|
||||||
|
realmMessageBundleUserProfile: undefined,
|
||||||
|
realmMessageBundleTermsText: undefined
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const loginUrl = {
|
const loginUrl = {
|
||||||
|
@ -29,7 +29,7 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
|
|
||||||
const { kcClsx } = getKcClsx({ doUseDefaultCss, classes });
|
const { kcClsx } = getKcClsx({ doUseDefaultCss, classes });
|
||||||
|
|
||||||
const { msg, msgStr, getChangeLocalUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n;
|
const { msg, msgStr, getChangeLocaleUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n;
|
||||||
|
|
||||||
const { realm, locale, auth, url, message, isAppInitiatedAction, authenticationSession, scripts } = kcContext;
|
const { realm, locale, auth, url, message, isAppInitiatedAction, authenticationSession, scripts } = kcContext;
|
||||||
|
|
||||||
@ -153,7 +153,7 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
|||||||
role="menuitem"
|
role="menuitem"
|
||||||
id={`language-${i + 1}`}
|
id={`language-${i + 1}`}
|
||||||
className={kcClsx("kcLocaleItemClass")}
|
className={kcClsx("kcLocaleItemClass")}
|
||||||
href={getChangeLocalUrl(languageTag)}
|
href={getChangeLocaleUrl(languageTag)}
|
||||||
>
|
>
|
||||||
{labelBySupportedLanguageTag[languageTag]}
|
{labelBySupportedLanguageTag[languageTag]}
|
||||||
</a>
|
</a>
|
||||||
|
@ -1,10 +1,8 @@
|
|||||||
import "keycloakify/tools/Object.fromEntries";
|
import "keycloakify/tools/Object.fromEntries";
|
||||||
import { useEffect, useState } from "react";
|
|
||||||
import { assert } from "tsafe/assert";
|
import { assert } from "tsafe/assert";
|
||||||
import messages_fallbackLanguage from "./baseMessages/en";
|
import messages_fallbackLanguage from "./baseMessages/en";
|
||||||
import { getMessages } from "./baseMessages";
|
import { getMessages } from "./baseMessages";
|
||||||
import type { KcContext } from "../KcContext";
|
import type { KcContext } from "../KcContext";
|
||||||
import { Reflect } from "tsafe/Reflect";
|
|
||||||
|
|
||||||
export const fallbackLanguageTag = "en";
|
export const fallbackLanguageTag = "en";
|
||||||
|
|
||||||
@ -13,7 +11,10 @@ export type KcContextLike = {
|
|||||||
currentLanguageTag: string;
|
currentLanguageTag: string;
|
||||||
supported: { languageTag: string; url: string; label: string }[];
|
supported: { languageTag: string; url: string; label: string }[];
|
||||||
};
|
};
|
||||||
__localizationRealmOverridesUserProfile?: Record<string, string>;
|
"x-keycloakify": {
|
||||||
|
realmMessageBundleUserProfile: Record<string, string> | undefined;
|
||||||
|
realmMessageBundleTermsText: string | undefined;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
assert<KcContext extends KcContextLike ? true : false>();
|
assert<KcContext extends KcContextLike ? true : false>();
|
||||||
@ -31,7 +32,7 @@ export type GenericI18n<MessageKey extends string> = {
|
|||||||
* Redirect to this url to change the language.
|
* Redirect to this url to change the language.
|
||||||
* After reload currentLanguageTag === newLanguageTag
|
* After reload currentLanguageTag === newLanguageTag
|
||||||
*/
|
*/
|
||||||
getChangeLocalUrl: (newLanguageTag: string) => string;
|
getChangeLocaleUrl: (newLanguageTag: string) => string;
|
||||||
/**
|
/**
|
||||||
* e.g. "en" => "English", "fr" => "Français", ...
|
* e.g. "en" => "English", "fr" => "Français", ...
|
||||||
*
|
*
|
||||||
@ -89,7 +90,9 @@ export type GenericI18n<MessageKey extends string> = {
|
|||||||
isFetchingTranslations: boolean;
|
isFetchingTranslations: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: { [languageTag: string]: { [key in ExtraMessageKey]: string } }) {
|
export function createGetI18n<ExtraMessageKey extends string = never>(messageBundle: {
|
||||||
|
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||||
|
}) {
|
||||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||||
|
|
||||||
type Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined };
|
type Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined };
|
||||||
@ -109,9 +112,9 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|||||||
return cachedResult;
|
return cachedResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
const partialI18n: Pick<I18n, "currentLanguageTag" | "getChangeLocalUrl" | "labelBySupportedLanguageTag"> = {
|
const partialI18n: Pick<I18n, "currentLanguageTag" | "getChangeLocaleUrl" | "labelBySupportedLanguageTag"> = {
|
||||||
currentLanguageTag: kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag,
|
currentLanguageTag: kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag,
|
||||||
getChangeLocalUrl: newLanguageTag => {
|
getChangeLocaleUrl: newLanguageTag => {
|
||||||
const { locale } = kcContext;
|
const { locale } = kcContext;
|
||||||
|
|
||||||
assert(locale !== undefined, "Internationalization not enabled");
|
assert(locale !== undefined, "Internationalization not enabled");
|
||||||
@ -127,9 +130,10 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|||||||
|
|
||||||
const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
|
const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
|
||||||
messages_fallbackLanguage,
|
messages_fallbackLanguage,
|
||||||
extraMessages_fallbackLanguage: extraMessages[fallbackLanguageTag],
|
messageBundle_fallbackLanguage: messageBundle[fallbackLanguageTag],
|
||||||
extraMessages: extraMessages[partialI18n.currentLanguageTag],
|
messageBundle_currentLanguage: messageBundle[partialI18n.currentLanguageTag],
|
||||||
__localizationRealmOverridesUserProfile: kcContext.__localizationRealmOverridesUserProfile
|
realmMessageBundleUserProfile: kcContext["x-keycloakify"].realmMessageBundleUserProfile,
|
||||||
|
realmMessageBundleTermsText: kcContext["x-keycloakify"].realmMessageBundleTermsText
|
||||||
});
|
});
|
||||||
|
|
||||||
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
|
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
|
||||||
@ -137,17 +141,19 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|||||||
const result: Result = {
|
const result: Result = {
|
||||||
i18n: {
|
i18n: {
|
||||||
...partialI18n,
|
...partialI18n,
|
||||||
...createI18nTranslationFunctions({ messages: undefined }),
|
...createI18nTranslationFunctions({
|
||||||
|
messages_currentLanguage: isCurrentLanguageFallbackLanguage ? messages_fallbackLanguage : undefined
|
||||||
|
}),
|
||||||
isFetchingTranslations: !isCurrentLanguageFallbackLanguage
|
isFetchingTranslations: !isCurrentLanguageFallbackLanguage
|
||||||
},
|
},
|
||||||
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
|
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
|
||||||
? undefined
|
? undefined
|
||||||
: (async () => {
|
: (async () => {
|
||||||
const messages = await getMessages(partialI18n.currentLanguageTag);
|
const messages_currentLanguage = await getMessages(partialI18n.currentLanguageTag);
|
||||||
|
|
||||||
const i18n_currentLanguage: I18n = {
|
const i18n_currentLanguage: I18n = {
|
||||||
...partialI18n,
|
...partialI18n,
|
||||||
...createI18nTranslationFunctions({ messages }),
|
...createI18nTranslationFunctions({ messages_currentLanguage }),
|
||||||
isFetchingTranslations: false
|
isFetchingTranslations: false
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -170,67 +176,40 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|||||||
return { getI18n };
|
return { getI18n };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createUseI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
|
||||||
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
|
||||||
}) {
|
|
||||||
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
|
||||||
|
|
||||||
const { getI18n } = createGetI18n(extraMessages);
|
|
||||||
|
|
||||||
function useI18n(params: { kcContext: KcContextLike }): { i18n: I18n } {
|
|
||||||
const { kcContext } = params;
|
|
||||||
|
|
||||||
const { i18n, prI18n_currentLanguage } = getI18n({ kcContext });
|
|
||||||
|
|
||||||
const [i18n_toReturn, setI18n_toReturn] = useState<I18n>(i18n);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
let isActive = true;
|
|
||||||
|
|
||||||
prI18n_currentLanguage?.then(i18n => {
|
|
||||||
if (!isActive) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setI18n_toReturn(i18n);
|
|
||||||
});
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
isActive = false;
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return { i18n: i18n_toReturn };
|
|
||||||
}
|
|
||||||
|
|
||||||
return { useI18n, ofTypeI18n: Reflect<I18n>() };
|
|
||||||
}
|
|
||||||
|
|
||||||
function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraMessageKey extends string>(params: {
|
function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraMessageKey extends string>(params: {
|
||||||
messages_fallbackLanguage: Record<MessageKey, string>;
|
messages_fallbackLanguage: Record<MessageKey, string>;
|
||||||
extraMessages_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
messageBundle_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||||
extraMessages: Partial<Record<ExtraMessageKey, string>> | undefined;
|
messageBundle_currentLanguage: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||||
__localizationRealmOverridesUserProfile: Record<string, string> | undefined;
|
realmMessageBundleUserProfile: Record<string, string> | undefined;
|
||||||
|
realmMessageBundleTermsText: string | undefined;
|
||||||
}) {
|
}) {
|
||||||
const { __localizationRealmOverridesUserProfile, extraMessages } = params;
|
const { messageBundle_currentLanguage, realmMessageBundleUserProfile, realmMessageBundleTermsText } = params;
|
||||||
|
|
||||||
const messages_fallbackLanguage = {
|
const messages_fallbackLanguage = {
|
||||||
...params.messages_fallbackLanguage,
|
...params.messages_fallbackLanguage,
|
||||||
...params.extraMessages_fallbackLanguage
|
...params.messageBundle_fallbackLanguage
|
||||||
};
|
};
|
||||||
|
|
||||||
function createI18nTranslationFunctions(params: {
|
function createI18nTranslationFunctions(params: {
|
||||||
messages: Partial<Record<MessageKey, string>> | undefined;
|
messages_currentLanguage: Partial<Record<MessageKey, string>> | undefined;
|
||||||
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
|
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
|
||||||
const messages = {
|
const messages_currentLanguage = {
|
||||||
...params.messages,
|
...params.messages_currentLanguage,
|
||||||
...extraMessages
|
...messageBundle_currentLanguage
|
||||||
};
|
};
|
||||||
|
|
||||||
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
|
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
|
||||||
const { key, args, doRenderAsHtml } = props;
|
const { key, args, doRenderAsHtml } = props;
|
||||||
|
|
||||||
const messageOrUndefined: string | undefined = (messages as any)[key] ?? (messages_fallbackLanguage as any)[key];
|
const messageOrUndefined: string | undefined = (() => {
|
||||||
|
const messageOrUndefined = (messages_currentLanguage as any)[key] ?? (messages_fallbackLanguage as any)[key];
|
||||||
|
|
||||||
|
if (key === "termsText") {
|
||||||
|
return realmMessageBundleTermsText;
|
||||||
|
}
|
||||||
|
|
||||||
|
return messageOrUndefined;
|
||||||
|
})();
|
||||||
|
|
||||||
if (messageOrUndefined === undefined) {
|
if (messageOrUndefined === undefined) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -281,8 +260,8 @@ function createI18nTranslationFunctionsFactory<MessageKey extends string, ExtraM
|
|||||||
function resolveMsgAdvanced(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): JSX.Element | string {
|
function resolveMsgAdvanced(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): JSX.Element | string {
|
||||||
const { key, args, doRenderAsHtml } = props;
|
const { key, args, doRenderAsHtml } = props;
|
||||||
|
|
||||||
if (__localizationRealmOverridesUserProfile !== undefined && key in __localizationRealmOverridesUserProfile) {
|
if (realmMessageBundleUserProfile !== undefined && key in realmMessageBundleUserProfile) {
|
||||||
const resolvedMessage = __localizationRealmOverridesUserProfile[key];
|
const resolvedMessage = realmMessageBundleUserProfile[key];
|
||||||
|
|
||||||
return doRenderAsHtml ? (
|
return doRenderAsHtml ? (
|
||||||
<span
|
<span
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
|
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
|
||||||
export type { MessageKey, KcContextLike };
|
export type { MessageKey, KcContextLike };
|
||||||
export type I18n = GenericI18n<MessageKey>;
|
export type I18n = GenericI18n<MessageKey>;
|
||||||
export { createUseI18n } from "./i18n";
|
export { createUseI18n } from "./useI18n";
|
||||||
export { fallbackLanguageTag } from "./i18n";
|
export { fallbackLanguageTag } from "./i18n";
|
||||||
|
44
src/login/i18n/useI18n.ts
Normal file
44
src/login/i18n/useI18n.ts
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import {
|
||||||
|
createGetI18n,
|
||||||
|
type GenericI18n,
|
||||||
|
type MessageKey,
|
||||||
|
type KcContextLike
|
||||||
|
} from "./i18n";
|
||||||
|
import { Reflect } from "tsafe/Reflect";
|
||||||
|
|
||||||
|
export function createUseI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||||
|
[languageTag: string]: { [key in ExtraMessageKey]: string };
|
||||||
|
}) {
|
||||||
|
type I18n = GenericI18n<MessageKey | ExtraMessageKey>;
|
||||||
|
|
||||||
|
const { getI18n } = createGetI18n(extraMessages);
|
||||||
|
|
||||||
|
function useI18n(params: { kcContext: KcContextLike }): { i18n: I18n } {
|
||||||
|
const { kcContext } = params;
|
||||||
|
|
||||||
|
const { i18n, prI18n_currentLanguage } = getI18n({ kcContext });
|
||||||
|
|
||||||
|
const [i18n_toReturn, setI18n_toReturn] = useState<I18n>(i18n);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let isActive = true;
|
||||||
|
|
||||||
|
prI18n_currentLanguage?.then(i18n => {
|
||||||
|
if (!isActive) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setI18n_toReturn(i18n);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
isActive = false;
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { i18n: i18n_toReturn };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { useI18n, ofTypeI18n: Reflect<I18n>() };
|
||||||
|
}
|
@ -1,57 +0,0 @@
|
|||||||
import { fallbackLanguageTag } from "keycloakify/login/i18n";
|
|
||||||
import { assert } from "tsafe/assert";
|
|
||||||
import {
|
|
||||||
createStatefulObservable,
|
|
||||||
useRerenderOnChange
|
|
||||||
} from "keycloakify/tools/StatefulObservable";
|
|
||||||
import { useOnFistMount } from "keycloakify/tools/useOnFirstMount";
|
|
||||||
import { KcContext } from "../KcContext";
|
|
||||||
|
|
||||||
const obs = createStatefulObservable<
|
|
||||||
| {
|
|
||||||
termsMarkdown: string;
|
|
||||||
termsLanguageTag: string | undefined;
|
|
||||||
}
|
|
||||||
| undefined
|
|
||||||
>(() => undefined);
|
|
||||||
|
|
||||||
export type KcContextLike = {
|
|
||||||
pageId: string;
|
|
||||||
locale?: {
|
|
||||||
currentLanguageTag: string;
|
|
||||||
};
|
|
||||||
termsAcceptanceRequired?: boolean;
|
|
||||||
};
|
|
||||||
|
|
||||||
assert<KcContext extends KcContextLike ? true : false>();
|
|
||||||
|
|
||||||
/** Allow to avoid bundling the terms and download it on demand*/
|
|
||||||
export function useDownloadTerms(params: {
|
|
||||||
kcContext: KcContextLike;
|
|
||||||
downloadTermsMarkdown: (params: {
|
|
||||||
currentLanguageTag: string;
|
|
||||||
}) => Promise<{ termsMarkdown: string; termsLanguageTag: string | undefined }>;
|
|
||||||
}) {
|
|
||||||
const { kcContext, downloadTermsMarkdown } = params;
|
|
||||||
|
|
||||||
useOnFistMount(async () => {
|
|
||||||
if (kcContext.pageId === "terms.ftl" || kcContext.termsAcceptanceRequired) {
|
|
||||||
obs.current = await downloadTermsMarkdown({
|
|
||||||
currentLanguageTag:
|
|
||||||
kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useTermsMarkdown() {
|
|
||||||
useRerenderOnChange(obs);
|
|
||||||
|
|
||||||
if (obs.current === undefined) {
|
|
||||||
return { isDownloadComplete: false as const };
|
|
||||||
}
|
|
||||||
|
|
||||||
const { termsMarkdown, termsLanguageTag } = obs.current;
|
|
||||||
|
|
||||||
return { isDownloadComplete: true, termsMarkdown, termsLanguageTag };
|
|
||||||
}
|
|
88
src/login/lib/useDownloadTerms.tsx
Normal file
88
src/login/lib/useDownloadTerms.tsx
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import { useState, useEffect } from "react";
|
||||||
|
import { fallbackLanguageTag } from "keycloakify/login/i18n";
|
||||||
|
import { assert } from "tsafe/assert";
|
||||||
|
import { createStatefulObservable, useRerenderOnChange } from "keycloakify/tools/StatefulObservable";
|
||||||
|
import { useOnFistMount } from "keycloakify/tools/useOnFirstMount";
|
||||||
|
import { KcContext } from "../KcContext";
|
||||||
|
import type { Options as ReactMarkdownOptions } from "../../tools/react-markdown";
|
||||||
|
|
||||||
|
const obs = createStatefulObservable<
|
||||||
|
| {
|
||||||
|
ReactMarkdown: (props: Readonly<ReactMarkdownOptions>) => JSX.Element;
|
||||||
|
termsMarkdown: string;
|
||||||
|
}
|
||||||
|
| undefined
|
||||||
|
>(() => undefined);
|
||||||
|
|
||||||
|
export type KcContextLike_useDownloadTerms = {
|
||||||
|
pageId: string;
|
||||||
|
locale?: {
|
||||||
|
currentLanguageTag: string;
|
||||||
|
};
|
||||||
|
termsAcceptanceRequired?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
assert<KcContext extends KcContextLike_useDownloadTerms ? true : false>();
|
||||||
|
|
||||||
|
/** Allow to avoid bundling the terms and download it on demand*/
|
||||||
|
export function useDownloadTerms(params: {
|
||||||
|
kcContext: KcContextLike_useDownloadTerms;
|
||||||
|
downloadTermsMarkdown: (params: { currentLanguageTag: string }) => Promise<{ termsMarkdown: string; termsLanguageTag: string | undefined }>;
|
||||||
|
}) {
|
||||||
|
const { kcContext, downloadTermsMarkdown } = params;
|
||||||
|
|
||||||
|
useOnFistMount(async () => {
|
||||||
|
if (kcContext.pageId === "terms.ftl" || kcContext.termsAcceptanceRequired) {
|
||||||
|
const currentLanguageTag = kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag;
|
||||||
|
|
||||||
|
const [ReactMarkdown_base, { termsMarkdown, termsLanguageTag }] = await Promise.all([
|
||||||
|
import("../../tools/react-markdown").then(_ => _.default),
|
||||||
|
downloadTermsMarkdown({ currentLanguageTag })
|
||||||
|
] as const);
|
||||||
|
|
||||||
|
const htmlLang = termsLanguageTag !== currentLanguageTag ? termsLanguageTag : undefined;
|
||||||
|
|
||||||
|
const ReactMarkdown: (props: Readonly<ReactMarkdownOptions>) => JSX.Element =
|
||||||
|
htmlLang === undefined
|
||||||
|
? ReactMarkdown_base
|
||||||
|
: props => {
|
||||||
|
const [anchor, setAnchor] = useState<HTMLDivElement | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (anchor === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parent = anchor.parentElement;
|
||||||
|
|
||||||
|
assert(parent !== null);
|
||||||
|
|
||||||
|
parent.setAttribute("lang", htmlLang);
|
||||||
|
|
||||||
|
anchor.remove();
|
||||||
|
}, [anchor]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<ReactMarkdown_base {...props} />
|
||||||
|
<div ref={setAnchor} style={{ display: "none" }} aria-hidden />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
obs.current = { ReactMarkdown, termsMarkdown };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useTermsMarkdown() {
|
||||||
|
useRerenderOnChange(obs);
|
||||||
|
|
||||||
|
if (obs.current === undefined) {
|
||||||
|
return { isDownloadComplete: false as const };
|
||||||
|
}
|
||||||
|
|
||||||
|
const { ReactMarkdown, termsMarkdown } = obs.current;
|
||||||
|
|
||||||
|
return { isDownloadComplete: true, ReactMarkdown, termsMarkdown };
|
||||||
|
}
|
@ -1,5 +1,4 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { Markdown } from "keycloakify/tools/Markdown";
|
|
||||||
import type { LazyOrNot } from "keycloakify/tools/LazyOrNot";
|
import type { LazyOrNot } from "keycloakify/tools/LazyOrNot";
|
||||||
import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
|
import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
|
||||||
import { getKcClsx, type KcClsx } from "keycloakify/login/lib/kcClsx";
|
import { getKcClsx, type KcClsx } from "keycloakify/login/lib/kcClsx";
|
||||||
@ -78,23 +77,14 @@ export default function Register(props: RegisterProps) {
|
|||||||
function TermsAcceptance(props: { i18n: I18n; kcClsx: KcClsx; messagesPerField: Pick<KcContext["messagesPerField"], "existsError" | "get"> }) {
|
function TermsAcceptance(props: { i18n: I18n; kcClsx: KcClsx; messagesPerField: Pick<KcContext["messagesPerField"], "existsError" | "get"> }) {
|
||||||
const { i18n, kcClsx, messagesPerField } = props;
|
const { i18n, kcClsx, messagesPerField } = props;
|
||||||
|
|
||||||
const { msg } = i18n;
|
const { msg, msgStr } = i18n;
|
||||||
|
|
||||||
// NOTE: Refer to https://docs.keycloakify.dev/terms-and-conditions to load your terms and conditions.
|
|
||||||
const { termsMarkdown } = useTermsMarkdown();
|
|
||||||
|
|
||||||
if (termsMarkdown === undefined) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<div className={kcClsx("kcInputWrapperClass")}>
|
<div className={kcClsx("kcInputWrapperClass")}>
|
||||||
{msg("termsTitle")}
|
{msg("termsTitle")}
|
||||||
<div id="kc-registration-terms-text">
|
<div id="kc-registration-terms-text">{msgStr("termsText") ? msg("termsText") : <TermsMarkdown />}</div>
|
||||||
<Markdown>{termsMarkdown}</Markdown>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
@ -121,3 +111,13 @@ function TermsAcceptance(props: { i18n: I18n; kcClsx: KcClsx; messagesPerField:
|
|||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function TermsMarkdown() {
|
||||||
|
const { isDownloadComplete, termsMarkdown, ReactMarkdown } = useTermsMarkdown();
|
||||||
|
|
||||||
|
if (!isDownloadComplete) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <ReactMarkdown>{termsMarkdown}</ReactMarkdown>;
|
||||||
|
}
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
import { Markdown } from "keycloakify/tools/Markdown";
|
|
||||||
import { getKcClsx } from "keycloakify/login/lib/kcClsx";
|
import { getKcClsx } from "keycloakify/login/lib/kcClsx";
|
||||||
import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
|
import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
|
||||||
import type { PageProps } from "keycloakify/login/pages/PageProps";
|
import type { PageProps } from "keycloakify/login/pages/PageProps";
|
||||||
@ -15,13 +14,7 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
|
|||||||
|
|
||||||
const { msg, msgStr } = i18n;
|
const { msg, msgStr } = i18n;
|
||||||
|
|
||||||
const { locale, url } = kcContext;
|
const { url } = kcContext;
|
||||||
|
|
||||||
const { isDownloadComplete, termsMarkdown, termsLanguageTag } = useTermsMarkdown();
|
|
||||||
|
|
||||||
if (!isDownloadComplete) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Template
|
<Template
|
||||||
@ -32,9 +25,7 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
|
|||||||
displayMessage={false}
|
displayMessage={false}
|
||||||
headerNode={msg("termsTitle")}
|
headerNode={msg("termsTitle")}
|
||||||
>
|
>
|
||||||
<div id="kc-terms-text" lang={termsLanguageTag !== locale?.currentLanguageTag ? termsLanguageTag : undefined}>
|
<div id="kc-terms-text">{msgStr("termsText") ? msg("termsText") : <TermsMarkdown />}</div>
|
||||||
<Markdown>{termsMarkdown}</Markdown>
|
|
||||||
</div>
|
|
||||||
<form className="form-actions" action={url.loginAction} method="POST">
|
<form className="form-actions" action={url.loginAction} method="POST">
|
||||||
<input
|
<input
|
||||||
className={kcClsx("kcButtonClass", "kcButtonClass", "kcButtonClass", "kcButtonPrimaryClass", "kcButtonLargeClass")}
|
className={kcClsx("kcButtonClass", "kcButtonClass", "kcButtonClass", "kcButtonPrimaryClass", "kcButtonLargeClass")}
|
||||||
@ -55,3 +46,13 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
|
|||||||
</Template>
|
</Template>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function TermsMarkdown() {
|
||||||
|
const { isDownloadComplete, termsMarkdown, ReactMarkdown } = useTermsMarkdown();
|
||||||
|
|
||||||
|
if (!isDownloadComplete) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return <ReactMarkdown>{termsMarkdown}</ReactMarkdown>;
|
||||||
|
}
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
import Markdown from "react-markdown";
|
|
||||||
|
|
||||||
export { Markdown };
|
|
3
src/tools/react-markdown.ts
Normal file
3
src/tools/react-markdown.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * from "react-markdown";
|
||||||
|
import Markdown from "react-markdown";
|
||||||
|
export default Markdown;
|
@ -14,10 +14,41 @@ export default meta;
|
|||||||
type Story = StoryObj<typeof meta>;
|
type Story = StoryObj<typeof meta>;
|
||||||
|
|
||||||
export const Default: Story = {
|
export const Default: Story = {
|
||||||
render: () => <KcPageStory />
|
render: () => (
|
||||||
|
<KcPageStory
|
||||||
|
kcContext={{
|
||||||
|
"x-keycloakify": {
|
||||||
|
realmMessageBundleTermsText: "<p>My terms in <strong>English</strong></p>"
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
export const French: Story = {
|
export const French: Story = {
|
||||||
|
render: () => (
|
||||||
|
<KcPageStory
|
||||||
|
kcContext={{
|
||||||
|
locale: {
|
||||||
|
currentLanguageTag: "fr"
|
||||||
|
},
|
||||||
|
"x-keycloakify": {
|
||||||
|
// cSpell: disable
|
||||||
|
realmMessageBundleTermsText: "<p>Mes terme en <strong>Français</strong></p>"
|
||||||
|
// cSpell: enable
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
// NOTE: Only works if using `useDownloadTerms()`
|
||||||
|
export const RenderedFromMarkdown: Story = {
|
||||||
|
render: () => <KcPageStory />
|
||||||
|
};
|
||||||
|
|
||||||
|
// NOTE: Only works if using `useDownloadTerms()`
|
||||||
|
export const RenderedFromMarkdownFrench: Story = {
|
||||||
render: () => (
|
render: () => (
|
||||||
<KcPageStory
|
<KcPageStory
|
||||||
kcContext={{
|
kcContext={{
|
||||||
@ -28,15 +59,3 @@ export const French: Story = {
|
|||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
export const Spanish: Story = {
|
|
||||||
render: () => (
|
|
||||||
<KcPageStory
|
|
||||||
kcContext={{
|
|
||||||
locale: {
|
|
||||||
currentLanguageTag: "es"
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
@ -1,11 +1,6 @@
|
|||||||
import { replaceImportsInJsCode_vite } from "keycloakify/bin/keycloakify/replacers/replaceImportsInJsCode/vite";
|
import { replaceImportsInJsCode_vite } from "keycloakify/bin/keycloakify/replacers/replaceImportsInJsCode/vite";
|
||||||
import { replaceImportsInJsCode_webpack } from "keycloakify/bin/keycloakify/replacers/replaceImportsInJsCode/webpack";
|
import { replaceImportsInJsCode_webpack } from "keycloakify/bin/keycloakify/replacers/replaceImportsInJsCode/webpack";
|
||||||
import {
|
import { replaceImportsInCssCode } from "keycloakify/bin/keycloakify/replacers/replaceImportsInCssCode";
|
||||||
generateCssCodeToDefineGlobals,
|
|
||||||
replaceImportsInCssCode
|
|
||||||
} from "keycloakify/bin/keycloakify/replacers/replaceImportsInCssCode";
|
|
||||||
import { replaceImportsInInlineCssCode } from "keycloakify/bin/keycloakify/replacers/replaceImportsInInlineCssCode";
|
|
||||||
import { same } from "evt/tools/inDepth/same";
|
|
||||||
import { expect, it, describe } from "vitest";
|
import { expect, it, describe } from "vitest";
|
||||||
import { basenameOfTheKeycloakifyResourcesDir } from "keycloakify/bin/shared/constants";
|
import { basenameOfTheKeycloakifyResourcesDir } from "keycloakify/bin/shared/constants";
|
||||||
|
|
||||||
@ -272,13 +267,13 @@ describe("js replacer - webpack", () => {
|
|||||||
|
|
||||||
const fixedJsCodeExpected = `
|
const fixedJsCodeExpected = `
|
||||||
function f() {
|
function f() {
|
||||||
return window.kcContext.url.resourcesPath + "/build/static/js/" + ({}[e] || e) + "." + {
|
return window.kcContext.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/static/js/" + ({}[e] || e) + "." + {
|
||||||
3: "0664cdc0"
|
3: "0664cdc0"
|
||||||
}[e] + ".chunk.js"
|
}[e] + ".chunk.js"
|
||||||
}
|
}
|
||||||
|
|
||||||
function sameAsF() {
|
function sameAsF() {
|
||||||
return window.kcContext.url.resourcesPath + "/build/static/js/" + ({}[e] || e) + "." + {
|
return window.kcContext.url.resourcesPath + "/${basenameOfTheKeycloakifyResourcesDir}/static/js/" + ({}[e] || e) + "." + {
|
||||||
3: "0664cdc0"
|
3: "0664cdc0"
|
||||||
}[e] + ".chunk.js"
|
}[e] + ".chunk.js"
|
||||||
}
|
}
|
||||||
@ -293,7 +288,7 @@ describe("js replacer - webpack", () => {
|
|||||||
}
|
}
|
||||||
return "u";
|
return "u";
|
||||||
})()] = function(e) {
|
})()] = function(e) {
|
||||||
return "/build/static/js/" + e + "." + {
|
return "/${basenameOfTheKeycloakifyResourcesDir}/static/js/" + e + "." + {
|
||||||
147: "6c5cee76",
|
147: "6c5cee76",
|
||||||
787: "8da10fcf",
|
787: "8da10fcf",
|
||||||
922: "be170a73"
|
922: "be170a73"
|
||||||
@ -310,7 +305,7 @@ describe("js replacer - webpack", () => {
|
|||||||
}
|
}
|
||||||
return "miniCssF";
|
return "miniCssF";
|
||||||
})()] = function(e) {
|
})()] = function(e) {
|
||||||
return "/build/static/css/" + e + "." + {
|
return "/${basenameOfTheKeycloakifyResourcesDir}/static/css/" + e + "." + {
|
||||||
164:"dcfd7749",
|
164:"dcfd7749",
|
||||||
908:"67c9ed2c"
|
908:"67c9ed2c"
|
||||||
} [e] + ".chunk.css"
|
} [e] + ".chunk.css"
|
||||||
@ -325,7 +320,7 @@ describe("js replacer - webpack", () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
return "u";
|
return "u";
|
||||||
})()] = e => "/build/static/js/"+e+"."+{69:"4f205f87",128:"49264537",453:"b2fed72e",482:"f0106901"}[e]+".chunk.js"
|
})()] = e => "/${basenameOfTheKeycloakifyResourcesDir}/static/js/"+e+"."+{69:"4f205f87",128:"49264537",453:"b2fed72e",482:"f0106901"}[e]+".chunk.js"
|
||||||
|
|
||||||
t[(function(){
|
t[(function(){
|
||||||
var pd = Object.getOwnPropertyDescriptor(t, "p");
|
var pd = Object.getOwnPropertyDescriptor(t, "p");
|
||||||
@ -336,7 +331,7 @@ describe("js replacer - webpack", () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
return "miniCssF";
|
return "miniCssF";
|
||||||
})()] = e => "/build/static/css/"+e+"."+{164:"dcfd7749",908:"67c9ed2c"}[e]+".chunk.css"
|
})()] = e => "/${basenameOfTheKeycloakifyResourcesDir}/static/css/"+e+"."+{164:"dcfd7749",908:"67c9ed2c"}[e]+".chunk.css"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
expect(isSameCode(fixedJsCode, fixedJsCodeExpected)).toBe(true);
|
expect(isSameCode(fixedJsCode, fixedJsCodeExpected)).toBe(true);
|
||||||
@ -385,279 +380,156 @@ describe("js replacer - webpack", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe("css replacer", () => {
|
describe("css replacer", () => {
|
||||||
it("transforms absolute urls to css globals properly with no urlPathname", () => {
|
it("replaceImportsInCssCode - 1", () => {
|
||||||
const { fixedCssCode, cssGlobalsToDefine } = replaceImportsInCssCode({
|
const { fixedCssCode } = replaceImportsInCssCode({
|
||||||
cssCode: `
|
cssCode: `
|
||||||
.my-div {
|
.my-div {
|
||||||
background: url(/logo192.png) no-repeat center center;
|
background: url(/background.png) no-repeat center center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.my-div2 {
|
.my-div2 {
|
||||||
background: url(/logo192.png) repeat center center;
|
background: url(/assets/background.png) repeat center center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.my-div {
|
.my-div3 {
|
||||||
background-image: url(/static/media/something.svg);
|
background-image: url(/assets/media/something.svg);
|
||||||
}
|
}
|
||||||
`
|
`,
|
||||||
});
|
cssFileRelativeDirPath: "assets/",
|
||||||
|
|
||||||
const fixedCssCodeExpected = `
|
|
||||||
.my-div {
|
|
||||||
background: var(--urla882a969fd39473) no-repeat center center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.my-div2 {
|
|
||||||
background: var(--urla882a969fd39473) repeat center center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.my-div {
|
|
||||||
background-image: var(--urldd75cab58377c19);
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
|
||||||
|
|
||||||
const cssGlobalsToDefineExpected = {
|
|
||||||
urla882a969fd39473: "url(/logo192.png)",
|
|
||||||
urldd75cab58377c19: "url(/static/media/something.svg)"
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(same(cssGlobalsToDefine, cssGlobalsToDefineExpected)).toBe(true);
|
|
||||||
|
|
||||||
const { cssCodeToPrependInHead } = generateCssCodeToDefineGlobals({
|
|
||||||
cssGlobalsToDefine,
|
|
||||||
buildContext: {
|
buildContext: {
|
||||||
urlPathname: undefined
|
urlPathname: undefined
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const cssCodeToPrependInHeadExpected = `
|
|
||||||
:root {
|
|
||||||
--urla882a969fd39473: url(\${url.resourcesPath}/build/logo192.png);
|
|
||||||
--urldd75cab58377c19: url(\${url.resourcesPath}/build/static/media/something.svg);
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
expect(isSameCode(cssCodeToPrependInHead, cssCodeToPrependInHeadExpected)).toBe(
|
|
||||||
true
|
|
||||||
);
|
|
||||||
});
|
|
||||||
it("transforms absolute urls to css globals properly with custom urlPathname", () => {
|
|
||||||
const { fixedCssCode, cssGlobalsToDefine } = replaceImportsInCssCode({
|
|
||||||
cssCode: `
|
|
||||||
.my-div {
|
|
||||||
background: url(/x/y/z/logo192.png) no-repeat center center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.my-div2 {
|
|
||||||
background: url(/x/y/z/logo192.png) no-repeat center center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.my-div {
|
|
||||||
background-image: url(/x/y/z/static/media/something.svg);
|
|
||||||
}
|
|
||||||
`
|
|
||||||
});
|
|
||||||
|
|
||||||
const fixedCssCodeExpected = `
|
const fixedCssCodeExpected = `
|
||||||
.my-div {
|
.my-div {
|
||||||
background: var(--url749a3139386b2c8) no-repeat center center;
|
background: url(../background.png) no-repeat center center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.my-div2 {
|
.my-div2 {
|
||||||
background: var(--url749a3139386b2c8) no-repeat center center;
|
background: url(background.png) repeat center center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.my-div {
|
.my-div3 {
|
||||||
background-image: var(--url8bdc0887b97ac9a);
|
background-image: url(media/something.svg);
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
const cssGlobalsToDefineExpected = {
|
it("replaceImportsInCssCode - 2", () => {
|
||||||
url749a3139386b2c8: "url(/x/y/z/logo192.png)",
|
const { fixedCssCode } = replaceImportsInCssCode({
|
||||||
url8bdc0887b97ac9a: "url(/x/y/z/static/media/something.svg)"
|
cssCode: `
|
||||||
};
|
.my-div {
|
||||||
|
background: url(/a/b/background.png) no-repeat center center;
|
||||||
expect(same(cssGlobalsToDefine, cssGlobalsToDefineExpected)).toBe(true);
|
}
|
||||||
|
|
||||||
const { cssCodeToPrependInHead } = generateCssCodeToDefineGlobals({
|
.my-div2 {
|
||||||
cssGlobalsToDefine,
|
background: url(/a/b/assets/background.png) repeat center center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.my-div3 {
|
||||||
|
background-image: url(/a/b/assets/media/something.svg);
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
cssFileRelativeDirPath: "assets/",
|
||||||
buildContext: {
|
buildContext: {
|
||||||
urlPathname: "/x/y/z/"
|
urlPathname: "/a/b/"
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const cssCodeToPrependInHeadExpected = `
|
const fixedCssCodeExpected = `
|
||||||
:root {
|
.my-div {
|
||||||
--url749a3139386b2c8: url(\${url.resourcesPath}/build/logo192.png);
|
background: url(../background.png) no-repeat center center;
|
||||||
--url8bdc0887b97ac9a: url(\${url.resourcesPath}/build/static/media/something.svg);
|
}
|
||||||
|
|
||||||
|
.my-div2 {
|
||||||
|
background: url(background.png) repeat center center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.my-div3 {
|
||||||
|
background-image: url(media/something.svg);
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
expect(isSameCode(cssCodeToPrependInHead, cssCodeToPrependInHeadExpected)).toBe(
|
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
||||||
true
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
describe("inline css replacer", () => {
|
it("replaceImportsInCssCode - 3", () => {
|
||||||
describe("no url pathName", () => {
|
const { fixedCssCode } = replaceImportsInCssCode({
|
||||||
const cssCode = `
|
cssCode: `
|
||||||
@font-face {
|
.my-div {
|
||||||
font-family: "Work Sans";
|
background: url(/a/b/background.png) no-repeat center center;
|
||||||
font-style: normal;
|
|
||||||
font-weight: 400;
|
|
||||||
font-display: swap;
|
|
||||||
src: url("/fonts/WorkSans/worksans-regular-webfont.woff2") format("woff2");
|
|
||||||
}
|
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 500;
|
|
||||||
font-display: swap;
|
|
||||||
src: url("/fonts/WorkSans/worksans-medium-webfont.woff2") format("woff2");
|
|
||||||
}
|
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 600;
|
|
||||||
font-display: swap;
|
|
||||||
src: url("/fonts/WorkSans/worksans-semibold-webfont.woff2") format("woff2");
|
|
||||||
}
|
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 700;
|
|
||||||
font-display: swap;
|
|
||||||
src: url("/fonts/WorkSans/worksans-bold-webfont.woff2") format("woff2");
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
it("transforms css for standalone app properly", () => {
|
|
||||||
const { fixedCssCode } = replaceImportsInInlineCssCode({
|
|
||||||
cssCode,
|
|
||||||
buildContext: {
|
|
||||||
urlPathname: undefined
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
.my-div2 {
|
||||||
|
background: url(/a/b/assets/background.png) repeat center center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.my-div3 {
|
||||||
|
background-image: url(/a/b/assets/media/something.svg);
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
cssFileRelativeDirPath: undefined,
|
||||||
|
buildContext: {
|
||||||
|
urlPathname: "/a/b/"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
const fixedCssCodeExpected = `
|
const fixedCssCodeExpected = `
|
||||||
@font-face {
|
.my-div {
|
||||||
font-family: "Work Sans";
|
background: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/background.png) no-repeat center center;
|
||||||
font-style: normal;
|
|
||||||
font-weight: 400;
|
|
||||||
font-display: swap;
|
|
||||||
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-regular-webfont.woff2)
|
|
||||||
format("woff2");
|
|
||||||
}
|
}
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
.my-div2 {
|
||||||
font-style: normal;
|
background: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/assets/background.png) repeat center center;
|
||||||
font-weight: 500;
|
|
||||||
font-display: swap;
|
|
||||||
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-medium-webfont.woff2)
|
|
||||||
format("woff2");
|
|
||||||
}
|
}
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
.my-div3 {
|
||||||
font-style: normal;
|
background-image: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/assets/media/something.svg);
|
||||||
font-weight: 600;
|
|
||||||
font-display: swap;
|
|
||||||
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-semibold-webfont.woff2)
|
|
||||||
format("woff2");
|
|
||||||
}
|
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 700;
|
|
||||||
font-display: swap;
|
|
||||||
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-bold-webfont.woff2)
|
|
||||||
format("woff2");
|
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("with url pathName", () => {
|
it("replaceImportsInCssCode - 4", () => {
|
||||||
const cssCode = `
|
const { fixedCssCode } = replaceImportsInCssCode({
|
||||||
@font-face {
|
cssCode: `
|
||||||
font-family: "Work Sans";
|
.my-div {
|
||||||
font-style: normal;
|
background: url(/background.png) no-repeat center center;
|
||||||
font-weight: 400;
|
|
||||||
font-display: swap;
|
|
||||||
src: url("/x/y/z/fonts/WorkSans/worksans-regular-webfont.woff2") format("woff2");
|
|
||||||
}
|
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 500;
|
|
||||||
font-display: swap;
|
|
||||||
src: url("/x/y/z/fonts/WorkSans/worksans-medium-webfont.woff2") format("woff2");
|
|
||||||
}
|
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 600;
|
|
||||||
font-display: swap;
|
|
||||||
src: url("/x/y/z/fonts/WorkSans/worksans-semibold-webfont.woff2") format("woff2");
|
|
||||||
}
|
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 700;
|
|
||||||
font-display: swap;
|
|
||||||
src: url("/x/y/z/fonts/WorkSans/worksans-bold-webfont.woff2") format("woff2");
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
it("transforms css for standalone app properly", () => {
|
|
||||||
const { fixedCssCode } = replaceImportsInInlineCssCode({
|
|
||||||
cssCode,
|
|
||||||
buildContext: {
|
|
||||||
urlPathname: "/x/y/z/"
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
.my-div2 {
|
||||||
const fixedCssCodeExpected = `
|
background: url(/assets/background.png) repeat center center;
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 400;
|
|
||||||
font-display: swap;
|
|
||||||
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-regular-webfont.woff2)
|
|
||||||
format("woff2");
|
|
||||||
}
|
}
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
.my-div3 {
|
||||||
font-style: normal;
|
background-image: url(/assets/media/something.svg);
|
||||||
font-weight: 500;
|
|
||||||
font-display: swap;
|
|
||||||
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-medium-webfont.woff2)
|
|
||||||
format("woff2");
|
|
||||||
}
|
}
|
||||||
@font-face {
|
`,
|
||||||
font-family: "Work Sans";
|
cssFileRelativeDirPath: undefined,
|
||||||
font-style: normal;
|
buildContext: {
|
||||||
font-weight: 600;
|
urlPathname: undefined
|
||||||
font-display: swap;
|
}
|
||||||
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-semibold-webfont.woff2)
|
|
||||||
format("woff2");
|
|
||||||
}
|
|
||||||
@font-face {
|
|
||||||
font-family: "Work Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 700;
|
|
||||||
font-display: swap;
|
|
||||||
src: url(\${url.resourcesPath}/build/fonts/WorkSans/worksans-bold-webfont.woff2)
|
|
||||||
format("woff2");
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const fixedCssCodeExpected = `
|
||||||
|
.my-div {
|
||||||
|
background: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/background.png) no-repeat center center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.my-div2 {
|
||||||
|
background: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/assets/background.png) repeat center center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.my-div3 {
|
||||||
|
background-image: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/assets/media/something.svg);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user