tqsk-braft-editor
v1.0.0
Published
Rich Text Editor Based On Draft.js
Maintainers
Readme
Braft Editor-EN
installation
# Install using yarn
yarn add tqsk-braft-editor
# Install using npm
npm install tqsk-braft-editor --saveuse
The editor supports value and onChange properties, which are similar to the native input components in React. In general, you can use this editor in the form of a typical controlled component:
import React from 'react'
import BraftEditor from 'tqsk-braft-editor'
import 'tqsk-braft-editor/dist/index.css'
export default class EditorDemo extends React.Component {
state = {
editorState: null
}
async componentDidMount () {
// Assume here to get the editor content in html format from the server
const htmlContent = await fetchEditorContent()
// Use BraftEditor.createEditorState to convert html strings to editorState data needed by the editor
this.setState({
editorState: BraftEditor.createEditorState(htmlContent)
})
}
submitContent = async () => {
// Pressing ctrl + s when the editor has focus will execute this method
// Before the editor content is submitted to the server, you can directly call editorState.toHTML () to get the HTML content
const htmlContent = this.state.editorState.toHTML()
const result = await saveEditorContent(htmlContent)
}
handleEditorChange = (editorState) => {
this.setState({ editorState })
}
render () {
const { editorState } = this.state
return (
<div className="my-component">
<BraftEditor
value={editorState}
onChange={this.handleEditorChange}
onSave={this.submitContent}
/>
</div>
)
}
}