Braft Editor
是一個基于 React
和 Draft-js
開發的富文本編輯器,提供了豐富的基礎功能,如基本文本格式化、列表、鏈接、圖片上傳、視頻插入等,并且還支持擴展。
-
首先,確保你已經在項目中安裝了
Braft Editor
和它的依賴項,如果沒有,可以運行以下命令進行安裝:npm install braft-editor --save
-
實現代碼如下
備注:可以配置table的option,也可以配置工具欄
import BraftEditor from 'braft-editor'; import 'braft-editor/dist/index.css'; import TableEditor from 'braft-extensions/dist/table'; import 'braft-extensions/dist/table.css';const options = {defaultColumns: 2, // 默認列數defaultRows: 2, // 默認行數withDropdown: false, // 插入表格前是否彈出下拉菜單columnResizable: true, // 是否允許拖動調整列寬,默認false }; // 啟用表格擴展 BraftEditor.use(TableEditor(options));const Editor = ({ onSuccess }, ref) => {const [editorState, setEditorState] = useState(BraftEditor.createEditorState(null));const handleChange = (newEditorState: any) => {setEditorState(newEditorState);};return (<BraftEditorvalue={editorState}onChange={handleChange}controls={['blockquote','bold','code','clear','emoji','font-family','font-size','fullscreen','headings','italic','letter-spacing','line-height','link','list-ol','list-ul','redo','remove-styles','separator','strike-through','text-align','text-color','text-indent','underline','undo','table', //可以自定義顯示工具欄內容]}/>); };export default Editor;