Skip to content

Commit

Permalink
site: add code-hide Button for CodePreviewer (ant-design#44449)
Browse files Browse the repository at this point in the history
* site: add code-hide Button for CodePreviewer

* fix: fix style

* add token

* fix: fix css props

* fix

* fix: fix

* fix: fix

* Update .dumi/theme/builtins/Previewer/CodePreviewer.tsx

Co-authored-by: MadCcc <[email protected]>
Signed-off-by: lijianan <[email protected]>

* fix: fix

---------

Signed-off-by: lijianan <[email protected]>
Co-authored-by: MadCcc <[email protected]>
  • Loading branch information
li-jia-nan and MadCcc authored Aug 28, 2023
1 parent 07056fa commit 4d1bac4
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
41 changes: 38 additions & 3 deletions .dumi/theme/builtins/Previewer/CodePreviewer.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
import React, { useContext, useEffect, useRef, useState } from 'react';
import {
CheckOutlined,
LinkOutlined,
SnippetsOutlined,
ThunderboltOutlined,
UpOutlined,
} from '@ant-design/icons';
import type { Project } from '@stackblitz/sdk';
import stackblitzSdk from '@stackblitz/sdk';
import { Alert, Badge, Space, Tooltip } from 'antd';
import { createStyles, css } from 'antd-style';
import classNames from 'classnames';
import { FormattedMessage, useSiteData } from 'dumi';
import LZString from 'lz-string';
import React, { useContext, useEffect, useRef, useState } from 'react';
import CopyToClipboard from 'react-copy-to-clipboard';
import { Alert, Badge, Space, Tooltip } from 'antd';

import type { AntdPreviewerProps } from '.';
import useLocation from '../../../hooks/useLocation';
import BrowserFrame from '../../common/BrowserFrame';
Expand Down Expand Up @@ -63,6 +67,31 @@ function useShowRiddleButton() {
return showRiddleButton;
}

const useStyle = createStyles(({ token }) => {
const { borderRadius } = token;
return {
codeHideBtn: css`
width: 100%;
height: 40px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 0 0 ${borderRadius}px ${borderRadius}px;
border-top: 1px solid ${token.colorSplit};
color: ${token.colorTextSecondary};
transition: all 0.2s ease-in-out;
background-color: ${token.colorBgElevated};
cursor: pointer;
&:hover {
color: ${token.colorPrimary};
}
span {
margin-right: ${token.marginXXS}px;
}
`,
};
});

const CodePreviewer: React.FC<AntdPreviewerProps> = (props) => {
const {
asset,
Expand All @@ -86,6 +115,8 @@ const CodePreviewer: React.FC<AntdPreviewerProps> = (props) => {
const { pkg } = useSiteData();
const location = useLocation();

const { styles } = useStyle();

const entryCode = asset.dependencies['index.tsx'].value;
const showRiddleButton = useShowRiddleButton();

Expand Down Expand Up @@ -509,8 +540,12 @@ createRoot(document.getElementById('container')).render(<Demo />);
sourceCode={entryCode}
jsxCode={jsx}
styleCode={style}
onCodeTypeChange={(type) => setCodeType(type)}
onCodeTypeChange={setCodeType}
/>
<div tabIndex={0} className={styles.codeHideBtn} onClick={() => setCodeExpand(false)}>
<UpOutlined />
<FormattedMessage id="app.demo.code.hide.simplify" />
</div>
</section>
)}
</section>
Expand Down
22 changes: 8 additions & 14 deletions .dumi/theme/common/CodePreview.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { useEffect, useMemo } from 'react';
import Prism from 'prismjs';
import { Tabs } from 'antd';
import toReactElement from 'jsonml-to-react-element';
import JsonML from 'jsonml.js/lib/utils';
import { Tabs } from 'antd';
import Prism from 'prismjs';

const LANGS = {
tsx: 'TypeScript',
Expand All @@ -17,22 +17,16 @@ interface CodePreviewProps {
onCodeTypeChange?: (activeKey: string) => void;
}

function toReactComponent(jsonML: any) {
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
(node: any, index: number) => {
const attr = JsonML.getAttributes(node);
return React.createElement(
'pre',
{
key: index,
className: `language-${attr.lang}`,
},
React.createElement('code', {
dangerouslySetInnerHTML: { __html: attr.highlighted },
}),
return (
<pre key={index} className={`language-${attr.lang}`}>
<code dangerouslySetInnerHTML={{ __html: attr.highlighted }} />
</pre>
);
},
],
Expand Down
2 changes: 1 addition & 1 deletion .dumi/theme/common/styles/Demo.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { css, Global } from '@emotion/react';
import React from 'react';
import { css, Global } from '@emotion/react';
import { useTheme } from 'antd-style';

const GlobalDemoStyles: React.FC = () => {
Expand Down
1 change: 1 addition & 0 deletions .dumi/theme/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"app.demo.copied": "Copied!",
"app.demo.code.show": "Show code",
"app.demo.code.hide": "Hide code",
"app.demo.code.hide.simplify": "Hide",
"app.demo.codepen": "Open in CodePen",
"app.demo.codesandbox": "Open in CodeSandbox",
"app.demo.stackblitz": "Open in Stackblitz",
Expand Down
1 change: 1 addition & 0 deletions .dumi/theme/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"app.demo.copied": "复制成功",
"app.demo.code.show": "显示代码",
"app.demo.code.hide": "收起代码",
"app.demo.code.hide.simplify": "收起",
"app.demo.codepen": "在 CodePen 中打开",
"app.demo.codesandbox": "在 CodeSandbox 中打开",
"app.demo.stackblitz": "在 Stackblitz 中打开",
Expand Down

0 comments on commit 4d1bac4

Please sign in to comment.