for-editor
for-editor 是一个基于 react 的 markdown 语法编辑器
fork from kkfor/for-editor
在基础上增加一些灵活性,比如可添加自定义的toolbar
安装
npm install for-editor-plus -S
使用
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import Editor from 'for-editor-plus'
class App extends Component {
constructor() {
super()
this.state = {
value: ''
}
}
handleChange(value) {
this.setState({
value
})
}
render() {
const { value } = this.state
return <Editor value={value} onChange={() => this.handleChange()} />
}
}
ReactDOM.render(<App />, document.getElementById('root'))
Api
属性
name | type | default | description |
---|
value | String | - | 输入框内容 |
placeholder | String | 开始编辑... | 占位文本 |
lineNum | Boolean | true | 是否显示行号 |
style | Object | - | 编辑器样式 |
height | String | 600px | 编辑器高度 |
preview | Boolean | false | 预览模式 |
expand | Boolean | false | 全屏模式 |
subfield | Boolean | false | 双栏模式(预览模式激活下有效) |
language | String | zh-CN | 语言(支持 zh-CN:中文简体, en:英文) |
toolbar | Object | 如下 | 自定义工具栏 |
toolbar: {
h1: true,
h2: true,
h3: true,
h4: true,
img: true,
link: true,
code: true,
preview: true,
expand: true,
undo: true,
redo: true,
save: true,
subfield: true,
}
事件
name | params 参数 | default | description |
---|
onChange | String: value | - | 内容改变时回调 |
onSave | String: value | - | 保存时回调 |
addImg | File: file | - | 添加图片时回调 |
图片上传
class App extends Component {
constructor() {
super()
this.state = {
value: '',
}
this.$vm = React.createRef()
}
handleChange(value) {
this.setState({
value
})
}
addImg($file) {
this.$vm.current.$img2Url($file.name, 'file_url')
console.log($file)
}
render() {
const { value } = this.state
return (
<Editor
ref={this.$vm}
value={value}
addImg={($file) => this.addImg($file)}
onChange={value => this.handleChange(value)}
/>
)
}
}
预览
let value = ``
return (
<div
ref={this.$scrollPreview}
className="for-preview for-markdown-preview"
dangerouslySetInnerHTML={{ __html: marked(value) }}
/>
)
快捷键
name | description |
---|
tab | 两个空格缩进 |
ctrl+s | 保存 |
ctrl+z | 上一步 |
ctrl+y | 下一步 |
更新
Licence
for-editor is MIT Licence.