Skip to content

Commit 92ae9f0

Browse files
committed
feat: Add a default height.
1 parent b087114 commit 92ae9f0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/index.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export interface ICodePreviewState {
5757

5858
export default class CodePreview extends React.PureComponent<ICodePreviewProps, ICodePreviewState> {
5959
public demoDom = React.createRef<HTMLDivElement>();
60+
public editor = React.createRef<CodeMirror>();
61+
public initHeight: number = 3;
6062
public playerId: string = `${parseInt(String(Math.random() * 1e9), 10).toString(36)}`;
6163
public static defaultProps: ICodePreviewProps = {
6264
prefixCls: 'w-code-preview',
@@ -136,14 +138,26 @@ export default class CodePreview extends React.PureComponent<ICodePreviewProps,
136138
}
137139
});
138140
}
141+
initOldHeight() {
142+
const demo = this.demoDom.current;
143+
if (this.initHeight === 3 && demo) {
144+
this.initHeight = demo.clientHeight;
145+
}
146+
}
139147
/**
140148
* onSwitchSource
141149
*/
142150
public onSwitchSource() {
143151
const { width } = this.state;
152+
this.initOldHeight();
144153
this.setState({
145154
width: width === 1 ? '50%' : 1,
146155
showEdit: true,
156+
}, () => {
157+
const editor = this.editor.current;
158+
if (editor) {
159+
editor.editor.setSize('100%', width !== 1 ? this.initHeight : '100%');
160+
}
147161
});
148162
}
149163
public render() {
@@ -187,6 +201,7 @@ export default class CodePreview extends React.PureComponent<ICodePreviewProps,
187201
{this.state.showEdit && (
188202
<CodeMirror
189203
value={code}
204+
ref={this.editor}
190205
onChange={(editor) => {
191206
this.executeCode(editor.getValue());
192207
}}

website/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const code = `import { Button, Divider, Icon } from 'uiw';
1111
ReactDOM.render(
1212
<div>
1313
<Button type="primary">主要按钮</Button>
14-
<Button type="dark">暗按钮</Button><br /><br /><br /><br /><br /><br />
14+
<Button type="success">成功按钮</Button>
15+
<Button type="warning">警告按钮</Button>
16+
<Button type="danger">错误按钮</Button>
17+
<Button type="light">亮按钮</Button>
1518
<Button type="dark">暗按钮</Button>
1619
</div>,
1720
_mount_

0 commit comments

Comments
 (0)