Newer
Older
import { Plugin, PluginKey } from 'prosemirror-state';
import { Decoration, DecorationSet } from 'prosemirror-view';
import { CommentDecorationPluginKey } from './CommentDecorationPlugin';
const getComment = state => {
const commentsMap = CommentDecorationPluginKey.getState(state).getMap();
if (commentsMap.size === 0) return;
let commentData = [];
commentsMap.forEach(comment => {
if (inRange(state.selection.from, comment.data.pmFrom, comment.data.pmTo)) {
commentData.push(comment);
}
});
(state.selection.from !== state.selection.to &&
last(commentData).data.conversation.length === 0) ||
(state.selection.from === state.selection.to &&
last(commentData).data.conversation.length !== 0)
state: {
init: (_, state) => {
return { comment: getComment(state) };
},
apply(tr, prev, _, newState) {
const comment = getComment(newState);
let createDecoration;
if (comment) {
createDecoration = DecorationSet.create(newState.doc, [
Decoration.inline(comment.data.pmFrom, comment.data.pmTo, {
]);
}
return {
comment,
const commentPluginState = state && commentPlugin.getState(state);
return commentPluginState.createDecoration;