Compare commits
25 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 |
17
.github/workflows/ci.yaml
vendored
17
.github/workflows/ci.yaml
vendored
@ -16,8 +16,8 @@ jobs:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
- uses: bahmutov/npm-install@v1
|
||||
- name: If this step fails run 'yarn format' then commit again.
|
||||
run: yarn format:check
|
||||
- name: If this step fails run 'npm run format' then commit again.
|
||||
run: npm run format:check
|
||||
test:
|
||||
runs-on: ${{ matrix.os }}
|
||||
needs: test_lint
|
||||
@ -32,13 +32,12 @@ jobs:
|
||||
with:
|
||||
node-version: ${{ matrix.node }}
|
||||
- uses: bahmutov/npm-install@v1
|
||||
- run: yarn build
|
||||
- run: yarn test
|
||||
#- run: yarn test:keycloakify-starter
|
||||
- run: npm run build
|
||||
- run: npm run test
|
||||
|
||||
storybook:
|
||||
runs-on: ubuntu-latest
|
||||
if: github.event_name == 'push'
|
||||
#if: github.event_name == 'push'
|
||||
needs: test
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
@ -46,11 +45,11 @@ jobs:
|
||||
with:
|
||||
node-version: '18'
|
||||
- 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
|
||||
env:
|
||||
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:
|
||||
name: Check if version upgrade
|
||||
@ -112,7 +111,7 @@ jobs:
|
||||
with:
|
||||
registry-url: https://registry.npmjs.org/
|
||||
- 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
|
||||
env:
|
||||
DRY_RUN: "0"
|
||||
|
12
package.json
12
package.json
@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "keycloakify",
|
||||
"version": "10.0.0-rc.71",
|
||||
"version": "10.0.0-rc.81",
|
||||
"description": "Create Keycloak themes using React",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/keycloakify/keycloakify.git"
|
||||
},
|
||||
"scripts": {
|
||||
"prepare": "patch-package && tsx scripts/generate-i18n-messages.ts",
|
||||
"prepare": "tsx scripts/generate-i18n-messages.ts",
|
||||
"build": "tsx scripts/build.ts",
|
||||
"storybook": "tsx scripts/start-storybook.ts",
|
||||
"link-in-starter": "tsx scripts/link-in-starter.ts",
|
||||
@ -41,7 +41,8 @@
|
||||
"!dist/bin/",
|
||||
"dist/bin/main.js",
|
||||
"dist/bin/*.index.js",
|
||||
"dist/bin/shared/*.js",
|
||||
"!dist/bin/shared/*.js",
|
||||
"dist/bin/shared/constants.js",
|
||||
"dist/bin/shared/*.d.ts",
|
||||
"dist/bin/shared/*.js.map",
|
||||
"!dist/vite-plugin/",
|
||||
@ -65,7 +66,7 @@
|
||||
"react": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"react-markdown": "^5.0.3",
|
||||
"react-markdown": "^9.0.1",
|
||||
"tsafe": "^1.6.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
@ -101,7 +102,6 @@
|
||||
"lint-staged": "^11.0.0",
|
||||
"magic-string": "^0.30.7",
|
||||
"make-fetch-happen": "^11.0.3",
|
||||
"patch-package": "^8.0.0",
|
||||
"powerhooks": "^1.0.10",
|
||||
"prettier": "^3.2.5",
|
||||
"properties-parser": "^0.3.1",
|
||||
@ -110,7 +110,7 @@
|
||||
"recast": "^0.23.3",
|
||||
"run-exclusive": "^2.2.19",
|
||||
"storybook-dark-mode": "^1.1.2",
|
||||
"termost": "^0.12.0",
|
||||
"termost": "^v0.12.1",
|
||||
"tsc-alias": "^1.8.10",
|
||||
"tss-react": "^4.9.10",
|
||||
"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 { msg, msgStr, getChangeLocalUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n;
|
||||
const { msg, msgStr, getChangeLocaleUrl, labelBySupportedLanguageTag, currentLanguageTag } = i18n;
|
||||
|
||||
const { locale, url, features, realm, message, referrer } = kcContext;
|
||||
|
||||
@ -79,7 +79,7 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
||||
<ul>
|
||||
{locale.supported.map(({ languageTag }) => (
|
||||
<li key={languageTag} className="kc-dropdown-item">
|
||||
<a href={getChangeLocalUrl(languageTag)}>{labelBySupportedLanguageTag[languageTag]}</a>
|
||||
<a href={getChangeLocaleUrl(languageTag)}>{labelBySupportedLanguageTag[languageTag]}</a>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
@ -1,10 +1,8 @@
|
||||
import "keycloakify/tools/Object.fromEntries";
|
||||
import { useEffect, useState } from "react";
|
||||
import { assert } from "tsafe/assert";
|
||||
import messages_fallbackLanguage from "./baseMessages/en";
|
||||
import { getMessages } from "./baseMessages";
|
||||
import type { KcContext } from "../KcContext";
|
||||
import { Reflect } from "tsafe/Reflect";
|
||||
|
||||
export const fallbackLanguageTag = "en";
|
||||
|
||||
@ -30,7 +28,7 @@ export type GenericI18n<MessageKey extends string> = {
|
||||
* Redirect to this url to change the language.
|
||||
* After reload currentLanguageTag === newLanguageTag
|
||||
*/
|
||||
getChangeLocalUrl: (newLanguageTag: string) => string;
|
||||
getChangeLocaleUrl: (newLanguageTag: string) => string;
|
||||
/**
|
||||
* e.g. "en" => "English", "fr" => "Français", ...
|
||||
*
|
||||
@ -88,7 +86,9 @@ export type GenericI18n<MessageKey extends string> = {
|
||||
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 Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined };
|
||||
@ -108,9 +108,9 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
return cachedResult;
|
||||
}
|
||||
|
||||
const partialI18n: Pick<I18n, "currentLanguageTag" | "getChangeLocalUrl" | "labelBySupportedLanguageTag"> = {
|
||||
const partialI18n: Pick<I18n, "currentLanguageTag" | "getChangeLocaleUrl" | "labelBySupportedLanguageTag"> = {
|
||||
currentLanguageTag: kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag,
|
||||
getChangeLocalUrl: newLanguageTag => {
|
||||
getChangeLocaleUrl: newLanguageTag => {
|
||||
const { locale } = kcContext;
|
||||
|
||||
assert(locale !== undefined, "Internationalization not enabled");
|
||||
@ -126,8 +126,8 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
|
||||
const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
|
||||
messages_fallbackLanguage,
|
||||
extraMessages_fallbackLanguage: extraMessages[fallbackLanguageTag],
|
||||
extraMessages: extraMessages[partialI18n.currentLanguageTag]
|
||||
messageBundle_fallbackLanguage: messageBundle[fallbackLanguageTag],
|
||||
messageBundle_currentLanguage: messageBundle[partialI18n.currentLanguageTag]
|
||||
});
|
||||
|
||||
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
|
||||
@ -135,17 +135,19 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
const result: Result = {
|
||||
i18n: {
|
||||
...partialI18n,
|
||||
...createI18nTranslationFunctions({ messages: undefined }),
|
||||
...createI18nTranslationFunctions({
|
||||
messages_currentLanguage: isCurrentLanguageFallbackLanguage ? messages_fallbackLanguage : undefined
|
||||
}),
|
||||
isFetchingTranslations: !isCurrentLanguageFallbackLanguage
|
||||
},
|
||||
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
|
||||
? undefined
|
||||
: (async () => {
|
||||
const messages = await getMessages(partialI18n.currentLanguageTag);
|
||||
const messages_currentLanguage = await getMessages(partialI18n.currentLanguageTag);
|
||||
|
||||
const i18n_currentLanguage: I18n = {
|
||||
...partialI18n,
|
||||
...createI18nTranslationFunctions({ messages }),
|
||||
...createI18nTranslationFunctions({ messages_currentLanguage }),
|
||||
isFetchingTranslations: false
|
||||
};
|
||||
|
||||
@ -168,66 +170,30 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
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: {
|
||||
messages_fallbackLanguage: Record<MessageKey, string>;
|
||||
extraMessages_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||
extraMessages: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||
messageBundle_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||
messageBundle_currentLanguage: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||
}) {
|
||||
const { extraMessages } = params;
|
||||
const { messageBundle_currentLanguage } = params;
|
||||
|
||||
const messages_fallbackLanguage = {
|
||||
...params.messages_fallbackLanguage,
|
||||
...params.extraMessages_fallbackLanguage
|
||||
...params.messageBundle_fallbackLanguage
|
||||
};
|
||||
|
||||
function createI18nTranslationFunctions(params: {
|
||||
messages: Partial<Record<MessageKey, string>> | undefined;
|
||||
messages_currentLanguage: Partial<Record<MessageKey, string>> | undefined;
|
||||
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
|
||||
const messages = {
|
||||
...params.messages,
|
||||
...extraMessages
|
||||
const messages_currentLanguage = {
|
||||
...params.messages_currentLanguage,
|
||||
...messageBundle_currentLanguage
|
||||
};
|
||||
|
||||
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
|
||||
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) {
|
||||
return undefined;
|
||||
|
@ -1,5 +1,5 @@
|
||||
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
|
||||
export type { MessageKey, KcContextLike };
|
||||
export type I18n = GenericI18n<MessageKey>;
|
||||
export { createUseI18n } from "./i18n";
|
||||
export { createUseI18n } from "./useI18n";
|
||||
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 componentName = componentBasename.replace(/.tsx$/, "");
|
||||
|
||||
console.log(
|
||||
[
|
||||
``,
|
||||
@ -207,10 +209,7 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
|
||||
`// ...`,
|
||||
``,
|
||||
chalk.green(
|
||||
`+const ${componentBasename.replace(
|
||||
/.tsx$/,
|
||||
""
|
||||
)} = lazy(() => import("./pages/${componentBasename}"));`
|
||||
`+const ${componentName} = lazy(() => import("./pages/${componentName}"));`
|
||||
),
|
||||
...[
|
||||
``,
|
||||
@ -224,7 +223,7 @@ export async function command(params: { cliCommandOptions: CliCommandOptions })
|
||||
` switch (kcContext.pageId) {`,
|
||||
` // ...`,
|
||||
`+ case "${pageIdOrComponent}": return (`,
|
||||
`+ <${componentBasename}`,
|
||||
`+ <${componentName}`,
|
||||
`+ {...{ kcContext, i18n, classes }}`,
|
||||
`+ Template={Template}`,
|
||||
`+ doUseDefaultCss={true}`,
|
||||
|
@ -1,7 +1,6 @@
|
||||
import cheerio from "cheerio";
|
||||
import { replaceImportsInJsCode } from "../replacers/replaceImportsInJsCode";
|
||||
import { generateCssCodeToDefineGlobals } from "../replacers/replaceImportsInCssCode";
|
||||
import { replaceImportsInInlineCssCode } from "../replacers/replaceImportsInInlineCssCode";
|
||||
import { replaceImportsInCssCode } from "../replacers/replaceImportsInCssCode";
|
||||
import * as fs from "fs";
|
||||
import { join as pathJoin } from "path";
|
||||
import type { BuildContext } from "../../shared/buildContext";
|
||||
@ -9,8 +8,7 @@ import { assert } from "tsafe/assert";
|
||||
import {
|
||||
type ThemeType,
|
||||
basenameOfTheKeycloakifyResourcesDir,
|
||||
resources_common,
|
||||
nameOfTheLocalizationRealmOverridesUserProfileProperty
|
||||
resources_common
|
||||
} from "../../shared/constants";
|
||||
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
|
||||
|
||||
@ -28,7 +26,6 @@ assert<BuildContext extends BuildContextLike ? true : false>();
|
||||
export function generateFtlFilesCodeFactory(params: {
|
||||
themeName: string;
|
||||
indexHtmlCode: string;
|
||||
cssGlobalsToDefine: Record<string, string>;
|
||||
buildContext: BuildContextLike;
|
||||
keycloakifyVersion: string;
|
||||
themeType: ThemeType;
|
||||
@ -36,7 +33,6 @@ export function generateFtlFilesCodeFactory(params: {
|
||||
}) {
|
||||
const {
|
||||
themeName,
|
||||
cssGlobalsToDefine,
|
||||
indexHtmlCode,
|
||||
buildContext,
|
||||
keycloakifyVersion,
|
||||
@ -65,8 +61,9 @@ export function generateFtlFilesCodeFactory(params: {
|
||||
|
||||
assert(cssCode !== null);
|
||||
|
||||
const { fixedCssCode } = replaceImportsInInlineCssCode({
|
||||
const { fixedCssCode } = replaceImportsInCssCode({
|
||||
cssCode,
|
||||
cssFileRelativeDirPath: undefined,
|
||||
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.
|
||||
@ -136,10 +118,6 @@ export function generateFtlFilesCodeFactory(params: {
|
||||
.replace("KEYCLOAKIFY_THEME_TYPE_dExKd3xEdr", themeType)
|
||||
.replace("KEYCLOAKIFY_THEME_NAME_cXxKd3xEer", themeName)
|
||||
.replace("RESOURCES_COMMON_cLsLsMrtDkpVv", resources_common)
|
||||
.replace(
|
||||
"lOCALIZATION_REALM_OVERRIDES_USER_PROFILE_PROPERTY_KEY_aaGLsPgGIdeeX",
|
||||
nameOfTheLocalizationRealmOverridesUserProfileProperty
|
||||
)
|
||||
.replace(
|
||||
"USER_DEFINED_EXCLUSIONS_eKsaY4ZsZ4eMr2",
|
||||
buildContext.kcContextExclusionsFtlCode ?? ""
|
||||
|
@ -33,8 +33,9 @@ kcContext.pageId = "${pageId}";
|
||||
if( kcContext.url && kcContext.url.resourcesPath ){
|
||||
kcContext.url.resourcesCommonPath = kcContext.url.resourcesPath + "/" + "RESOURCES_COMMON_cLsLsMrtDkpVv";
|
||||
}
|
||||
kcContext["x-keycloakify"] = {};
|
||||
<#if profile?? && profile.attributes??>
|
||||
kcContext.lOCALIZATION_REALM_OVERRIDES_USER_PROFILE_PROPERTY_KEY_aaGLsPgGIdeeX = {
|
||||
kcContext["x-keycloakify"].realmMessageBundleUserProfile = {
|
||||
<#list profile.attributes as attribute>
|
||||
<#if attribute.annotations?? && attribute.displayName??>
|
||||
"${attribute.displayName}": decodeHtmlEntities("${advancedMsg(attribute.displayName)?js_string}"),
|
||||
@ -61,6 +62,9 @@ if( kcContext.url && kcContext.url.resourcesPath ){
|
||||
</#list>
|
||||
};
|
||||
</#if>
|
||||
<#if pageId == "terms.ftl" || termsAcceptanceRequired?? && termsAcceptanceRequired>
|
||||
kcContext["x-keycloakify"].realmMessageBundleTermsText= decodeHtmlEntities("${msg("termsText")?js_string}");
|
||||
</#if>
|
||||
attributes_to_attributesByName: {
|
||||
if( !kcContext.profile ){
|
||||
break attributes_to_attributesByName;
|
||||
@ -198,6 +202,9 @@ function decodeHtmlEntities(htmlStr){
|
||||
) || (
|
||||
key == "execution" &&
|
||||
are_same_path(path, [])
|
||||
) || (
|
||||
key == "entity" &&
|
||||
are_same_path(path, ["user"])
|
||||
)
|
||||
>
|
||||
<#-- <#local out_seq += ["/*" + path?join(".") + "." + key + " excluded*/"]> -->
|
||||
|
@ -9,6 +9,8 @@ import * as babelParser from "@babel/parser";
|
||||
import babelGenerate from "@babel/generator";
|
||||
import * as babelTypes from "@babel/types";
|
||||
import { escapeStringForPropertiesFile } from "../../tools/escapeStringForPropertiesFile";
|
||||
import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath";
|
||||
import * as fs from "fs";
|
||||
|
||||
export function generateMessageProperties(params: {
|
||||
themeSrcDirPath: string;
|
||||
@ -39,10 +41,6 @@ export function generateMessageProperties(params: {
|
||||
readFileSync(file).toString("utf8").includes("createUseI18n")
|
||||
);
|
||||
|
||||
if (files.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const extraMessages = files
|
||||
.map(file => {
|
||||
const root = recast.parse(readFileSync(file).toString("utf8"), {
|
||||
@ -99,15 +97,28 @@ export function generateMessageProperties(params: {
|
||||
return extraMessages;
|
||||
});
|
||||
|
||||
const languageTags = extraMessages
|
||||
.map(extraMessage => Object.keys(extraMessage))
|
||||
.flat()
|
||||
.reduce(...removeDuplicates<string>());
|
||||
const languageTags = [
|
||||
...extraMessages.map(extraMessage => Object.keys(extraMessage)).flat(),
|
||||
...fs
|
||||
.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>> = {};
|
||||
|
||||
for (const languageTag of languageTags) {
|
||||
const keyValueMap: Record<string, string> = {};
|
||||
const keyValueMap: Record<string, string> = {
|
||||
termsText: ""
|
||||
};
|
||||
|
||||
for (const extraMessage of extraMessages) {
|
||||
const keyValueMap_i = extraMessage[languageTag];
|
||||
@ -117,7 +128,7 @@ export function generateMessageProperties(params: {
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(keyValueMap_i)) {
|
||||
if (keyValueMap[key] !== undefined) {
|
||||
if (key !== "termsText" && keyValueMap[key] !== undefined) {
|
||||
console.warn(
|
||||
[
|
||||
"WARNING: The following key is defined multiple times:",
|
||||
@ -152,14 +163,9 @@ export function generateMessageProperties(params: {
|
||||
|
||||
out.push({
|
||||
languageTag,
|
||||
propertiesFileSource: [
|
||||
"# This file was generated by keycloakify",
|
||||
"",
|
||||
"parent=base",
|
||||
"",
|
||||
propertiesFileSource,
|
||||
""
|
||||
].join("\n")
|
||||
propertiesFileSource: ["", "parent=base", "", propertiesFileSource, ""].join(
|
||||
"\n"
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,11 @@
|
||||
import { transformCodebase } from "../../tools/transformCodebase";
|
||||
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 { replaceImportsInCssCode } from "../replacers/replaceImportsInCssCode";
|
||||
import {
|
||||
@ -64,8 +69,6 @@ export async function generateResourcesForMainTheme(params: {
|
||||
return pathJoin(resourcesDirPath, "theme", themeName, themeType);
|
||||
};
|
||||
|
||||
const cssGlobalsToDefine: Record<string, string> = {};
|
||||
|
||||
for (const themeType of ["login", "account"] as const) {
|
||||
if (!buildContext.recordIsImplementedByThemeType[themeType]) {
|
||||
continue;
|
||||
@ -127,21 +130,14 @@ export async function generateResourcesForMainTheme(params: {
|
||||
transformCodebase({
|
||||
srcDirPath: buildContext.projectBuildDirPath,
|
||||
destDirPath,
|
||||
transformSourceCode: ({ filePath, sourceCode }) => {
|
||||
transformSourceCode: ({ filePath, fileRelativePath, sourceCode }) => {
|
||||
if (filePath.endsWith(".css")) {
|
||||
const {
|
||||
cssGlobalsToDefine: cssGlobalsToDefineForThisFile,
|
||||
fixedCssCode
|
||||
} = replaceImportsInCssCode({
|
||||
cssCode: sourceCode.toString("utf8")
|
||||
const { fixedCssCode } = replaceImportsInCssCode({
|
||||
cssCode: sourceCode.toString("utf8"),
|
||||
cssFileRelativeDirPath: pathDirname(fileRelativePath),
|
||||
buildContext
|
||||
});
|
||||
|
||||
Object.entries(cssGlobalsToDefineForThisFile).forEach(
|
||||
([key, value]) => {
|
||||
cssGlobalsToDefine[key] = value;
|
||||
}
|
||||
);
|
||||
|
||||
return {
|
||||
modifiedSourceCode: Buffer.from(fixedCssCode, "utf8")
|
||||
};
|
||||
@ -168,7 +164,6 @@ export async function generateResourcesForMainTheme(params: {
|
||||
indexHtmlCode: fs
|
||||
.readFileSync(pathJoin(buildContext.projectBuildDirPath, "index.html"))
|
||||
.toString("utf8"),
|
||||
cssGlobalsToDefine,
|
||||
buildContext,
|
||||
keycloakifyVersion: readThisNpmPackageVersion(),
|
||||
themeType,
|
||||
|
@ -1,7 +1,7 @@
|
||||
import * as crypto from "crypto";
|
||||
import type { BuildContext } from "../../shared/buildContext";
|
||||
import { assert } from "tsafe/assert";
|
||||
import { basenameOfTheKeycloakifyResourcesDir } from "../../shared/constants";
|
||||
import { assert } from "tsafe/assert";
|
||||
import { posix } from "path";
|
||||
|
||||
export type BuildContextLike = {
|
||||
urlPathname: string | undefined;
|
||||
@ -9,68 +9,45 @@ export type BuildContextLike = {
|
||||
|
||||
assert<BuildContext extends BuildContextLike ? true : false>();
|
||||
|
||||
export function replaceImportsInCssCode(params: { cssCode: string }): {
|
||||
fixedCssCode: string;
|
||||
cssGlobalsToDefine: Record<string, string>;
|
||||
} {
|
||||
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>;
|
||||
export function replaceImportsInCssCode(params: {
|
||||
cssCode: string;
|
||||
cssFileRelativeDirPath: string | undefined;
|
||||
buildContext: BuildContextLike;
|
||||
}): {
|
||||
cssCodeToPrependInHead: string;
|
||||
fixedCssCode: string;
|
||||
} {
|
||||
const { cssGlobalsToDefine, buildContext } = params;
|
||||
const { cssCode, cssFileRelativeDirPath, buildContext } = params;
|
||||
|
||||
return {
|
||||
cssCodeToPrependInHead: [
|
||||
":root {",
|
||||
...Object.keys(cssGlobalsToDefine)
|
||||
.map(cssVariableName =>
|
||||
[
|
||||
`--${cssVariableName}:`,
|
||||
cssGlobalsToDefine[cssVariableName].replace(
|
||||
new RegExp(
|
||||
`url\\(${(buildContext.urlPathname ?? "/").replace(
|
||||
/\//g,
|
||||
"\\/"
|
||||
)}`,
|
||||
"g"
|
||||
),
|
||||
`url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/`
|
||||
)
|
||||
].join(" ")
|
||||
)
|
||||
.map(line => ` ${line};`),
|
||||
"}"
|
||||
].join("\n")
|
||||
};
|
||||
const fixedCssCode = cssCode.replace(
|
||||
/url\(["']?(\/[^/][^)"']+)["']?\)/g,
|
||||
(match, assetFileAbsoluteUrlPathname) => {
|
||||
if (buildContext.urlPathname !== undefined) {
|
||||
if (!assetFileAbsoluteUrlPathname.startsWith(buildContext.urlPathname)) {
|
||||
// NOTE: Should never happen
|
||||
return match;
|
||||
}
|
||||
assetFileAbsoluteUrlPathname = assetFileAbsoluteUrlPathname.replace(
|
||||
buildContext.urlPathname,
|
||||
"/"
|
||||
);
|
||||
}
|
||||
|
||||
inline_style_in_html: {
|
||||
if (cssFileRelativeDirPath !== undefined) {
|
||||
break inline_style_in_html;
|
||||
}
|
||||
|
||||
return `url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}${assetFileAbsoluteUrlPathname})`;
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
@ -1,5 +1,3 @@
|
||||
export const nameOfTheLocalizationRealmOverridesUserProfileProperty =
|
||||
"__localizationRealmOverridesUserProfile";
|
||||
export const keycloak_resources = "keycloak-resources";
|
||||
export const resources_common = "resources-common";
|
||||
export const lastKeycloakVersionWithAccountV1 = "21.1.2";
|
||||
|
@ -112,6 +112,18 @@ export async function downloadKeycloakDefaultTheme(params: {
|
||||
"dist",
|
||||
"fonts",
|
||||
"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",
|
||||
"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(
|
||||
"patternfly",
|
||||
"dist",
|
||||
"fonts",
|
||||
"fontawesome-webfont.woff2"
|
||||
),
|
||||
pathJoin(
|
||||
"patternfly",
|
||||
"dist",
|
||||
"fonts",
|
||||
"PatternFlyIcons-webfont.ttf"
|
||||
),
|
||||
pathJoin(
|
||||
"patternfly",
|
||||
"dist",
|
||||
"fonts",
|
||||
"PatternFlyIcons-webfont.woff"
|
||||
),
|
||||
pathJoin("jquery", "dist", "jquery.min.js")
|
||||
];
|
||||
}
|
||||
|
@ -1,8 +1,4 @@
|
||||
import type {
|
||||
ThemeType,
|
||||
LoginThemePageId,
|
||||
nameOfTheLocalizationRealmOverridesUserProfileProperty
|
||||
} from "keycloakify/bin/shared/constants";
|
||||
import type { ThemeType, LoginThemePageId } from "keycloakify/bin/shared/constants";
|
||||
import type { ExtractAfterStartingWith } from "keycloakify/tools/ExtractAfterStartingWith";
|
||||
import type { ValueOf } from "keycloakify/tools/ValueOf";
|
||||
import { assert } from "tsafe/assert";
|
||||
@ -158,7 +154,10 @@ export declare namespace KcContext {
|
||||
ssoLoginInOtherTabsUrl: string;
|
||||
};
|
||||
properties: {};
|
||||
__localizationRealmOverridesUserProfile?: Record<string, string>;
|
||||
"x-keycloakify": {
|
||||
realmMessageBundleUserProfile: Record<string, string> | undefined;
|
||||
realmMessageBundleTermsText: string | undefined;
|
||||
};
|
||||
};
|
||||
|
||||
export type SamlPostForm = Common & {
|
||||
@ -276,6 +275,7 @@ export declare namespace KcContext {
|
||||
lastName?: string;
|
||||
markedForEviction?: boolean;
|
||||
};
|
||||
__localizationRealmOverridesTermsText?: string;
|
||||
};
|
||||
|
||||
export type LoginDeviceVerifyUserCode = Common & {
|
||||
@ -772,11 +772,3 @@ export type PasswordPolicies = {
|
||||
/** Whether the password can be the email address */
|
||||
notEmail?: boolean;
|
||||
};
|
||||
|
||||
assert<
|
||||
KcContext.Common extends Partial<
|
||||
Record<typeof nameOfTheLocalizationRealmOverridesUserProfileProperty, unknown>
|
||||
>
|
||||
? true
|
||||
: false
|
||||
>();
|
||||
|
@ -161,7 +161,10 @@ export const kcContextCommonMock: KcContext.Common = {
|
||||
scripts: [],
|
||||
isAppInitiatedAction: false,
|
||||
properties: {},
|
||||
__localizationRealmOverridesUserProfile: {}
|
||||
"x-keycloakify": {
|
||||
realmMessageBundleUserProfile: undefined,
|
||||
realmMessageBundleTermsText: undefined
|
||||
}
|
||||
};
|
||||
|
||||
const loginUrl = {
|
||||
|
@ -29,7 +29,7 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
||||
|
||||
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;
|
||||
|
||||
@ -153,7 +153,7 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
|
||||
role="menuitem"
|
||||
id={`language-${i + 1}`}
|
||||
className={kcClsx("kcLocaleItemClass")}
|
||||
href={getChangeLocalUrl(languageTag)}
|
||||
href={getChangeLocaleUrl(languageTag)}
|
||||
>
|
||||
{labelBySupportedLanguageTag[languageTag]}
|
||||
</a>
|
||||
|
@ -1,10 +1,8 @@
|
||||
import "keycloakify/tools/Object.fromEntries";
|
||||
import { useEffect, useState } from "react";
|
||||
import { assert } from "tsafe/assert";
|
||||
import messages_fallbackLanguage from "./baseMessages/en";
|
||||
import { getMessages } from "./baseMessages";
|
||||
import type { KcContext } from "../KcContext";
|
||||
import { Reflect } from "tsafe/Reflect";
|
||||
|
||||
export const fallbackLanguageTag = "en";
|
||||
|
||||
@ -13,7 +11,10 @@ export type KcContextLike = {
|
||||
currentLanguageTag: 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>();
|
||||
@ -31,7 +32,7 @@ export type GenericI18n<MessageKey extends string> = {
|
||||
* Redirect to this url to change the language.
|
||||
* After reload currentLanguageTag === newLanguageTag
|
||||
*/
|
||||
getChangeLocalUrl: (newLanguageTag: string) => string;
|
||||
getChangeLocaleUrl: (newLanguageTag: string) => string;
|
||||
/**
|
||||
* e.g. "en" => "English", "fr" => "Français", ...
|
||||
*
|
||||
@ -89,7 +90,9 @@ export type GenericI18n<MessageKey extends string> = {
|
||||
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 Result = { i18n: I18n; prI18n_currentLanguage: Promise<I18n> | undefined };
|
||||
@ -109,9 +112,9 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
return cachedResult;
|
||||
}
|
||||
|
||||
const partialI18n: Pick<I18n, "currentLanguageTag" | "getChangeLocalUrl" | "labelBySupportedLanguageTag"> = {
|
||||
const partialI18n: Pick<I18n, "currentLanguageTag" | "getChangeLocaleUrl" | "labelBySupportedLanguageTag"> = {
|
||||
currentLanguageTag: kcContext.locale?.currentLanguageTag ?? fallbackLanguageTag,
|
||||
getChangeLocalUrl: newLanguageTag => {
|
||||
getChangeLocaleUrl: newLanguageTag => {
|
||||
const { locale } = kcContext;
|
||||
|
||||
assert(locale !== undefined, "Internationalization not enabled");
|
||||
@ -127,9 +130,10 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
|
||||
const { createI18nTranslationFunctions } = createI18nTranslationFunctionsFactory<MessageKey, ExtraMessageKey>({
|
||||
messages_fallbackLanguage,
|
||||
extraMessages_fallbackLanguage: extraMessages[fallbackLanguageTag],
|
||||
extraMessages: extraMessages[partialI18n.currentLanguageTag],
|
||||
__localizationRealmOverridesUserProfile: kcContext.__localizationRealmOverridesUserProfile
|
||||
messageBundle_fallbackLanguage: messageBundle[fallbackLanguageTag],
|
||||
messageBundle_currentLanguage: messageBundle[partialI18n.currentLanguageTag],
|
||||
realmMessageBundleUserProfile: kcContext["x-keycloakify"].realmMessageBundleUserProfile,
|
||||
realmMessageBundleTermsText: kcContext["x-keycloakify"].realmMessageBundleTermsText
|
||||
});
|
||||
|
||||
const isCurrentLanguageFallbackLanguage = partialI18n.currentLanguageTag === fallbackLanguageTag;
|
||||
@ -137,17 +141,19 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
const result: Result = {
|
||||
i18n: {
|
||||
...partialI18n,
|
||||
...createI18nTranslationFunctions({ messages: undefined }),
|
||||
...createI18nTranslationFunctions({
|
||||
messages_currentLanguage: isCurrentLanguageFallbackLanguage ? messages_fallbackLanguage : undefined
|
||||
}),
|
||||
isFetchingTranslations: !isCurrentLanguageFallbackLanguage
|
||||
},
|
||||
prI18n_currentLanguage: isCurrentLanguageFallbackLanguage
|
||||
? undefined
|
||||
: (async () => {
|
||||
const messages = await getMessages(partialI18n.currentLanguageTag);
|
||||
const messages_currentLanguage = await getMessages(partialI18n.currentLanguageTag);
|
||||
|
||||
const i18n_currentLanguage: I18n = {
|
||||
...partialI18n,
|
||||
...createI18nTranslationFunctions({ messages }),
|
||||
...createI18nTranslationFunctions({ messages_currentLanguage }),
|
||||
isFetchingTranslations: false
|
||||
};
|
||||
|
||||
@ -170,67 +176,40 @@ function createGetI18n<ExtraMessageKey extends string = never>(extraMessages: {
|
||||
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: {
|
||||
messages_fallbackLanguage: Record<MessageKey, string>;
|
||||
extraMessages_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||
extraMessages: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||
__localizationRealmOverridesUserProfile: Record<string, string> | undefined;
|
||||
messageBundle_fallbackLanguage: Record<ExtraMessageKey, string> | undefined;
|
||||
messageBundle_currentLanguage: Partial<Record<ExtraMessageKey, string>> | undefined;
|
||||
realmMessageBundleUserProfile: Record<string, string> | undefined;
|
||||
realmMessageBundleTermsText: string | undefined;
|
||||
}) {
|
||||
const { __localizationRealmOverridesUserProfile, extraMessages } = params;
|
||||
const { messageBundle_currentLanguage, realmMessageBundleUserProfile, realmMessageBundleTermsText } = params;
|
||||
|
||||
const messages_fallbackLanguage = {
|
||||
...params.messages_fallbackLanguage,
|
||||
...params.extraMessages_fallbackLanguage
|
||||
...params.messageBundle_fallbackLanguage
|
||||
};
|
||||
|
||||
function createI18nTranslationFunctions(params: {
|
||||
messages: Partial<Record<MessageKey, string>> | undefined;
|
||||
messages_currentLanguage: Partial<Record<MessageKey, string>> | undefined;
|
||||
}): Pick<GenericI18n<MessageKey | ExtraMessageKey>, "msg" | "msgStr" | "advancedMsg" | "advancedMsgStr"> {
|
||||
const messages = {
|
||||
...params.messages,
|
||||
...extraMessages
|
||||
const messages_currentLanguage = {
|
||||
...params.messages_currentLanguage,
|
||||
...messageBundle_currentLanguage
|
||||
};
|
||||
|
||||
function resolveMsg(props: { key: string; args: (string | undefined)[]; doRenderAsHtml: boolean }): string | JSX.Element | undefined {
|
||||
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) {
|
||||
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 {
|
||||
const { key, args, doRenderAsHtml } = props;
|
||||
|
||||
if (__localizationRealmOverridesUserProfile !== undefined && key in __localizationRealmOverridesUserProfile) {
|
||||
const resolvedMessage = __localizationRealmOverridesUserProfile[key];
|
||||
if (realmMessageBundleUserProfile !== undefined && key in realmMessageBundleUserProfile) {
|
||||
const resolvedMessage = realmMessageBundleUserProfile[key];
|
||||
|
||||
return doRenderAsHtml ? (
|
||||
<span
|
||||
|
@ -1,5 +1,5 @@
|
||||
import type { GenericI18n, MessageKey, KcContextLike } from "./i18n";
|
||||
export type { MessageKey, KcContextLike };
|
||||
export type I18n = GenericI18n<MessageKey>;
|
||||
export { createUseI18n } from "./i18n";
|
||||
export { createUseI18n } from "./useI18n";
|
||||
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 { Markdown } from "keycloakify/tools/Markdown";
|
||||
import type { LazyOrNot } from "keycloakify/tools/LazyOrNot";
|
||||
import { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
|
||||
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"> }) {
|
||||
const { i18n, kcClsx, messagesPerField } = props;
|
||||
|
||||
const { msg } = 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;
|
||||
}
|
||||
const { msg, msgStr } = i18n;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="form-group">
|
||||
<div className={kcClsx("kcInputWrapperClass")}>
|
||||
{msg("termsTitle")}
|
||||
<div id="kc-registration-terms-text">
|
||||
<Markdown>{termsMarkdown}</Markdown>
|
||||
</div>
|
||||
<div id="kc-registration-terms-text">{msgStr("termsText") ? msg("termsText") : <TermsMarkdown />}</div>
|
||||
</div>
|
||||
</div>
|
||||
<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 { useTermsMarkdown } from "keycloakify/login/lib/useDownloadTerms";
|
||||
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 { locale, url } = kcContext;
|
||||
|
||||
const { isDownloadComplete, termsMarkdown, termsLanguageTag } = useTermsMarkdown();
|
||||
|
||||
if (!isDownloadComplete) {
|
||||
return null;
|
||||
}
|
||||
const { url } = kcContext;
|
||||
|
||||
return (
|
||||
<Template
|
||||
@ -32,9 +25,7 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
|
||||
displayMessage={false}
|
||||
headerNode={msg("termsTitle")}
|
||||
>
|
||||
<div id="kc-terms-text" lang={termsLanguageTag !== locale?.currentLanguageTag ? termsLanguageTag : undefined}>
|
||||
<Markdown>{termsMarkdown}</Markdown>
|
||||
</div>
|
||||
<div id="kc-terms-text">{msgStr("termsText") ? msg("termsText") : <TermsMarkdown />}</div>
|
||||
<form className="form-actions" action={url.loginAction} method="POST">
|
||||
<input
|
||||
className={kcClsx("kcButtonClass", "kcButtonClass", "kcButtonClass", "kcButtonPrimaryClass", "kcButtonLargeClass")}
|
||||
@ -55,3 +46,13 @@ export default function Terms(props: PageProps<Extract<KcContext, { pageId: "ter
|
||||
</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>;
|
||||
|
||||
export const Default: Story = {
|
||||
render: () => <KcPageStory />
|
||||
render: () => (
|
||||
<KcPageStory
|
||||
kcContext={{
|
||||
"x-keycloakify": {
|
||||
realmMessageBundleTermsText: "<p>My terms in <strong>English</strong></p>"
|
||||
}
|
||||
}}
|
||||
/>
|
||||
)
|
||||
};
|
||||
|
||||
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: () => (
|
||||
<KcPageStory
|
||||
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_webpack } from "keycloakify/bin/keycloakify/replacers/replaceImportsInJsCode/webpack";
|
||||
import {
|
||||
generateCssCodeToDefineGlobals,
|
||||
replaceImportsInCssCode
|
||||
} from "keycloakify/bin/keycloakify/replacers/replaceImportsInCssCode";
|
||||
import { replaceImportsInInlineCssCode } from "keycloakify/bin/keycloakify/replacers/replaceImportsInInlineCssCode";
|
||||
import { same } from "evt/tools/inDepth/same";
|
||||
import { replaceImportsInCssCode } from "keycloakify/bin/keycloakify/replacers/replaceImportsInCssCode";
|
||||
import { expect, it, describe } from "vitest";
|
||||
import { basenameOfTheKeycloakifyResourcesDir } from "keycloakify/bin/shared/constants";
|
||||
|
||||
@ -385,279 +380,156 @@ describe("js replacer - webpack", () => {
|
||||
});
|
||||
|
||||
describe("css replacer", () => {
|
||||
it("transforms absolute urls to css globals properly with no urlPathname", () => {
|
||||
const { fixedCssCode, cssGlobalsToDefine } = replaceImportsInCssCode({
|
||||
it("replaceImportsInCssCode - 1", () => {
|
||||
const { fixedCssCode } = replaceImportsInCssCode({
|
||||
cssCode: `
|
||||
.my-div {
|
||||
background: url(/logo192.png) no-repeat center center;
|
||||
background: url(/background.png) no-repeat center center;
|
||||
}
|
||||
|
||||
.my-div2 {
|
||||
background: url(/logo192.png) repeat center center;
|
||||
background: url(/assets/background.png) repeat center center;
|
||||
}
|
||||
|
||||
.my-div {
|
||||
background-image: url(/static/media/something.svg);
|
||||
.my-div3 {
|
||||
background-image: url(/assets/media/something.svg);
|
||||
}
|
||||
`
|
||||
});
|
||||
|
||||
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: {
|
||||
urlPathname: undefined
|
||||
}
|
||||
});
|
||||
|
||||
const cssCodeToPrependInHeadExpected = `
|
||||
:root {
|
||||
--urla882a969fd39473: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/logo192.png);
|
||||
--urldd75cab58377c19: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/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 = `
|
||||
.my-div {
|
||||
background: var(--url749a3139386b2c8) no-repeat center center;
|
||||
}
|
||||
|
||||
.my-div2 {
|
||||
background: var(--url749a3139386b2c8) no-repeat center center;
|
||||
}
|
||||
|
||||
.my-div {
|
||||
background-image: var(--url8bdc0887b97ac9a);
|
||||
}
|
||||
`;
|
||||
|
||||
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
||||
|
||||
const cssGlobalsToDefineExpected = {
|
||||
url749a3139386b2c8: "url(/x/y/z/logo192.png)",
|
||||
url8bdc0887b97ac9a: "url(/x/y/z/static/media/something.svg)"
|
||||
};
|
||||
|
||||
expect(same(cssGlobalsToDefine, cssGlobalsToDefineExpected)).toBe(true);
|
||||
|
||||
const { cssCodeToPrependInHead } = generateCssCodeToDefineGlobals({
|
||||
cssGlobalsToDefine,
|
||||
buildContext: {
|
||||
urlPathname: "/x/y/z/"
|
||||
}
|
||||
});
|
||||
|
||||
const cssCodeToPrependInHeadExpected = `
|
||||
:root {
|
||||
--url749a3139386b2c8: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/logo192.png);
|
||||
--url8bdc0887b97ac9a: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/static/media/something.svg);
|
||||
}
|
||||
`;
|
||||
|
||||
expect(isSameCode(cssCodeToPrependInHead, cssCodeToPrependInHeadExpected)).toBe(
|
||||
true
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("inline css replacer", () => {
|
||||
describe("no url pathName", () => {
|
||||
const cssCode = `
|
||||
@font-face {
|
||||
font-family: "Work Sans";
|
||||
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,
|
||||
`,
|
||||
cssFileRelativeDirPath: "assets/",
|
||||
buildContext: {
|
||||
urlPathname: undefined
|
||||
}
|
||||
});
|
||||
|
||||
const fixedCssCodeExpected = `
|
||||
@font-face {
|
||||
font-family: "Work Sans";
|
||||
font-style: normal;
|
||||
font-weight: 400;
|
||||
font-display: swap;
|
||||
src: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/fonts/WorkSans/worksans-regular-webfont.woff2)
|
||||
format("woff2");
|
||||
.my-div {
|
||||
background: url(../background.png) no-repeat center center;
|
||||
}
|
||||
@font-face {
|
||||
font-family: "Work Sans";
|
||||
font-style: normal;
|
||||
font-weight: 500;
|
||||
font-display: swap;
|
||||
src: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/fonts/WorkSans/worksans-medium-webfont.woff2)
|
||||
format("woff2");
|
||||
|
||||
.my-div2 {
|
||||
background: url(background.png) repeat center center;
|
||||
}
|
||||
@font-face {
|
||||
font-family: "Work Sans";
|
||||
font-style: normal;
|
||||
font-weight: 600;
|
||||
font-display: swap;
|
||||
src: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/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}/${basenameOfTheKeycloakifyResourcesDir}/fonts/WorkSans/worksans-bold-webfont.woff2)
|
||||
format("woff2");
|
||||
|
||||
.my-div3 {
|
||||
background-image: url(media/something.svg);
|
||||
}
|
||||
`;
|
||||
|
||||
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("with url pathName", () => {
|
||||
const cssCode = `
|
||||
@font-face {
|
||||
font-family: "Work Sans";
|
||||
font-style: normal;
|
||||
font-weight: 400;
|
||||
font-display: swap;
|
||||
src: url("/x/y/z/fonts/WorkSans/worksans-regular-webfont.woff2") format("woff2");
|
||||
it("replaceImportsInCssCode - 2", () => {
|
||||
const { fixedCssCode } = replaceImportsInCssCode({
|
||||
cssCode: `
|
||||
.my-div {
|
||||
background: url(/a/b/background.png) no-repeat center center;
|
||||
}
|
||||
@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");
|
||||
|
||||
.my-div2 {
|
||||
background: url(/a/b/assets/background.png) repeat center center;
|
||||
}
|
||||
@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");
|
||||
|
||||
.my-div3 {
|
||||
background-image: url(/a/b/assets/media/something.svg);
|
||||
}
|
||||
@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,
|
||||
`,
|
||||
cssFileRelativeDirPath: "assets/",
|
||||
buildContext: {
|
||||
urlPathname: "/x/y/z/"
|
||||
urlPathname: "/a/b/"
|
||||
}
|
||||
});
|
||||
|
||||
const fixedCssCodeExpected = `
|
||||
@font-face {
|
||||
font-family: "Work Sans";
|
||||
font-style: normal;
|
||||
font-weight: 400;
|
||||
font-display: swap;
|
||||
src: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/fonts/WorkSans/worksans-regular-webfont.woff2)
|
||||
format("woff2");
|
||||
.my-div {
|
||||
background: url(../background.png) no-repeat center center;
|
||||
}
|
||||
@font-face {
|
||||
font-family: "Work Sans";
|
||||
font-style: normal;
|
||||
font-weight: 500;
|
||||
font-display: swap;
|
||||
src: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/fonts/WorkSans/worksans-medium-webfont.woff2)
|
||||
format("woff2");
|
||||
|
||||
.my-div2 {
|
||||
background: url(background.png) repeat center center;
|
||||
}
|
||||
@font-face {
|
||||
font-family: "Work Sans";
|
||||
font-style: normal;
|
||||
font-weight: 600;
|
||||
font-display: swap;
|
||||
src: url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}/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}/${basenameOfTheKeycloakifyResourcesDir}/fonts/WorkSans/worksans-bold-webfont.woff2)
|
||||
format("woff2");
|
||||
|
||||
.my-div3 {
|
||||
background-image: url(media/something.svg);
|
||||
}
|
||||
`;
|
||||
|
||||
expect(isSameCode(fixedCssCode, fixedCssCodeExpected)).toBe(true);
|
||||
});
|
||||
|
||||
it("replaceImportsInCssCode - 3", () => {
|
||||
const { fixedCssCode } = replaceImportsInCssCode({
|
||||
cssCode: `
|
||||
.my-div {
|
||||
background: url(/a/b/background.png) no-repeat center center;
|
||||
}
|
||||
|
||||
.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 = `
|
||||
.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);
|
||||
});
|
||||
|
||||
it("replaceImportsInCssCode - 4", () => {
|
||||
const { fixedCssCode } = replaceImportsInCssCode({
|
||||
cssCode: `
|
||||
.my-div {
|
||||
background: url(/background.png) no-repeat center center;
|
||||
}
|
||||
|
||||
.my-div2 {
|
||||
background: url(/assets/background.png) repeat center center;
|
||||
}
|
||||
|
||||
.my-div3 {
|
||||
background-image: url(/assets/media/something.svg);
|
||||
}
|
||||
`,
|
||||
cssFileRelativeDirPath: undefined,
|
||||
buildContext: {
|
||||
urlPathname: undefined
|
||||
}
|
||||
});
|
||||
|
||||
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