Skip to content

Commit

Permalink
feat(): 平台title支持自定义配置
Browse files Browse the repository at this point in the history
  • Loading branch information
WHChen-Alex committed Jul 19, 2024
1 parent e21a5ea commit d58363d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 11 deletions.
46 changes: 36 additions & 10 deletions packages/brick-container/src/loadBootstrapData.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ jest.spyOn(http, "get").mockImplementation(async (url) => {
id: "app-a",
name: "App A",
locales: {
zh: { name: "应用 A" },
en: { name: "Application A" },
zh: {
name: "应用 A",
},
en: {
name: "Application A",
},
},
defaultConfig: {
defaultConf: 7,
Expand Down Expand Up @@ -194,8 +198,12 @@ jest.spyOn(http, "get").mockImplementation(async (url) => {
id: "app-h",
name: "App H",
locales: {
zh: { name: "应用 H" },
en: { name: "Application H" },
zh: {
name: "应用 H",
},
en: {
name: "Application H",
},
},
defaultConfig: {
defaultConf: 7,
Expand All @@ -204,8 +212,8 @@ jest.spyOn(http, "get").mockImplementation(async (url) => {
userConf: 8,
settings: {
locales: {
zh: { name: "应用 H 别名" },
en: { name: "Application H Alias" },
zh: { name: "应用 H 别名", title: "你好 H" },
en: { name: "Application H Alias", title: "Hi there H" },
},
},
},
Expand Down Expand Up @@ -350,6 +358,7 @@ describe("loadBootstrapData", () => {
id: "app-a",
name: "App A",
localeName: "Application A",
localeTitle: "",
locales: {
zh: { name: "应用 A" },
en: { name: "Application A" },
Expand Down Expand Up @@ -382,6 +391,8 @@ describe("loadBootstrapData", () => {
defaultConfig: {
overrideDefault: 4,
},
localeName: undefined,
localeTitle: "",
userConfig: {
overrideUser: 5,
},
Expand Down Expand Up @@ -456,6 +467,7 @@ describe("loadBootstrapData", () => {
homepage: "/app-g",
id: "app-g",
localeName: "Application G",
localeTitle: "",
locales: { en: { name: "Application G" }, zh: { name: "应用 G" } },
name: "App G",
userConfig: { runtimeUserConf: 9 },
Expand All @@ -468,6 +480,8 @@ describe("loadBootstrapData", () => {
{
overrideApp: {
config: { overrideDefault: 4, overrideUser: 5 },
localeName: undefined,
localeTitle: "",
defaultConfig: { overrideDefault: 4 },
userConfig: { overrideUser: 5 },
},
Expand All @@ -487,6 +501,7 @@ describe("loadBootstrapData", () => {
homepage: "/app-a",
id: "app-a",
localeName: "Application A",
localeTitle: "",
locales: { en: { name: "Application A" }, zh: { name: "应用 A" } },
name: "App A",
userConfig: { runtimeUserConf: 9 },
Expand All @@ -498,6 +513,8 @@ describe("loadBootstrapData", () => {
{
overrideApp: {
config: { overrideDefault: 4, overrideUser: 5 },
localeName: undefined,
localeTitle: "",
defaultConfig: { overrideDefault: 4 },
userConfig: { overrideUser: 5 },
},
Expand Down Expand Up @@ -583,6 +600,7 @@ describe("loadBootstrapData", () => {
homepage: "/app-g",
id: "app-g",
localeName: "Application G",
localeTitle: "",
locales: { en: { name: "Application G" }, zh: { name: "应用 G" } },
name: "App G",
userConfig: { runtimeUserConf: 9 },
Expand All @@ -595,6 +613,8 @@ describe("loadBootstrapData", () => {
{
overrideApp: {
config: { overrideDefault: 4, overrideUser: 5 },
localeName: undefined,
localeTitle: "",
defaultConfig: { overrideDefault: 4 },
userConfig: { overrideUser: 5 },
},
Expand All @@ -614,6 +634,7 @@ describe("loadBootstrapData", () => {
homepage: "/app-a",
id: "app-a",
localeName: "Application A",
localeTitle: "",
locales: { en: { name: "Application A" }, zh: { name: "应用 A" } },
name: "App A",
userConfig: { runtimeUserConf: 9 },
Expand All @@ -625,6 +646,8 @@ describe("loadBootstrapData", () => {
{
overrideApp: {
config: { overrideDefault: 4, overrideUser: 5 },
localeName: undefined,
localeTitle: "",
defaultConfig: { overrideDefault: 4 },
userConfig: { overrideUser: 5 },
},
Expand Down Expand Up @@ -800,13 +823,15 @@ describe("loadBootstrapData", () => {
id: "app-a",
name: "App A",
localeName: "App A",
localeTitle: "",
userConfig: {
userConf: 42,
},
config: {
userConf: 42,
},
},
meta: undefined,
routes: [],
});
});
Expand All @@ -823,14 +848,15 @@ describe("loadBootstrapData", () => {
app: {
id: "app-h",
name: "App H",
localeTitle: "Hi there H",
locales: { zh: { name: "应用 H" }, en: { name: "Application H" } },
defaultConfig: { defaultConf: 7 },
userConfig: {
userConf: 8,
settings: {
locales: {
zh: { name: "应用 H 别名" },
en: { name: "Application H Alias" },
zh: { name: "应用 H 别名", title: "你好 H" },
en: { name: "Application H Alias", title: "Hi there H" },
},
},
},
Expand All @@ -839,8 +865,8 @@ describe("loadBootstrapData", () => {
userConf: 8,
settings: {
locales: {
zh: { name: "应用 H 别名" },
en: { name: "Application H Alias" },
zh: { name: "应用 H 别名", title: "你好 H" },
en: { name: "Application H Alias", title: "Hi there H" },
},
},
},
Expand Down
3 changes: 3 additions & 0 deletions packages/brick-container/src/loadBootstrapData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,15 @@ function initializeAppLocales(app: MicroApp) {
});
// Use `app.name` as the fallback `app.localeName`.
app.localeName = i18n.getFixedT(null, ns)("name", app.name) as string;

app.localeTitle = i18n.getFixedT(null, ns)("title", "") as string;
// Remove the temporary i18n resource bundles.
Object.keys(locales).forEach((lang) => {
i18n.removeResourceBundle(lang, ns);
});
} else {
app.localeName = app.name;
app.localeTitle = "";
}
}

Expand Down
28 changes: 28 additions & 0 deletions packages/runtime/src/internal/Runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ const getBootstrapData = (options?: {
id: "app-b",
homepage: "/app-b",
name: "App B",
localeTitle: "Hi there",
},
routes: [
{
Expand Down Expand Up @@ -1393,6 +1394,33 @@ describe("Runtime", () => {
});
});

test("render locale title", async () => {
consoleError.mockReturnValueOnce();
const finishPageView = jest.fn();
createRuntime({
hooks: {
pageView: {
create: jest.fn(() => finishPageView),
},
},
}).initialize(getBootstrapData());
getHistory().push("/app-b");
await getRuntime().bootstrap();

expect(document.title).toBe("DevOps 管理专家");

getRuntime().applyPageTitle("Hello");
expect(document.title).toBe("Hi there");
getRuntime().applyPageTitle("");
expect(document.title).toBe("DevOps 管理专家");
expect(finishPageView).toBeCalledTimes(1);
expect(finishPageView).toBeCalledWith({
pageTitle: "DevOps 管理专家",
path: "/app-b",
status: "ok",
});
});

test("API unauthenticated", async () => {
consoleError.mockReturnValueOnce();
const error = new HttpResponseError({ status: 401 } as any, {
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime/src/internal/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ export class Runtime {

applyPageTitle(pageTitle: string): void {
const baseTitle = this.getBrandSettings().base_title;
document.title = pageTitle ? `${pageTitle} - ${baseTitle}` : baseTitle;
document.title = pageTitle
? (router?.getRecentApps().currentApp?.localeTitle as string) ||
`${pageTitle} - ${baseTitle}`
: baseTitle;
}

getNavConfig() {
Expand Down
5 changes: 5 additions & 0 deletions packages/types/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export interface MicroApp {
*/
localeName?: string;

/**
* 本地化后的页面标题(运行时得出),在运行环境由用户维护,本地化获取失败时为“”。
*/
localeTitle?: string;

/**
* 整个应用不启用登录守卫。
*/
Expand Down

0 comments on commit d58363d

Please sign in to comment.