Skip to content

Commit

Permalink
site: sync all code type of demos when change one of them
Browse files Browse the repository at this point in the history
  • Loading branch information
gin-lsl committed Apr 13, 2024
1 parent 7f990bb commit e51d9f8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
4 changes: 1 addition & 3 deletions .dumi/theme/builtins/Previewer/CodePreviewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ const CodePreviewer: React.FC<AntdPreviewerProps> = (props) => {
clientOnly,
pkgDependencyList,
} = props;
const { showDebug } = useContext(DemoContext);
const { showDebug, codeType } = useContext(DemoContext);

const { pkg } = useSiteData();
const location = useLocation();
Expand All @@ -134,7 +134,6 @@ const CodePreviewer: React.FC<AntdPreviewerProps> = (props) => {
const riddleIconRef = useRef<HTMLFormElement>(null);
const codepenIconRef = useRef<HTMLFormElement>(null);
const [codeExpand, setCodeExpand] = useState<boolean>(false);
const [codeType, setCodeType] = useState<string>('tsx');
const { theme } = useContext<SiteContextProps>(SiteContext);

const { hash, pathname, search } = location;
Expand Down Expand Up @@ -541,7 +540,6 @@ createRoot(document.getElementById('container')).render(<Demo />);
styleCode={style}
error={liveDemoError}
entryName={entryName}
onCodeTypeChange={setCodeType}
onSourceChange={setLiveDemoSource}
/>
<div
Expand Down
16 changes: 12 additions & 4 deletions .dumi/theme/common/CodePreview.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { ComponentProps } from 'react';
import React, { useEffect, useMemo } from 'react';
import React, { useContext, useEffect, useMemo } from 'react';
import { Button, Tabs, Typography } from 'antd';
import { createStyles } from 'antd-style';
import toReactElement from 'jsonml-to-react-element';
import JsonML from 'jsonml.js/lib/utils';
import Prism from 'prismjs';

import DemoContext from '../slots/DemoContext';
import LiveCode from './LiveCode';

const useStyle = createStyles(({ token, css }) => {
Expand Down Expand Up @@ -67,7 +68,6 @@ interface CodePreviewProps
jsxCode?: string;
styleCode?: string;
entryName: string;
onCodeTypeChange?: (activeKey: string) => void;
onSourceChange?: (source: Record<string, string>) => void;
}

Expand All @@ -92,7 +92,6 @@ const CodePreview: React.FC<CodePreviewProps> = ({
jsxCode = '',
styleCode = '',
entryName,
onCodeTypeChange,
onSourceChange,
error,
}) => {
Expand All @@ -108,6 +107,7 @@ const CodePreview: React.FC<CodePreviewProps> = ({
initialCodes.style = '';
}
const [highlightedCodes, setHighlightedCodes] = React.useState(initialCodes);
const { codeType, setCodeType } = useContext(DemoContext);
const sourceCodes = {
// omit trailing line break
tsx: sourceCode?.trim(),
Expand Down Expand Up @@ -178,7 +178,15 @@ const CodePreview: React.FC<CodePreviewProps> = ({
);
}

return <Tabs centered className="highlight" onChange={onCodeTypeChange} items={items} />;
return (
<Tabs
centered
className="highlight"
activeKey={codeType}
onChange={setCodeType}
items={items}
/>
);
};

export default CodePreview;
15 changes: 12 additions & 3 deletions .dumi/theme/slots/Content/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useContext, useLayoutEffect, useMemo } from 'react';
import React, { useContext, useEffect, useLayoutEffect, useMemo, useState } from 'react';
import { Col, Flex, Typography } from 'antd';
import { createStyles } from 'antd-style';
import classNames from 'classnames';
Expand All @@ -21,6 +21,8 @@ const PrevAndNext = React.lazy(() => import('../../common/PrevAndNext'));
const ComponentChangelog = React.lazy(() => import('../../common/ComponentChangelog'));
const EditButton = React.lazy(() => import('../../common/EditButton'));

const DEFAULT_DEMOS_CODE_TYPE = 'default-demos-code-type';

const useStyle = createStyles(({ token, css }) => ({
articleWrapper: css`
padding: 0 170px 32px 64px;
Expand All @@ -43,6 +45,9 @@ const Content: React.FC<React.PropsWithChildren> = ({ children }) => {
const { styles } = useStyle();

const [showDebug, setShowDebug] = useLayoutState(false);
const [codeType, setCodeType] = useState<string>(
() => localStorage.getItem(DEFAULT_DEMOS_CODE_TYPE) || 'tsx',
);
const debugDemos = useMemo(
() => meta.toc?.filter((item) => item._debug_demo).map((item) => item.id) || [],
[meta],
Expand All @@ -55,10 +60,14 @@ const Content: React.FC<React.PropsWithChildren> = ({ children }) => {
}, []);

const contextValue = useMemo<DemoContextProps>(
() => ({ showDebug, setShowDebug }),
[showDebug, debugDemos],
() => ({ showDebug, setShowDebug, codeType, setCodeType }),
[showDebug, codeType, debugDemos],
);

useEffect(() => {
localStorage.setItem(DEFAULT_DEMOS_CODE_TYPE, codeType);
}, [codeType]);

const isRTL = direction === 'rtl';

return (
Expand Down
3 changes: 3 additions & 0 deletions .dumi/theme/slots/DemoContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { createContext } from 'react';

export type DemoContextProps = {
showDebug?: boolean;
codeType?: string;
};

const DemoContext = createContext<{
showDebug?: boolean;
setShowDebug?: (showDebug: boolean) => void;
codeType?: string;
setCodeType?: (codeType: string) => void;
}>({});

export default DemoContext;

0 comments on commit e51d9f8

Please sign in to comment.