Skip to content

Commit

Permalink
refactor: fixed up(#235) and adding docs to the sourceState prop (#237
Browse files Browse the repository at this point in the history
)

* fix: fixed up ts error on `/website`

* feat(deps): 增加@types react|react-dom to ^18.2.0 version

* feat: Add an additional 'sourceState' props attribute to the component.

* fix: fixed up eslint error

* fix: Option 'emitDeclarationOnly' cannot be specified with option 'noEmit'.

* refactor: fixed up(#235) and adding docs to the `sourceState` prop
  • Loading branch information
LonelyFellas committed Mar 28, 2024
1 parent ce93a5e commit e296f83
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
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
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
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
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

0 comments on commit e296f83

Please sign in to comment.