【发布时间】:2018-04-14 13:01:09
【问题描述】:
我正在尝试将使用 draft.js 的富文本编辑器添加到反应项目中。我已经能够添加它并按照他们的文档处理键盘命令。我还添加了两个按钮来做粗体和斜体但问题是当我点击按钮时编辑器状态不会改变并且没有添加内联样式但是如果我选择文本并点击按钮样式是添加到该文本中。我不明白我哪里做错了。
import React, { Component } from 'react';
import {Editor, EditorState, RichUtils} from 'draft-js';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.onChange = (editorState) => this.setState({editorState});
this.handleKeyCommand = this.handleKeyCommand.bind(this);
}
handleKeyCommand(command, editorState) {
const newState = RichUtils.handleKeyCommand(editorState, command);
if (newState) {
this.onChange(newState);
return 'handled';
}
return 'not-handled';
}
_onBoldClick() {
this.onChange(RichUtils.toggleInlineStyle(
this.state.editorState, 'BOLD'));
}
_onItalicClick() {
this.onChange(RichUtils.toggleInlineStyle(
this.state.editorState, 'ITALIC'));
}
render() {
return (
<div className="App">
<header className="App-header">
<h1 className="App-title">Text formatting using Draft.js</h1>
</header>
<div className="toolbar">
<button className="btn" onClick={this._onBoldClick.bind(this)}>B</button>
<button className="btn" onClick={this._onItalicClick.bind(this)}>I</button>
</div>
<div className="notePage">
<Editor
editorState={this.state.editorState}
handleKeyCommand={this.handleKeyCommand}
onChange={this.onChange}
isCollapsed="true"
/>
</div>
</div>
);
}
}
【问题讨论】:
标签: javascript reactjs draftjs