2024-09-30 20:21:18 +08:00
|
|
|
import React, { cloneElement, isValidElement } from 'react';
|
|
|
|
import { BugOutlined } from '@ant-design/icons';
|
2024-10-01 12:56:23 +08:00
|
|
|
import { Drawer, Flex, Grid, Popover, Tag, Timeline, Typography } from 'antd';
|
2024-04-05 11:41:30 +08:00
|
|
|
import type { TimelineItemProps } from 'antd';
|
2023-09-18 15:30:30 +08:00
|
|
|
import { createStyles } from 'antd-style';
|
2024-04-04 20:10:00 +08:00
|
|
|
import semver from 'semver';
|
2023-09-18 15:30:30 +08:00
|
|
|
|
2024-04-04 20:10:00 +08:00
|
|
|
import deprecatedVersions from '../../../../BUG_VERSIONS.json';
|
2023-08-03 14:45:51 +08:00
|
|
|
import useFetch from '../../../hooks/useFetch';
|
2023-09-18 15:30:30 +08:00
|
|
|
import useLocale from '../../../hooks/useLocale';
|
2024-09-30 20:21:18 +08:00
|
|
|
import useLocation from '../../../hooks/useLocation';
|
2023-09-18 15:30:30 +08:00
|
|
|
import Link from '../Link';
|
2023-08-03 10:58:38 +08:00
|
|
|
|
2024-04-05 11:41:30 +08:00
|
|
|
interface MatchDeprecatedResult {
|
2024-04-04 20:10:00 +08:00
|
|
|
match?: string;
|
|
|
|
reason: string[];
|
2024-04-05 11:41:30 +08:00
|
|
|
}
|
2024-04-04 20:10:00 +08:00
|
|
|
|
2024-09-28 19:56:10 +08:00
|
|
|
interface ChangelogInfo {
|
|
|
|
version: string;
|
|
|
|
changelog: string;
|
|
|
|
refs: string[];
|
2024-09-29 18:02:55 +08:00
|
|
|
releaseDate: string;
|
2024-09-28 19:56:10 +08:00
|
|
|
}
|
|
|
|
|
2024-04-04 20:10:00 +08:00
|
|
|
function matchDeprecated(v: string): MatchDeprecatedResult {
|
|
|
|
const match = Object.keys(deprecatedVersions).find((depreciated) =>
|
|
|
|
semver.satisfies(v, depreciated),
|
|
|
|
);
|
|
|
|
const reason = deprecatedVersions[match as keyof typeof deprecatedVersions] || [];
|
|
|
|
return {
|
|
|
|
match,
|
|
|
|
reason: Array.isArray(reason) ? reason : [reason],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-03 10:58:38 +08:00
|
|
|
const useStyle = createStyles(({ token, css }) => ({
|
2024-10-01 12:56:23 +08:00
|
|
|
listWrap: css`
|
|
|
|
> li {
|
|
|
|
line-height: 2;
|
|
|
|
}
|
|
|
|
`,
|
2024-07-27 16:13:25 +08:00
|
|
|
linkRef: css`
|
2024-04-04 20:10:00 +08:00
|
|
|
margin-inline-start: ${token.marginXS}px;
|
|
|
|
`,
|
|
|
|
bug: css`
|
2024-04-06 16:11:17 +08:00
|
|
|
font-size: ${token.fontSize}px;
|
2024-04-04 20:10:00 +08:00
|
|
|
color: #aaa;
|
2024-04-05 11:41:30 +08:00
|
|
|
margin-inline-start: ${token.marginXS}px;
|
2024-04-04 20:10:00 +08:00
|
|
|
display: inline-block;
|
|
|
|
vertical-align: inherit;
|
2024-04-05 11:41:30 +08:00
|
|
|
cursor: pointer;
|
2024-04-04 20:10:00 +08:00
|
|
|
&:hover {
|
|
|
|
color: #333;
|
|
|
|
}
|
|
|
|
`,
|
2024-04-05 11:41:30 +08:00
|
|
|
bugReasonTitle: css`
|
|
|
|
padding: ${token.paddingXXS}px ${token.paddingXS}px;
|
|
|
|
`,
|
|
|
|
bugReasonList: css`
|
|
|
|
width: 100%;
|
|
|
|
max-width: 100%;
|
2024-04-04 20:10:00 +08:00
|
|
|
li {
|
2024-04-05 11:41:30 +08:00
|
|
|
padding: ${token.paddingXXS}px ${token.paddingXS}px;
|
|
|
|
a {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
gap: ${token.marginXXS}px;
|
|
|
|
}
|
2024-04-04 20:10:00 +08:00
|
|
|
}
|
2023-08-03 10:58:38 +08:00
|
|
|
`,
|
2024-07-27 16:13:25 +08:00
|
|
|
extraLink: css`
|
|
|
|
font-size: ${token.fontSize}px;
|
|
|
|
`,
|
|
|
|
drawerContent: {
|
|
|
|
position: 'relative',
|
|
|
|
[`> ${token.antCls}-drawer-body`]: {
|
|
|
|
scrollbarWidth: 'thin',
|
|
|
|
scrollbarColor: 'unset',
|
|
|
|
},
|
|
|
|
},
|
2024-10-01 12:56:23 +08:00
|
|
|
versionWrap: css`
|
|
|
|
margin-bottom: 1em;
|
|
|
|
`,
|
|
|
|
versionTitle: css`
|
|
|
|
margin: 0 !important;
|
|
|
|
`,
|
|
|
|
versionTag: css`
|
|
|
|
user-select: none;
|
|
|
|
display: inline-flex;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
&:last-child {
|
|
|
|
margin-inline-end: 0;
|
|
|
|
}
|
|
|
|
`,
|
2023-08-03 10:58:38 +08:00
|
|
|
}));
|
|
|
|
|
|
|
|
const locales = {
|
|
|
|
cn: {
|
2024-06-22 16:51:41 +08:00
|
|
|
full: '查看完整日志',
|
2023-08-03 10:58:38 +08:00
|
|
|
changelog: '更新日志',
|
|
|
|
loading: '加载中...',
|
|
|
|
empty: '暂无更新',
|
2024-04-04 20:10:00 +08:00
|
|
|
bugList: 'Bug 版本',
|
2023-08-03 10:58:38 +08:00
|
|
|
},
|
|
|
|
en: {
|
2023-08-03 17:45:49 +08:00
|
|
|
full: 'Full Changelog',
|
2023-08-03 10:58:38 +08:00
|
|
|
changelog: 'Changelog',
|
|
|
|
loading: 'loading...',
|
|
|
|
empty: 'Nothing update',
|
2024-04-04 20:10:00 +08:00
|
|
|
bugList: 'Bug Versions',
|
2023-08-03 10:58:38 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-09-28 19:56:10 +08:00
|
|
|
const ParseChangelog: React.FC<{ changelog: string }> = (props) => {
|
|
|
|
const { changelog = '' } = props;
|
2023-08-03 10:58:38 +08:00
|
|
|
|
|
|
|
const parsedChangelog = React.useMemo(() => {
|
2023-09-18 15:30:30 +08:00
|
|
|
const nodes: React.ReactNode[] = [];
|
2023-08-03 10:58:38 +08:00
|
|
|
|
|
|
|
let isQuota = false;
|
2024-09-30 17:45:10 +08:00
|
|
|
let isBold = false;
|
2023-08-03 10:58:38 +08:00
|
|
|
let lastStr = '';
|
|
|
|
|
|
|
|
for (let i = 0; i < changelog.length; i += 1) {
|
|
|
|
const char = changelog[i];
|
|
|
|
|
2024-09-30 17:45:10 +08:00
|
|
|
if (char !== '`' && char !== '*') {
|
2023-08-03 10:58:38 +08:00
|
|
|
lastStr += char;
|
|
|
|
} else {
|
2023-09-18 15:30:30 +08:00
|
|
|
let node: React.ReactNode = lastStr;
|
2023-08-03 10:58:38 +08:00
|
|
|
if (isQuota) {
|
|
|
|
node = <code>{node}</code>;
|
2024-09-30 17:45:10 +08:00
|
|
|
} else if (isBold) {
|
|
|
|
node = <strong>{node}</strong>;
|
2023-08-03 10:58:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nodes.push(node);
|
|
|
|
lastStr = '';
|
2024-09-30 17:45:10 +08:00
|
|
|
if (char === '`') {
|
|
|
|
isQuota = !isQuota;
|
|
|
|
} else if (char === '*' && changelog[i + 1] === '*') {
|
|
|
|
isBold = !isBold;
|
|
|
|
i += 1; // Skip the next '*'
|
|
|
|
}
|
2023-08-03 10:58:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes.push(lastStr);
|
|
|
|
|
|
|
|
return nodes;
|
|
|
|
}, [changelog]);
|
|
|
|
|
2024-10-01 12:56:23 +08:00
|
|
|
return <span>{parsedChangelog}</span>;
|
2024-04-05 11:41:30 +08:00
|
|
|
};
|
2023-08-03 10:58:38 +08:00
|
|
|
|
2024-10-01 12:56:23 +08:00
|
|
|
const RenderChangelogList: React.FC<{ changelogList: ChangelogInfo[] }> = ({ changelogList }) => {
|
|
|
|
const elements: React.ReactNode[] = [];
|
|
|
|
const { styles } = useStyle();
|
2024-10-02 13:06:30 +08:00
|
|
|
const len = changelogList.length;
|
|
|
|
for (let i = 0; i < len; i += 1) {
|
2024-09-28 19:56:10 +08:00
|
|
|
const { refs, changelog } = changelogList[i];
|
|
|
|
// Check if the next line is an image link and append it to the current line
|
2024-10-02 13:06:30 +08:00
|
|
|
if (i + 1 < len && changelogList[i + 1].changelog.trim().startsWith('<img')) {
|
2024-09-28 19:56:10 +08:00
|
|
|
const imgDom = new DOMParser().parseFromString(changelogList[i + 1].changelog, 'text/html');
|
2024-10-02 13:06:30 +08:00
|
|
|
const imgElement = imgDom.querySelector<HTMLImageElement>('img');
|
2024-09-28 19:56:10 +08:00
|
|
|
elements.push(
|
|
|
|
<li key={i}>
|
|
|
|
<ParseChangelog changelog={changelog} />
|
2024-10-02 13:06:30 +08:00
|
|
|
{refs?.map<React.ReactNode>((ref) => (
|
2024-09-28 19:56:10 +08:00
|
|
|
<a className={styles.linkRef} key={ref} href={ref} target="_blank" rel="noreferrer">
|
|
|
|
#{ref.match(/^.*\/(\d+)$/)?.[1]}
|
|
|
|
</a>
|
|
|
|
))}
|
|
|
|
<br />
|
|
|
|
<img
|
|
|
|
src={imgElement?.getAttribute('src') || ''}
|
|
|
|
alt={imgElement?.getAttribute('alt') || ''}
|
|
|
|
width={imgElement?.getAttribute('width') || ''}
|
|
|
|
/>
|
|
|
|
</li>,
|
|
|
|
);
|
|
|
|
i += 1; // Skip the next line
|
|
|
|
} else {
|
|
|
|
elements.push(
|
|
|
|
<li key={i}>
|
|
|
|
<ParseChangelog changelog={changelog} />
|
|
|
|
</li>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2024-10-01 12:56:23 +08:00
|
|
|
return <ul className={styles.listWrap}>{elements}</ul>;
|
2024-09-28 19:56:10 +08:00
|
|
|
};
|
2023-08-03 10:58:38 +08:00
|
|
|
|
2024-04-05 11:41:30 +08:00
|
|
|
const useChangelog = (componentPath: string, lang: 'cn' | 'en'): ChangelogInfo[] => {
|
2024-06-13 20:37:02 +08:00
|
|
|
const logFileName = `components-changelog-${lang}.json`;
|
|
|
|
|
2024-04-05 11:41:30 +08:00
|
|
|
const data = useFetch({
|
|
|
|
key: `component-changelog-${lang}`,
|
2024-06-13 20:37:02 +08:00
|
|
|
request: () => import(`../../../preset/${logFileName}`),
|
2024-04-05 11:41:30 +08:00
|
|
|
});
|
|
|
|
return React.useMemo(() => {
|
2023-08-03 10:58:38 +08:00
|
|
|
const component = componentPath.replace(/-/g, '');
|
2023-08-03 14:45:51 +08:00
|
|
|
const componentName = Object.keys(data).find(
|
2023-08-03 10:58:38 +08:00
|
|
|
(name) => name.toLowerCase() === component.toLowerCase(),
|
|
|
|
);
|
2024-04-05 11:41:30 +08:00
|
|
|
return data[componentName as keyof typeof data] as ChangelogInfo[];
|
2023-08-03 14:45:51 +08:00
|
|
|
}, [data, componentPath]);
|
2024-04-05 11:41:30 +08:00
|
|
|
};
|
2023-08-03 10:58:38 +08:00
|
|
|
|
2024-10-01 12:56:23 +08:00
|
|
|
const ComponentChangelog: React.FC<Readonly<React.PropsWithChildren>> = (props) => {
|
2024-09-30 20:21:18 +08:00
|
|
|
const { children } = props;
|
2023-08-03 10:58:38 +08:00
|
|
|
const [locale, lang] = useLocale(locales);
|
|
|
|
const [show, setShow] = React.useState(false);
|
2024-09-30 20:21:18 +08:00
|
|
|
const { pathname } = useLocation();
|
2023-08-03 10:58:38 +08:00
|
|
|
|
|
|
|
const { styles } = useStyle();
|
|
|
|
|
|
|
|
const componentPath = pathname.match(/\/components\/([^/]+)/)?.[1] || '';
|
|
|
|
|
|
|
|
const list = useChangelog(componentPath, lang);
|
|
|
|
|
2024-04-05 11:41:30 +08:00
|
|
|
const timelineItems = React.useMemo<TimelineItemProps[]>(() => {
|
2023-09-18 15:30:30 +08:00
|
|
|
const changelogMap: Record<string, ChangelogInfo[]> = {};
|
2023-08-03 10:58:38 +08:00
|
|
|
|
|
|
|
list?.forEach((info) => {
|
|
|
|
changelogMap[info.version] = changelogMap[info.version] || [];
|
|
|
|
changelogMap[info.version].push(info);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Object.keys(changelogMap).map((version) => {
|
|
|
|
const changelogList = changelogMap[version];
|
2024-04-04 20:10:00 +08:00
|
|
|
const bugVersionInfo = matchDeprecated(version);
|
2023-08-03 10:58:38 +08:00
|
|
|
return {
|
|
|
|
children: (
|
|
|
|
<Typography>
|
2024-10-01 12:56:23 +08:00
|
|
|
<Flex className={styles.versionWrap} justify="flex-start" align="center" gap="middle">
|
|
|
|
<Typography.Title className={styles.versionTitle} level={4}>
|
|
|
|
{version}
|
|
|
|
{bugVersionInfo.match && (
|
|
|
|
<Popover
|
|
|
|
destroyTooltipOnHide
|
|
|
|
placement="right"
|
|
|
|
title={<span className={styles.bugReasonTitle}>{locale.bugList}</span>}
|
|
|
|
content={
|
|
|
|
<ul className={styles.bugReasonList}>
|
|
|
|
{bugVersionInfo.reason.map<React.ReactNode>((reason, index) => (
|
|
|
|
<li key={`reason-${index}`}>
|
|
|
|
<a type="link" target="_blank" rel="noreferrer" href={reason}>
|
|
|
|
<BugOutlined />
|
|
|
|
{reason
|
|
|
|
?.replace(/#.*$/, '')
|
|
|
|
?.replace(
|
|
|
|
/^https:\/\/github\.com\/ant-design\/ant-design\/(issues|pull)\//,
|
|
|
|
'#',
|
|
|
|
)}
|
|
|
|
</a>
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<BugOutlined className={styles.bug} />
|
|
|
|
</Popover>
|
|
|
|
)}
|
|
|
|
</Typography.Title>
|
|
|
|
<Tag className={styles.versionTag} bordered={false} color="blue">
|
|
|
|
{changelogList[0]?.releaseDate}
|
|
|
|
</Tag>
|
|
|
|
</Flex>
|
|
|
|
<RenderChangelogList changelogList={changelogList} />
|
2023-08-03 10:58:38 +08:00
|
|
|
</Typography>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}, [list]);
|
|
|
|
|
2024-01-04 15:42:11 +08:00
|
|
|
const screens = Grid.useBreakpoint();
|
|
|
|
const width = screens.md ? '48vw' : '90vw';
|
|
|
|
|
2024-09-30 20:21:18 +08:00
|
|
|
if (!pathname.startsWith('/components/') || !list || !list.length) {
|
2023-08-03 10:58:38 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2024-09-30 20:21:18 +08:00
|
|
|
{isValidElement(children) &&
|
|
|
|
cloneElement(children as React.ReactElement, {
|
|
|
|
onClick: () => setShow(true),
|
|
|
|
})}
|
2023-08-03 10:58:38 +08:00
|
|
|
<Drawer
|
2024-07-27 16:13:25 +08:00
|
|
|
destroyOnClose
|
|
|
|
className={styles.drawerContent}
|
2023-08-03 10:58:38 +08:00
|
|
|
title={locale.changelog}
|
2023-08-03 17:45:49 +08:00
|
|
|
extra={
|
2024-07-27 16:13:25 +08:00
|
|
|
<Link className={styles.extraLink} to={`/changelog${lang === 'cn' ? '-cn' : ''}`}>
|
2023-08-03 17:45:49 +08:00
|
|
|
{locale.full}
|
|
|
|
</Link>
|
|
|
|
}
|
2023-08-03 10:58:38 +08:00
|
|
|
open={show}
|
2024-01-04 15:42:11 +08:00
|
|
|
width={width}
|
2024-07-27 16:13:25 +08:00
|
|
|
onClose={() => setShow(false)}
|
2023-08-03 10:58:38 +08:00
|
|
|
>
|
|
|
|
<Timeline items={timelineItems} />
|
|
|
|
</Drawer>
|
|
|
|
</>
|
|
|
|
);
|
2024-04-05 11:41:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default ComponentChangelog;
|