Skip to content

Commit 1ee6026

Browse files
committed
Add fullscreen mode.
1 parent ea4aeb7 commit 1ee6026

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

src/components/CodeMirror/codemirror.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
:global {
33
.CodeMirror {
44
font-family: monospace;
5-
height: 300px;
5+
height: auto;
66
color: black;
77
direction: ltr;
88
}

src/components/ToolBarMode/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ export default class ToolBarMode extends React.PureComponent<IToolBarModeProps,
2121
public static defaultProps: IToolBarModeProps = {
2222
onClick: () => null,
2323
prefixCls: 'md-editor',
24-
// toolbarsMode: ['preview', 'fullscreen'],
25-
toolbarsMode: ['preview'],
24+
toolbarsMode: ['preview', 'fullscreen'],
2625
};
2726
constructor(props: IToolBarModeProps) {
2827
super(props);

src/index.less

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,19 @@
1111
&-content {
1212
position: relative;
1313
}
14+
&-fullscreen & {
15+
border-radius: 0;
16+
}
17+
&-fullscreen {
18+
z-index: 999;
19+
position: fixed;
20+
top: 0;
21+
bottom: 0;
22+
left: 0;
23+
right: 0;
24+
}
25+
&-fullscreen &-toolbar {
26+
border-radius: 0;
27+
}
1428
}
1529
}

src/index.tsx

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,30 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
3030
value: '',
3131
visble: true,
3232
};
33+
public container!: HTMLDivElement;
34+
public editorClientHeight!: number;
3335
public preview!: PreviewMarkdown;
3436
public toolbarsMode!: ToolBarMode;
3537
public CodeMirror!: CodeMirror;
3638
public render() {
3739
const { prefixCls, className, toolbars, toolbarsMode, onChange, visble, ...codemirrorProps } = this.props;
3840
return (
39-
<div className={classnames(prefixCls, className)}>
40-
<ToolBarMode ref={(mode: ToolBarMode) => this.toolbarsMode = mode} toolbarsMode={toolbarsMode} onClick={this.onClickMode} />
41-
<ToolBar toolbars={toolbars} onClick={this.onClick} />
42-
<div className={classnames(`${prefixCls}-content`)}>
43-
<CodeMirror
44-
ref={this.getInstance}
45-
{...codemirrorProps}
46-
onChange={this.onChange}
47-
/>
48-
<PreviewMarkdown
49-
visble={visble}
50-
ref={(pmd: PreviewMarkdown) => this.preview = pmd}
51-
value={this.props.value}
52-
/>
41+
<div ref={(node: HTMLDivElement) => this.container = node}>
42+
<div className={classnames(prefixCls, className)}>
43+
<ToolBarMode ref={(mode: ToolBarMode) => this.toolbarsMode = mode} toolbarsMode={toolbarsMode} onClick={this.onClickMode} />
44+
<ToolBar toolbars={toolbars} onClick={this.onClick} />
45+
<div className={classnames(`${prefixCls}-content`)}>
46+
<CodeMirror
47+
ref={this.getInstance}
48+
{...codemirrorProps}
49+
onChange={this.onChange}
50+
/>
51+
<PreviewMarkdown
52+
visble={visble}
53+
ref={(pmd: PreviewMarkdown) => this.preview = pmd}
54+
value={this.props.value}
55+
/>
56+
</div>
5357
</div>
5458
</div>
5559
);
@@ -59,6 +63,20 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
5963
if (this.preview) {
6064
this.props.visble ? this.preview.show() : this.preview.hide();
6165
this.CodeMirror.editor.setSize(this.props.visble ? '50%' : '100%');
66+
const { clientHeight } = this.CodeMirror.editor.getScrollInfo();
67+
this.editorClientHeight = clientHeight;
68+
window.addEventListener('resize', this.handleResize, true);
69+
}
70+
}
71+
72+
public componentWillUnmount() {
73+
window.removeEventListener('resize', this.handleResize, true);
74+
}
75+
76+
public handleResize = () => {
77+
const isfullscreen = this.toolbarsMode.state.fullscreen
78+
if (isfullscreen) {
79+
this.setEditorSize(isfullscreen);
6280
}
6381
}
6482

@@ -93,11 +111,21 @@ export default class MarkdownEditor extends React.PureComponent<IMarkdownEditor,
93111
}
94112

95113
private fullScreen() {
96-
if (this.toolbarsMode) {
97-
this.toolbarsMode.updateMode('fullscreen', !this.toolbarsMode.state.fullscreen);
114+
const { prefixCls } = this.props;
115+
if (this.toolbarsMode && this.container) {
116+
const isfullscreen = !this.toolbarsMode.state.fullscreen
117+
this.toolbarsMode.updateMode('fullscreen', isfullscreen);
118+
this.container.className = isfullscreen ? classnames(`${prefixCls}-fullscreen`) : '';
119+
document.body.style.overflow = isfullscreen ? 'hidden' : 'initial';
120+
this.setEditorSize(isfullscreen);
98121
}
99122
}
100123

124+
private setEditorSize(isfullscreen: boolean) {
125+
const clientHeight = document.body.clientHeight;
126+
this.CodeMirror.editor.setSize(this.preview.state.visble ? '50%' : '100%', isfullscreen ? clientHeight - 33 : this.editorClientHeight);
127+
}
128+
101129
private onClickMode = (type: string) => {
102130
if (type === 'preview') {
103131
this.previewMarkdown();

0 commit comments

Comments
 (0)