This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathgetPageTitle.ts
157 lines (146 loc) · 3.48 KB
/
getPageTitle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import pathToRegexp from 'path-to-regexp';
import { MenuDataItem } from './typings';
import { ProSettings } from './defaultSettings';
export const matchParamsPath = (
pathname: string,
breadcrumb?: { [path: string]: MenuDataItem },
breadcrumbMap?: Map<string, MenuDataItem>,
): MenuDataItem => {
// Internal logic use breadcrumbMap to ensure the order
// 内部逻辑使用 breadcrumbMap 来确保查询顺序
if (breadcrumbMap) {
const pathKey = [...breadcrumbMap.keys()].find(key =>
pathToRegexp(key).test(pathname),
);
if (pathKey) {
return breadcrumbMap.get(pathKey) as MenuDataItem;
}
}
// External uses use breadcrumb
// 外部用户使用 breadcrumb 参数
if (breadcrumb) {
const pathKey = Object.keys(breadcrumb).find(key =>
pathToRegexp(key).test(pathname),
);
if (pathKey) {
return breadcrumb[pathKey];
}
}
return {
path: '',
};
};
export interface GetPageTitleProps {
pathname?: string;
breadcrumb?: { [path: string]: MenuDataItem };
breadcrumbMap?: Map<string, MenuDataItem>;
menu?: ProSettings['menu'];
title?: ProSettings['title'];
pageName?: string;
formatMessage?: (data: { id: any; defaultMessage?: string }) => string;
}
const getPageTitle = (props: GetPageTitleProps, ignoreTile?: boolean) => {
const {
pathname = '/',
breadcrumb,
breadcrumbMap,
formatMessage,
title = 'Ant Design Pro',
menu = {
locale: false,
},
} = props;
const pageTitle = ignoreTile ? '' : title;
if (!pathname) {
return pageTitle;
}
const currRouterData = matchParamsPath(pathname, breadcrumb, breadcrumbMap);
if (!currRouterData) {
return pageTitle;
}
let pageName = currRouterData.name;
if (menu.locale !== false && currRouterData.locale && formatMessage) {
pageName = formatMessage({
id: currRouterData.locale || '',
defaultMessage: currRouterData.name,
});
}
if (!pageName) {
return pageTitle;
}
if (ignoreTile) {
return pageName;
}
return `${pageName} - ${title}`;
};
/**
* 获取关于 pageTile 的所有信息方便包装
* @param props
* @param ignoreTile
*/
const getPageTitleInfo = (
props: GetPageTitleProps,
ignoreTile?: boolean,
): {
// 页面标题
title: string;
// locale 的 title
id: string;
// 页面标题不带默认的 title
pageName: string;
} => {
const {
pathname = '/',
breadcrumb,
breadcrumbMap,
formatMessage,
title = 'Ant Design Pro',
menu = {
locale: false,
},
} = props;
const pageTitle = ignoreTile ? '' : title;
if (!pathname) {
return {
title: pageTitle,
id: '',
pageName: pageTitle,
};
}
const currRouterData = matchParamsPath(pathname, breadcrumb, breadcrumbMap);
if (!currRouterData) {
return {
title: pageTitle,
id: '',
pageName: pageTitle,
};
}
let pageName = currRouterData.name;
if (menu.locale !== false && currRouterData.locale && formatMessage) {
pageName = formatMessage({
id: currRouterData.locale || '',
defaultMessage: currRouterData.name,
});
}
if (!pageName) {
return {
title: pageTitle,
id: currRouterData.locale || '',
pageName: pageTitle,
};
}
if (ignoreTile) {
return {
title: pageName,
id: currRouterData.locale || '',
pageName,
};
}
return {
title: `${pageName} - ${title}`,
id: currRouterData.locale || '',
pageName,
};
};
export { getPageTitleInfo };
export default getPageTitle;