@@ -56,35 +56,21 @@ const Dome = () => (
5656);
5757```
5858
59- controlled usage
59+ ## Controlled Usage
6060
6161``` jsx
6262import MarkdownEditor from ' @uiw/react-markdown-editor' ;
63- import React from ' react' ;
63+ import React , { useState } from ' react' ;
6464import ReactDOM from ' react-dom' ;
6565
66-
67- class App extends React .Component {
68- constructor () {
69- super ();
70- this .state = {
71- markdown: ' # This is a H1 \n ## This is a H2 \n ###### This is a H6' ,
72- };
73- this .updateMarkdown = this .updateMarkdown .bind (this );
74- }
75-
76- updateMarkdown (editor , data , value ) {
77- this .setState ({ markdown: value });
78- }
79-
80- render () {
81- return (
82- < MarkdownEditor
83- value= {this .state .markdown }
84- onChange= {this .updateMarkdown }
85- / >
86- );
87- }
66+ function App () {
67+ const [markdown , setMarkdown ] = useState (' # This is a H1 \n ## This is a H2 \n ###### This is a H6' );
68+ return (
69+ < MarkdownEditor
70+ value= {markdown}
71+ onChange= {(editor , data , value ) => setMarkdown (value)}
72+ / >
73+ );
8874}
8975
9076ReactDOM .render (
@@ -93,17 +79,52 @@ ReactDOM.render(
9379);
9480```
9581
82+ ## Custom Toolbars
83+
84+ ``` jsx
85+ import React from " react" ;
86+ import ReactDOM from " react-dom" ;
87+
88+ const title2 = {
89+ name: ' title2' ,
90+ keyCommand: ' title2' ,
91+ icon: (
92+ < svg width= " 12" height= " 12" viewBox= " 0 0 512 512" >
93+ < path fill= " currentColor" d= " M496 80V48c0-8.837-7.163-16-16-16H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.621v128H154.379V96H192c8.837 0 16-7.163 16-16V48c0-8.837-7.163-16-16-16H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h37.275v320H32c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.621V288H357.62v128H320c-8.837 0-16 7.163-16 16v32c0 8.837 7.163 16 16 16h160c8.837 0 16-7.163 16-16v-32c0-8.837-7.163-16-16-16h-37.275V96H480c8.837 0 16-7.163 16-16z" / >
94+ < / svg>
95+ ),
96+ execute : (editor , selection , position ) => {
97+ const value = selection ? ` ## ${ selection} ` : ' ## ' ;
98+ editor .replaceSelection (value);
99+ position .ch = !! selection ? position .ch : position .ch + 3 ;
100+ editor .setCursor (position .line , position .ch );
101+ editor .focus ();
102+ },
103+ };
104+
105+ const Dome = () => (
106+ < MarkdownEditor
107+ value= " Hello Markdown!"
108+ toolbars= {[
109+ ' bold' , ' italic' , ' header' , title2
110+ ]}
111+ / >
112+ );
113+
114+ ReactDOM .render (< Dome / > , document .getElementById (' app' ));
115+ ```
116+
96117## Props
97118
98119- ` value (string) ` - the raw markdown that will be converted to html (** required** )
99- - ` visible?:boolean ` - Shows a preview that will be converted to html.
100- - ` toolbars?:array ` - Tool display settings.
101- - ` toolbarsMode?:array ` - Tool display settings.
102- - ` onChange?:function(editor: IInstance, data: CodeMirror.EditorChange, value: string) ` - called when a change is made ( ** required ** )
120+ - ` visible?: boolean ` - Shows a preview that will be converted to html.
121+ - ` toolbars?: ICommand[] | string[] ` - Tool display settings.
122+ - ` toolbarsMode?: ICommand[] | string[] ` - Tool display settings.
123+ - ` onChange?:function(editor: IInstance, data: CodeMirror.EditorChange, value: string) ` - called when a change is made
103124- ` onBlur?: function(editor: IInstance, event: Event) ` - event occurs when an object loses focus
104- - ` previewProps ` - [ react-markdown options] ( https://github.com/rexxars /react-markdown#options )
125+ - ` previewProps ` - [ react-markdown options] ( https://github.com/uiwjs /react-markdown-preview/tree/v2.1.0 #options-props )
105126
106- > [ Other Props Options] ( https://github.com/uiwjs/react-markdown-editor/blob/8de6abbf628b6d272d7da1c28e985fbbcba71b93 /src/components/CodeMirror/ index.tsx#L21-L60 )
127+ > [ Other Props Options] ( https://github.com/uiwjs/react-markdown-editor/blob/812937bf90abadd5f795d06d97ead9f59cd35954 /src/index.tsx#L11-L21 )
107128
108129
109130### Development
0 commit comments