2.0.6 • Published 5 months ago

prosemirror-suggest v2.0.6

Weekly downloads
4,154
License
MIT
Repository
github
Last release
5 months ago

prosemirror-suggest

npm bundle size (scoped) npm

The problem

You want to create a suggestion plugin for your prosemirror editor but are unsure how to get started. The suggestions could be for mentions, emojis, responding to a keypress with a dropdown of potential actions or anything that needs to extract a query from the current editor when a matching character is entered.

This solution

prosemirror-suggest provides the suggestion primitives you will need for within your editor. It doesn't try to be magical and even with this library setting up suggestions can be difficult. However, with this toolkit, you will be able to build pretty much any suggestion plugin you can think of.

Installation

yarn add prosemirror-suggest prosemirror-view

Getting Started

The configuration of prosemirror-suggests is based on a Suggester interface which defines the suggestion behaviour. The suggest method receives the configured suggestersand creates a suggestion plugin which can be used within your prosemirror editor.

In the following example we're creating an emoji suggestion plugin that responds to the colon character with a query and presents a list of matching emojis based on the query typed.

import { Suggester, suggest } from 'prosemirror-suggest';

const maxResults = 10;
let selectedIndex = 0;
let emojiList: string[] = [];
let showSuggestions = false;

const suggestEmojis: Suggester = {
  /**
   * By default decorations are used to highlight the currently matched suggestion in the dom.
   * In this example we don't need decorations (in fact they cause problems when the
   * emoji string replaces the query text in the dom).
   */
  noDecorations: true,
  char: ':', // The character to match against
  name: 'emoji-suggestion', // a unique name
  appendText: '', // Text to append to the created match

  // Keybindings are similar to prosemirror keymaps with a few extra niceties.
  // The key identifier can also include modifiers (e.g.) `Cmd-Space: () => false`
  // Return true to prevent any further keyboard handlers from running.
  keyBindings: {
    ArrowUp: () => {
      selectedIndex = rotateSelectionBackwards(selectedIndex, emojiList.length);
    },
    ArrowDown: () => {
      selectedIndex = rotateSelectionForwards(selectedIndex, emojiList.length);
    },
    Enter: ({ command }) => {
      if (showSuggestions) {
        command(emojiList[selectedIndex]);
      }
    },
    Esc: () => {
      showSuggestions = false;
    },
  },

  onChange: params => {
    const query = params.query.full;
    emojiList = sortEmojiMatches({ query, maxResults });
    selectedIndex = 0;
    showSuggestions = true;
  },

  onExit: () => {
    showSuggestions = false;
    emojiList = [];
    selectedIndex = 0;
  },

  // Create a  function that is passed into the change, exit and keybinding handlers.
  // This is useful when these handlers are called in a different part of the app.
  createCommand: ({ match, view }) => {
    return (emoji, skinVariation) => {
      if (!emoji) {
        throw new Error('An emoji is required when calling the emoji suggestions command');
      }

      const tr = view.state.tr;
      const { from, end: to } = match.range;
      tr.insertText(emoji, from, to);
      view.dispatch(tr);
    };
  },
};

// Create the plugin with the above configuration. It supports multiple plugins being added.
const suggestionPlugin = suggest(suggestEmojis, suggestMentions);

// Later on in the codebase
const state = EditorState.create({
  schema,
  plugins: [suggestionPlugin],
});

API

suggest variable

This creates a suggestion plugin with all the suggestions provided.

Signature:

suggest: <GSchema extends import("prosemirror-model").Schema<string, string> = any>(...suggesters: Suggester<import("@remirror/core-types").AnyFunction<void>>[]) => Plugin<SuggestState<any>, GSchema>

The priority of the suggestions is the order in which they are passed into this function.

const plugin = suggest(two, one, three);

In the above example two will be checked first, then one and then three.

Only one suggestion can match at any given time. The order and specificity of the regex parameters help determines which suggestion will be active.

Suggester interface

This Suggester interface provides the options object which is used within the suggest plugin creator.

Properties

PropertyTypeDescription
appendTextstringText to append after the mention has been added.
charstringThe character to match against.
decorationsTagkeyof HTMLElementTagNameMapTag which wraps an active match.
noDecorationsbooleanWhen true, decorations are not created when this mention is being edited..
invalidPrefixCharactersRegExp | stringA regex expression used to invalidate the text directly before the match.
keyBindingsSuggestKeyBindingMap<GCommand>An object that describes how certain key bindings should be handled.
matchOffsetnumberSets the characters that need to be present after the initial character match before a match is triggered.For example with char = @ the following is true.- matchOffset: 0 matches '@' immediately - matchOffset: 1 matches '@a' but not '@' - matchOffset: 2 matches '@ab' but not '@a' or '@' - matchOffset: 3 matches '@abc' but not '@ab' or '@a' or '@' - And so on...
namestringA unique identifier for the matching character.
startOfLinebooleanWhether to only match from the start of the line
suggestionClassNamestringClass name to use for the decoration (while the plugin is still being written)
supportedCharactersRegExp | stringA regex containing all supported characters when within a suggestion.
validPrefixCharactersRegExp | stringA regex expression used to validate the text directly before the match.

Methods

MethodDescription
createCommand(params)Create the suggested actions which are made available to the onExit and ononChange handlers.
getStage(params)Check the current match and editor state to determine whether this match is being newly created or edited.
onChange(params)Called whenever a suggestion becomes active or changes in anyway.
onCharacterEntry(params)Called for each character entry and can be used to disable certain characters.
onExit(params)Called when a suggestion is exited with the pre-exit match value.
0.0.0-pr2222.1

5 months ago

0.0.0-pr2223.1

5 months ago

0.0.0-pr2169.1

6 months ago

0.0.0-pr2169.2

6 months ago

3.0.0-beta.1

6 months ago

3.0.0-beta.0

7 months ago

3.0.0-beta.3

6 months ago

3.0.0-beta.2

6 months ago

0.0.0-pr2166.2

7 months ago

0.0.0-pr2166.3

7 months ago

0.0.0-pr2166.4

7 months ago

0.0.0-pr2156.1

8 months ago

0.0.0-pr2166.1

8 months ago

0.0.0-pr2118.1

10 months ago

0.0.0-pr2100.1

11 months ago

2.0.5

10 months ago

2.0.6

9 months ago

0.0.0-pr2128.2

10 months ago

0.0.0-pr2128.3

10 months ago

0.0.0-pr2128.4

10 months ago

0.0.0-pr2128.1

10 months ago

2.0.4

1 year ago

0.0.0-pr1948.1

1 year ago

0.0.0-pr1938.1

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

2 years ago

0.0.0-pr1942.1

1 year ago

0.0.0-pr1966.1

1 year ago

0.0.0-pr1922.1

2 years ago

0.0.0-pr1922.2

2 years ago

0.0.0-pr1887.1

2 years ago

0.0.0-pr1881.1

2 years ago

0.0.0-pr1881.2

2 years ago

0.0.0-pr1879.1

2 years ago

2.0.0

2 years ago

0.0.0-pr1873.1

2 years ago

2.0.0-beta.15

2 years ago

2.0.0-beta.14

2 years ago

2.0.0-beta.19

2 years ago

2.0.0-beta.18

2 years ago

2.0.0-beta.17

2 years ago

2.0.0-beta.16

2 years ago

0.0.0-pr1862.1

2 years ago

0.0.0-pr1885.1

2 years ago

0.0.0-pr1713.12

2 years ago

0.0.0-pr1713.11

2 years ago

0.0.0-pr1713.10

2 years ago

2.0.0-beta.9

2 years ago

2.0.0-beta.8

2 years ago

2.0.0-beta.7

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

2.0.0-beta.2

2 years ago

2.0.0-beta.1

2 years ago

2.0.0-beta.0

2 years ago

2.0.0-beta.6

2 years ago

2.0.0-beta.5

2 years ago

2.0.0-beta.4

2 years ago

2.0.0-beta.3

2 years ago

0.0.0-pr1713.2

2 years ago

0.0.0-pr1713.1

2 years ago

0.0.0-pr1713.8

2 years ago

0.0.0-pr1713.7

2 years ago

0.0.0-pr1713.9

2 years ago

0.0.0-pr1713.4

2 years ago

0.0.0-pr1713.3

2 years ago

0.0.0-pr1713.6

2 years ago

0.0.0-pr1713.5

2 years ago

0.0.0-pr1654.1

2 years ago

2.0.0-beta.11

2 years ago

2.0.0-beta.10

2 years ago

2.0.0-beta.13

2 years ago

2.0.0-beta.12

2 years ago

0.0.0-pr1801.2

2 years ago

0.0.0-pr1801.1

2 years ago

0.0.0-pr1801.3

2 years ago

0.0.0-pr1629.1

2 years ago

1.1.2

2 years ago

0.0.0-pr1581.2

2 years ago

0.0.0-pr1581.3

2 years ago

0.0.0-pr1581.1

2 years ago

0.0.0-pr1608.1

2 years ago

0.0.0-pr1549.1

2 years ago

0.0.0-pr1558.1

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.8

2 years ago

0.0.0-pr1586.1

2 years ago

0.0.0-pr1552.3

2 years ago

0.0.0-pr1552.2

2 years ago

0.0.0-pr1552.1

2 years ago

0.0.0-pr1545.1

2 years ago

0.0.0-pr1532.1

2 years ago

0.0.0-pr1496.1

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

0.0.0-pr1452.1

2 years ago

0.0.0-pr1463.1

2 years ago

0.0.0-pr1367.1

2 years ago

0.0.0-pr1392.1

2 years ago

0.0.0-pr1365.1

3 years ago

1.0.5

3 years ago

0.0.0-pr1344.1

3 years ago

0.0.0-pr1340.1

3 years ago

1.0.4

3 years ago

0.0.0-pr1310.1

3 years ago

0.0.0-pr1305.1

3 years ago

1.0.3

3 years ago

0.0.0-pr1242.4

3 years ago

0.0.0-pr1242.2

3 years ago

0.0.0-pr1242.3

3 years ago

0.0.0-pr1242.1

3 years ago

0.0.0-pr1174.1

3 years ago

0.0.0-pr1161.2

3 years ago

0.0.0-pr1161.1

3 years ago

0.0.0-pr1158.1

3 years ago

0.0.0-pr1102.1

3 years ago

0.0.0-pr1031.1

3 years ago

1.0.2

3 years ago

0.0.0-pr706.36

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.0-pr997.1

3 years ago

0.0.0-pr993.1

3 years ago

0.0.0-pr706.35

3 years ago

0.0.0-pr706.34

3 years ago

0.0.0-pr706.32

3 years ago

0.0.0-pr706.33

3 years ago

0.0.0-pr950.7

3 years ago

0.0.0-pr950.6

3 years ago

0.0.0-pr950.5

3 years ago

0.0.0-pr706.30

3 years ago

0.0.0-pr706.31

3 years ago

0.0.0-pr963.1

3 years ago

0.0.0-pr706.28

3 years ago

0.0.0-pr706.29

3 years ago

0.0.0-pr965.1

3 years ago

0.0.0-pr959.1

3 years ago

0.0.0-pr950.4

3 years ago

0.0.0-pr960.1

3 years ago

0.0.0-pr956.1

3 years ago

0.0.0-pr706.27

3 years ago

0.0.0-pr950.3

3 years ago

0.0.0-pr706.20

3 years ago

0.0.0-pr706.21

3 years ago

0.0.0-pr706.22

3 years ago

0.0.0-pr706.23

3 years ago

0.0.0-pr706.24

3 years ago

0.0.0-pr706.25

3 years ago

0.0.0-pr706.26

3 years ago

0.0.0-pr950.2

3 years ago

0.0.0-pr950.1

3 years ago

0.0.0-pr930.1

3 years ago

0.0.0-pr941.2

3 years ago

0.0.0-pr941.1

3 years ago

0.0.0-pr706.19

3 years ago

0.0.0-pr706.18

3 years ago

0.0.0-pr920.1

3 years ago

0.0.0-pr905.4

3 years ago

0.0.0-pr905.3

3 years ago

0.0.0-pr922.1

3 years ago

0.0.0-pr706.17

3 years ago

0.0.0-pr919.1

3 years ago

0.0.0-pr919.2

3 years ago

0.0.0-pr706.16

3 years ago

0.0.0-pr911.2

3 years ago

0.0.0-pr905.2

3 years ago

0.0.0-pr706.15

3 years ago

0.0.0-pr911.1

3 years ago

0.0.0-pr877.9

3 years ago

0.0.0-pr877.8

3 years ago

0.0.0-pr905.1

3 years ago

0.0.0-pr877.7

3 years ago

0.0.0-pr706.14

3 years ago

0.0.0-pr901.1

3 years ago

0.0.0-pr877.6

3 years ago

0.0.0-pr706.13

3 years ago

0.0.0-pr877.5

3 years ago

0.0.0-pr877.4

3 years ago

0.0.0-pr706.12

3 years ago

0.0.0-pr877.2

3 years ago

0.0.0-pr877.3

3 years ago

0.0.0-pr706.10

3 years ago

0.0.0-pr706.11

3 years ago

0.0.0-pr885.1

3 years ago

0.0.0-pr877.1

3 years ago

0.0.0-pr706.9

3 years ago

0.0.0-pr706.8

3 years ago

0.0.0-pr706.7

3 years ago

0.0.0-pr706.6

3 years ago

0.0.0-pr706.5

3 years ago

0.0.0-pr706.4

3 years ago

0.0.0-pr706.3

3 years ago

0.0.0-pr862.2

3 years ago

0.0.0-pr862.1

3 years ago

0.0.0-pr706.2

3 years ago

0.0.0-pr706.1

3 years ago

1.0.0-pr706

3 years ago

1.0.0-next.60

3 years ago

1.0.0-next.59

3 years ago

1.0.0-next.58

3 years ago

1.0.0-next.57

3 years ago

1.0.0-next.56

3 years ago

1.0.0-next.55

3 years ago

1.0.0-next.54

3 years ago

1.0.0-next.53

3 years ago

1.0.0-next.52

4 years ago

1.0.0-next.51

4 years ago

1.0.0-next.50

4 years ago

1.0.0-next.49

4 years ago

1.0.0-next.47

4 years ago

1.0.0-next.44

4 years ago

1.0.0-next.40

4 years ago

1.0.0-next.39

4 years ago

1.0.0-next.38

4 years ago

1.0.0-next.37

4 years ago

1.0.0-next.35

4 years ago

1.0.0-next.34

4 years ago

1.0.0-next.33

4 years ago

1.0.0-next.32

4 years ago

1.0.0-next.31

4 years ago

1.0.0-next.28

4 years ago

1.0.0-next.26

4 years ago

1.0.0-next.22

4 years ago

0.7.7-next.6

4 years ago

1.0.0-next.5

4 years ago

1.0.0-next.4

4 years ago

1.0.0-next.3

4 years ago

1.0.0-next.2

4 years ago

1.0.0-next.0

4 years ago

1.0.0-next.1

4 years ago

0.7.6

4 years ago

0.7.5

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.6.5

4 years ago

0.7.0

4 years ago

0.6.4

5 years ago

0.6.3

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago