Skip to content
Snippets Groups Projects
CommentPlugin.js 1.42 KiB
Newer Older
chris's avatar
chris committed
/* eslint-disable */

chris's avatar
chris committed
import { minBy, maxBy, last } from 'lodash';
import { Plugin, PluginKey } from 'prosemirror-state';
import { Decoration, DecorationSet } from 'prosemirror-view';
chris's avatar
chris committed
import { DocumentHelpers } from 'wax-prosemirror-core';
chris's avatar
chris committed
const commentPlugin = new PluginKey('commentPlugin');
chris's avatar
chris committed
const getComment = (state, context) => {
  const {
    options: { comments },
  } = context;
  if (!comments?.length) return;
  console.log('sds', comments);

  return {
    from: comments[0].from,
    to: comments[0].to,
    attrs: comments[0].data,
    id: comments[0].id,
    // contained: commentOnSelection.contained,
  };
chris's avatar
chris committed
c;
export default (key, context) => {
chris's avatar
chris committed
  return new Plugin({
    key: commentPlugin,
    state: {
      init: (_, state) => {
chris's avatar
chris committed
        return { comment: getComment(state, context) };
      },
      apply(tr, prev, _, newState) {
chris's avatar
chris committed
        const comment = getComment(newState, context);
        let createDecoration;
        if (comment) {
          createDecoration = DecorationSet.create(newState.doc, [
            Decoration.inline(comment.from, comment.to, {
chris's avatar
chris committed
              class: 'active-comment',
            }),
chris's avatar
chris committed
          createDecoration,
chris's avatar
chris committed
      },
chris's avatar
chris committed
    props: {
      decorations: state => {
        const commentPluginState = state && commentPlugin.getState(state);
        return commentPluginState.createDecoration;
chris's avatar
chris committed
      },
    },
chris's avatar
chris committed
  });
};