UNPKG

dumi-theme-antd

Version:

Ant Design 5.0 官网风格类似的 dumi2 主题插件

266 lines (264 loc) 13.5 kB
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"; import _typeof from "@babel/runtime/helpers/esm/typeof"; import { Tag } from 'antd'; import { Link, useFullSidebarData, useLocation, useLocale } from 'dumi'; import { useMemo } from 'react'; import { removeTitleCode, handleFullSidebarData } from "../utils"; import useAdditionalThemeConfig from "./useAdditionalThemeConfig"; import pkgJSON from "../../package.json"; import { jsx as ___EmotionJSX } from "@emotion/react"; var useMenu = function useMenu() { var _pathname$split, _suffix2; var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var _useLocation = useLocation(), pathname = _useLocation.pathname, search = _useLocation.search; var _useAdditionalThemeCo = useAdditionalThemeConfig(), sidebarGroupModePath = _useAdditionalThemeCo.sidebarGroupModePath, _useAdditionalThemeCo2 = _useAdditionalThemeCo.sidebarEnhance, sidebarEnhance = _useAdditionalThemeCo2 === void 0 ? {} : _useAdditionalThemeCo2; var before = options.before, after = options.after; var fullSidebarData = useFullSidebarData(); var navSecondSidebarData = handleFullSidebarData(fullSidebarData); var locale = useLocale(); // 提取一级导航下侧边栏数据 var currentNavKey = "/".concat((_pathname$split = pathname.split('/')) === null || _pathname$split === void 0 ? void 0 : _pathname$split[1]); var sidebarData = navSecondSidebarData[currentNavKey]; var linkTitleMap = useMemo(function () { return Object.values(fullSidebarData).reduce(function (res, sidebar) { var sidebarItems = sidebar.map(function (item) { return item.children; }).flat(); sidebarItems.forEach(function (item) { res[item.link] = item.title; }); return res; }, {}); }, [fullSidebarData]); var currentSidebarEnhanceData = useMemo(function () { var currentLink = Object.keys(sidebarEnhance).find(function (link) { return pathname.startsWith(link); }); if (!currentLink) return undefined; return sidebarEnhance[currentLink]; }, [pathname, sidebarEnhance]); var sidebarEnhanceMenuItems = useMemo(function () { var isItemMenu = function isItemMenu(v) { return v && _typeof(v) === 'object' && 'link' in v && 'title' in v; }; var isGroupMenu = function isGroupMenu(v) { return v && _typeof(v) === 'object' && v.type === 'group'; }; var isSubMenu = function isSubMenu(v) { return v && _typeof(v) === 'object' && 'children' in v; }; function processMenu(menu) { if (typeof menu === 'string') { // menu: '/introduction' return { key: menu, label: ___EmotionJSX(Link, { to: "".concat(menu).concat(search) }, before, linkTitleMap[menu], after) }; } if (isGroupMenu(menu)) { // menu: { type: 'group', title: '组件', children: ['/components/button', { title: 'Installation', children: ['/aaa'] }] } return { type: 'group', label: menu.title, key: menu.title, children: menu.children.map(processMenu) }; } if (isSubMenu(menu)) { return { key: menu.title, label: ___EmotionJSX("span", { style: { paddingLeft: 10 } }, menu.title), children: menu.children.map(processMenu) }; } if (isItemMenu(menu)) { // { title: 'aaa', link: '/xxx' } return { label: ___EmotionJSX(Link, { target: menu.target, to: menu.link }, menu.title), key: menu.link }; } return null; } if (!currentSidebarEnhanceData) return undefined; return currentSidebarEnhanceData.map(processMenu); }, [after, before, currentSidebarEnhanceData, linkTitleMap, search]); var menuItems = useMemo(function () { var _suffix, _sidebarItems$reduce; var suffixRegExp = new RegExp("".concat((_suffix = locale === null || locale === void 0 ? void 0 : locale.suffix) !== null && _suffix !== void 0 ? _suffix : '', "$"), 'g'); var sidebarItems = _toConsumableArray(sidebarData !== null && sidebarData !== void 0 ? sidebarData : []); var getItemTag = function getItemTag(tag) { var show = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; return tag && show && ___EmotionJSX(Tag, { color: typeof tag === 'string' ? 'processing' : tag.color, bordered: false, style: { marginInlineStart: 'auto', marginInlineEnd: 0, marginTop: -2 } }, (typeof tag === 'string' ? tag : tag.title).replace('VERSION', "v".concat(pkgJSON.version))); }; return (_sidebarItems$reduce = sidebarItems === null || sidebarItems === void 0 ? void 0 : sidebarItems.reduce(function (result, group) { if (group !== null && group !== void 0 && group.title) { // sideBar menu group 模式, 默认以非 group 模式渲染 var isSideBarGroupMode = sidebarGroupModePath === true ? true : (sidebarGroupModePath !== null && sidebarGroupModePath !== void 0 ? sidebarGroupModePath : []).filter(function (rule) { return pathname.startsWith(rule); }).length > 0; if (isSideBarGroupMode) { var _group$children; result.push({ type: 'group', label: group === null || group === void 0 ? void 0 : group.title, order: group === null || group === void 0 ? void 0 : group.order, key: group === null || group === void 0 ? void 0 : group.title, children: (_group$children = group.children) === null || _group$children === void 0 ? void 0 : _group$children.map(function (item) { var _item$frontmatter, _item$frontmatter2; return { label: ___EmotionJSX(Link, { to: "".concat(item.link).concat(search), style: { display: 'flex', alignItems: 'center' } }, before, ___EmotionJSX("span", { key: "english" }, removeTitleCode(item === null || item === void 0 ? void 0 : item.title)), ___EmotionJSX("span", { className: "chinese", key: "chinese" }, removeTitleCode((_item$frontmatter = item.frontmatter) === null || _item$frontmatter === void 0 ? void 0 : _item$frontmatter.subtitle)), getItemTag((_item$frontmatter2 = item.frontmatter) === null || _item$frontmatter2 === void 0 ? void 0 : _item$frontmatter2.tag, !before && !after), after), key: item.link.replace(suffixRegExp, '') }; }) }); } else { var _childrenGroupOrdered, _childrenGroupOrdered2; var childrenResultTypeOrder = {}; var childrenGroup = group.children.reduce(function (childrenResult, child) { var _child$frontmatter, _childType$title; var nextChildrenResult = childrenResult; var childType = child === null || child === void 0 ? void 0 : (_child$frontmatter = child.frontmatter) === null || _child$frontmatter === void 0 ? void 0 : _child$frontmatter.type; // 兼容 type 为字符串 && object,object 支持排序 var type = typeof childType === 'string' ? childType : (_childType$title = childType === null || childType === void 0 ? void 0 : childType.title) !== null && _childType$title !== void 0 ? _childType$title : 'default'; if (!nextChildrenResult[type]) { nextChildrenResult[type] = []; } if (!childrenResultTypeOrder[type]) { var _childType$order; childrenResultTypeOrder[type] = { title: type, order: (_childType$order = childType === null || childType === void 0 ? void 0 : childType.order) !== null && _childType$order !== void 0 ? _childType$order : -1 }; } else if (childType !== null && childType !== void 0 && childType.order) { childrenResultTypeOrder[type].order = childType.order; } nextChildrenResult[type].push(child); return nextChildrenResult; }, {}); var childrenGroupOrdered = Object.keys(childrenGroup).sort(function (a, b) { return childrenResultTypeOrder[a].order - childrenResultTypeOrder[b].order; }).reduce(function (obj, key) { var _obj = obj; _obj[key] = childrenGroup[key]; return _obj; }, {}); var childItems = []; childItems.push.apply(childItems, _toConsumableArray((_childrenGroupOrdered = (_childrenGroupOrdered2 = childrenGroupOrdered.default) === null || _childrenGroupOrdered2 === void 0 ? void 0 : _childrenGroupOrdered2.map(function (item) { var _item$frontmatter3; return { label: ___EmotionJSX(Link, { to: "".concat(item.link).concat(search), style: { display: 'flex', alignItems: 'center' } }, before, removeTitleCode(item === null || item === void 0 ? void 0 : item.title), getItemTag((_item$frontmatter3 = item.frontmatter) === null || _item$frontmatter3 === void 0 ? void 0 : _item$frontmatter3.tag, !before && !after), after), key: item.link.replace(suffixRegExp, '') }; })) !== null && _childrenGroupOrdered !== void 0 ? _childrenGroupOrdered : [])); Object.entries(childrenGroupOrdered).forEach(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), type = _ref2[0], children = _ref2[1]; if (type !== 'default') { childItems.push({ type: 'group', label: type, key: type, children: children === null || children === void 0 ? void 0 : children.map(function (item) { var _item$frontmatter4; return { label: ___EmotionJSX(Link, { to: "".concat(item.link).concat(search), style: { display: 'flex', alignItems: 'center' } }, before, removeTitleCode(item === null || item === void 0 ? void 0 : item.title), getItemTag((_item$frontmatter4 = item.frontmatter) === null || _item$frontmatter4 === void 0 ? void 0 : _item$frontmatter4.tag, !before && !after), after), key: item.link.replace(suffixRegExp, '') }; }) }); } }); result.push({ label: group === null || group === void 0 ? void 0 : group.title, key: group === null || group === void 0 ? void 0 : group.title, order: group === null || group === void 0 ? void 0 : group.order, children: childItems }); } } else { var list = group.children || []; // 如果有 date 字段,我们就对其进行排序 if (list.every(function (info) { var _info$frontmatter; return info === null || info === void 0 ? void 0 : (_info$frontmatter = info.frontmatter) === null || _info$frontmatter === void 0 ? void 0 : _info$frontmatter.date; })) { list.sort(function (a, b) { var _a$frontmatter, _b$frontmatter; return (a === null || a === void 0 ? void 0 : (_a$frontmatter = a.frontmatter) === null || _a$frontmatter === void 0 ? void 0 : _a$frontmatter.date) > (b === null || b === void 0 ? void 0 : (_b$frontmatter = b.frontmatter) === null || _b$frontmatter === void 0 ? void 0 : _b$frontmatter.date) ? -1 : 1; }); } result.push.apply(result, _toConsumableArray(list.map(function (item) { var _item$frontmatter5; return { order: item === null || item === void 0 ? void 0 : item.order, label: ___EmotionJSX(Link, { to: "".concat(item.link).concat(search), style: { display: 'flex', alignItems: 'center' } }, before, removeTitleCode(item === null || item === void 0 ? void 0 : item.title), getItemTag((_item$frontmatter5 = item.frontmatter) === null || _item$frontmatter5 === void 0 ? void 0 : _item$frontmatter5.tag, !before && !after), after), key: item.link.replace(suffixRegExp, '') }; }))); } // group 模式与 single 模式混合排序 result.sort(function (a, b) { return (a === null || a === void 0 ? void 0 : a.order) < (b === null || b === void 0 ? void 0 : b.order) ? -1 : 1; }); return result; }, [])) !== null && _sidebarItems$reduce !== void 0 ? _sidebarItems$reduce : []; }, [sidebarData, sidebarGroupModePath, pathname, search, before, after, locale]); var selectedKey = pathname.replace(new RegExp("".concat((_suffix2 = locale === null || locale === void 0 ? void 0 : locale.suffix) !== null && _suffix2 !== void 0 ? _suffix2 : '', "$"), 'g'), ''); return [sidebarEnhanceMenuItems || menuItems, selectedKey]; }; export default useMenu;