插件名称:braft-editor
在antd中的使用注意事项:
antd版本需要设置为3.26.14,否则会报错
安装braft-editor插件后的示例代码
import 'braft-editor/dist/index.css';
import React from 'react';
import BraftEditor from 'braft-editor';
import { Form, Input, Button } from 'antd';
const FormItem = Form.Item;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 5 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 12 },
},
};
class RichTextPage extends React.Component {
componentDidMount () {
// 异步设置编辑器内容
setTimeout(() => {
this.props.form.setFieldsValue({
content: BraftEditor.createEditorState('<p>Hello <b>World!</b></p>')
})
}, 1000)
}
handleSubmit = (event) => {
event.preventDefault()
this.props.form.validateFields((error, values) => {
if (!error) {
const submitData = {
title: values.title,
content: values.content.toRAW() // or values.content.toHTML()
}
console.log(submitData)
}
})
}
render () {
const { getFieldDecorator } = this.props.form
const controls = ['bold', 'italic', 'underline', 'text-color', 'separator', 'link', 'separator', 'media' ] // 富文本菜单选项
return (
<div className="demo-container">
<Form onSubmit={this.handleSubmit}>
<FormItem {...formItemLayout} label="文章标题">
{getFieldDecorator('title', {
rules: [{
required: true,
message: '请输入标题',
}],
})(
<Input size="large" placeholder="请输入标题"/>
)}
</FormItem>
<FormItem {...formItemLayout} label="文章正文">
{getFieldDecorator('content', {
validateTrigger: 'onBlur',
rules: [{
required: true,
validator: (_, value, callback) => {
if (value.isEmpty()) {
callback('请输入正文内容')
} else {
callback()
}
}
}],
})(
<BraftEditor
className="my-editor"
controls={controls}
placeholder="请输入正文内容"
/>
)}
</FormItem>
<FormItem {...formItemLayout}>
<Button size="large" type="primary" htmlType="submit">提交</Button>
</FormItem>
</Form>
</div>
)
}
}
export default Form.create({})(RichTextPage);
页面效果如下:
富文本.png
其中
可以通过修改 const controls = ['bold', 'italic', 'underline', 'text-color', 'separator', 'link', 'separator', 'media' ]来动态显示富文本菜单
如去掉 'media这一选项,则菜单栏会不显示媒体这一个按钮
网友评论