Skip to content

Commit ac33e63

Browse files
committed
Fix preview markdown errors.
1 parent 369908f commit ac33e63

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/components/PreviewMarkdown/index.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface IPreviewMarkdown extends IProps {
2121

2222
export interface IPreviewMarkdownState {
2323
value?: string;
24+
visble?: boolean;
2425
}
2526

2627
export default class PreviewMarkdown extends React.Component<IPreviewMarkdown, IPreviewMarkdownState> {
@@ -33,13 +34,28 @@ export default class PreviewMarkdown extends React.Component<IPreviewMarkdown, I
3334
super(props);
3435
this.state = {
3536
value: props.value,
37+
visble: props.visble,
3638
}
3739
}
3840

3941
public componentDidMount() {
4042
this.highlight();
4143
}
4244

45+
public show() {
46+
this.setState({ visble: true });
47+
}
48+
49+
public hide() {
50+
this.setState({ visble: false });
51+
}
52+
53+
public componentWillReceiveProps(nextProps: IPreviewMarkdown) {
54+
if (nextProps.visble !== this.props.visble) {
55+
this.setState({ visble: nextProps.visble });
56+
}
57+
}
58+
4359
public highlight() {
4460
const code = this.node.getElementsByTagName('code') as unknown as HTMLElement[];
4561
for (const value of code) {
@@ -62,7 +78,7 @@ export default class PreviewMarkdown extends React.Component<IPreviewMarkdown, I
6278
<div
6379
ref={(node: HTMLDivElement) => this.node = node}
6480
className={classnames(`${prefixCls}-preview`, {
65-
[`${prefixCls}-visble`]: visble
81+
[`${prefixCls}-visble`]: this.state.visble,
6682
})}
6783
{...elementProps}
6884
>

src/index.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface IMarkdownEditor extends IProps, ICodeMirror {
1111
prefixCls?: string,
1212
value?: string,
1313
height?: number,
14+
visble?: boolean,
1415
toolbars?: string[],
1516
options?: CodeMirror.EditorConfiguration,
1617
}
@@ -25,36 +26,45 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
2526
onChange: () => null,
2627
prefixCls: 'md-editor',
2728
value: '',
29+
visble: true,
2830
};
2931
public preview!: PreviewMarkdown;
3032
public CodeMirror!: CodeMirror;
31-
constructor(props: IMarkdownEditor) {
32-
super(props);
33-
this.state = {
34-
preview: true,
35-
}
36-
}
3733
public render() {
38-
const { prefixCls, className, toolbars, onChange, ...codemirrorProps } = this.props;
34+
const { prefixCls, className, toolbars, onChange, visble, ...codemirrorProps } = this.props;
3935
return (
4036
<div className={classnames(prefixCls, className)}>
4137
<ToolBar toolbars={toolbars} onClick={this.onClick} />
4238
<div className={classnames(`${prefixCls}-content`)}>
4339
<CodeMirror
44-
width={this.state.preview ? '50%' : '100%'}
4540
ref={this.getInstance}
4641
{...codemirrorProps}
4742
onChange={this.onChange}
4843
/>
4944
<PreviewMarkdown
45+
visble={visble}
5046
ref={(pmd: PreviewMarkdown) => this.preview = pmd}
5147
value={this.props.value}
52-
visble={this.state.preview}
5348
/>
5449
</div>
5550
</div>
5651
);
5752
}
53+
54+
public componentDidMount() {
55+
if (this.preview) {
56+
this.props.visble ? this.preview.show() : this.preview.hide();
57+
this.CodeMirror.editor.setSize(this.props.visble ? '50%' : '100%');
58+
}
59+
}
60+
61+
public componentWillReceiveProps(nextProps: IMarkdownEditor) {
62+
if (nextProps.visble !== this.props.visble) {
63+
nextProps.visble ? this.preview.show() : this.preview.hide();
64+
this.CodeMirror.editor.setSize(nextProps.visble ? '50%' : '100%');
65+
}
66+
}
67+
5868
public getInstance = (editor: CodeMirror) => {
5969
if (editor) {
6070
this.CodeMirror = editor;
@@ -70,7 +80,10 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
7080
}
7181
}
7282
private previewMarkdown() {
73-
this.setState({ preview: !this.state.preview });
83+
if (this.preview) {
84+
this.preview.state.visble ? this.preview.hide() : this.preview.show();
85+
this.CodeMirror.editor.setSize(this.preview.state.visble ? '100%' : '50%');
86+
}
7487
}
7588
private onClick = (type: string) => {
7689
if (type === 'preview') {

0 commit comments

Comments
 (0)