UNPKG

2.91 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.matchParamsPath = exports.getPageTitleInfo = exports.default = void 0;
9
10var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
12var _pathToRegexp = _interopRequireDefault(require("path-to-regexp"));
13
14var matchParamsPath = function matchParamsPath(pathname, breadcrumb, breadcrumbMap) {
15 // Internal logic use breadcrumbMap to ensure the order
16 // 内部逻辑使用 breadcrumbMap 来确保查询顺序
17 if (breadcrumbMap) {
18 var pathKey = (0, _toConsumableArray2.default)(breadcrumbMap.keys()).find(function (key) {
19 return (0, _pathToRegexp.default)(key).test(pathname);
20 });
21
22 if (pathKey) {
23 return breadcrumbMap.get(pathKey);
24 }
25 } // External uses use breadcrumb
26 // 外部用户使用 breadcrumb 参数
27
28
29 if (breadcrumb) {
30 var _pathKey = Object.keys(breadcrumb).find(function (key) {
31 return (0, _pathToRegexp.default)(key).test(pathname);
32 });
33
34 if (_pathKey) {
35 return breadcrumb[_pathKey];
36 }
37 }
38
39 return {
40 path: ''
41 };
42};
43/**
44 * 获取关于 pageTitle 的所有信息方便包装
45 *
46 * @param props
47 * @param ignoreTitle
48 */
49
50
51exports.matchParamsPath = matchParamsPath;
52
53var getPageTitleInfo = function getPageTitleInfo(props, ignoreTitle) {
54 var _props$pathname = props.pathname,
55 pathname = _props$pathname === void 0 ? '/' : _props$pathname,
56 breadcrumb = props.breadcrumb,
57 breadcrumbMap = props.breadcrumbMap,
58 formatMessage = props.formatMessage,
59 title = props.title,
60 _props$menu = props.menu,
61 menu = _props$menu === void 0 ? {
62 locale: false
63 } : _props$menu;
64 var pageTitle = ignoreTitle ? '' : title || '';
65 var currRouterData = matchParamsPath(pathname, breadcrumb, breadcrumbMap);
66
67 if (!currRouterData) {
68 return {
69 title: pageTitle,
70 id: '',
71 pageName: pageTitle
72 };
73 }
74
75 var pageName = currRouterData.name;
76
77 if (menu.locale !== false && currRouterData.locale && formatMessage) {
78 pageName = formatMessage({
79 id: currRouterData.locale || '',
80 defaultMessage: currRouterData.name
81 });
82 }
83
84 if (!pageName) {
85 return {
86 title: pageTitle,
87 id: currRouterData.locale || '',
88 pageName: pageTitle
89 };
90 }
91
92 if (ignoreTitle || !title) {
93 return {
94 title: pageName,
95 id: currRouterData.locale || '',
96 pageName: pageName
97 };
98 }
99
100 return {
101 title: "".concat(pageName, " - ").concat(title),
102 id: currRouterData.locale || '',
103 pageName: pageName
104 };
105};
106
107exports.getPageTitleInfo = getPageTitleInfo;
108
109var getPageTitle = function getPageTitle(props, ignoreTitle) {
110 return getPageTitleInfo(props, ignoreTitle).title;
111};
112
113var _default = getPageTitle;
114exports.default = _default;
\No newline at end of file