Newer
Older
/* eslint-disable consistent-return */
import { inRange, sortBy, last } from 'lodash';
import { Plugin, PluginKey } from 'prosemirror-state';
import { Decoration, DecorationSet } from 'prosemirror-view';
const getComment = (state, context) => {
const {
options: { comments },
} = context;
if (!comments?.length) return;
let commentData = comments.filter(comment =>
inRange(state.selection.from, comment.from, comment.to),
);
commentData = sortBy(commentData, ['from']);
if (commentData.length > 0) {
return {
from: last(commentData).from,
to: last(commentData).to,
attrs: last(commentData).data,
id: last(commentData).id,
// contained: commentOnSelection.contained,
};
}
return {};
state: {
init: (_, state) => {
},
apply(tr, prev, _, newState) {
let createDecoration;
if (comment) {
createDecoration = DecorationSet.create(newState.doc, [
Decoration.inline(comment.from, comment.to, {
]);
}
return {
comment,
const commentPluginState = state && commentPlugin.getState(state);
return commentPluginState.createDecoration;