【问题标题】:Custom button in toolbar draftjs editor工具栏 Draftjs 编辑器中的自定义按钮
【发布时间】:2021-05-16 10:11:06
【问题描述】:

如何将带有自定义功能的按钮添加到 Draftjs 编辑器?我觉得这应该很简单,但我找不到示例或文档。

我有一个只有粗斜体和列表按钮的编辑器,我想添加一个显示弹出窗口的按钮,我可以在其中选择 3 个选项中的 1 个,然后在编辑器中“注入”它。

有人能指出我正确的方向(或文档)吗?

【问题讨论】:

    标签: reactjs react-hooks draftjs


    【解决方案1】:

    这里我是如何尝试这个的,纯粹是反应钩子的方式来实现目标,

    App.js 文件

    import MyEditor from './Component/MyEditor';
    
    function App() {
       <MyEditor />
    }
    

    MyEditor.js 文件导出 App.js 中使用和导入的 MyEditor 组件

    import React from 'react';
    import ReactDOM from 'react-dom';
    import {Editor, EditorState, RichUtils} from 'draft-js';
    import 'draft-js/dist/Draft.css';
    import './MyEditor.css'
    
    export default function MyEditor() {
      
      const [editorState, setEditorState] = React.useState(
        () => EditorState.createEmpty(),
      );
    
    
      const _onBoldClick = () => {
        setEditorState(RichUtils.toggleInlineStyle(editorState, 'BOLD'))
      }  
    
      return(
        <div>
            <button onClick={_onBoldClick}>BOLD</button>
            <div
            >
              <Editor 
                textAlignment="left" placeholder="Enter something here" 
                editorState={editorState} onChange={setEditorState} />
            </div>
            
        </div>
      ) 
      
        
    }
    
    //ReactDOM.render(<MyEditor />, document.getElementById('container'));
    

    用于样式的css文件

    div.DraftEditor-root {
        border: 1px solid black;
        
        height: 200px;
        width: 200px;
        margin-left: 20%;
    
        overflow-y: auto;
    }
    
    div.DraftEditor-editorContainer, div.public-DraftEditor-content {
        height: 100%;
    }
    

    【讨论】:

    • 谢谢你的回答,我明白了:)
    • @Randomz 记住不要使用onClick 使用onMouseDown 事件
    猜你喜欢
    • 2022-12-02
    • 2018-02-05
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2021-12-17
    • 2011-07-28
    • 2015-01-05
    • 1970-01-01
    相关资源
    最近更新 更多