|
|
@ -6,9 +6,9 @@ import { |
|
|
|
} from '@ant-design/icons'; |
|
|
|
import type { Project } from '@stackblitz/sdk'; |
|
|
|
import stackblitzSdk from '@stackblitz/sdk'; |
|
|
|
import { Alert, Badge, Space, Tooltip } from 'antd'; |
|
|
|
import { Alert, Badge, ConfigProvider, Divider, Space, Tag, Tooltip } from 'antd'; |
|
|
|
import classNames from 'classnames'; |
|
|
|
import { FormattedMessage, useSiteData } from 'dumi'; |
|
|
|
import { FormattedMessage, Link, useSiteData } from 'dumi'; |
|
|
|
import toReactElement from 'jsonml-to-react-element'; |
|
|
|
import JsonML from 'jsonml.js/lib/utils'; |
|
|
|
import LZString from 'lz-string'; |
|
|
@ -26,11 +26,24 @@ import ExternalLinkIcon from '../../common/ExternalLinkIcon'; |
|
|
|
import RiddleIcon from '../../common/RiddleIcon'; |
|
|
|
import type { SiteContextProps } from '../../slots/SiteContext'; |
|
|
|
import SiteContext from '../../slots/SiteContext'; |
|
|
|
import { ping } from '../../utils'; |
|
|
|
import { getLocalizedPathname, ping } from '../../utils'; |
|
|
|
import type { AntdPreviewerProps } from '.'; |
|
|
|
import useSiteToken from '../../../hooks/useSiteToken'; |
|
|
|
import useLocale from '../../../hooks/useLocale'; |
|
|
|
|
|
|
|
const { ErrorBoundary } = Alert; |
|
|
|
|
|
|
|
const locales = { |
|
|
|
cn: { |
|
|
|
hoverToken: '鼠标悬浮以查看 Design Token 影响范围。', |
|
|
|
howToUseToken: '如何使用 Design Token', |
|
|
|
}, |
|
|
|
en: { |
|
|
|
hoverToken: 'Hover to see what is changed by Design Token.', |
|
|
|
howToUseToken: 'How to use Design Token', |
|
|
|
}, |
|
|
|
}; |
|
|
|
|
|
|
|
function toReactComponent(jsonML: any) { |
|
|
|
return toReactElement(jsonML, [ |
|
|
|
[ |
|
|
@ -105,10 +118,14 @@ const CodePreviewer: React.FC<AntdPreviewerProps> = (props) => { |
|
|
|
filename, |
|
|
|
version, |
|
|
|
clientOnly, |
|
|
|
tokens, |
|
|
|
showToken, |
|
|
|
} = props; |
|
|
|
|
|
|
|
const { pkg } = useSiteData(); |
|
|
|
const location = useLocation(); |
|
|
|
const { token } = useSiteToken(); |
|
|
|
const [locale, lang] = useLocale(locales); |
|
|
|
|
|
|
|
const entryCode = asset.dependencies['index.tsx'].value; |
|
|
|
const showRiddleButton = useShowRiddleButton(); |
|
|
@ -129,6 +146,8 @@ const CodePreviewer: React.FC<AntdPreviewerProps> = (props) => { |
|
|
|
|
|
|
|
const [showOnlineUrl, setShowOnlineUrl] = useState<boolean>(false); |
|
|
|
|
|
|
|
const hasToken = !!tokens?.split(',').length; |
|
|
|
|
|
|
|
const highlightedCodes = { |
|
|
|
jsx: Prism.highlight(jsx, Prism.languages.javascript, 'jsx'), |
|
|
|
tsx: Prism.highlight(entryCode, Prism.languages.javascript, 'jsx'), |
|
|
@ -384,11 +403,51 @@ createRoot(document.getElementById('container')).render(<Demo />); |
|
|
|
backgroundColor: background === 'grey' ? backgroundGrey : undefined, |
|
|
|
}; |
|
|
|
|
|
|
|
const [activeToken, setActiveToken] = useState<string>(); |
|
|
|
const onMouseEnterToken = (target: string) => { |
|
|
|
setActiveToken(target); |
|
|
|
}; |
|
|
|
|
|
|
|
const onMouseLeaveToken = () => { |
|
|
|
setActiveToken(undefined); |
|
|
|
}; |
|
|
|
|
|
|
|
const codeBox: React.ReactNode = ( |
|
|
|
<section className={codeBoxClass} id={asset.id}> |
|
|
|
<section className="code-box-demo" style={codeBoxDemoStyle}> |
|
|
|
<ErrorBoundary> |
|
|
|
<React.StrictMode>{liveDemo.current}</React.StrictMode> |
|
|
|
<React.StrictMode> |
|
|
|
<ConfigProvider |
|
|
|
theme={ |
|
|
|
activeToken && activeToken.startsWith('color') |
|
|
|
? { token: { [activeToken]: token.colorWarning } } |
|
|
|
: undefined |
|
|
|
} |
|
|
|
> |
|
|
|
{liveDemo.current} |
|
|
|
</ConfigProvider> |
|
|
|
{showToken && hasToken && ( |
|
|
|
<> |
|
|
|
<Divider /> |
|
|
|
<div style={{ color: token.colorTextDescription, marginBottom: 12 }}> |
|
|
|
{locale.hoverToken} |
|
|
|
<Link to={getLocalizedPathname('/docs/react/customize-theme', lang === 'cn')}> |
|
|
|
{locale.howToUseToken} |
|
|
|
</Link> |
|
|
|
</div> |
|
|
|
{tokens?.split(',').map((item) => ( |
|
|
|
<Tag |
|
|
|
style={{ cursor: 'default' }} |
|
|
|
key={item} |
|
|
|
onMouseEnter={() => onMouseEnterToken(item.trim())} |
|
|
|
onMouseLeave={onMouseLeaveToken} |
|
|
|
> |
|
|
|
{item.trim()} |
|
|
|
</Tag> |
|
|
|
))} |
|
|
|
</> |
|
|
|
)} |
|
|
|
</React.StrictMode> |
|
|
|
</ErrorBoundary> |
|
|
|
{style ? <style dangerouslySetInnerHTML={{ __html: style }} /> : null} |
|
|
|
</section> |
|
|
|