Skip to content

keyBindingFn not working for draft-js-plugin #246

@mrolafsson

Description

@mrolafsson

Describe the bug

keyBindingFn events are not fired for plugins in Draftail (unless I'm doing something wrong here :)

Which terms did you search for in the documentation and issue tracker?

I've found limited evidence in all three issue trackers (Draft, Draftail and Draft plugins). I did see one issue in Draftail referring to up/down arrows not working in the emoji plugin. I verified the same behaviour.

I suspect draft-js-plugins/issues/1117 could be the cause, meaning that your behaviour.js shortcuts take precedence and some handling is required for subsequent plugins to work?

I can confirm that when I use the draft-js-plugins-editor both of the following key events will fire:

const plugins = [
  {
    keyBindingFn: () => console.log("First"),
  }, {
    keyBindingFn: () => console.log("Other")
  }
];

Environment

Latest Draftail and Draft JS Plugins dependencies. Per Draftail installation I am using 0.10.5 version of Draft.

Steps to reproduce

Here's a sample plugin editor component that will log out key events:

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

const plugins = [
  {
    keyBindingFn: (e) => console.log(e),
    handleReturn: (e) => console.log("Return!!!")
  }
];

export default class TextEditor extends Component {

  state = {
    editorState: EditorState.createEmpty(),
  };

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

  render() {
    return (
      <Editor
        editorState={this.state.editorState}
        onChange={this.onChange}
        plugins={plugins}
      />
    );
  }
}

Whereas this snippet of code will not:

import React, {Component} from 'react';
import {DraftailEditor} from "draftail"
import {EditorState} from 'draft-js';

const plugins = [
  {
    keyBindingFn: (e) => console.log(e),
    handleReturn: (e) => console.log("Return!!!")
  }
];

export default class TextEditor extends Component {

  state = {
    editorState: EditorState.createEmpty(),
  };

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

  render() {
    return (
      <DraftailEditor
        editorState={this.state.editorState}
        onChange={this.onChange}
        plugins={plugins}
      />
    );
  }
}

Actual behavior

Here are the two corresponding console dumps:

Screenshot 2019-10-15 at 09 46 17

Screenshot 2019-10-15 at 09 42 14

Reproducible demo

https://github.com/mrolafsson/draft-js-plugins-vs-draftail-keyboard-bug

Thank you for a great project though! Very thoughtful and elegant. Makes Draft much easier to work with.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions