107 lines
3.1 KiB
JavaScript
107 lines
3.1 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.matchParamsPath = exports.getPageTitleInfo = exports.getPageTitle = void 0;
|
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
var _pathToRegexp = require("path-to-regexp");
|
|
var matchParamsPath = exports.matchParamsPath = function matchParamsPath(pathname, breadcrumb, breadcrumbMap) {
|
|
// Internal logic use breadcrumbMap to ensure the order
|
|
// 内部逻辑使用 breadcrumbMap 来确保查询顺序
|
|
if (breadcrumbMap) {
|
|
var pathKey = (0, _toConsumableArray2.default)(breadcrumbMap.keys()).find(function (key) {
|
|
try {
|
|
if (key.startsWith('http')) {
|
|
return false;
|
|
}
|
|
return (0, _pathToRegexp.match)(key)(pathname);
|
|
} catch (error) {
|
|
console.log('key', key, error);
|
|
return false;
|
|
}
|
|
});
|
|
if (pathKey) {
|
|
return breadcrumbMap.get(pathKey);
|
|
}
|
|
}
|
|
|
|
// External uses use breadcrumb
|
|
// 外部用户使用 breadcrumb 参数
|
|
if (breadcrumb) {
|
|
var _pathKey = Object.keys(breadcrumb).find(function (key) {
|
|
try {
|
|
if (key !== null && key !== void 0 && key.startsWith('http')) {
|
|
return false;
|
|
}
|
|
return (0, _pathToRegexp.match)(key)(pathname);
|
|
} catch (error) {
|
|
console.log('key', key, error);
|
|
return false;
|
|
}
|
|
});
|
|
if (_pathKey) {
|
|
return breadcrumb[_pathKey];
|
|
}
|
|
}
|
|
return {
|
|
path: ''
|
|
};
|
|
};
|
|
/**
|
|
* 获取关于 pageTitle 的所有信息方便包装
|
|
*
|
|
* @param props
|
|
* @param ignoreTitle
|
|
*/
|
|
var getPageTitleInfo = exports.getPageTitleInfo = function getPageTitleInfo(props, ignoreTitle) {
|
|
var _props$pathname = props.pathname,
|
|
pathname = _props$pathname === void 0 ? '/' : _props$pathname,
|
|
breadcrumb = props.breadcrumb,
|
|
breadcrumbMap = props.breadcrumbMap,
|
|
formatMessage = props.formatMessage,
|
|
title = props.title,
|
|
_props$menu = props.menu,
|
|
menu = _props$menu === void 0 ? {
|
|
locale: false
|
|
} : _props$menu;
|
|
var pageTitle = ignoreTitle ? '' : title || '';
|
|
var currRouterData = matchParamsPath(pathname, breadcrumb, breadcrumbMap);
|
|
if (!currRouterData) {
|
|
return {
|
|
title: pageTitle,
|
|
id: '',
|
|
pageName: pageTitle
|
|
};
|
|
}
|
|
var 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 (ignoreTitle || !title) {
|
|
return {
|
|
title: pageName,
|
|
id: currRouterData.locale || '',
|
|
pageName: pageName
|
|
};
|
|
}
|
|
return {
|
|
title: "".concat(pageName, " - ").concat(title),
|
|
id: currRouterData.locale || '',
|
|
pageName: pageName
|
|
};
|
|
};
|
|
var getPageTitle = exports.getPageTitle = function getPageTitle(props, ignoreTitle) {
|
|
return getPageTitleInfo(props, ignoreTitle).title;
|
|
}; |