springload / draftail

📝🍸 A configurable rich text editor built with Draft.js
https://www.draftail.org/
MIT License
612 stars 64 forks source link

How to only allow newline when shift + return #370

Closed fridaystreet closed 4 years ago

fridaystreet commented 4 years ago

Hi,

Have a chat app requirement. I would like to only allow the creation of a newline if shift + return is pressed. And capture the return only keypress to be used for sending the message.

Is this possible with Draftail?

Any assistance or direction greatly appreciated

fridaystreet commented 4 years ago

Update: I found this issue #246 which is related to draft-js-plugins. It references a fix over in this is in the draft-js-plgin repo https://github.com/draft-js-plugins/draft-js-plugins/issues/1117

I've followed that to try and create a plugin but, the keybinding function never fires in the plugin.

So I've tried passing the keybing fn directly to the editor, but this doesn't seem to fire either. Thinking this would be the way with some additional logic to detect shift key on how to block the enter key on it's own?

const sendOnReturn = (event) => {
   console.log('test', event.keyCode, event);
   return undefined;
};

<Editor
keyBindingFn={sendOnReturn}
handleKeyCommand={command => ('not-handled')}
...
fridaystreet commented 4 years ago

looks like as per the issues mentioned, keyBindingFn isn't hooked up in draftail.

Managed to solve the problem with handleReturn hook in a plugin

const createHandleReturnPlugin = (onReturn) => ({
  handleReturn: (e) => {
    if (e.keyCode === 13 && e.shiftKey) return;
      if (isFunction(onReturn)) {
        onReturn(e);
     }
    return 'handled';
  }
});