Giter Club home page Giter Club logo

prosemirror-suggestions's Introduction

@quartzy/prosemirror-suggestions

Latest Version on NPM Software License Build Status Total Downloads

A plugin for ProseMirror that helps you add suggestions to your editor.

"Suggestions" is a loose concept; while we originally designed this plugin around @mentions or #hash-tags, we hope that it can be adapted to other use cases.

This plugin will not render any UI for you. The goal is to handle triggers, giving you tools to render your own UI elements however you choose.

Installation

yarn add @quartzy/prosemirror-suggestions

Usage

An example project can be found in the example folder in this repository.

To use this plugin, first import the helpers:

import {
  addMentionNodes,
  suggestionsPlugin,
  triggerCharacter
} from "@quartzy/prosemirror-suggestions";
// Or, for commonjs:
//   const { addMentionNodes, suggestionsPlugin, triggerCharacter } = require('@quartzy/prosemirror-suggestions');

Next extend the nodes of your schema with addMentionNodes:

import { Schema } from "prosemirror-model";
import { schema as basicSchema } from "prosemirror-schema-basic";

const schema = new Schema({
  nodes: addMentionNodes(basicSchema.spec.nodes),
  marks: basicSchema.spec.marks
});

Finally add suggestionsPlugin(...) to your list of plugins:

const plugins = [
  suggestionsPlugin({
    debug: true,
    matcher: triggerCharacter("@", { allowSpaces: true }),
    onEnter(args) {
      console.log("start", args);
      return false;
    },
    onChange(args) {
      console.log("change", args);
      return false;
    },
    onExit(args) {
      console.log("stop", args);
      return false;
    },
    onKeyDown({ view, event }) {
      // console.log(event.key);
      return false;
    }
  })
  /* OTHER PLUGINS HERE */
];

suggestionsPlugin accepts the following options:

  • matcher — determines when to trigger suggestions; defaults to triggerCharacter('#')
  • suggestionClass — adds a class to the active suggestion; defaults to 'ProseMirror-suggestion'
  • debug — adds a blue background/border to the active suggestion; defaults to false
  • onEnter({ view, range, text}) — when entering suggestion mode (return value ignored)
  • onChange({view, range, text}) — when the current suggestion text changes (return value ignored)
  • onExit({view, range, text}) — when leaving suggestion mode (return value ignored)
  • onKeyDown({view, event}) — equivalent to the ProseMirror handleKeyDown callback, except only called when suggestion is active (return true to prevent default, otherwise return false)

Development

# Run tests
yarn test

# Build distributable
yarn build

# Build and watch for changes
yarn watch

Credits

License

The Apache License, v2.0. Please see License File for more information.

prosemirror-suggestions's People

Contributors

adrianheine avatar benjie avatar joelewis avatar tristanpemble avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.