Compare commits
13 Commits
v10.0.0-rc
...
v10.0.0-rc
Author | SHA1 | Date | |
---|---|---|---|
8cba3aae2c | |||
01b32f78ed | |||
b6066dfd5f | |||
3ad554ed59 | |||
6aacc6361b | |||
638e4e6410 | |||
aa9b7cccc7 | |||
41739c8528 | |||
89b32dc7fc | |||
44aec23251 | |||
12fd6160c5 | |||
8819abc418 | |||
96b627095c |
@ -1,13 +1,13 @@
|
||||
{
|
||||
"name": "keycloakify",
|
||||
"version": "10.0.0-rc.74",
|
||||
"version": "10.0.0-rc.80",
|
||||
"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",
|
||||
@ -66,7 +66,7 @@
|
||||
"react": "*"
|
||||
},
|
||||
"dependencies": {
|
||||
"react-markdown": "^5.0.3",
|
||||
"react-markdown": "^9.0.1",
|
||||
"tsafe": "^1.6.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
@ -102,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",
|
||||
@ -111,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}`,
|
||||
|
@ -8,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";
|
||||
|
||||
@ -64,7 +63,7 @@ export function generateFtlFilesCodeFactory(params: {
|
||||
|
||||
const { fixedCssCode } = replaceImportsInCssCode({
|
||||
cssCode,
|
||||
fileRelativeDirPath: ".",
|
||||
cssFileRelativeDirPath: undefined,
|
||||
buildContext
|
||||
});
|
||||
|
||||
@ -119,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"
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -134,7 +134,7 @@ export async function generateResourcesForMainTheme(params: {
|
||||
if (filePath.endsWith(".css")) {
|
||||
const { fixedCssCode } = replaceImportsInCssCode({
|
||||
cssCode: sourceCode.toString("utf8"),
|
||||
fileRelativeDirPath: pathDirname(fileRelativePath),
|
||||
cssFileRelativeDirPath: pathDirname(fileRelativePath),
|
||||
buildContext
|
||||
});
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
import type { BuildContext } from "../../shared/buildContext";
|
||||
import { basenameOfTheKeycloakifyResourcesDir } from "../../shared/constants";
|
||||
import { assert } from "tsafe/assert";
|
||||
import { posix } from "path";
|
||||
|
||||
@ -10,12 +11,12 @@ assert<BuildContext extends BuildContextLike ? true : false>();
|
||||
|
||||
export function replaceImportsInCssCode(params: {
|
||||
cssCode: string;
|
||||
fileRelativeDirPath: string;
|
||||
cssFileRelativeDirPath: string | undefined;
|
||||
buildContext: BuildContextLike;
|
||||
}): {
|
||||
fixedCssCode: string;
|
||||
} {
|
||||
const { cssCode, fileRelativeDirPath, buildContext } = params;
|
||||
const { cssCode, cssFileRelativeDirPath, buildContext } = params;
|
||||
|
||||
const fixedCssCode = cssCode.replace(
|
||||
/url\(["']?(\/[^/][^)"']+)["']?\)/g,
|
||||
@ -31,8 +32,16 @@ export function replaceImportsInCssCode(params: {
|
||||
);
|
||||
}
|
||||
|
||||
inline_style_in_html: {
|
||||
if (cssFileRelativeDirPath !== undefined) {
|
||||
break inline_style_in_html;
|
||||
}
|
||||
|
||||
return `url(\${url.resourcesPath}/${basenameOfTheKeycloakifyResourcesDir}${assetFileAbsoluteUrlPathname})`;
|
||||
}
|
||||
|
||||
const assetFileRelativeUrlPathname = posix.relative(
|
||||
fileRelativeDirPath.replace(/\\/g, "/"),
|
||||
cssFileRelativeDirPath.replace(/\\/g, "/"),
|
||||
assetFileAbsoluteUrlPathname.replace(/^\//, "")
|
||||
);
|
||||
|
||||
|
@ -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";
|
||||
|
@ -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;
|
@ -395,7 +395,7 @@ describe("css replacer", () => {
|
||||
background-image: url(/assets/media/something.svg);
|
||||
}
|
||||
`,
|
||||
fileRelativeDirPath: "assets/",
|
||||
cssFileRelativeDirPath: "assets/",
|
||||
buildContext: {
|
||||
urlPathname: undefined
|
||||
}
|
||||
@ -433,7 +433,7 @@ describe("css replacer", () => {
|
||||
background-image: url(/a/b/assets/media/something.svg);
|
||||
}
|
||||
`,
|
||||
fileRelativeDirPath: "assets/",
|
||||
cssFileRelativeDirPath: "assets/",
|
||||
buildContext: {
|
||||
urlPathname: "/a/b/"
|
||||
}
|
||||
@ -455,6 +455,82 @@ describe("css replacer", () => {
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
||||
export function isSameCode(code1: string, code2: string): boolean {
|
||||
|
Reference in New Issue
Block a user