Newer
Older
/* eslint-disable no-param-reassign */
import React, { useContext, useMemo, useState, useEffect } from 'react';
import { WaxContext, DocumentHelpers, Commands } from 'wax-prosemirror-core';
const ConnectedCommentStyled = styled.div`
margin-left: ${props => (props.active ? `${-20}px` : `${50}px`)};
transition: ${props =>
props.active && props.length ? `none!important` : `all 1.3s`};
export default ({ comment, top, commentId, recalculateTops, users }) => {
const [isActive, setIsActive] = useState(false);
const viewId = comment.attrs ? comment.attrs.viewid : comment.data.viewId;
const conversation = comment.attrs
? comment.attrs.conversation
: comment.data.conversation;
allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId(
const commentConfig = app.config.get('config.CommentsService');
const isReadOnly =
commentConfig && commentConfig.readOnly ? commentConfig.readOnly : false;
const showTitle =
commentConfig && commentConfig.showTitle ? commentConfig.showTitle : false;
const commentPlugin = app.PmPlugins.get('commentPlugin');
const activeComment = commentPlugin.getState(activeView.state).comment;
useEffect(() => {
setIsActive(false);
if (activeComment && commentId === activeComment.attrs.id) {
setIsActive(true);
recalculateTops();
}
}, [activeComment]);
const onClickPost = ({ commentValue, title }) => {
const currentUser = user || (users || []).find(u => u.currentUser === true);
displayName: currentUser
? currentUser.displayName || currentUser.username
: 'Anonymous',
userId: currentUser ? currentUser.userId : '1',
comment.attrs.title = title || comment.attrs.title;
activeView.dispatch(
activeView.state.tr.removeMark(
singleComment.pos,
singleComment.pos + singleComment.node.nodeSize,
commentMark,
),
);
const minPos = minBy(allCommentsWithSameId, 'pos');
const maxPos = maxBy(allCommentsWithSameId, 'pos');
Commands.createComment(
activeView.state,
activeView.dispatch,
comment.attrs.group,
comment.attrs.viewid,
comment.attrs.conversation,
comment.attrs.title,
minPos.pos,
maxPos.pos + last(allCommentsWithSameId).node.nodeSize,
);
const maxPos = maxBy(allCommentsWithSameId, 'pos');
maxPos.pos += last(allCommentsWithSameId).node.nodeSize;
pmViews[viewId].dispatch(
pmViews[viewId].state.tr.setSelection(
new TextSelection(pmViews[viewId].state.tr.doc.resolve(maxPos.pos)),
return true;
};
const onClickResolve = () => {
let maxPos = comment.pos;
let minPos = comment.pos;
allCommentsWithSameId.forEach(singleComment => {
const markPosition = DocumentHelpers.findMarkPosition(
state,
singleComment.pos,
'comment',
);
if (markPosition.from < minPos) minPos = markPosition.from;
if (markPosition.to > maxPos) maxPos = markPosition.to;
});
// if (allCommentsWithSameId.length > 1);
// maxPos += last(allCommentsWithSameId).node.nodeSize;
dispatch(state.tr.removeMark(minPos, maxPos, commentMark));
activeView.focus();
};
const onTextAreaBlur = () => {
if (comment.attrs.conversation.length === 0 && !clickPost) {
const MemorizedComponent = useMemo(
() => (
<ConnectedCommentStyled
commentId={commentId}
onClickPost={onClickPost}
onClickResolve={onClickResolve}
onTextAreaBlur={onTextAreaBlur}
recalculateTops={recalculateTops}
showTitle={showTitle}
title={comment.attrs.title}
[isActive, top, comment.attrs.conversation.length, users],