Newer
Older
/* eslint-disable no-param-reassign */
import React, { useContext, useMemo, useState, useEffect } from 'react';
import { TextSelection } from 'prosemirror-state';
import { last, maxBy } from 'lodash';
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 { state, dispatch } = activeView;
const viewId = comment.attrs.viewid;
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]);
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// const onClickPost = ({ commentValue, title }) => {
// setClickPost(true);
// const currentUser = user || (users || []).find(u => u.currentUser === true);
// const obj = {
// content: commentValue,
// displayName: currentUser
// ? currentUser.displayName || currentUser.username
// : 'Anonymous',
// userId: currentUser ? currentUser.userId : '1',
// timestamp: Math.floor(Date.now()),
// };
// comment.attrs.title = title || comment.attrs.title;
// comment.attrs.conversation.push(obj);
// const id = uuidv4();
// allCommentsWithSameId.forEach(singleComment => {
// activeView.dispatch(
// activeView.state.tr.removeMark(
// singleComment.pos,
// singleComment.pos + singleComment.node.nodeSize,
// commentMark,
// ),
// );
// if (activeViewId !== 'main') {
// activeView.dispatch(
// activeView.state.tr
// .addMark(
// singleComment.pos,
// singleComment.pos + singleComment.node.nodeSize,
// commentMark.create({
// id,
// group: comment.attrs.group,
// viewid: comment.attrs.viewid,
// conversation: comment.attrs.conversation,
// title: comment.attrs.title,
// }),
// )
// .setMeta('forceUpdate', true),
// );
// }
// });
// if (activeViewId === 'main') {
// Commands.createComment(
// pmViews.main.state,
// pmViews.main.dispatch,
// comment.attrs.group,
// comment.attrs.viewid,
// comment.attrs.conversation,
// );
// }
// activeView.focus();
// recalculateTops();
// };
const onClickPost = ({ commentValue, title }) => {
comment.attrs.title = title || comment.attrs.title;
activeView.dispatch(
activeView.state.tr.removeMark(
singleComment.pos,
singleComment.pos + singleComment.node.nodeSize,
commentMark,
),
);
activeView.dispatch(
activeView.state.tr
.addMark(
singleComment.pos,
singleComment.pos + singleComment.node.nodeSize,
commentMark.create({
id,
group: comment.attrs.group,
viewid: comment.attrs.viewid,
conversation: comment.attrs.conversation,
title: comment.attrs.title,
}),
)
.setMeta('forceUpdate', true),
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],