Newer
Older
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`
position: absolute;
`;
const {
view,
view: {
main: {
props: { user },
},
},
app,
activeView,
} = useContext(WaxContext);
const allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId(
const styles = {
top: `${top}px`,
};
const commentPlugin = app.PmPlugins.get('commentPlugin');
const activeComment = commentPlugin.getState(activeView.state).comment;
if (activeComment && commentId === activeComment.attrs.id) active = true;
const onClickPost = content => {
const { tr } = state;
const obj = {
content,
displayName: user.username,
timestamp: Math.floor(Date.now() / 300000),
};
allCommentsWithSameId.forEach(singleComment => {
dispatch(
tr.addMark(
singleComment.pos,
singleComment.pos + singleComment.nodeSize,
commentMark.create({
...((comment && comment.attrs) || {}),
conversation: comment.attrs.conversation,
73
74
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
const onClickBox = () => {
if (active) {
view[viewId].focus();
return false;
}
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, 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();
};
<ConnectedCommentStyled data-box={commentId} style={styles}>
<CommentBox
key={commentId}
active={active}
commentId={commentId}