Newer
Older
import React, { useContext, useMemo, useState, useEffect } from 'react';
import { TextSelection } from 'prosemirror-state';
import { last, maxBy } from 'lodash';
import styled from 'styled-components';
import { DocumentHelpers } from 'wax-prosemirror-utilities';
import { WaxContext } from 'wax-prosemirror-core';
import CommentBox from '../../ui/comments/CommentBox';
const ConnectedCommentStyled = styled.div`
margin-left: ${props => (props.active ? `${-20}px` : `${50}px`)};
export default ({ comment, top, commentId, recalculateTops }) => {
const [isActive, setIsActive] = useState(false);
const { state, dispatch } = activeView;
const viewId = comment.attrs.viewid;
if (view[viewId]) {
allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId(
view[viewId].state,
comment,
);
}
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]);
allCommentsWithSameId.forEach(singleComment => {
dispatch(
tr
.addMark(
singleComment.pos,
singleComment.pos + singleComment.nodeSize,
commentMark.create({
...((comment && comment.attrs) || {}),
conversation: comment.attrs.conversation,
}),
)
.setMeta('forceUpdate', true),
const maxPos = maxBy(allCommentsWithSameId, 'pos');
maxPos.pos += last(allCommentsWithSameId).node.nodeSize;
view[viewId].dispatch(
view[viewId].state.tr.setSelection(
new TextSelection(view[viewId].state.tr.doc.resolve(maxPos.pos)),
view[viewId].focus();
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 = (content, isNewComment) => {
// TODO Save into local storage
if (
comment.attrs.conversation.length === 0 &&
isNewComment &&
!clickPost
) {
const MemorizedComponent = useMemo(
() => (
<ConnectedCommentStyled
data-box={commentId}
style={styles}
>
<CommentBox
commentId={commentId}
key={commentId}
onClickPost={onClickPost}
onClickResolve={onClickResolve}
onTextAreaBlur={onTextAreaBlur}
recalculateTops={recalculateTops}
[isActive, top, comment.attrs.conversation.length],