UNPKG

dumi-theme-lobehub

Version:

dumi-theme-lobehub is a documentation site theme package designed for dumi2. It provides a more beautiful and user-friendly development and reading experience based on @lobehub/ui

134 lines (133 loc) 5.32 kB
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import { Button } from 'antd'; import { Link, history, useLocation } from 'dumi'; import { memo, useEffect, useState } from 'react'; import NativeSelect from "../../components/NativeSelect"; import { useSiteStore } from "../../store"; import { jsx as _jsx } from "react/jsx-runtime"; function getTargetLocalePath(_ref) { var pathname = _ref.pathname, current = _ref.current, target = _ref.target; var clearPath = 'base' in current ? // handle '/en-US/a' => '/a' or '/en-US' => '' => '/' pathname.replace(current.base.replace(/\/$/, ''), '') || '/' : pathname.replace(new RegExp("".concat(current.suffix, "$")), ''); return 'base' in target ? "".concat( // for `/` base, strip duplicated leading slash target.base.replace(/\/$/, '')).concat(clearPath) // for `/` clearPath, strip duplicated ending slash .replace(/([^/])\/$/, '$1') : "".concat(clearPath).concat(target.suffix); } var languageMap = { 'de-DE': '🇩🇪', 'en-US': '🇺🇸', 'es-ES': '🇪🇸', 'fr-FR': '🇫🇷', 'it-IT': '🇮🇹', 'jp-JP': '🇯🇵', 'ko-KR': '🇰🇷', 'pt-BR': '🇧🇷', 'ru-RU': '🇷🇺', 'tr-TR': '🇹🇷', 'vi-VN': '🇻🇳', 'zh-CN': '🇨🇳' }; var displayLangMap = { 'en-US': 'EN', 'zh-CN': '中' }; var SingleSwitch = /*#__PURE__*/memo(function (_ref2) { var locale = _ref2.locale, current = _ref2.current; var _useLocation = useLocation(), pathname = _useLocation.pathname; var _useState = useState(function () { return getTargetLocalePath({ current: current, pathname: pathname, target: locale }); }), _useState2 = _slicedToArray(_useState, 2), path = _useState2[0], setPath = _useState2[1]; useEffect(function () { setPath(getTargetLocalePath({ current: current, pathname: pathname, target: locale })); }, [pathname, current.id, locale.id]); return /*#__PURE__*/_jsx(Link, { to: path, children: /*#__PURE__*/_jsx(Button, { style: { alignItems: 'center', display: 'flex', justifyContent: 'center', minWidth: 34, padding: 0 }, children: displayLangMap[locale.id] }) }); }); var LangSwitch = /*#__PURE__*/memo(function () { var _useSiteStore = useSiteStore(function (s) { return [s.siteData.locales, s.locale, s.location.pathname]; }), _useSiteStore2 = _slicedToArray(_useSiteStore, 3), locales = _useSiteStore2[0], current = _useSiteStore2[1], pathname = _useSiteStore2[2]; // do not render in single language if (locales.length <= 1) return; return locales.length > 2 ? /*#__PURE__*/_jsx(NativeSelect, { onChange: function onChange(index) { console.log(getTargetLocalePath({ current: current, pathname: pathname, target: locales[index] })); history.push(getTargetLocalePath({ current: current, pathname: pathname, target: locales[index] })); }, options: locales.map(function (item) { return { label: displayLangMap[item.id], value: item.id }; }), renderItem: function renderItem(item, index) { return "".concat(languageMap[locales[index].id], " ").concat(locales[index].name); }, style: { alignItems: 'center', display: 'flex', height: 32, justifyContent: 'center', minWidth: 32, padding: 0 }, value: locales.findIndex(function (l) { return l.id === current.id; }) }) : /*#__PURE__*/ // single language switch _jsx(SingleSwitch, { current: current, locale: locales.find(function (_ref3) { var id = _ref3.id; return id !== current.id; }) }); }); export default LangSwitch;