Skip to content
Snippets Groups Projects
Commit 96c38b12 authored by chris's avatar chris
Browse files

comment memo for now

parent 35b3aa2e
No related branches found
No related tags found
1 merge request!161Connect ui
...@@ -12,118 +12,118 @@ const ConnectedCommentStyled = styled.div` ...@@ -12,118 +12,118 @@ const ConnectedCommentStyled = styled.div`
`; `;
export default ({ comment, top, commentId }) => { export default ({ comment, top, commentId }) => {
const MemorizedComponent = memo(() => { // const MemorizedComponent = memo(() => {
const { const {
view, view,
view: { view: {
main: { main: {
props: { user }, props: { user },
},
}, },
app, },
activeView, app,
} = useContext(WaxContext); activeView,
} = useContext(WaxContext);
const { state, dispatch } = activeView; const { state, dispatch } = activeView;
const viewId = comment.attrs.viewid; const viewId = comment.attrs.viewid;
const allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId( const allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId(
view[viewId].state, view[viewId].state,
comment, comment,
); );
const commentMark = state.schema.marks.comment; const commentMark = state.schema.marks.comment;
let active = false; let active = false;
const styles = { const styles = {
top: `${top}px`, top: `${top}px`,
}; };
const commentPlugin = app.PmPlugins.get('commentPlugin'); const commentPlugin = app.PmPlugins.get('commentPlugin');
const activeComment = commentPlugin.getState(activeView.state).comment; const activeComment = commentPlugin.getState(activeView.state).comment;
if (activeComment && commentId === activeComment.attrs.id) active = true; 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),
};
comment.attrs.conversation.push(obj);
allCommentsWithSameId.forEach(singleComment => {
dispatch(
tr.addMark(
singleComment.pos,
singleComment.pos + singleComment.nodeSize,
commentMark.create({
...((comment && comment.attrs) || {}),
conversation: comment.attrs.conversation,
}),
),
);
});
};
const onClickBox = () => { const onClickPost = content => {
if (active) { const { tr } = state;
view[viewId].focus();
return false;
}
const maxPos = maxBy(allCommentsWithSameId, 'pos'); const obj = {
maxPos.pos += last(allCommentsWithSameId).node.nodeSize; content,
displayName: user.username,
timestamp: Math.floor(Date.now() / 300000),
};
comment.attrs.conversation.push(obj);
view[viewId].dispatch( allCommentsWithSameId.forEach(singleComment => {
view[viewId].state.tr.setSelection( dispatch(
new TextSelection( tr.addMark(
view[viewId].state.tr.doc.resolve(maxPos.pos, maxPos.pos), singleComment.pos,
), singleComment.pos + singleComment.nodeSize,
commentMark.create({
...((comment && comment.attrs) || {}),
conversation: comment.attrs.conversation,
}),
), ),
); );
});
};
const onClickBox = () => {
if (active) {
view[viewId].focus(); view[viewId].focus();
return true; return false;
}; }
const onClickResolve = () => { const maxPos = maxBy(allCommentsWithSameId, 'pos');
let maxPos = comment.pos; maxPos.pos += last(allCommentsWithSameId).node.nodeSize;
let minPos = comment.pos;
allCommentsWithSameId.forEach(singleComment => { view[viewId].dispatch(
const markPosition = DocumentHelpers.findMarkPosition( view[viewId].state.tr.setSelection(
state, new TextSelection(
singleComment.pos, view[viewId].state.tr.doc.resolve(maxPos.pos, maxPos.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();
};
return (
<ConnectedCommentStyled data-box={commentId} style={styles}>
<CommentBox
key={commentId}
active={active}
commentId={commentId}
commentData={comment.attrs.conversation}
onClickPost={onClickPost}
onClickBox={onClickBox}
onClickResolve={onClickResolve}
/>
</ConnectedCommentStyled>
); );
});
return <MemorizedComponent />; 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();
};
return (
<ConnectedCommentStyled data-box={commentId} style={styles}>
<CommentBox
key={commentId}
active={active}
commentId={commentId}
commentData={comment.attrs.conversation}
onClickPost={onClickPost}
onClickBox={onClickBox}
onClickResolve={onClickResolve}
/>
</ConnectedCommentStyled>
);
// });
// return <MemorizedComponent />;
}; };
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment