Skip to content

Commit

Permalink
Merge branch 'master' into feature-merge-master
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyo837 committed Aug 23, 2023
2 parents 477b1ec + 5d522a3 commit f06d851
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 85 deletions.
48 changes: 6 additions & 42 deletions .dumi/theme/builtins/Previewer/CodePreviewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import type { Project } from '@stackblitz/sdk';
import stackblitzSdk from '@stackblitz/sdk';
import classNames from 'classnames';
import { FormattedMessage, useSiteData } from 'dumi';
import toReactElement from 'jsonml-to-react-element';
import JsonML from 'jsonml.js/lib/utils';
import LZString from 'lz-string';
import Prism from 'prismjs';
import React, { useContext, useEffect, useRef, useState } from 'react';
import CopyToClipboard from 'react-copy-to-clipboard';
import { Alert, Badge, Space, Tooltip } from 'antd';
Expand All @@ -31,28 +28,6 @@ import { ping } from '../../utils';

const { ErrorBoundary } = Alert;

function toReactComponent(jsonML: any) {
return toReactElement(jsonML, [
[
(node: any) => JsonML.isElement(node) && JsonML.getTagName(node) === 'pre',
(node: any, index: any) => {
// ref: https://github.com/benjycui/bisheng/blob/master/packages/bisheng/src/bisheng-plugin-highlight/lib/browser.js#L7
const attr = JsonML.getAttributes(node);
return React.createElement(
'pre',
{
key: index,
className: `language-${attr.lang}`,
},
React.createElement('code', {
dangerouslySetInnerHTML: { __html: attr.highlighted },
}),
);
},
],
]);
}

function compress(string: string): string {
return LZString.compressToBase64(string)
.replace(/\+/g, '-') // Convert '+' to '-'
Expand Down Expand Up @@ -130,13 +105,6 @@ const CodePreviewer: React.FC<AntdPreviewerProps> = (props) => {

const [showOnlineUrl, setShowOnlineUrl] = useState<boolean>(false);

const highlightedCodes = {
jsx: Prism.highlight(jsx, Prism.languages.javascript, 'jsx'),
tsx: Prism.highlight(entryCode, Prism.languages.javascript, 'jsx'),
};

const highlightedStyle = style ? Prism.highlight(style, Prism.languages.css, 'css') : '';

useEffect(() => {
const regexp = /preview-(\d+)-ant-design/; // matching PR preview addresses
setShowOnlineUrl(
Expand Down Expand Up @@ -538,17 +506,11 @@ createRoot(document.getElementById('container')).render(<Demo />);
{codeExpand && (
<section className={highlightClass} key="code">
<CodePreview
codes={highlightedCodes}
toReactComponent={toReactComponent}
sourceCode={entryCode}
jsxCode={jsx}
styleCode={style}
onCodeTypeChange={(type) => setCodeType(type)}
/>
{highlightedStyle ? (
<div key="style" className="highlight">
<pre>
<code className="css" dangerouslySetInnerHTML={{ __html: highlightedStyle }} />
</pre>
</div>
) : null}
</section>
)}
</section>
Expand All @@ -560,7 +522,9 @@ createRoot(document.getElementById('container')).render(<Demo />);
// resulting in some response delays like following issue:
// https://github.com/ant-design/ant-design/issues/39995
// So we insert style tag into head tag.
if (!style) return;
if (!style) {
return;
}
const styleTag = document.createElement('style');
styleTag.type = 'text/css';
styleTag.innerHTML = style;
Expand Down
3 changes: 2 additions & 1 deletion .dumi/theme/builtins/Previewer/Previewer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import type { IPreviewerProps } from 'dumi';
import { useTabMeta } from 'dumi';
import React from 'react';

import CodePreviewer from './CodePreviewer';
import DesignPreviewer from './DesignPreviewer';

Expand Down
40 changes: 35 additions & 5 deletions .dumi/theme/builtins/Previewer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
import React, { Suspense } from 'react';
import type { IPreviewerProps } from 'dumi';
import { Skeleton, Alert } from 'antd';
import { createStyles } from 'antd-style';

const { ErrorBoundary } = Alert;

const Previewer = React.lazy(() => import('./Previewer'));

export default (props: IPreviewerProps) => (
<Suspense fallback={null}>
<Previewer {...props} />
</Suspense>
);
const useStyle = createStyles(({ css }) => ({
skeletonWrapper: css`
width: 100% !important;
height: 500px;
margin-bottom: 16px;
`,
}));

export default (props: IPreviewerProps) => {
const { styles } = useStyle();
return (
<ErrorBoundary>
<Suspense
fallback={
<Skeleton.Node
active
className={styles.skeletonWrapper}
style={{
width: '100%',
height: '100%',
}}
>
{' '}
</Skeleton.Node>
}
>
<Previewer {...props} />
</Suspense>
</ErrorBoundary>
);
};
100 changes: 82 additions & 18 deletions .dumi/theme/common/CodePreview.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,101 @@
import React from 'react';
import React, { useEffect, useMemo } from 'react';
import Prism from 'prismjs';
import toReactElement from 'jsonml-to-react-element';
import JsonML from 'jsonml.js/lib/utils';
import { Tabs } from 'antd';

const LANGS = {
tsx: 'TypeScript',
jsx: 'JavaScript',
style: 'CSS',
};

interface CodePreviewProps {
codes?: Record<PropertyKey, string>;
toReactComponent?: (node: any) => React.ReactNode;
sourceCode?: string;
jsxCode?: string;
styleCode?: string;
onCodeTypeChange?: (activeKey: string) => void;
}

const CodePreview: React.FC<CodePreviewProps> = ({ toReactComponent, codes, onCodeTypeChange }) => {
const langList = Object.keys(codes).sort().reverse();
function toReactComponent(jsonML: any) {
return toReactElement(jsonML, [
[
(node: any) => JsonML.isElement(node) && JsonML.getTagName(node) === 'pre',
(node: any, index: any) => {
// ref: https://github.com/benjycui/bisheng/blob/master/packages/bisheng/src/bisheng-plugin-highlight/lib/browser.js#L7
const attr = JsonML.getAttributes(node);
return React.createElement(
'pre',
{
key: index,
className: `language-${attr.lang}`,
},
React.createElement('code', {
dangerouslySetInnerHTML: { __html: attr.highlighted },
}),
);
},
],
]);
}

const CodePreview: React.FC<CodePreviewProps> = ({
sourceCode = '',
jsxCode = '',
styleCode = '',
onCodeTypeChange,
}) => {
// 避免 Tabs 数量不稳定的闪动问题
const initialCodes = {};
if (sourceCode) {
initialCodes.tsx = '';
}
if (jsxCode) {
initialCodes.jsx = '';
}
if (styleCode) {
initialCodes.style = '';
}
const [highlightedCodes, setHighlightedCodes] = React.useState(initialCodes);

useEffect(() => {
const codes = {
tsx: Prism.highlight(sourceCode, Prism.languages.javascript, 'jsx'),
jsx: Prism.highlight(jsxCode, Prism.languages.javascript, 'jsx'),
style: Prism.highlight(styleCode, Prism.languages.css, 'css'),
};
// 去掉空的代码类型
Object.keys(codes).forEach((key) => {
if (!codes[key]) {
delete codes[key];
}
});
setHighlightedCodes(codes);
}, [jsxCode, sourceCode, styleCode]);

const langList = Object.keys(highlightedCodes);
const items = useMemo(
() =>
langList.map((lang) => ({
label: LANGS[lang],
key: lang,
children: toReactComponent(['pre', { lang, highlighted: highlightedCodes[lang] }]),
})),
[JSON.stringify(highlightedCodes)],
);

if (!langList.length) {
return null;
}

if (langList.length === 1) {
return toReactComponent([
'pre',
{ lang: langList[0], highlighted: codes[langList[0]], className: 'highlight' },
{ lang: langList[0], highlighted: highlightedCodes[langList[0]], className: 'highlight' },
]);
}
return (
<Tabs
centered
className="highlight"
onChange={onCodeTypeChange}
items={langList.map((lang) => ({
label: LANGS[lang],
key: lang,
children: toReactComponent(['pre', { lang, highlighted: codes[lang] }]),
}))}
/>
);

return <Tabs centered className="highlight" onChange={onCodeTypeChange} items={items} />;
};

export default CodePreview;
19 changes: 14 additions & 5 deletions .dumi/theme/common/styles/Common.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { css, Global } from '@emotion/react';
import React from 'react';
import { useTheme } from 'antd-style';

export default () => (
<Global
styles={css`
export default () => {
const { headerHeight, margin } = useTheme();

return (
<Global
styles={css`
body,
div,
dl,
Expand Down Expand Up @@ -55,6 +59,11 @@ export default () => (
vertical-align: middle;
border-style: none;
}
html {
scroll-padding-top: ${headerHeight + margin}px;
}
`}
/>
);
/>
);
};
4 changes: 2 additions & 2 deletions .dumirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ export default defineConfig({
crossorigin: true,
},
{
rel: 'prefetch',
rel: 'preload',
as: 'font',
href: '//at.alicdn.com/wf/webfont/exMpJIukiCms/Gsw2PSKrftc1yNWMNlXgw.woff2',
type: 'font/woff2',
crossorigin: true,
},
{
rel: 'prefetch',
rel: 'preload',
as: 'font',
href: '//at.alicdn.com/wf/webfont/exMpJIukiCms/vtu73by4O2gEBcvBuLgeu.woff',
type: 'font/woff2',
Expand Down
28 changes: 27 additions & 1 deletion .github/workflows/pr-contributor-welcome.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ on:
- 'components/**'

jobs:
read-file:
runs-on: ubuntu-latest
outputs:
require-result: ${{ steps.contributors.outputs.content }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Read contributors.json
id: contributors
uses: juliangruber/read-file-action@v1
with:
path: ./contributors.json

output-log:
runs-on: ubuntu-latest
needs: read-file
steps:
- name: contributors.json
run: echo "${{ needs.read-file.outputs.require-result }}"
- name: creator
run: echo "${{ github.event.pull_request.user.login }}"
- name: contains
run: echo "${{ contains(fromJSON(needs.read-file.outputs.require-result), github.event.pull_request.user.login) }}"
- name: merged
run: echo "${{ github.event.pull_request.merged }}"

check-merged:
runs-on: ubuntu-latest
needs: read-file
Expand All @@ -25,4 +51,4 @@ jobs:
<img src="https://github.com/ant-design/ant-design/assets/5378891/e24c6080-bf38-4523-b1cd-f6c43ad7375f" height="200" />
<!-- WELCOME_CONTRIBUTION -->
body-include: '<!-- WELCOME_CONTRIBUTION -->'
body-include: '<!-- WELCOME_CONTRIBUTION -->'
6 changes: 2 additions & 4 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
"trailingComma": "all",
"printWidth": 100,
"proseWrap": "never",
"importOrder": ["^(react|react-dom)$", "^([a-z]|@[a-z])", ".*"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"plugins": ["@trivago/prettier-plugin-sort-imports"],
"importOrder": ["^(react|react-dom)$", "^([a-z]|@[a-z])", "", ".*"],
"plugins": ["@ianvs/prettier-plugin-sort-imports"],
"overrides": [
{
"files": ".prettierrc",
Expand Down
14 changes: 14 additions & 0 deletions components/date-picker/__tests__/DatePicker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ describe('DatePicker', () => {
expect(mouseDownEvent).not.toThrow();
});

it('showTime should work correctly when format is Array', () => {
const { container } = render(
<DatePicker defaultValue={dayjs()} showTime format={['YYYY-MM-DD HH:mm']} open />,
);
const fuousEvent = () => {
fireEvent.focus(container.querySelector('input')!);
};
const mouseDownEvent = () => {
fireEvent.mouseDown(container.querySelector('input')!);
};
expect(fuousEvent).not.toThrow();
expect(mouseDownEvent).not.toThrow();
});

it('12 hours', () => {
const { container } = render(
<DatePicker defaultValue={dayjs()} showTime format="YYYY-MM-DD HH:mm:ss A" open />,
Expand Down
5 changes: 5 additions & 0 deletions components/date-picker/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export function getTimeProps<DateType, DisabledTime>(
const firstFormat = toArray(format)[0];
const showTimeObj = { ...props };

// https://github.com/ant-design/ant-design/issues/44275
if (format && Array.isArray(format)) {
showTimeObj.format = firstFormat;
}

if (firstFormat && typeof firstFormat === 'string') {
if (!firstFormat.includes('s') && showSecond === undefined) {
showTimeObj.showSecond = false;
Expand Down
Loading

0 comments on commit f06d851

Please sign in to comment.