1
- import React , { useState , useRef , useImperativeHandle } from 'react' ;
1
+ import React , { useState , useRef , useImperativeHandle , Fragment } from 'react' ;
2
2
import { markdown , markdownLanguage } from '@codemirror/lang-markdown' ;
3
3
import { languages } from '@codemirror/language-data' ;
4
4
import { EditorView , ViewUpdate } from '@codemirror/view' ;
@@ -28,16 +28,18 @@ export interface IMarkdownEditor extends ReactCodeMirrorProps {
28
28
/** Shows a preview that will be converted to html. */
29
29
visible ?: boolean ;
30
30
visibleEditor ?: boolean ;
31
- /** Option to hide the tool bar. */
32
- hideToolbar ?: boolean ;
33
31
/** Override the default preview component */
34
32
renderPreview ?: ( props : MarkdownPreviewProps , initVisible : boolean ) => React . ReactNode ;
35
33
/** Preview expanded width @default `50%` */
36
34
previewWidth ?: string ;
37
35
/** Tool display settings. */
38
36
toolbars ?: IToolBarProps [ 'toolbars' ] ;
39
- /** Tool display settings. */
37
+ /** The tool on the right shows the settings. */
40
38
toolbarsMode ?: IToolBarProps [ 'toolbars' ] ;
39
+ /** Toolbar on bottom */
40
+ toolbarBottom ?: boolean ;
41
+ /** Option to hide the tool bar. */
42
+ hideToolbar ?: boolean ;
41
43
/** [@uiw/react-markdown-preview](https://github.com/uiwjs/react-markdown-preview#options-props) options */
42
44
previewProps ?: MarkdownPreviewProps ;
43
45
/** replace the default `extensions` */
@@ -60,6 +62,7 @@ export interface MarkdownEditorRef {
60
62
const MarkdownEditor : MarkdownEditorComponent = React . forwardRef < MarkdownEditorRef , IMarkdownEditor > (
61
63
MarkdownEditorInternal ,
62
64
) as unknown as MarkdownEditorComponent ;
65
+
63
66
type MarkdownEditorComponent = React . FC < React . PropsWithRef < IMarkdownEditor > > & {
64
67
Markdown : typeof MarkdownPreview ;
65
68
} ;
@@ -82,6 +85,7 @@ function MarkdownEditorInternal(
82
85
renderPreview,
83
86
visibleEditor = true ,
84
87
hideToolbar = true ,
88
+ toolbarBottom = false ,
85
89
previewProps = { } ,
86
90
extensions = [ ] ,
87
91
previewWidth = '50%' ,
@@ -121,35 +125,56 @@ function MarkdownEditorInternal(
121
125
setValue ( value ) ;
122
126
onChange && onChange ( value , viewUpdate ) ;
123
127
} ;
128
+ const conentView = (
129
+ < div className = { `${ prefixCls } -content` } style = { { height : codemirrorProps . height } } >
130
+ < div className = { `${ prefixCls } -content-editor` } ref = { containerEditor } >
131
+ { visibleEditor && (
132
+ < CodeMirror
133
+ theme = { defaultTheme }
134
+ { ...codemirrorProps }
135
+ extensions = { extensionsData }
136
+ height = { height }
137
+ ref = { codeMirror }
138
+ onChange = { handleChange }
139
+ />
140
+ ) }
141
+ </ div >
142
+ < div className = { clsPreview } ref = { preview } >
143
+ { renderPreview ? (
144
+ renderPreview ( previewProps , ! ! visible )
145
+ ) : (
146
+ < MarkdownPreview { ...previewProps } data-visible = { ! ! visible } />
147
+ ) }
148
+ </ div >
149
+ </ div >
150
+ ) ;
151
+
152
+ const clsToolbar = [
153
+ prefixCls && `${ prefixCls } -toolbar-warp` ,
154
+ prefixCls && toolbarBottom && `${ prefixCls } -toolbar-bottom` ,
155
+ ]
156
+ . filter ( Boolean )
157
+ . join ( ' ' ) ;
158
+ const toolbarView = hideToolbar && (
159
+ < div className = { clsToolbar } >
160
+ < ToolBar { ...toolBarProps } toolbars = { toolbars } />
161
+ < ToolBar { ...toolBarProps } toolbars = { toolbarsMode } mode />
162
+ </ div >
163
+ ) ;
164
+ const child = toolbarBottom ? (
165
+ < Fragment >
166
+ { conentView }
167
+ { toolbarView }
168
+ </ Fragment >
169
+ ) : (
170
+ < Fragment >
171
+ { toolbarView }
172
+ { conentView }
173
+ </ Fragment >
174
+ ) ;
124
175
return (
125
176
< div className = { cls } ref = { container } >
126
- { hideToolbar && (
127
- < div className = { `${ prefixCls } -toolbar-warp` } >
128
- < ToolBar { ...toolBarProps } toolbars = { toolbars } />
129
- < ToolBar { ...toolBarProps } toolbars = { toolbarsMode } mode />
130
- </ div >
131
- ) }
132
- < div className = { `${ prefixCls } -content` } style = { { height : codemirrorProps . height } } >
133
- < div className = { `${ prefixCls } -content-editor` } ref = { containerEditor } >
134
- { visibleEditor && (
135
- < CodeMirror
136
- theme = { defaultTheme }
137
- { ...codemirrorProps }
138
- extensions = { extensionsData }
139
- height = { height }
140
- ref = { codeMirror }
141
- onChange = { handleChange }
142
- />
143
- ) }
144
- </ div >
145
- < div className = { clsPreview } ref = { preview } >
146
- { renderPreview ? (
147
- renderPreview ( previewProps , ! ! visible )
148
- ) : (
149
- < MarkdownPreview { ...previewProps } data-visible = { ! ! visible } />
150
- ) }
151
- </ div >
152
- </ div >
177
+ { child }
153
178
</ div >
154
179
) ;
155
180
}
0 commit comments