Skip to content
Snippets Groups Projects
CommentsService.js 2.37 KiB
Newer Older
chris's avatar
chris committed
import { Service } from 'wax-prosemirror-core';
chris's avatar
chris committed
import CommentBubbleComponent from './components/ui/comments/CommentBubbleComponent';
import RightArea from './components/RightArea';
chris's avatar
chris committed
import commentMark from './schema/commentMark';
chris's avatar
chris committed
import CommentPlugin from './plugins/CommentPlugin';
chris's avatar
chris committed
// import CopyPasteCommentPlugin from './plugins/CopyPasteCommentPlugin';
chris's avatar
chris committed
import { CommentDecorationPlugin } from './plugins/CommentDecorationPlugin';
import './comments.css';
chris's avatar
chris committed
export default class CommentsService extends Service {
  allCommentsFromStates = [];
chris's avatar
chris committed
  boot() {
chris's avatar
chris committed
    // this.app.PmPlugins.add(
    //   'copyPasteCommentPlugin',
    //   CopyPasteCommentPlugin('copyPasteCommentPlugin', this.app.context),
    // );
chris's avatar
chris committed
    const commentsConfig = this.app.config.get('config.CommentsService');
chris's avatar
chris committed

    this.app.PmPlugins.add(
      'commentPlugin',
      CommentPlugin('commentPlugin', this.app.context),
    );

chris's avatar
chris committed
    const options = {
chris's avatar
chris committed
      existingComments: () => {
        const map = new Map();
        if (commentsConfig.setComments().length > 0) {
          commentsConfig.setComments().forEach(value => {
            map.set(value.id, value);
          });
        }
        return map;
      },
chris's avatar
chris committed
      onSelectionChange: items => {
        this.allCommentsFromStates = this.allCommentsFromStates.filter(
          comm =>
            (items.find(item => item.id === comm.id) || {}).id !== comm.id,
        );
        this.allCommentsFromStates = this.allCommentsFromStates.concat([
          ...items,
        ]);
        commentsConfig.getComments(this.allCommentsFromStates);
chris's avatar
chris committed

        this.app.context.setOption({ comments: this.allCommentsFromStates });
chris's avatar
chris committed
      },
chris's avatar
chris committed
    };

    this.app.PmPlugins.add(
chris's avatar
chris committed
      'CommentDecorationPlugin',
      CommentDecorationPlugin('commentDecorationPlugin', options),
chris's avatar
chris committed
    );
chris's avatar
chris committed

chris's avatar
chris committed
    const createOverlay = this.container.get('CreateOverlay');
    const layout = this.container.get('Layout');
    createOverlay(
      CommentBubbleComponent,
chris's avatar
chris committed
      {},
chris's avatar
chris committed
        nodeType: '',
chris's avatar
chris committed
        markType: '',
        followCursor: false,
chris's avatar
chris committed
        selection: true,
      },
chris's avatar
chris committed
    layout.addComponent('rightArea', RightArea);
chris's avatar
chris committed
  }

  register() {
    const commentConfig = this.config.get('config.CommentsService');
chris's avatar
chris committed
    const createMark = this.container.get('CreateMark');
        comment: commentMark(commentConfig?.showTitle || false),
chris's avatar
chris committed
      { toWaxSchema: true },
chris's avatar
chris committed
  }
}