1
1
/*
2
2
* TuiEditor
3
+ * wrap toast-ui editor with react
3
4
*/
4
-
5
- import React , { useState } from "react" ;
5
+ import React from "react" ;
6
6
import PropTypes from "prop-types" ;
7
- import cn from "classnames" ;
8
- import { Editor } from "@toast-ui/react-editor" ;
9
- import styles from "./styles.module.scss" ;
7
+ import Editor from "@toast-ui/editor" ;
10
8
11
- const TuiEditor = ( props ) => {
12
- const [ editorElement , setEditorElement ] = useState ( null ) ;
13
- const onChange = ( ) => {
14
- const mk = editorElement . editorInst . getMarkdown ( ) ;
15
- props . onChange ( mk ) ;
16
- } ;
17
- return (
18
- < div className = { cn ( styles [ "editor-container" ] , props . className ) } >
19
- < Editor
20
- { ...props }
21
- ref = { setEditorElement }
22
- onChange = { onChange }
23
- initialValue = { props . value }
24
- />
25
- </ div >
26
- ) ;
27
- } ;
9
+ class TuiEditor extends React . Component {
10
+ constructor ( props ) {
11
+ super ( props ) ;
12
+ this . rootEl = React . createRef ( ) ;
13
+ this . editorInst = null ;
14
+ }
15
+
16
+ getRootElement ( ) {
17
+ return this . rootEl . current ;
18
+ }
19
+
20
+ getInstance ( ) {
21
+ return this . editorInst ;
22
+ }
23
+
24
+ bindEventHandlers ( props ) {
25
+ Object . keys ( this . props )
26
+ . filter ( ( key ) => / ^ o n [ A - Z ] [ a - z A - Z ] + / . test ( key ) )
27
+ . forEach ( ( key ) => {
28
+ const eventName = key [ 2 ] . toLowerCase ( ) + key . slice ( 3 ) ;
29
+ // off function has issue
30
+ // when add `onFocus` function, the headings popup will not hide automatically
31
+ // this.editorInst.off(eventName, props[key]);
32
+ this . editorInst . on ( eventName , props [ key ] ) ;
33
+ } ) ;
34
+ }
35
+
36
+ componentDidMount ( ) {
37
+ this . editorInst = new Editor ( {
38
+ el : this . rootEl . current ,
39
+ ...this . props ,
40
+ } ) ;
41
+
42
+ this . bindEventHandlers ( this . props ) ;
43
+ }
44
+
45
+ shouldComponentUpdate ( nextProps ) {
46
+ const instance = this . getInstance ( ) ;
47
+ const { height, previewStyle } = nextProps ;
48
+
49
+ if ( this . props . height !== height ) {
50
+ instance . height ( height ) ;
51
+ }
52
+
53
+ if ( this . props . previewStyle !== previewStyle ) {
54
+ instance . changePreviewStyle ( previewStyle ) ;
55
+ }
56
+
57
+ this . bindEventHandlers ( nextProps , this . props ) ;
58
+
59
+ return false ;
60
+ }
61
+
62
+ render ( ) {
63
+ return < div ref = { this . rootEl } /> ;
64
+ }
65
+ }
28
66
29
67
TuiEditor . defaultProps = {
30
68
height : "320px" ,
@@ -44,8 +82,7 @@ TuiEditor.defaultProps = {
44
82
45
83
TuiEditor . propTypes = {
46
84
// Editor's initial value
47
- value : PropTypes . string ,
48
- className : PropTypes . string ,
85
+ initialValue : PropTypes . string ,
49
86
// Markdown editor's preview style (tab, vertical)
50
87
previewStyle : PropTypes . string . isRequired ,
51
88
// Editor's height style value. Height is applied as border-box ex) '300px', '100%', 'auto'
0 commit comments