tiptap: [Suggestion extension]: Can't espace suggestion mode when allowSpaces is enabled

What’s the bug you are facing?

When allowSpaces option is set to false in Suggestion extension, hitting space allow us to quit the suggestion mode. But when allowSpaces is set to true, we cannot quit the suggestion mode because space is the only key to escape that mode.

How can we reproduce the bug on our side?

You can reproduce by adding the suggestion extension with allowSpaces at true

Can you provide a CodeSandbox?

No response

What did you expect to happen?

It would be really nice to have multiple ways to espace that mode: escape key, but also clicking anywhere on the editor (or just at the right of the suggestion word if the suggestion is the last thing typed in the editor).

Anything to add? (optional)

https://user-images.githubusercontent.com/6103456/141477468-10775f75-d209-47a5-93bf-90840f01eef6.mov

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. 💖

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 7
  • Comments: 18 (8 by maintainers)

Most upvoted comments

@rfgamaral We are not using that workaround more less due to same reasons.

What we do, is that we basically have option allowSpaces disabled and we use _ character as replacement for white space character. Then on request we replace _ for white space.

Like this:

    suggestion: {
      // TODO allow space bugfix https://github.com/ueberdosis/tiptap/issues/214#issuecomment-964557956
      // allowSpaces: true,
      items: async ({ query, editor }) => {
        const mentionableUsers = editor.storage.props
          .mentionableUsers as MentionableUsers;

        query = query.replaceAll('_', ' ');

        return mentionableUsers
          .filter((user) => isSubstring(query, user.name))
          .map((user) => ({
            ...user,
            image: user.avatar?.src,
            name: `${user.name} ${user.last_name ?? ''}`,
          }));
      },
      
      ...
    },

This workaround allow us to fake white space and result of mentioning can look like this.

Screenshot 2022-02-10 at 13 25 56

It’s maybe not best UX but it works.

You can still always note somewhere that to search for mention of multiple words user should use _ instead of white space.

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.

@cayasso Thanks for sharing your workaround.

I have not implemented it myself on our side, but we have something similar to that, i.e. hide popup if there are spaces and 0 results. But IMO the solution should exist in the suggestion utility, one that keeps the implementation on the consumer side as simple as possible.

@rfgamaral obviously can’t make any promises 😄

From what I see remirror doing here: https://remirror.vercel.app/?path=/story/react-hooks-usementionatom--including-whitespace they seem to be keeping the decoration (i.e. keep marking it as a suggestion with a decoration whether it actually matched or not) I think that is the only way to resolve this without a complete rewrite of how suggestions work.

It seems like like lexical only display the suggestion menu based on the cursor position, and does not try to do anything special to the text, which is also a nice tradeoff to make https://playground.lexical.dev/

@philippkuehn Do you think this and #214 can be addressed soon? IMO, these completely botch a great mentions experience. Maybe @jakedolan suggestion could be turned into a PR?

@filippofilip95 do you replace the space character with _ as users type? If so, how do you run that transformation?

Edit, I figured out where to run that replacement:

onUpdate(props: SuggestionRenderProps) {
  // replace trailing space with _
  const { editor } = props;
  const { $cursor, state, text } = getSelection(editor);
  const pos = $cursor.pos;
  const last = state.doc.textBetween(pos-1, pos);
  if (last === ' ') {
    editor
    .chain()
    .setTextSelection({ from: pos-1, to: pos })
    .insertContent('_')
    .run()
  }
  tooltip.setProps({
    getReferenceClientRect: props.clientRect,
  });
  reactRenderer.updateProps(extendProps(props));
},