2022-12-08 16:46:48 +08:00
|
|
|
import { CalendarOutlined } from '@ant-design/icons';
|
2023-07-20 19:27:33 +08:00
|
|
|
import { createStyles, useTheme } from 'antd-style';
|
2023-01-22 01:30:01 +08:00
|
|
|
import ContributorsList from '@qixian.cs/github-contributors-list';
|
2022-11-30 11:05:41 +08:00
|
|
|
import classNames from 'classnames';
|
2023-01-22 01:30:01 +08:00
|
|
|
import DayJS from 'dayjs';
|
2023-02-20 10:51:18 +08:00
|
|
|
import { FormattedMessage, useIntl, useRouteMeta, useTabMeta } from 'dumi';
|
2023-01-22 01:30:01 +08:00
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import React, { useContext, useLayoutEffect, useMemo, useState } from 'react';
|
2023-08-08 19:48:41 +08:00
|
|
|
import { Anchor, Avatar, Col, Skeleton, Space, Tooltip, Typography } from 'antd';
|
2023-05-24 14:27:03 +08:00
|
|
|
import useLayoutState from '../../../hooks/useLayoutState';
|
2022-11-09 12:28:04 +08:00
|
|
|
import useLocation from '../../../hooks/useLocation';
|
2023-01-22 01:30:01 +08:00
|
|
|
import EditButton from '../../common/EditButton';
|
2022-11-09 12:28:04 +08:00
|
|
|
import PrevAndNext from '../../common/PrevAndNext';
|
2023-08-03 10:58:38 +08:00
|
|
|
import ComponentChangelog from '../../common/ComponentChangelog';
|
2022-11-30 11:05:41 +08:00
|
|
|
import type { DemoContextProps } from '../DemoContext';
|
|
|
|
import DemoContext from '../DemoContext';
|
2023-01-22 01:30:01 +08:00
|
|
|
import Footer from '../Footer';
|
2022-11-15 10:09:24 +08:00
|
|
|
import SiteContext from '../SiteContext';
|
2023-06-22 21:11:12 +08:00
|
|
|
import ColumnCard from './ColumnCard';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-07-20 19:27:33 +08:00
|
|
|
const useStyle = createStyles(({ token, css }) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
const { antCls } = token;
|
|
|
|
|
|
|
|
return {
|
|
|
|
contributorsList: css`
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
margin-top: 120px !important;
|
2023-05-24 14:27:03 +08:00
|
|
|
clear: both;
|
2023-07-24 19:56:17 +08:00
|
|
|
|
|
|
|
li {
|
|
|
|
height: 24px;
|
|
|
|
}
|
2023-07-21 15:04:42 +08:00
|
|
|
|
|
|
|
li,
|
2022-11-09 12:28:04 +08:00
|
|
|
${antCls}-avatar + ${antCls}-avatar {
|
2023-01-22 01:30:01 +08:00
|
|
|
transition: all ${token.motionDurationSlow};
|
2023-01-04 13:45:02 +08:00
|
|
|
margin-inline-end: -8px;
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
2023-01-22 01:30:01 +08:00
|
|
|
&:hover {
|
2023-07-21 15:04:42 +08:00
|
|
|
li,
|
2023-01-22 01:30:01 +08:00
|
|
|
${antCls}-avatar {
|
|
|
|
margin-inline-end: 0;
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 12:28:04 +08:00
|
|
|
`,
|
2023-07-30 19:16:30 +08:00
|
|
|
listMobile: css`
|
|
|
|
margin: 1em 0 !important;
|
|
|
|
`,
|
2022-11-09 12:28:04 +08:00
|
|
|
toc: css`
|
|
|
|
${antCls}-anchor {
|
|
|
|
${antCls}-anchor-link-title {
|
|
|
|
font-size: 12px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
tocWrapper: css`
|
2023-08-08 19:48:41 +08:00
|
|
|
position: fixed;
|
|
|
|
top: ${token.headerHeight + token.contentMarginTop}px;
|
2023-05-30 10:33:35 +08:00
|
|
|
inset-inline-end: 0;
|
2022-11-09 12:28:04 +08:00
|
|
|
width: 160px;
|
2023-08-08 19:48:41 +08:00
|
|
|
margin: 0 0 12px 0;
|
2023-05-30 10:33:35 +08:00
|
|
|
padding: 8px 0;
|
|
|
|
padding-inline: 4px 8px;
|
2022-11-09 12:28:04 +08:00
|
|
|
backdrop-filter: blur(8px);
|
|
|
|
border-radius: ${token.borderRadius}px;
|
|
|
|
box-sizing: border-box;
|
2023-08-08 19:48:41 +08:00
|
|
|
z-index: 1000;
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
.toc-debug {
|
2023-03-05 20:49:26 +08:00
|
|
|
color: ${token.purple6};
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
&:hover {
|
2023-03-05 20:49:26 +08:00
|
|
|
color: ${token.purple5};
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> div {
|
|
|
|
box-sizing: border-box;
|
|
|
|
width: 100%;
|
2023-01-06 16:47:40 +08:00
|
|
|
max-height: calc(100vh - 40px) !important;
|
2022-11-09 12:28:04 +08:00
|
|
|
margin: 0 auto;
|
|
|
|
overflow: auto;
|
|
|
|
padding-inline: 4px;
|
|
|
|
}
|
2022-11-15 10:09:24 +08:00
|
|
|
|
|
|
|
@media only screen and (max-width: ${token.screenLG}px) {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
articleWrapper: css`
|
|
|
|
padding: 0 170px 32px 64px;
|
|
|
|
|
|
|
|
&.rtl {
|
|
|
|
padding: 0 64px 144px 170px;
|
|
|
|
}
|
|
|
|
|
|
|
|
@media only screen and (max-width: ${token.screenLG}px) {
|
2022-11-24 20:11:50 +08:00
|
|
|
&,
|
|
|
|
&.rtl {
|
2023-01-22 01:30:01 +08:00
|
|
|
padding: 0 48px;
|
2022-11-15 10:09:24 +08:00
|
|
|
}
|
|
|
|
}
|
2022-11-09 12:28:04 +08:00
|
|
|
`,
|
|
|
|
};
|
2023-07-20 19:27:33 +08:00
|
|
|
});
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
type AnchorItem = {
|
|
|
|
id: string;
|
|
|
|
title: string;
|
|
|
|
children?: AnchorItem[];
|
|
|
|
};
|
|
|
|
|
2023-06-26 12:11:31 +08:00
|
|
|
const AvatarPlaceholder: React.FC<{ num?: number }> = ({ num = 3 }) => (
|
2023-07-21 15:04:42 +08:00
|
|
|
<li>
|
2023-03-16 11:27:25 +08:00
|
|
|
{Array.from({ length: num }).map((_, i) => (
|
|
|
|
<Skeleton.Avatar size="small" active key={i} style={{ marginLeft: i === 0 ? 0 : -8 }} />
|
|
|
|
))}
|
2023-07-21 15:04:42 +08:00
|
|
|
</li>
|
2023-03-16 11:27:25 +08:00
|
|
|
);
|
|
|
|
|
2023-07-30 19:16:30 +08:00
|
|
|
const AuthorAvatar: React.FC<{ name: string; avatar: string }> = ({ name, avatar }) => {
|
2023-03-16 11:27:25 +08:00
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const [error, setError] = useState(false);
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
const img = new Image();
|
|
|
|
img.src = avatar;
|
|
|
|
img.onload = () => setLoading(false);
|
|
|
|
img.onerror = () => setError(true);
|
|
|
|
}, []);
|
2023-07-30 19:16:30 +08:00
|
|
|
if (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (loading) {
|
|
|
|
return <Skeleton.Avatar size="small" active />;
|
|
|
|
}
|
2023-03-16 11:27:25 +08:00
|
|
|
return (
|
|
|
|
<Avatar size="small" src={avatar} alt={name}>
|
|
|
|
{name}
|
|
|
|
</Avatar>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-11-24 20:11:50 +08:00
|
|
|
const Content: React.FC<{ children: ReactNode }> = ({ children }) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
const meta = useRouteMeta();
|
2023-02-20 10:51:18 +08:00
|
|
|
const tab = useTabMeta();
|
2022-12-01 10:43:52 +08:00
|
|
|
const { pathname, hash } = useLocation();
|
2022-11-09 12:28:04 +08:00
|
|
|
const { formatMessage } = useIntl();
|
2023-07-20 19:27:33 +08:00
|
|
|
const { styles } = useStyle();
|
|
|
|
const token = useTheme();
|
2023-07-30 19:16:30 +08:00
|
|
|
const { direction, isMobile } = useContext(SiteContext);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2023-04-13 18:13:13 +08:00
|
|
|
const [showDebug, setShowDebug] = useLayoutState(false);
|
2022-11-17 01:22:09 +08:00
|
|
|
const debugDemos = useMemo(
|
|
|
|
() => meta.toc?.filter((item) => item._debug_demo).map((item) => item.id) || [],
|
|
|
|
[meta],
|
|
|
|
);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2022-12-01 10:43:52 +08:00
|
|
|
const isDebugDemo = debugDemos.includes(hash.slice(1));
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
useLayoutEffect(() => {
|
2022-12-01 10:43:52 +08:00
|
|
|
setShowDebug(process.env.NODE_ENV === 'development' || isDebugDemo);
|
2022-11-09 12:28:04 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const contextValue = useMemo<DemoContextProps>(
|
2022-11-17 01:22:09 +08:00
|
|
|
() => ({ showDebug, setShowDebug }),
|
2022-11-09 12:28:04 +08:00
|
|
|
[showDebug, debugDemos],
|
|
|
|
);
|
|
|
|
|
2022-11-30 11:05:41 +08:00
|
|
|
const anchorItems = useMemo(
|
|
|
|
() =>
|
2023-02-20 10:51:18 +08:00
|
|
|
(tab?.toc || meta.toc).reduce<AnchorItem[]>((result, item) => {
|
2022-11-30 11:05:41 +08:00
|
|
|
if (item.depth === 2) {
|
|
|
|
result.push({ ...item });
|
|
|
|
} else if (item.depth === 3) {
|
|
|
|
const parent = result[result.length - 1];
|
|
|
|
if (parent) {
|
|
|
|
parent.children = parent.children || [];
|
|
|
|
parent.children.push({ ...item });
|
|
|
|
}
|
2022-11-09 12:28:04 +08:00
|
|
|
}
|
2022-11-30 11:05:41 +08:00
|
|
|
return result;
|
|
|
|
}, []),
|
2023-02-20 10:51:18 +08:00
|
|
|
[tab?.toc, meta.toc],
|
2022-11-30 11:05:41 +08:00
|
|
|
);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2022-11-15 10:09:24 +08:00
|
|
|
const isRTL = direction === 'rtl';
|
|
|
|
|
2023-03-16 11:27:25 +08:00
|
|
|
const mergedAuthorInfos = useMemo(() => {
|
|
|
|
const { author } = meta.frontmatter;
|
|
|
|
if (!author) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
if (typeof author === 'string') {
|
|
|
|
return author.split(',').map((item) => ({
|
|
|
|
name: item,
|
|
|
|
avatar: `https://github.com/${item}.png`,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (Array.isArray(author)) {
|
|
|
|
return author;
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}, [meta.frontmatter.author]);
|
2023-01-04 13:45:02 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
return (
|
|
|
|
<DemoContext.Provider value={contextValue}>
|
|
|
|
<Col xxl={20} xl={19} lg={18} md={18} sm={24} xs={24}>
|
2023-08-04 19:27:38 +08:00
|
|
|
{!!meta.frontmatter.toc && (
|
2023-08-08 19:48:41 +08:00
|
|
|
<section className={styles.tocWrapper}>
|
|
|
|
<Anchor
|
|
|
|
className={styles.toc}
|
|
|
|
affix={false}
|
|
|
|
targetOffset={token.marginXXL}
|
|
|
|
showInkInFixed
|
|
|
|
items={anchorItems.map((item) => ({
|
|
|
|
href: `#${item.id}`,
|
|
|
|
title: item.title,
|
|
|
|
key: item.id,
|
|
|
|
children: item.children
|
|
|
|
?.filter((child) => showDebug || !debugDemos.includes(child.id))
|
|
|
|
.map((child) => ({
|
|
|
|
key: child.id,
|
|
|
|
href: `#${child.id}`,
|
|
|
|
title: (
|
|
|
|
<span className={classNames(debugDemos.includes(child.id) && 'toc-debug')}>
|
|
|
|
{child?.title}
|
|
|
|
</span>
|
|
|
|
),
|
|
|
|
})),
|
|
|
|
}))}
|
|
|
|
/>
|
|
|
|
</section>
|
2023-08-04 19:27:38 +08:00
|
|
|
)}
|
2023-07-20 19:27:33 +08:00
|
|
|
<article className={classNames(styles.articleWrapper, { rtl: isRTL })}>
|
2023-02-08 22:54:21 +08:00
|
|
|
{meta.frontmatter?.title ? (
|
2023-08-03 10:58:38 +08:00
|
|
|
<Typography.Title style={{ fontSize: 30, position: 'relative' }}>
|
|
|
|
<Space size="small">
|
|
|
|
{meta.frontmatter?.title}
|
|
|
|
{meta.frontmatter?.subtitle}
|
|
|
|
|
|
|
|
{!pathname.startsWith('/components/overview') && (
|
|
|
|
<EditButton
|
|
|
|
title={<FormattedMessage id="app.content.edit-page" />}
|
|
|
|
filename={meta.frontmatter.filename}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Space>
|
|
|
|
{pathname.startsWith('/components/') && <ComponentChangelog pathname={pathname} />}
|
2023-02-01 21:55:20 +08:00
|
|
|
</Typography.Title>
|
|
|
|
) : null}
|
2022-12-08 16:46:48 +08:00
|
|
|
{/* 添加作者、时间等信息 */}
|
|
|
|
{meta.frontmatter.date || meta.frontmatter.author ? (
|
2023-03-16 11:27:25 +08:00
|
|
|
<Typography.Paragraph>
|
2022-12-08 16:46:48 +08:00
|
|
|
<Space>
|
|
|
|
{meta.frontmatter.date && (
|
2023-03-16 11:27:25 +08:00
|
|
|
<span style={{ opacity: 0.65 }}>
|
2022-12-08 16:46:48 +08:00
|
|
|
<CalendarOutlined /> {DayJS(meta.frontmatter.date).format('YYYY-MM-DD')}
|
|
|
|
</span>
|
|
|
|
)}
|
2023-03-16 11:27:25 +08:00
|
|
|
{mergedAuthorInfos.map((info) => (
|
|
|
|
<a
|
|
|
|
href={`https://github.com/${info.name}`}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
key={info.name}
|
|
|
|
>
|
|
|
|
<Space size={3}>
|
|
|
|
<AuthorAvatar name={info.name} avatar={info.avatar} />
|
|
|
|
<span style={{ opacity: 0.65 }}>@{info.name}</span>
|
|
|
|
</Space>
|
|
|
|
</a>
|
|
|
|
))}
|
2022-12-08 16:46:48 +08:00
|
|
|
</Space>
|
|
|
|
</Typography.Paragraph>
|
|
|
|
) : null}
|
2023-03-14 20:34:36 +08:00
|
|
|
{!meta.frontmatter.__autoDescription && meta.frontmatter.description}
|
2023-07-24 19:56:17 +08:00
|
|
|
<div style={{ minHeight: 'calc(100vh - 64px)' }}>{children}</div>
|
2023-06-25 12:27:03 +08:00
|
|
|
{(meta.frontmatter?.zhihu_url ||
|
|
|
|
meta.frontmatter?.yuque_url ||
|
|
|
|
meta.frontmatter?.juejin_url) && (
|
2023-06-22 21:11:12 +08:00
|
|
|
<ColumnCard
|
|
|
|
zhihuLink={meta.frontmatter.zhihu_url}
|
|
|
|
yuqueLink={meta.frontmatter.yuque_url}
|
2023-06-25 12:27:03 +08:00
|
|
|
juejinLink={meta.frontmatter.juejin_url}
|
2023-06-22 21:11:12 +08:00
|
|
|
/>
|
|
|
|
)}
|
2022-11-18 23:54:37 +08:00
|
|
|
{meta.frontmatter.filename && (
|
|
|
|
<ContributorsList
|
2023-07-30 19:16:30 +08:00
|
|
|
cache
|
2023-01-22 01:30:01 +08:00
|
|
|
repo="ant-design"
|
|
|
|
owner="ant-design"
|
2023-07-30 19:16:30 +08:00
|
|
|
className={classNames(styles.contributorsList, { [styles.listMobile]: isMobile })}
|
2022-11-18 23:54:37 +08:00
|
|
|
fileName={meta.frontmatter.filename}
|
2023-06-26 12:11:31 +08:00
|
|
|
renderItem={(item, loading) => {
|
|
|
|
if (!item || loading) {
|
|
|
|
return <AvatarPlaceholder />;
|
|
|
|
}
|
|
|
|
if (item.username?.includes('github-actions')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
2023-01-04 13:45:02 +08:00
|
|
|
<Tooltip
|
2023-01-22 01:30:01 +08:00
|
|
|
mouseEnterDelay={0.3}
|
2023-01-04 13:45:02 +08:00
|
|
|
title={`${formatMessage({ id: 'app.content.contributors' })}: ${item.username}`}
|
|
|
|
key={item.username}
|
|
|
|
>
|
2023-07-21 15:04:42 +08:00
|
|
|
<li>
|
|
|
|
<a
|
|
|
|
href={`https://github.com/${item.username}`}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
<Avatar size="small" src={item.url} alt={item.username}>
|
|
|
|
{item.username}
|
|
|
|
</Avatar>
|
|
|
|
</a>
|
|
|
|
</li>
|
2023-01-04 13:45:02 +08:00
|
|
|
</Tooltip>
|
2023-06-26 12:11:31 +08:00
|
|
|
);
|
|
|
|
}}
|
2022-11-18 23:54:37 +08:00
|
|
|
/>
|
|
|
|
)}
|
2022-11-15 10:09:24 +08:00
|
|
|
</article>
|
2023-06-05 09:46:52 +08:00
|
|
|
<PrevAndNext rtl={isRTL} />
|
2022-11-09 12:28:04 +08:00
|
|
|
<Footer />
|
|
|
|
</Col>
|
|
|
|
</DemoContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Content;
|