Skip to content

Commit 54687f6

Browse files
cbschuldjaywcjlove
authored andcommitted
added onBlur, updated spelling errors on visible, readability update, updated docs
1 parent 09e1fb2 commit 54687f6

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ ReactDOM.render(
9393
## Props
9494

9595
- value (*string*) - the raw markdown that will be converted to html (**required**)
96-
- `visble?:boolean` - Shows a preview that will be converted to html.
96+
- `visible?:boolean` - Shows a preview that will be converted to html.
9797
- `toolbars?:array` - Tool display settings.
9898
- `toolbarsMode?:array` - Tool display settings.
9999
- onChange (*function(editor: IInstance, data: CodeMirror.EditorChange, value: string)*) - called when a change is made (**required**)
100+
- onBlur? (*function(editor: IInstance, event: Event) - event occurs when an object loses focus
100101
- `previewProps` - [react-markdown options](https://github.com/rexxars/react-markdown#options)
101102

102103
> [Other Props Options](https://github.com/uiwjs/react-markdown-editor/blob/8de6abbf628b6d272d7da1c28e985fbbcba71b93/src/components/CodeMirror/index.tsx#L21-L60)

src/components/PreviewMarkdown/index.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
top: 0;
1111
bottom: 0;
1212
}
13-
&-visble {
13+
&-visible {
1414
border-left: 1px solid #dfdfe0;
1515
overflow: auto;
1616
width: 50%;

src/components/PreviewMarkdown/index.tsx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ hljs.configure({
1313
});
1414

1515
export interface IPreviewMarkdown extends IProps {
16-
visble: boolean;
16+
visible: boolean;
1717
previewProps?: ReactMarkdownProps;
1818
value?: string;
1919
prefixCls?: string;
@@ -22,20 +22,20 @@ export interface IPreviewMarkdown extends IProps {
2222

2323
export interface IPreviewMarkdownState {
2424
value?: string;
25-
visble?: boolean;
25+
visible?: boolean;
2626
}
2727

2828
export default class PreviewMarkdown extends React.Component<IPreviewMarkdown, IPreviewMarkdownState> {
2929
public static defaultProps: IPreviewMarkdown = {
3030
prefixCls: 'md-editor',
31-
visble: true,
31+
visible: true,
3232
}
3333
public node!: HTMLDivElement;
3434
constructor(props: IPreviewMarkdown) {
3535
super(props);
3636
this.state = {
3737
value: props.value,
38-
visble: props.visble,
38+
visible: props.visible,
3939
}
4040
}
4141

@@ -44,16 +44,16 @@ export default class PreviewMarkdown extends React.Component<IPreviewMarkdown, I
4444
}
4545

4646
public show() {
47-
this.setState({ visble: true });
47+
this.setState({ visible: true });
4848
}
4949

5050
public hide() {
51-
this.setState({ visble: false });
51+
this.setState({ visible: false });
5252
}
5353

5454
public componentWillReceiveProps(nextProps: IPreviewMarkdown) {
55-
if (nextProps.visble !== this.props.visble) {
56-
this.setState({ visble: nextProps.visble });
55+
if (nextProps.visible !== this.props.visible) {
56+
this.setState({ visible: nextProps.visible });
5757
}
5858
}
5959

@@ -74,12 +74,12 @@ export default class PreviewMarkdown extends React.Component<IPreviewMarkdown, I
7474
}
7575

7676
public render() {
77-
const { prefixCls, visble, value, previewProps, ...elementProps } = this.props;
77+
const { prefixCls, visible, value, previewProps, ...elementProps } = this.props;
7878
return (
7979
<div
8080
ref={(node: HTMLDivElement) => this.node = node}
8181
className={classnames(`${prefixCls}-preview`, {
82-
[`${prefixCls}-visble`]: this.state.visble,
82+
[`${prefixCls}-visible`]: this.state.visible,
8383
})}
8484
{...elementProps}
8585
>

src/index.tsx

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export interface IMarkdownEditor extends IProps, ICodeMirror {
1212
prefixCls?: string,
1313
value?: string,
1414
height?: number,
15-
visble?: boolean,
16-
visbleEditor?: boolean,
15+
visible?: boolean,
16+
visibleEditor?: boolean,
1717
toolbars?: string[] | false,
1818
toolbarsMode?: IToolBarModeProps['toolbarsMode'] | false,
1919
previewProps?: IPreviewMarkdown['previewProps'];
@@ -26,34 +26,36 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
2626
public static displayName = 'MarkdownEditor';
2727
public static defaultProps: IMarkdownEditor = {
2828
onChange: () => null,
29+
onBlur: () => null,
2930
prefixCls: 'md-editor',
3031
value: '',
31-
visbleEditor: true,
32-
visble: true,
32+
visibleEditor: true,
33+
visible: true,
3334
};
3435
public container!: HTMLDivElement;
3536
public editorClientHeight!: number;
3637
public preview!: PreviewMarkdown;
3738
public toolbarsMode!: ToolBarMode;
3839
public CodeMirror!: CodeMirror;
3940
public render() {
40-
const { prefixCls, className, toolbars, toolbarsMode, onChange, visble, visbleEditor, previewProps, ...codemirrorProps } = this.props;
41+
const { prefixCls, className, toolbars, toolbarsMode, onChange, onBlur, visible: visible, visibleEditor: visibleEditor, previewProps, ...codemirrorProps } = this.props;
4142
return (
4243
<div ref={(node: HTMLDivElement) => this.container = node}>
4344
<div className={classnames(prefixCls, className)}>
4445
<ToolBarMode ref={(mode: ToolBarMode) => this.toolbarsMode = mode} toolbarsMode={toolbarsMode} onClick={this.onClickMode} />
4546
<ToolBar toolbars={toolbars} onClick={this.onClick} />
4647
<div className={classnames(`${prefixCls}-content`)}>
47-
{visbleEditor && (
48+
{visibleEditor && (
4849
<CodeMirror
49-
visbleEditor={visbleEditor}
50+
visibleEditor={visibleEditor}
5051
ref={this.getInstance}
5152
{...codemirrorProps}
5253
onChange={this.onChange}
54+
onBlur={this.onBlur}
5355
/>
5456
)}
5557
<PreviewMarkdown
56-
visble={visble}
58+
visible={visible}
5759
ref={(pmd: PreviewMarkdown) => this.preview = pmd}
5860
value={this.props.value}
5961
previewProps={previewProps}
@@ -66,8 +68,8 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
6668

6769
public componentDidMount() {
6870
if (this.preview && this.CodeMirror) {
69-
this.props.visble ? this.preview.show() : this.preview.hide();
70-
this.CodeMirror.editor.setSize(this.props.visble ? '50%' : '100%');
71+
this.props.visible ? this.preview.show() : this.preview.hide();
72+
this.CodeMirror.editor.setSize(this.props.visible ? '50%' : '100%');
7173
const { clientHeight } = this.CodeMirror.editor.getScrollInfo();
7274
this.editorClientHeight = clientHeight;
7375
window.addEventListener('resize', this.handleResize, true);
@@ -79,16 +81,16 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
7981
}
8082

8183
public handleResize = () => {
82-
const isfullscreen = this.toolbarsMode.state.fullscreen
83-
if (isfullscreen) {
84-
this.setEditorSize(isfullscreen);
84+
const isFullScreen = this.toolbarsMode.state.fullscreen
85+
if (isFullScreen) {
86+
this.setEditorSize(isFullScreen);
8587
}
8688
}
8789

8890
public componentWillReceiveProps(nextProps: IMarkdownEditor) {
89-
if (nextProps.visble !== this.props.visble) {
90-
nextProps.visble ? this.preview.show() : this.preview.hide();
91-
this.CodeMirror.editor.setSize(nextProps.visble ? '50%' : '100%');
91+
if (nextProps.visible !== this.props.visible) {
92+
nextProps.visible ? this.preview.show() : this.preview.hide();
93+
this.CodeMirror.editor.setSize(nextProps.visible ? '50%' : '100%');
9294
}
9395
}
9496

@@ -107,28 +109,35 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
107109
}
108110
}
109111

112+
private onBlur = (editor: IInstance, event: Event) => {
113+
const { onBlur } = this.props as IMarkdownEditor;
114+
if (onBlur) {
115+
onBlur(editor, event);
116+
}
117+
}
118+
110119
private previewMarkdown() {
111120
if (this.preview) {
112-
this.preview.state.visble ? this.preview.hide() : this.preview.show();
113-
this.toolbarsMode.updateMode('preview', !this.preview.state.visble);
114-
this.CodeMirror.editor.setSize(this.preview.state.visble ? '100%' : '50%');
121+
this.preview.state.visible ? this.preview.hide() : this.preview.show();
122+
this.toolbarsMode.updateMode('preview', !this.preview.state.visible);
123+
this.CodeMirror.editor.setSize(this.preview.state.visible ? '100%' : '50%');
115124
}
116125
}
117126

118127
private fullScreen() {
119128
const { prefixCls } = this.props;
120129
if (this.toolbarsMode && this.container) {
121-
const isfullscreen = !this.toolbarsMode.state.fullscreen
122-
this.toolbarsMode.updateMode('fullscreen', isfullscreen);
123-
this.container.className = isfullscreen ? classnames(`${prefixCls}-fullscreen`) : '';
124-
document.body.style.overflow = isfullscreen ? 'hidden' : 'initial';
125-
this.setEditorSize(isfullscreen);
130+
const isFullScreen = !this.toolbarsMode.state.fullscreen
131+
this.toolbarsMode.updateMode('fullscreen', isFullScreen);
132+
this.container.className = isFullScreen ? classnames(`${prefixCls}-fullscreen`) : '';
133+
document.body.style.overflow = isFullScreen ? 'hidden' : 'initial';
134+
this.setEditorSize(isFullScreen);
126135
}
127136
}
128137

129-
private setEditorSize(isfullscreen: boolean) {
138+
private setEditorSize(isFullScreen: boolean) {
130139
const clientHeight = document.body.clientHeight;
131-
this.CodeMirror.editor.setSize(this.preview.state.visble ? '50%' : '100%', isfullscreen ? clientHeight - 33 : this.editorClientHeight);
140+
this.CodeMirror.editor.setSize(this.preview.state.visible ? '50%' : '100%', isFullScreen ? clientHeight - 33 : this.editorClientHeight);
132141
}
133142

134143
private onClickMode = (type: string) => {

0 commit comments

Comments
 (0)