0.2.0 • Published 7 months ago

prosemirror-suggestcat-plugin-react v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

prosemirror-suggestcat-plugin

made by Emergence Engineering

Made by Emergence-Engineering

Basic UI for prosemirror-suggestcat-plugin in React.

Features

feature-gif

  • A slash menu to select and filter commands, implemented with prosemirror-slash-menu-react
  • A button over selection to open the menu
  • An overlay to show/cancel/reject suggestions

How to use?

import { SlashMenuPlugin } from "prosemirror-slash-menu";
import {
  promptCommands,
  ProsemirrorSuggestcatPluginReact,
  slashOpeningCondition,
} from "prosemirror-suggestcat-plugin-react";

const Editor: FC = () => {
  // Needed for re-renders on every tr.
  const [editorState, setEditorState] = useState<EditorState>();
  const [editorView, setEditorView] = useState<EditorView>();
  const editorRef = useRef<HTMLDivElement>(null);
  useEffect(() => {
    const state = EditorState.create({
      doc: schema.nodeFromJSON(initialDoc),
      plugins: [
        SlashMenuPlugin(promptCommands, undefined, slashOpeningCondition, true),
      ],
    });
    const view = new EditorView(document.querySelector("#editor"), {
      state,
      dispatchTransaction: (tr) => {
        try {
          const newState = view.state.apply(tr);
          view.updateState(newState);
          setEditorState(newState);
        } catch (e) {}
      },
    });
    setEditorView(view);
    setEditorState(view.state);
    return () => {
      view.destroy();
    };
  }, [editorRef]);

  const slashMenuPopperRef = useMemo(() => {
    if (!editorView || !editorView?.state) {
      return;
    }

    const currentNode = editorView.domAtPos(
      editorView.state.selection.to,
    )?.node;

    if (!currentNode) {
      return;
    }

    if (currentNode instanceof Text) {
      return currentNode.parentElement;
    }

    return currentNode instanceof HTMLElement ? currentNode : undefined;
  }, [editorView?.state?.selection, window.scrollY]);

  return (
    <Root>
      <StyledEditor id="editor" ref={editorRef} />
      {editorView && editorView?.state && slashMenuPopperRef && (
        <ProsemirrorSuggestcatPluginReact
          editorView={editorView}
          editorState={editorView?.state}
          domReference={slashMenuPopperRef}
        />
      )}
    </Root>
  );
};

Props

  • domReference This is a Popper referenceObject under which the menu and suggestion overlay will appear. In our example it's under the selected paragraph.
  • editorView prosemirror EditorView
  • editorState prosemirror EditorState

Styles

  • Import the styles from the package
import "prosemirror-suggestcat-plugin-react/dist/styles/styles.css";

UI behaviour

Navigation is intuitive with keyboard using arrows, Tab, Enter, Esc etc. and also with clicks. The prompt menu is using prosemirror-slash-menu-react, the exact behaviour is detailed in the Readme.

Customization

This package is not made with customization as a priority, it's intended to be a quick and easy way to use Suggestcat. With that in mind, with providing your own popper reference object and replacing our CSS classes, it is fairly simple to modify it to blend more into your app. You could also pass your own commands into SlashMenuPlugin to replace or delete the icons, change the labels. However, keep in mind that the actual command function needs to be the same to properly work together with prosemirror-suggestcat-plugin

0.1.0

8 months ago

0.2.0

7 months ago

0.0.21

8 months ago

0.0.20

2 years ago

0.0.18

2 years ago

0.0.19

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago