1
- import React from 'react' ;
2
- import classnames from 'classnames' ;
3
- import { ReactMarkdownProps } from 'react-markdown' ;
4
- import { IProps , ICommand , CommandOrchestrator } from './Type' ;
5
- import TextArea , { ITextAreaProps } from './components/TextArea' ;
6
- import Toolbar from './components/Toolbar' ;
7
- import DragBar from './components/DragBar' ;
8
- import MarkdownPreview from './components/Markdown' ;
9
- import { getCommands , TextAreaCommandOrchestrator } from './commands' ;
10
- import './index.less' ;
11
- import './markdowncolor.less' ;
12
- import './markdown.less' ;
1
+ import MDEditor , { IMDEditorProps , IMDEditorState } from './MDEditor' ;
2
+ import * as commands from './commands' ;
3
+ import * as MarkdownUtil from './utils/markdownUtils' ;
4
+ import { ICommand , CommandOrchestrator , TextState , TextApi , TextRange } from './Type' ;
5
+ import Markdown from './components/Markdown' ;
13
6
14
- export interface IMDEditorProps extends Omit < React . HTMLAttributes < HTMLDivElement > , 'onChange' > , IProps {
15
- /**
16
- * The Markdown value.
17
- */
18
- value ?: string ;
19
- /**
20
- * Event handler for the `onChange` event.
21
- */
22
- onChange ?: ( value : string ) => void ;
23
- /**
24
- * Can be used to make `Markdown Editor` focus itself on initialization. Defaults to on.
25
- * it will be set to true when either the source `textarea` is focused,
26
- * or it has an `autofocus` attribute and no other element is focused.
27
- */
28
- autoFocus ?: ITextAreaProps [ 'autoFocus' ] ;
29
- /**
30
- * The height of the editor.
31
- */
32
- height ?: React . CSSProperties [ 'height' ] ;
33
- /**
34
- * Show drag and drop tool. Set the height of the editor.
35
- */
36
- visiableDragbar ?: boolean ;
37
- /**
38
- * Show markdown preview.
39
- */
40
- preview ?: 'live' | 'edit' | 'preview' ;
41
- fullscreen ?: boolean ;
42
- /**
43
- * Maximum drag height. `visiableDragbar=true`
44
- */
45
- maxHeight ?: number ;
46
- previewOptions ?: ReactMarkdownProps ;
47
- /**
48
- * Minimum drag height. `visiableDragbar=true`
49
- */
50
- minHeight ?: number ;
51
- /**
52
- * You can create your own commands or reuse existing commands.
53
- */
54
- commands ?: ICommand [ ] ;
55
- }
7
+ MDEditor . Markdown = Markdown ;
56
8
57
- export interface IMDEditorState {
58
- height : React . CSSProperties [ 'height' ] ;
59
- preview ?: IMDEditorProps [ 'preview' ] ;
60
- fullscreen ?: boolean ;
9
+ export {
10
+ IMDEditorProps ,
11
+ IMDEditorState ,
12
+ MarkdownUtil ,
13
+ commands ,
14
+ ICommand ,
15
+ CommandOrchestrator ,
16
+ TextState ,
17
+ TextApi ,
18
+ TextRange ,
61
19
}
62
20
63
- export default class MDEditor extends React . PureComponent < IMDEditorProps , IMDEditorState > {
64
- public static displayName = 'MDEditor' ;
65
- public preview = React . createRef < MarkdownPreview > ( ) ;
66
- public textarea = React . createRef < TextArea > ( ) ;
67
- public commandOrchestrator ! : CommandOrchestrator ;
68
- public static defaultProps : IMDEditorProps = {
69
- value : '' ,
70
- prefixCls : 'w-md-editor' ,
71
- height : 200 ,
72
- minHeight : 100 ,
73
- maxHeight : 1200 ,
74
- visiableDragbar : true ,
75
- preview : 'live' ,
76
- fullscreen : false ,
77
- commands : getCommands ( ) ,
78
- }
79
- public constructor ( props : IMDEditorProps ) {
80
- super ( props ) ;
81
- this . state = {
82
- height : props . height ,
83
- preview : props . preview ,
84
- fullscreen : props . fullscreen ,
85
- } ;
86
- }
87
- public componentDidMount ( ) {
88
- this . handleChange ( this . props . value ) ;
89
- this . commandOrchestrator = new TextAreaCommandOrchestrator ( this . textarea . current ! . text . current as HTMLTextAreaElement ) ;
90
- }
91
- public UNSAFE_componentWillReceiveProps ( nextProps : IMDEditorProps ) {
92
- if ( nextProps . preview !== this . props . preview ) {
93
- this . setState ( { preview : nextProps . preview } ) ;
94
- }
95
- if ( nextProps . fullscreen !== this . props . fullscreen ) {
96
- this . setState ( { fullscreen : nextProps . fullscreen } ) ;
97
- }
98
- }
99
- private handleChange ( mdStr ?: string ) {
100
- const { onChange } = this . props ;
101
- this . preview . current ! . renderHTML ( mdStr ) ;
102
- onChange && onChange ( mdStr || '' ) ;
103
- }
104
- public handleCommand = ( command : ICommand ) => {
105
- if ( command . keyCommand === 'preview' ) {
106
- this . setState ( { preview : command . value as IMDEditorState [ 'preview' ] } ) ;
107
- }
108
- if ( command . keyCommand === 'fullscreen' ) {
109
- this . setState ( { fullscreen : ! this . state . fullscreen } ) ;
110
- document . body . style . overflow = this . state . fullscreen ? 'initial' : 'hidden' ;
111
- }
112
- this . commandOrchestrator . executeCommand ( command ) ;
113
- }
114
- public render ( ) {
115
- const { prefixCls, className, value, commands, height, visiableDragbar, preview, fullscreen, previewOptions, maxHeight, minHeight, autoFocus, onChange, ...other } = this . props ;
116
- const cls = classnames ( className , prefixCls , {
117
- [ `${ prefixCls } -show-${ this . state . preview } ` ] : this . state . preview ,
118
- [ `${ prefixCls } -fullscreen` ] : this . state . fullscreen ,
119
- } ) ;
120
- return (
121
- < div className = { cls } style = { { height : this . state . fullscreen ? '100%' : this . state . height } } { ...other } >
122
- < Toolbar
123
- active = { {
124
- fullscreen : this . state . fullscreen ,
125
- preview : this . state . preview ,
126
- } }
127
- prefixCls = { prefixCls } commands = { commands }
128
- onCommand = { this . handleCommand }
129
- />
130
- < div
131
- className = { `${ prefixCls } -content` }
132
- style = { { height : this . state . fullscreen ? 'calc(100% - 29px)' : ( this . state . height as number ) - 29 } }
133
- >
134
- < TextArea
135
- ref = { this . textarea }
136
- className = { `${ prefixCls } -input` }
137
- prefixCls = { prefixCls }
138
- value = { value }
139
- autoFocus = { autoFocus }
140
- onChange = { this . handleChange . bind ( this ) }
141
- />
142
- < MarkdownPreview
143
- { ...previewOptions }
144
- ref = { this . preview }
145
- className = { `${ prefixCls } -preview` }
146
- />
147
- { visiableDragbar && ! this . state . fullscreen && (
148
- < DragBar
149
- prefixCls = { prefixCls }
150
- height = { this . state . height as number }
151
- maxHeight = { maxHeight ! }
152
- minHeight = { minHeight ! }
153
- onChange = { ( newHeight ) => {
154
- this . setState ( { height : newHeight } ) ;
155
- } }
156
- />
157
- ) }
158
- </ div >
159
- </ div >
160
- )
161
- }
162
- }
21
+ export default MDEditor ;
0 commit comments