1
1
import React , { useState , useRef , useImperativeHandle } from 'react' ;
2
2
import { markdown , markdownLanguage } from '@codemirror/lang-markdown' ;
3
3
import { languages } from '@codemirror/language-data' ;
4
- import { EditorView } from '@codemirror/view' ;
4
+ import { EditorView , ViewUpdate } from '@codemirror/view' ;
5
5
import CodeMirror , { ReactCodeMirrorProps , ReactCodeMirrorRef } from '@uiw/react-codemirror' ;
6
- import MarkdownPreview , { MarkdownPreviewProps , MarkdownPreviewRef } from '@uiw/react-markdown-preview' ;
6
+ import MarkdownPreview , { MarkdownPreviewProps } from '@uiw/react-markdown-preview' ;
7
7
import ToolBar , { IToolBarProps } from './components/ToolBar' ;
8
8
import { getCommands , getModeCommands } from './commands' ;
9
9
import { defaultTheme } from './theme' ;
10
10
import './index.less' ;
11
11
12
12
export * from './commands' ;
13
+ export * from '@uiw/react-markdown-preview' ;
13
14
14
15
export const scrollerStyle = EditorView . theme ( {
15
16
'&.cm-editor, & .cm-scroller' : {
@@ -29,7 +30,7 @@ export interface IMarkdownEditor extends ReactCodeMirrorProps {
29
30
/** Option to hide the tool bar. */
30
31
hideToolbar ?: boolean ;
31
32
/** Override the default preview component */
32
- renderPreview ?: ( props : MarkdownPreviewProps , visible : boolean ) => React . ReactNode ;
33
+ renderPreview ?: ( props : MarkdownPreviewProps , initVisible : boolean ) => React . ReactNode ;
33
34
/** Tool display settings. */
34
35
toolbars ?: IToolBarProps [ 'toolbars' ] ;
35
36
/** Tool display settings. */
@@ -40,15 +41,15 @@ export interface IMarkdownEditor extends ReactCodeMirrorProps {
40
41
41
42
export interface ToolBarProps {
42
43
editor : React . RefObject < ReactCodeMirrorRef > ;
43
- preview : React . RefObject < MarkdownPreviewRef > ;
44
+ preview : React . RefObject < HTMLDivElement > ;
44
45
container : React . RefObject < HTMLDivElement > ;
45
46
containerEditor : React . RefObject < HTMLDivElement > ;
46
47
editorProps : IMarkdownEditor ;
47
48
}
48
49
49
50
export interface MarkdownEditorRef {
50
51
editor : React . RefObject < ReactCodeMirrorRef > ;
51
- preview ? : React . RefObject < MarkdownPreviewRef > | null ;
52
+ preview : React . RefObject < HTMLDivElement > | null ;
52
53
}
53
54
54
55
export default React . forwardRef < MarkdownEditorRef , IMarkdownEditor > ( MarkdownEditor ) ;
@@ -73,22 +74,22 @@ function MarkdownEditor(
73
74
} = props ;
74
75
const [ value , setValue ] = useState ( props . value || '' ) ;
75
76
const codeMirror = useRef < ReactCodeMirrorRef > ( null ) ;
76
- const previewContainer = useRef < MarkdownPreviewRef > ( null ) ;
77
77
const container = useRef < HTMLDivElement > ( null ) ;
78
78
const containerEditor = useRef < HTMLDivElement > ( null ) ;
79
+ const preview = useRef < HTMLDivElement > ( null ) ;
79
80
80
81
useImperativeHandle (
81
82
ref ,
82
83
( ) => ( {
83
84
editor : codeMirror ,
84
- preview : previewContainer ,
85
+ preview : preview ,
85
86
} ) ,
86
- [ codeMirror , previewContainer ] ,
87
+ [ codeMirror ] ,
87
88
) ;
88
89
89
90
const toolBarProps : ToolBarProps = {
91
+ preview : preview ,
90
92
editor : codeMirror ,
91
- preview : previewContainer ,
92
93
container : container ,
93
94
containerEditor : containerEditor ,
94
95
editorProps : props ,
@@ -99,10 +100,15 @@ function MarkdownEditor(
99
100
scrollerStyle ,
100
101
...extensions ,
101
102
] ;
102
- previewProps [ 'className' ] = `${ prefixCls } -preview` ;
103
+ const clsPreview = `${ prefixCls } -preview` ;
104
+ const cls = [ prefixCls , 'wmde-markdown-var' , className ] . filter ( Boolean ) . join ( ' ' ) ;
103
105
previewProps [ 'source' ] = value ;
106
+ const handleChange = ( value : string , viewUpdate : ViewUpdate ) => {
107
+ setValue ( value ) ;
108
+ onChange && onChange ( value , viewUpdate ) ;
109
+ } ;
104
110
return (
105
- < div className = { ` ${ prefixCls || '' } wmde-markdown-var ${ className || '' } ` } ref = { container } >
111
+ < div className = { cls } ref = { container } >
106
112
{ hideToolbar && (
107
113
< div >
108
114
< ToolBar { ...toolBarProps } toolbars = { toolbarsMode } mode />
@@ -118,18 +124,17 @@ function MarkdownEditor(
118
124
extensions = { extensionsData }
119
125
height = { height }
120
126
ref = { codeMirror }
121
- onChange = { ( value , viewUpdate ) => {
122
- setValue ( value ) ;
123
- onChange && onChange ( value , viewUpdate ) ;
124
- } }
127
+ onChange = { handleChange }
125
128
/>
126
129
) }
127
130
</ div >
128
- { renderPreview ? (
129
- renderPreview ( previewProps , ! ! visible )
130
- ) : (
131
- < MarkdownPreview { ...previewProps } data-visible = { ! ! visible } ref = { previewContainer } />
132
- ) }
131
+ < div className = { clsPreview } ref = { preview } >
132
+ { renderPreview ? (
133
+ renderPreview ( previewProps , ! ! visible )
134
+ ) : (
135
+ < MarkdownPreview { ...previewProps } data-visible = { ! ! visible } />
136
+ ) }
137
+ </ div >
133
138
</ div >
134
139
</ div >
135
140
) ;
0 commit comments