reactjs - React Draft.js 工具栏插件未显示

标签 reactjs plugins draftjs draft-js-plugins

我已关注docs关于安装内联和静态工具栏插件,但它们似乎不存在。

我正在使用 Create React App CLI。

组件:

import React from 'react';
import {EditorState} from 'draft-js';
import Editor from 'draft-js-plugins-editor';

import createInlineToolbarPlugin from 'draft-js-inline-toolbar-plugin';
import createToolbarPlugin from 'draft-js-static-toolbar-plugin';

import 'draft-js/dist/Draft.css';
import 'draft-js-inline-toolbar-plugin/lib/plugin.css';
import 'draft-js-static-toolbar-plugin/lib/plugin.css';

const inlineToolbarPlugin = createInlineToolbarPlugin({
 //I read somewhere that this plug-in needs this structure passed to it, 
 //but the example in the docs did not use it, and they are undefined anyway
  // structure: [
  //   BoldButton,
  //   ItalicButton,
  //   UnderlineButton,
  //   CodeButton,
  //   Separator,
  // ],
});

const toolbarPlugin = createToolbarPlugin();

class TextEditor extends React.Component {
  constructor(props) {
    super(props);
    this.state = {editorState: EditorState.createEmpty()};
    this.onChange = (editorState) => this.setState({editorState});
  }
  render() {
    return (
      <Editor 
        editorState={this.state.editorState} 
        onChange={this.onChange}
        plugins={[inlineToolbarPlugin, toolbarPlugin]}
      />
    );
  }
}

export default TextEditor;

然后将该组件传递给另一个组件,该组件仅呈现编辑器而不执行任何其他操作。

我一定错过了一些东西,或者没有给插件提供他们需要的东西,我只是不知道是什么。我猜我拥有的代码不足以首先开始添加插件?

最佳答案

您可以定义自定义按钮来执行所需的操作,如下所示:

<Editor 
editorState={this.state.editorState} 
onChange={this.onChange}
plugins={[inlineToolbarPlugin, toolbarPlugin]}
/>
<button onClick={this._onBoldClick.bind(this)}>Bold</button> //add button to make bold

现在您可以在_onBoldClick方法中编写加粗代码,如下所示:

_onBoldClick() {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState, 'BOLD'));
}

您可以引用docs .

关于reactjs - React Draft.js 工具栏插件未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49664421/

相关文章:

javascript - React & Draft.js - ConvertFromRaw 不显示 "INLINETEX"

javascript - 将密码存储在 Javascript localStorage 中

reactjs - react native 0.57 : 'Can' t find variable: require' with metro-react-native-babel-preset

reactjs - 来自 material-ui 的 DataTables 分页不起作用

python - 为什么某些 Vim 插件需要 "source"?

reactjs - react-rte(富文本编辑器): how to implement inline style for custom components?

javascript - 使用事件函数中的 componentDidMount 变量

c++ - 是否有用于格式化 C++ 代码的 Visual Studio 2010 插件?

mysql - 我需要编辑 MySQL 吗?或者有一个 WP 插件可以做到这一点?

javascript - getBoundingClientRect 在 Safari 中为顶部、左侧、右侧、底部返回 0