Skip to content

refactor: fixed up(#235) and adding docs to the sourceState prop #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 28, 2024
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ interface CodePreviewProps extends SplitProps {
* @default light
*/
theme?: ReactCodeMirrorProps['theme'];
/**
* Specifies the initial state of the source panel.
*/
sourceState?: 'hidden' | 'shown';
}
```

Expand Down
11 changes: 10 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
noPreview = false,
noScroll = false,
bgWhite = false,
sourceState,
...otherProps
} = props;
const {
Expand Down Expand Up @@ -183,6 +184,13 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
setWidth(width === 1 ? '50%' : 1);
setShowEdit(true);
};
// 通过状态props属性判断是否切换源码
const isShown = sourceState === 'shown';
useEffect(() => {
setWidth(isShown ? '50%' : 1);
setShowEdit(isShown);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isShown]);
const onCopyCode = () => {
copyTextToClipboard(code || '', (isCopy) => setCopied(isCopy));
setTimeout(() => setCopied(false), 2000);
Expand All @@ -196,7 +204,7 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
};
return (
<Split data-color-mode={theme} visiable={visiable} className={cls} style={{ flex: 1, ...style }} {...otherProps}>
{!noPreview && !onlyEdit && (
{!onlyEdit && (
<div
className={[
`${prefixCls}-demo`,
Expand All @@ -208,6 +216,7 @@ const CodePreview = React.forwardRef<CodePreviewRef, CodePreviewProps>((props, r
.trim()}
style={{
flex: 1,
display: !noPreview ? 'unset' : 'none',
...(width === 1 ? { width: '100%' } : {}),
}}
>
Expand Down
5 changes: 2 additions & 3 deletions src/useCodePreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ export const getReactDOMClient = () => {
};

export function useCodePreview(props: CodePreviewProps) {
const isShowEdit = props.sourceState === 'shown';
const [demoDom, setDemoDom] = useState<HTMLDivElement>();
const playerId = useRef(`${parseInt(String(Math.random() * 1e9), 10).toString(36)}`);
const [fullScreen, setFullScreen] = useState(false);
const [errorMessage, setErrorMessage] = useState('');
const [showEdit, setShowEdit] = useState(isShowEdit);
const [width, setWidth] = useState<number | string>(isShowEdit ? '50%' : 1);
const [showEdit, setShowEdit] = useState(false);
const [width, setWidth] = useState<number | string>(1);
const [copied, setCopied] = useState(false);
const [code, setCode] = useState(props.code || '');

Expand Down
6 changes: 6 additions & 0 deletions website/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Example = () => {
noScroll: false,
noPreview: false,
bordered: true,
sourceState: false,
codeSandbox: {
files: {
'sandbox.config.json': {
Expand Down Expand Up @@ -105,6 +106,7 @@ const Example = () => {
noScroll={state.noScroll}
bgWhite={state.bgWhite}
noCode={state.noCode}
sourceState={state.sourceState ? 'shown' : 'hidden'}
editProps={{
onChange: (value) => {
setState({
Expand Down Expand Up @@ -166,6 +168,10 @@ const Example = () => {
是否显示滚动条 `noScroll={state.noScroll.toString()}`
</Switch>
<br />
<Switch checked={state.sourceState} onChange={handleChange.bind(this, 'sourceState')}>
是否显示源码 `sourceState={state.sourceState ? 'shown' : 'hidden'}`
</Switch>
<br />
<Switch checked={state.bordered} onChange={handleChange.bind(this, 'bordered')}>
是否显示边框 `bordered={state.bordered.toString()}`
</Switch>
Expand Down