Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Default enter behavior and shift enter behaviour #2755

Closed
1 of 2 tasks
TestCK opened this issue May 4, 2022 · 8 comments
Closed
1 of 2 tasks

Switch Default enter behavior and shift enter behaviour #2755

TestCK opened this issue May 4, 2022 · 8 comments
Labels
Type: Bug The issue or pullrequest is related to a bug

Comments

@TestCK
Copy link

TestCK commented May 4, 2022

What’s the bug you are facing?

I am building a chat app and I want to submitted what ever user has typed on press of enter
By default, pressing enter, creates a new line or a listitem.

I wanted to setup in a way, I wanted to submit user input on enter and create a new line or listitem on shift +enter.

Which browser was this experienced in? Are any special extensions installed?

Chrome

How can we reproduce the bug on our side?

This is not a bug, checking for a config to make it work for my use case

Can you provide a CodeSandbox?

No response

What did you expect to happen?

A way to submit on enter and create a new line/ listitem on shift + enter

Anything to add? (optional)

No response

Did you update your dependencies?

  • Yes, I’ve updated my dependencies to use the latest version of all packages.

Are you sponsoring us?

  • Yes, I’m a sponsor. 💖
@TestCK TestCK added the Type: Bug The issue or pullrequest is related to a bug label May 4, 2022
@pcbowers
Copy link

pcbowers commented May 5, 2022

@TestCK There is an extension called HardBreak which will provide you with the functionality you need for Shift + Enter.

To submit on enter, simply implement the editorProps function called handleKeyDown. Tiptap is built on ProseMirror, which uses a sort of hierarchy when it comes to event handler functions. Tiptap has ensured this is the very first handler that is called before any base plugins or extensions. The first value that returns true gets precedence, so only this function actually runs. This means that you are free to run your own onKeyDown event without having an additional paragraph (newline) appended at the end of every message.

(See Access the ProseMirror API and ProseMirror EditorPros Interface for more documentation)

Here is a brief example on CodeSandbox.

@fridaystreet
Copy link
Contributor

fridaystreet commented May 31, 2022

need to make sure the priority of the extensions is correct otherwise you mention select ill fire after your onreturn

@stale
Copy link

stale bot commented Jul 30, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jul 30, 2022
@svenadlung svenadlung removed the stale label Aug 1, 2022
@puopg
Copy link

puopg commented Aug 23, 2022

For the OP:

I think you can just use the same logic of the default enter handling, for Shift+Enter

  addKeyboardShortcuts() {
    return {
      "Shift-Enter": () =>
        this.editor.commands.first(({ commands }) => [
          () => commands.newlineInCode(),
          () => commands.createParagraphNear(),
          () => commands.liftEmptyBlock(),
          () => commands.splitBlock(),
        ]),
    };
  },

@bdbch
Copy link
Member

bdbch commented Sep 14, 2022

As some people pointed out you should be able to do this by defining your own Keyboard shortcuts to the editor. Comment if you have any questions.

@bdbch bdbch closed this as completed Sep 14, 2022
@michelengelen
Copy link

@bdbch I did this by adding a custom extension which sole purpose is to add keyboard-shortcut overrides.

Is this the correct approach, or is there a better way to do that

// this is a dummy extension only to create custom keydown behavior
const KeyboardHandler = Extension.create({
    name: 'keyboardHandler',
});

... 

KeyboardShortcutHandler.extend({
    addKeyboardShortcuts() {
        return {
            Enter: () => {
                const isCodeBlockActive = this.editor.isActive('codeBlock');

                if (isCodeBlockActive || ctrlSend) {
                    return false;
                }

                onSubmit();
                return this.editor.commands.clearContent();
            },

            'Mod-Enter': () => {
                const isCodeBlockActive = this.editor.isActive('codeBlock');

                /**
                 * when inside of a codeblock and setting for sending the message with CMD/CTRL-Enter
                 * force calling the `onSubmit` function and clear the editor content
                 */
                if (isCodeBlockActive && codeBlockOnCtrlEnter) {
                    onSubmit();
                    return this.editor.commands.clearContent();
                }

                if (!isCodeBlockActive && ctrlSend) {
                    onSubmit();
                    return this.editor.commands.clearContent();
                }

                return false;
            },

            'Shift-Enter': () => {
                /**
                 * currently we do not have an option to show a soft line break in the posts, so we overwrite
                 * the behavior from tiptap with the default behavior on pressing enter
                 */
                return this.editor.commands.first(({commands}) => [
                    () => commands.newlineInCode(),
                    () => commands.createParagraphNear(),
                    () => commands.liftEmptyBlock(),
                    () => commands.splitBlock(),
                ]);
            },
        };
    },
}),

@jacksonkernion
Copy link

@puopg's answer echoes @philippkuehn's answer here, but for some reason the custom extension commands aren't behaving the same as the default 'Enter' behavior in a subtle but important way: it creates a new line inside an existing list item rather than creating a new list item. What worked for me:

addKeyboardShortcuts() {
  return {
    "Shift-Enter": ({ editor }) => 
         editor.commands.first(({ commands }) => [
            () => commands.newlineInCode(),
            () => commands.splitListItem("listItem"), // This line added
            () => commands.createParagraphNear(),
            () => commands.liftEmptyBlock(),
            () => commands.splitBlock(),
         ])
     },
  }
}

@punkpeye
Copy link

What about quote blocks?

There seems to be no way to escape them using these bindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug The issue or pullrequest is related to a bug
Projects
None yet
Development

No branches or pull requests

9 participants