1- ---
2- title : Simple Usage
3- order : 1
4- ---
5-
6- 本 Demo 演示基本用法。
7-
8- ``` jsx
91import React , { Component } from 'react' ;
10- import ReactDOM from ' react-dom' ;
2+ import ReactDOM from 'react-dom/client ' ;
113import ReactMarkdown from 'react-markdown' ;
12- import MdEditor , { Plugins } from ' react-markdown-editor-lite' ;
4+ import MdEditor , { Plugins } from '../src' ;
5+ import '../src/index.less' ;
136
147const PLUGINS = undefined ;
158// const PLUGINS = ['header', 'divider', 'image', 'divider', 'font-bold', 'full-screen'];
@@ -23,14 +16,18 @@ MdEditor.use(Plugins.TabInsert, {
2316 tabMapValue : 1 , // note that 1 means a '\t' instead of ' '.
2417} ) ;
2518
26- class Demo extends React .Component {
27- mdEditor = undefined ;
19+ interface State {
20+ value : string ;
21+ }
22+
23+ class Demo extends Component < { } , State > {
24+ mdEditor : MdEditor | undefined = undefined ;
2825
2926 constructor ( props ) {
3027 super ( props ) ;
3128 this . renderHTML = this . renderHTML . bind ( this ) ;
3229 this . state = {
33- value: " # Hello" ,
30+ value : ' # Hello' ,
3431 } ;
3532 }
3633
@@ -41,17 +38,17 @@ class Demo extends React.Component {
4138 } ) ;
4239 } ;
4340
44- handleImageUpload = ( file ) => {
41+ handleImageUpload = file => {
4542 return new Promise ( resolve => {
4643 const reader = new FileReader ( ) ;
4744 reader . onload = data => {
48- resolve (data .target .result );
45+ resolve ( ( data . target as any ) . result ) ;
4946 } ;
5047 reader . readAsDataURL ( file ) ;
5148 } ) ;
5249 } ;
5350
54- onCustomImageUpload = ( event ) => {
51+ onCustomImageUpload = event => {
5552 console . log ( 'onCustomImageUpload' , event ) ;
5653 return new Promise ( ( resolve , reject ) => {
5754 const result = window . prompt ( 'Please enter image url here...' ) ;
@@ -82,15 +79,15 @@ class Demo extends React.Component {
8279
8380 handleSetValue = ( ) => {
8481 const text = window . prompt ( 'Content' ) ;
85- this .setState ({
86- value: text,
87- });
82+ if ( text ) {
83+ this . setState ( {
84+ value : text ,
85+ } ) ;
86+ }
8887 } ;
8988
9089 renderHTML ( text ) {
91- return React .createElement (ReactMarkdown, {
92- source: text,
93- });
90+ return < ReactMarkdown > { text } </ ReactMarkdown > ;
9491 }
9592
9693 render ( ) {
@@ -104,7 +101,9 @@ class Demo extends React.Component {
104101 </ nav >
105102 < div className = "editor-wrap" style = { { marginTop : '30px' } } >
106103 < MdEditor
107- ref= {node => (this .mdEditor = node || undefined )}
104+ ref = { node => {
105+ this . mdEditor = node || undefined ;
106+ } }
108107 value = { this . state . value }
109108 style = { { height : '500px' , width : '100%' } }
110109 renderHTML = { this . renderHTML }
@@ -155,7 +154,12 @@ class Demo extends React.Component {
155154 }
156155}
157156
158- ReactDOM .render ((
159- < Demo / >
160- ), mountNode);
161- ```
157+ const rootEl = document . getElementById ( 'root' ) ;
158+ if ( rootEl ) {
159+ const root = ReactDOM . createRoot ( rootEl ) ;
160+ root . render (
161+ < React . StrictMode >
162+ < Demo />
163+ </ React . StrictMode > ,
164+ ) ;
165+ }
0 commit comments