@logicflow/dumi-theme-simple
Version:
Simple website theme based on dumi2.
128 lines • 6.25 kB
JavaScript
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 { Layout } from 'antd';
import { useLocale, useSiteData } from 'dumi';
import { every, find, get } from 'lodash-es';
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { useLocation, useNavigate, useParams } from 'react-router-dom';
import { useSnapshot } from 'valtio';
import { LogicFlowThemeContext } from "../../context";
import { store } from "../../model";
import { API } from "dumi/theme/slots/API";
import { CodeRunner } from "dumi/theme/slots/CodeRunner";
import { getDemoInfo } from "../../slots/CodeRunner/utils";
import { ExampleSider } from "dumi/theme/slots/ExampleSider";
import { Header } from "dumi/theme/slots/Header";
import { SEO } from "dumi/theme/slots/SEO";
import { CollapsedIcon } from "./components/CollapsedIcon";
import styles from "./index.module.less";
import { getCurrentTitle } from "./utils";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var Sider = Layout.Sider,
Content = Layout.Content;
/**
* 具体单个案例的页面
*/
var Example = function Example() {
var _useLocation = useLocation(),
hash = _useLocation.hash;
var nav = useNavigate();
var _useParams = useParams(),
topic = _useParams.topic,
example = _useParams.example;
/** 示例页面的元数据信息 */
var metaData = useContext(LogicFlowThemeContext);
var locale = useLocale();
var _useSiteData = useSiteData(),
themeConfig = _useSiteData.themeConfig;
var exampleTopics = metaData.meta.exampleTopics;
var demo = useMemo(function () {
var examples = get(exampleTopics, ['0', 'examples']);
var exampleDemo = find(examples, function (_ref) {
var id = _ref.id;
return id === example;
});
// examples/case/id hash 为空,可以默认第一个 example 对应的 demo
return hash.slice(1) || get(exampleDemo, ['demos', '0', 'id']);
}, [hash, exampleTopics, example]);
var _useState = useState(),
_useState2 = _slicedToArray(_useState, 2),
currentDemo = _useState2[0],
setCurrentDemo = _useState2[1];
var state = useSnapshot(store);
var _useState3 = useState({}),
_useState4 = _slicedToArray(_useState3, 2),
title = _useState4[0],
setTitle = _useState4[1];
useEffect(function () {
if (topic && example && demo) {
var targetDemoInfo = getDemoInfo(exampleTopics, topic, example, demo);
setCurrentDemo(targetDemoInfo);
setTitle(getCurrentTitle(exampleTopics, topic, example));
}
}, [topic, example, hash]);
var showAPI = every([get(themeConfig, 'showAPIDoc'), topic, example], Boolean);
return /*#__PURE__*/_jsxs("div", {
className: styles.example,
children: [/*#__PURE__*/_jsx(SEO, {
title: title[locale.id],
lang: locale.id
}), /*#__PURE__*/_jsx(Header, {
isHomePage: false
}), /*#__PURE__*/_jsxs(Layout, {
className: styles.container,
children: [/*#__PURE__*/_jsxs(Sider, {
collapsedWidth: 0,
width: 250,
trigger: null,
collapsible: true,
collapsed: state.hideMenu,
className: styles.menuSider,
theme: "light",
children: [currentDemo && /*#__PURE__*/_jsx(ExampleSider, {
showExampleDemoTitle: true,
currentDemo: currentDemo,
onDemoClicked: function onDemoClicked(example) {
var demoId = example.id,
targetExample = example.targetExample,
targetTopic = example.targetTopic;
// eg: /zh/examples/case/area/#area1
var newURL = "/".concat(locale.id, "/examples/").concat(targetTopic === null || targetTopic === void 0 ? void 0 : targetTopic.id, "/").concat(targetExample === null || targetExample === void 0 ? void 0 : targetExample.id, "/#").concat(demoId);
nav(newURL);
},
exampleTopics: exampleTopics
}), /*#__PURE__*/_jsx(CollapsedIcon, {
isCollapsed: state.hideMenu,
onClick: function onClick(show) {
store.hideMenu = show;
},
style: {
bottom: 0,
right: state.hideMenu ? -24 : 0
}
})]
}), /*#__PURE__*/_jsx(Content, {
className: styles.content,
children: topic && example && /*#__PURE__*/_jsx(CodeRunner, {
exampleTopics: exampleTopics,
topic: topic,
example: example,
demo: demo,
size: get(themeConfig, 'editor.size', 0.38)
})
}), showAPI && /*#__PURE__*/_jsx(API, {
exampleTopics: exampleTopics,
topic: topic,
example: example,
demo: demo,
language: locale.id
})]
})]
});
};
export default Example;