guardian / prosemirror-noting

Prosemirror plugin that adds the ability to have ranges added to the document that expand and contract around dependent on the input.
MIT License
57 stars 8 forks source link
production

prosemirror-noting

This plugin adds the ability to have ranges added to the document that expand and contract around dependent on the input. These notes are represented as marks in the document.

Very basic demo here


Install

npm install @guardian/prosemirror-noting

or

yarn add @guardian/prosemirror-noting

Usage

Add the mark to the schema

const mySchema = new Schema({
  nodes,
  marks: Object.assign({}, marks, {
    note: createNoteMark(
      {
        note: "span.note",
      },
      (meta) => ({
        class: meta.hidden ? "note--collapsed" : "",
        title: "My Title",
        contenteditable: !meta.hidden,
      })
    ),
  }),
});

Add the plugin to the state

const historyPlugin = history();
const noterPlugin = noter(mySchema.marks.note, doc, historyPlugin);

new EditorView(document.querySelector("#editor"), {
  state: EditorState.create({
    doc: DOMParser.fromSchema(mySchema).parse(
      document.querySelector("#content")
    ),
    plugins: [
      keymap({
        F10: toggleNote("note"),
      }),
      historyPlugin,
      noterPlugin,
    ],
  }),
});

And import the css (if needed) from prosemirror-noting/dist/noting.css.

API

createNoteMark(typeTagMap: string | object, attrGenerator: function): MarkType

Returns a mark to be added to the schema.

toggleNote(type: string = "note", cursorToEnd = false): CommandFunction

Returns a command used for toggling notes based on the cursor position.

Toggle note works in the following way:

toggleAllNotes(type: string): CommandFunction

Returns a command used for toggling all notes.

setNoteMeta(id: string, meta: object): CommandFunction

Returns a command to set the meta for a note id

noter(markType: MarkType, historyPlugin: Plugin, onNoteCreate: function = () => {}): Plugin

Returns the plugin to add to prosemirror

Roadmap