3

我想获取我的编辑器的内容并最终将其存储到 const 内容中。我收到 _draftJs.EditorState.getCurrentContent is not a function 的错误。

import React from 'react'
import ReactDOM from 'react-dom'
import {Editor, EditorState, RichUtils} from 'draft-js'

const content = EditorState.getCurrentContent()
console.log('str= ', EditorState.getCurrentContent())
console.log('content=', content)
class MyEditor extends React.Component {
  constructor (props) {
    super(props)
    this.state = {editorState: EditorState.createEmpty()}
    this.onChange = (editorState) => this.setState({editorState})
    this.handleKeyCommand = this.handleKeyCommand.bind(this)
  }
  _onBoldClick () {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState,     'BOLD'))
  }
  _onUnderlineClick () {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState, 'UNDERLINE'))
  }
  render () { 
    return (
      <div id='content'>
      <h1>Notejs</h1>
      <div id='editor'>
      <button onClick={this._onBoldClick.bind(this)}>Bold</button>
      <button onClick={this._onUnderlineClick.bind(this)}>Underline</button>
      <Editor editorState={this.state.editorState} onChange={this.onChange} />
      </div>
      </div>
    )
  }
}

ReactDOM.render(
  <MyEditor />,
  document.getElementById('app')
)
4

3 回答 3

2

this.state.editorState.getCurrentContent()不是EditorState.getCurrentContent()

于 2016-10-20T08:07:01.790 回答
0

导入 convertToRaw convertToRaw(this.state.editorState.getCurrentContent())

于 2016-10-20T04:31:25.590 回答
0

假设我们有一个用于保存数据的保存按钮。此外,我们需要导入Draft-js 提供的模块convertFromRawconvertToRaw

import React from 'react'
import ReactDOM from 'react-dom'
import { Editor, EditorState, RichUtils, ContentState, convertFromRaw, convertToRaw} from 'draft-js';

class MyEditor extends React.Component {
  constructor (props) {
    super(props)
    this.state = {editorState: EditorState.createEmpty()}
    this.onChange = (editorState) => this.setState({editorState})
    this.handleKeyCommand = this.handleKeyCommand.bind(this)
  }

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

  _onUnderlineClick () {
    this.onChange(RichUtils.toggleInlineStyle(this.state.editorState,'UNDERLINE'))
  }

  _onSave() {

    const contentState = this.state.editorState.getCurrentContent();
    const editorContentRaw = convertToRaw(contentState);

    /* we can save this editorContentRaw inside the DB. */

  }

  render () { 
    return (
      <div id='content'>
        <h1>Notejs</h1>
        <div id='editor'>
          <button onClick={this._onBoldClick.bind(this)}>Bold</button>
          <button onClick={this._onUnderlineClick.bind(this)}>Underline</button>
          <Editor editorState={this.state.editorState} onChange={this.onChange} />
          <button onClick={this._onSave.bind(this)}>Save</button>
        </div>
      </div>
    )
  }
}

ReactDOM.render(
  <MyEditor />,
  document.getElementById('app')
)
于 2016-11-24T05:50:28.020 回答