【问题标题】:How to customize the style for react-draft-wysiwyg?如何自定义 react-draft-wysiwyg 的样式?
【发布时间】:2020-07-21 12:59:02
【问题描述】:

我开始使用 react-draft-wysiwyg 并想知道是否可以使用样式和类扩展现有元素?

例如,默认有一个按钮B(粗体),我想给它添加一些样式和类,我该怎么做?

我的目标是获得类似<p className = "test"> BOLD TEXT </ p> 的内容,因此我将在课堂上获得粗体文本

class EditorContainer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      editorState: EditorState.createEmpty(),
    };
  }

  onEditorStateChange = (editorState) => {
    this.setState({
      editorState,
    });
  };

  render() {
    const { editorState } = this.state;
    return (
      <div className="editor">
        <Editor
          editorState={editorState}
          onEditorStateChange={this.onEditorStateChange}
          toolbar={{
            options: ["inline", "blockType", "fontSize"],
            inline: {
              inDropdown: false,
              className: "test",
              component: undefined,
              dropdownClassName: undefined,
              options: ["bold", "italic", "underline"],
              bold: { className: "test", style: { color: "red" } },
              italic: { className: undefined },
              underline: { className: undefined },
            },
          }}
        />

        <textarea
          disabled
          // value={draftToHtml(convertToRaw(editorState.getCurrentContent()))}
        />
      </div>
    );
  }
}

Example

或者也许有 react-wysiwyg 另一个为此目的的库

【问题讨论】:

    标签: javascript reactjs react-draft-wysiwyg


    【解决方案1】:

    应该能够使用 blockStyleFn 属性将样式应用于块元素。文档中有一个很好的例子:https://draftjs.org/docs/advanced-topics-block-styling#blockstylefn

    【讨论】:

    【解决方案2】:

    &lt;Editor/&gt; 组件中使用customBlockRenderFunc={myBlockStyleFn} 而不是blockStyleFn={myBlockStyleFn}

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多