From e4dd7096aff2085026911611a6c40964ec7e8246 Mon Sep 17 00:00:00 2001 From: lijianan <574980606@qq.com> Date: Thu, 5 Jan 2023 20:10:03 +0800 Subject: [PATCH] site: refactor CC => FC (#40043) * fix * fix * fix --- .dumi/theme/builtins/Previewer/index.tsx | 858 +++++++++++------------ 1 file changed, 399 insertions(+), 459 deletions(-) diff --git a/.dumi/theme/builtins/Previewer/index.tsx b/.dumi/theme/builtins/Previewer/index.tsx index 6d09440601..bf3b471c42 100644 --- a/.dumi/theme/builtins/Previewer/index.tsx +++ b/.dumi/theme/builtins/Previewer/index.tsx @@ -4,7 +4,7 @@ import type { Project } from '@stackblitz/sdk'; import { Alert, Badge, Tooltip, Space } from 'antd'; import classNames from 'classnames'; import LZString from 'lz-string'; -import React from 'react'; +import React, { useContext, useEffect, useRef, useState } from 'react'; import CopyToClipboard from 'react-copy-to-clipboard'; import ReactDOM from 'react-dom'; import { FormattedMessage } from 'dumi'; @@ -29,500 +29,440 @@ function compress(string: string): string { .replace(/=+$/, ''); // Remove ending '=' } +const track = ({ type, demo }: { type: string; demo: string }) => { + if (!window.gtag) { + return; + } + window.gtag('event', 'demo', { event_category: type, event_label: demo }); +}; + interface DemoProps { meta: any; + intl: any; + utils?: any; src: string; content: string; - preview: (react: typeof React, reactDOM: typeof ReactDOM) => React.ReactNode; highlightedCodes: Record; style: string; highlightedStyle: string; expand: boolean; - intl: any; sourceCodes: Record<'jsx' | 'tsx', string>; location: Location; showRiddleButton: boolean; - utils?: any; + preview: (react: typeof React, reactDOM: typeof ReactDOM) => React.ReactNode; } -interface DemoState { - codeType?: string; - copied?: boolean; - codeExpand?: boolean; - copyTooltipOpen?: boolean | string; -} +const Demo: React.FC = (props) => { + const { + location, + sourceCodes, + meta, + src, + utils, + content, + highlightedCodes, + style, + highlightedStyle, + expand, + intl: { locale }, + showRiddleButton, + preview, + } = props; -class Demo extends React.Component { - static contextType = SiteContext; + const liveDemo = useRef(null); + const anchorRef = useRef(null); + const codeSandboxIconRef = useRef(null); + const riddleIconRef = useRef(null); + const codepenIconRef = useRef(null); + const [codeExpand, setCodeExpand] = useState(false); + const [copyTooltipOpen, setCopyTooltipOpen] = useState(false); + const [copied, setCopied] = useState(false); + const [codeType, setCodeType] = useState('tsx'); - declare context: SiteContextProps; + const { theme } = useContext(SiteContext); - liveDemo: any; - - iframeRef = React.createRef(); - - anchorRef = React.createRef(); - - codeSandboxIconRef = React.createRef(); - - riddleIconRef = React.createRef(); - - codepenIconRef = React.createRef(); - - state: DemoState = { - codeExpand: false, - copied: false, - copyTooltipOpen: false, - codeType: 'tsx', + const handleCodeExpand = (demo: string) => { + setCodeExpand(!codeExpand); + track({ type: 'expand', demo }); }; - componentDidMount() { - const { meta, location } = this.props; - if (meta.id === location.hash.slice(1)) { - this.anchorRef.current?.click(); - } - } - - shouldComponentUpdate(nextProps: DemoProps, nextState: DemoState) { - const { codeExpand, copied, copyTooltipOpen, codeType } = this.state; - const { expand, showRiddleButton } = this.props; - return ( - (codeExpand || expand) !== (nextState.codeExpand || nextProps.expand) || - copied !== nextState.copied || - copyTooltipOpen !== nextState.copyTooltipOpen || - codeType !== nextState.copyTooltipOpen || - nextProps.showRiddleButton !== showRiddleButton - ); - } - - getSourceCode = () => { - const { sourceCodes } = this.props; - return [sourceCodes.jsx, sourceCodes.tsx]; + const handleCodeCopied = (demo: string) => { + setCopied(true); + track({ type: 'copy', demo }); }; - handleCodeExpand = (demo: string) => { - const { codeExpand } = this.state; - this.setState({ codeExpand: !codeExpand }); - this.track({ type: 'expand', demo }); - }; - - handleCodeCopied = (demo: string) => { - this.setState({ copied: true }); - this.track({ type: 'copy', demo }); - }; - - onCopyTooltipOpenChange = (open: boolean) => { + const onCopyTooltipOpenChange = (open: boolean) => { + setCopyTooltipOpen(open); if (open) { - this.setState({ copyTooltipOpen: open, copied: false }); - return; + setCopied(false); } - this.setState({ copyTooltipOpen: open }); }; - track = ({ type, demo }: { type: string; demo: string }) => { - if (!window.gtag) { - return; + useEffect(() => { + if (meta.id === location.hash.slice(1)) { + anchorRef.current?.click(); } - window.gtag('event', 'demo', { - event_category: type, - event_label: demo, - }); - }; + }, []); - render() { - const { state } = this; - const { props } = this; - const site = this.context; - const { - meta, - src, - content, - preview, - highlightedCodes, - style, - highlightedStyle, - expand, - intl: { locale }, - showRiddleButton, - } = props; - const { copied, copyTooltipOpen, codeType } = state; - if (!this.liveDemo) { - this.liveDemo = meta.iframe ? ( - -