Markdown Editor
In the react project, we need to use the Markdown editor. CodeMirror is the first one. It feels good to use, but it feels too big. I chose a lightweight React component that worked well and solved the problem, so I share it with you.
It has a lightweight preview editor, implemented using React. Js and TypeScript. This React component is designed to provide a simple Markdown editor with syntax highlighting support. This is based on textarea encapsulation, so it does not rely on any modern code editor such as Acs, CodeMirror, Monaco, etc
Advantage is
- Press Tab to indent or select text and drag to scale.
- It is based on textarea encapsulation and does not rely on any modern code editor.
- Not dependent on the author’s own encapsulation
UIW
Component library. - Automatically generate lists
Online experience
- Demo preview of CodeSandbox
- A demo preview of Github GH-Pages
Quick to use
npm i @uiw/react-md-editor
Copy the code
Editor use
import React from "react";
import ReactDOM from "react-dom";
import MEDitor from '@uiw/react-md-editor';
class App extends React.Component {
constructor() {
super(a);this.state = {
markdown: '# This is a H1 \n## This is a H2 \n###### This is a H6'};this.updateMarkdown = this.updateMarkdown.bind(this);
}
updateMarkdown(value) {
this.setState({ markdown: value });
}
render() {
return<div> <MDEditor // editor value={this.state.markdown} onChange={this.updatemarkdown} width={1200} // editor width height={480} // editor height /> < mdeditor.markdown // text display source={this.state.markdown} /> </div>); } } ReactDOM.render( <App />, document.getElementById('app') );Copy the code
Customize toolbars
import React from "react";
import ReactDOM from "react-dom";
import MEDitor, { commands } from '@uiw/react-md-editor';
const title3: commands.ICommand = {
name: 'title3'.keyCommand: 'title3'.buttonProps: { 'aria-label': 'Insert title3' },
icon: <path fill="currentColor" d="M15.7083333,468 C7.03242448,468 0,462.030833,454.666667 L0,421.333333 C0,413.969167 7.03242448,408 15.7083333,408 L361.291667,408 C369.967576,408 377,413.969167 377,421.333333 L377,454.666667 C377,462.030833 369.967576,468 361.291667,468 L15.7083333,468 Z M21.6666667,366 C9.69989583,366,359.831861,352.222222 L0, 317.777778c0,310.168139 9.69989583,304 21.6666667,304 L498.333333,304 C510.300104,304 520,310.168139 520,317.777778 L520,352.222222 C520,359.831861 510.300104,366 498.333333,366 L21.6666667,366 Z M136.835938,64 L136.835937,126 L107.25,126 L107.25,251 L40.75,251 L40.75,126 L40.75 L-5.68434189e-14,126 L-5.68434189E-14,64 L136.835938,64 Z M212,64 L212,251 L161.648438,251 L161.648438,64 L212,64 Z M378,64 L378,126 L343.25,126 L343.25,251 L281.75,251 L281.75,126 L238,126 L238,64 L378,64 Z M449.047619,189.550781 L520,189.550781 L520,251 L405,251 L405,64 L449.047619,64 L449.047619,189.550781 Z" /> </ SVG >), execute: (state: commands.TextState, api: commands.TextApi) => { let modifyText = `### ${state.selectedText}\n`; if (! state.selectedText) { modifyText = `### `; } api.replaceSelection(modifyText); }}; export default function App() { const [value, setValue] = React.useState("**Hello world!!! * * "); return ( <div className="container"> <MDEditor value="Hello Markdown!" commands={[ commands.bold, commands.hr, commands.italic, commands.divider, commands.codeEdit, commands.codeLive, commands.codePreview, commands.divider, commands.fullscreen, // Custom Toolbars title3, ]} /> </div> ); }Copy the code
Props parameters
parameter | instructions | type | The default value |
---|---|---|---|
value | cell | string | – |
onChange | OnChange event | funciton | – |
commands | Custom toolbar, array ICommand each contains a commands attribute. | Array | |
autoFocus | Whether to focus during initialization | boolean | true |
previewOptions | Reset edit preview display | ||
visiableDragbar | Show drag and drop tool, drag and drop editor size (three points in lower right corner) | boolean | true |
fullscreen | Edit preview display,Only edit .Show the editor .Just show |
Enum{edit, live, preview} | false |
maxHeight | Maximum drag height. The visiableDragbar=true value is valid | number | 1200 |
minHeights | Minimum drag height. The visiableDragbar=true value is valid | number | 100 |
tabSize | Number of characters to insert when pressing Tab. Default 2 Spaces | number | 2 |
The development of
NPM run watch: type # listen to create type of file. NPM run watch: ts # Listen compile.tsx files. NPM run doc: dev # Preview code examples.Copy the code