2022-12-08 16:46:48 +08:00
|
|
|
import { CalendarOutlined } from '@ant-design/icons';
|
2022-11-30 11:05:41 +08:00
|
|
|
import { css } from '@emotion/react';
|
2023-01-22 01:30:01 +08:00
|
|
|
import ContributorsList from '@qixian.cs/github-contributors-list';
|
|
|
|
import { Affix, Anchor, Avatar, Col, Skeleton, Space, Tooltip, Typography } from 'antd';
|
2022-11-30 11:05:41 +08:00
|
|
|
import classNames from 'classnames';
|
2023-01-22 01:30:01 +08:00
|
|
|
import DayJS from 'dayjs';
|
|
|
|
import { FormattedMessage, useIntl, useRouteMeta } from 'dumi';
|
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import React, { useContext, useLayoutEffect, useMemo, useState } from 'react';
|
2022-11-09 12:28:04 +08:00
|
|
|
import useLocation from '../../../hooks/useLocation';
|
|
|
|
import useSiteToken from '../../../hooks/useSiteToken';
|
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';
|
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';
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
const useStyle = () => {
|
|
|
|
const { token } = useSiteToken();
|
|
|
|
|
|
|
|
const { antCls } = token;
|
|
|
|
|
|
|
|
return {
|
|
|
|
contributorsList: css`
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
margin-top: 120px !important;
|
|
|
|
a,
|
|
|
|
${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 {
|
|
|
|
a,
|
|
|
|
${antCls}-avatar {
|
|
|
|
margin-inline-end: 0;
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 12:28:04 +08:00
|
|
|
`,
|
|
|
|
toc: css`
|
|
|
|
${antCls}-anchor {
|
|
|
|
${antCls}-anchor-link-title {
|
|
|
|
font-size: 12px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
tocWrapper: css`
|
|
|
|
position: absolute;
|
|
|
|
top: 8px;
|
|
|
|
right: 0;
|
|
|
|
width: 160px;
|
|
|
|
margin: 12px 0;
|
|
|
|
padding: 8px 8px 8px 4px;
|
|
|
|
backdrop-filter: blur(8px);
|
|
|
|
border-radius: ${token.borderRadius}px;
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
|
|
|
.toc-debug {
|
|
|
|
color: ${token['purple-6']};
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
color: ${token['purple-5']};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
> 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
|
|
|
|
|
|
|
&.rtl {
|
|
|
|
right: auto;
|
|
|
|
left: 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
`,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
type AnchorItem = {
|
|
|
|
id: string;
|
|
|
|
title: string;
|
|
|
|
children?: AnchorItem[];
|
|
|
|
};
|
|
|
|
|
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();
|
2022-12-01 10:43:52 +08:00
|
|
|
const { pathname, hash } = useLocation();
|
2022-11-09 12:28:04 +08:00
|
|
|
const { formatMessage } = useIntl();
|
|
|
|
const styles = useStyle();
|
2022-12-22 15:31:40 +08:00
|
|
|
const { token } = useSiteToken();
|
2022-11-15 10:09:24 +08:00
|
|
|
const { direction } = useContext(SiteContext);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
const [showDebug, setShowDebug] = useState(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(
|
|
|
|
() =>
|
|
|
|
meta.toc.reduce<AnchorItem[]>((result, item) => {
|
|
|
|
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;
|
|
|
|
}, []),
|
|
|
|
[meta.toc],
|
|
|
|
);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
2022-11-15 10:09:24 +08:00
|
|
|
const isRTL = direction === 'rtl';
|
|
|
|
|
2023-01-04 13:45:02 +08:00
|
|
|
const avatarPlaceholder = (
|
|
|
|
<>
|
|
|
|
<Skeleton.Avatar size="small" active />
|
|
|
|
<Skeleton.Avatar size="small" active style={{ marginLeft: -8 }} />
|
|
|
|
<Skeleton.Avatar size="small" active style={{ marginLeft: -8 }} />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
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}>
|
|
|
|
<Affix>
|
2022-11-15 10:09:24 +08:00
|
|
|
<section css={styles.tocWrapper} className={classNames({ rtl: isRTL })}>
|
2022-12-23 16:31:30 +08:00
|
|
|
<Anchor
|
|
|
|
css={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) => ({
|
2023-01-22 01:30:01 +08:00
|
|
|
key: child.id,
|
2022-12-23 16:31:30 +08:00
|
|
|
href: `#${child.id}`,
|
|
|
|
title: (
|
|
|
|
<span className={classNames(debugDemos.includes(child.id) && 'toc-debug')}>
|
|
|
|
{child?.title}
|
|
|
|
</span>
|
|
|
|
),
|
|
|
|
})),
|
|
|
|
}))}
|
|
|
|
/>
|
2022-11-15 10:09:24 +08:00
|
|
|
</section>
|
2022-11-09 12:28:04 +08:00
|
|
|
</Affix>
|
2022-11-15 10:09:24 +08:00
|
|
|
<article css={styles.articleWrapper} className={classNames({ rtl: isRTL })}>
|
2023-02-08 22:54:21 +08:00
|
|
|
{meta.frontmatter?.title ? (
|
2023-02-01 21:55:20 +08:00
|
|
|
<Typography.Title style={{ fontSize: 30 }}>
|
|
|
|
{meta.frontmatter?.title}
|
|
|
|
{meta.frontmatter.subtitle && (
|
|
|
|
<span style={{ marginLeft: 12 }}>{meta.frontmatter.subtitle}</span>
|
|
|
|
)}
|
|
|
|
{!pathname.startsWith('/components/overview') && (
|
|
|
|
<EditButton
|
|
|
|
title={<FormattedMessage id="app.content.edit-page" />}
|
|
|
|
filename={meta.frontmatter.filename}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Typography.Title>
|
|
|
|
) : null}
|
2022-12-08 16:46:48 +08:00
|
|
|
{/* 添加作者、时间等信息 */}
|
|
|
|
{meta.frontmatter.date || meta.frontmatter.author ? (
|
|
|
|
<Typography.Paragraph style={{ opacity: 0.65 }}>
|
|
|
|
<Space>
|
|
|
|
{meta.frontmatter.date && (
|
|
|
|
<span>
|
|
|
|
<CalendarOutlined /> {DayJS(meta.frontmatter.date).format('YYYY-MM-DD')}
|
|
|
|
</span>
|
|
|
|
)}
|
2022-12-19 11:50:11 +08:00
|
|
|
{meta.frontmatter.author &&
|
|
|
|
(meta.frontmatter.author as string)?.split(',')?.map((author) => (
|
|
|
|
<Typography.Link href={`https://github.com/${author}`} key={author}>
|
|
|
|
@{author}
|
|
|
|
</Typography.Link>
|
|
|
|
))}
|
2022-12-08 16:46:48 +08:00
|
|
|
</Space>
|
|
|
|
</Typography.Paragraph>
|
|
|
|
) : null}
|
2022-11-09 12:28:04 +08:00
|
|
|
{children}
|
2022-11-18 23:54:37 +08:00
|
|
|
{meta.frontmatter.filename && (
|
|
|
|
<ContributorsList
|
2023-01-22 01:30:01 +08:00
|
|
|
repo="ant-design"
|
|
|
|
owner="ant-design"
|
2022-11-18 23:54:37 +08:00
|
|
|
css={styles.contributorsList}
|
|
|
|
fileName={meta.frontmatter.filename}
|
|
|
|
renderItem={(item, loading) =>
|
2023-01-04 13:45:02 +08:00
|
|
|
loading || !item ? (
|
|
|
|
avatarPlaceholder
|
2022-11-18 23:54:37 +08:00
|
|
|
) : (
|
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}
|
|
|
|
>
|
|
|
|
<a
|
|
|
|
href={`https://github.com/${item.username}`}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
2022-11-09 12:28:04 +08:00
|
|
|
>
|
2023-01-04 13:45:02 +08:00
|
|
|
<Avatar size="small" src={item.url}>
|
|
|
|
{item.username}
|
|
|
|
</Avatar>
|
|
|
|
</a>
|
|
|
|
</Tooltip>
|
2022-11-09 12:28:04 +08:00
|
|
|
)
|
2022-11-18 23:54:37 +08:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
2022-11-15 10:09:24 +08:00
|
|
|
</article>
|
2022-11-09 12:28:04 +08:00
|
|
|
<PrevAndNext />
|
|
|
|
<Footer />
|
|
|
|
</Col>
|
|
|
|
</DemoContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Content;
|