Draft.js 中列表的处理选项卡

Cod*_*imp 6 draftjs

我有一个由 Draft.js 提供的编辑器的包装器,我想让 tab/shift-tab 键像它们应该用于 UL 和 OL 一样工作。我定义了以下方法:

  _onChange(editorState) {
    this.setState({editorState});
    if (this.props.onChange) {
      this.props.onChange(
        new CustomEvent('chimpeditor_update',
          {
            detail: stateToHTML(editorState.getCurrentContent())
          })
      );
    }
  }

  _onTab(event) {
    console.log('onTab');
    this._onChange(RichUtils.onTab(event, this.state.editorState, 6));
  }
Run Code Online (Sandbox Code Playgroud)

在这里,我有一个方法 ,_onTab它连接到Editor.onTab我调用的RichUtil.onTab(),我假设它返回更新的EditorState,然后我将其传递给更新 EditorState 并调用一些回调的通用方法。但是,当我点击 tab 或 shift-tab 时,什么也没有发生。

小智 3

import { Editor, RichUtils, getDefaultKeyBinding } from 'draft-js'


handleEditorChange = editorState => this.setState({ editorState })

handleKeyBindings = e => {
  const { editorState } = this.state
  if (e.keyCode === 9) {
    const newEditorState = RichUtils.onTab(e, editorState, 6 /* maxDepth */)
    if (newEditorState !== editorState) {
       this.handleEditorChange(newEditorState)
    }

    return
  }

  return getDefaultKeyBinding(e)
}

render() {
  return <Editor onTab={this.handleKeyBindings} />
}
Run Code Online (Sandbox Code Playgroud)

  • 这似乎没有回答OP。一个好的答案应该向OP解释为什么他的代码不起作用,然后用代码示例提出更改。 (3认同)