Newer
Older
import React, { useState, useRef, useEffect } from 'react';
import styled from 'styled-components';
import { DocumentHelpers } from 'wax-prosemirror-utilities';
const SinlgeCommentRow = styled.div`
padding: 4px;
border-bottom: 1px solid #ffab20;
export default ({ comment, activeView, user, active }) => {
const commentInput = useRef(null);
const [commentAnnotation, setCommentAnnotation] = useState(comment);
const [commentInputValue, setcommentInputValue] = useState('');
const allCommentsWithSameId = DocumentHelpers.findAllMarksWithSameId(
state,
comment,
);
const commentMark = state.schema.marks.comment;
if (commentInput.current !== null && conversation.length === 0)
commentInput.current.focus();
const saveComment = event => {
event.stopPropagation();
const obj = {
content: value,
displayName: user.username,
timestamp: Math.floor(Date.now() / 300000),
};
allCommentsWithSameId.forEach(singleComment => {
dispatch(
tr.addMark(
singleComment.pos,
singleComment.pos + singleComment.nodeSize,
commentMark.create({
...((commentAnnotation && commentAnnotation.attrs) || {}),
conversation: commentAnnotation.attrs.conversation,
}),
),
const onBlur = () => {
const {
current: { value },
} = commentInput;
if (value !== '') {
}
if (conversation.length === 0 && value === '') {
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));
<input
type="text"
ref={commentInput}
placeholder="add a new comment"
onChange={updateCommentInputValue}
onKeyPress={handleKeyDown}
onClick={event => {
event.stopPropagation();
}}
<button
disabled={!active}
type="button"
onClick={event => saveComment(event)}
>
<button
disabled={!active}
type="button"
onClick={event => resolveComment(event)}
>
<SinlgeCommentRow key={uuidv4()}>
{`${singleComment.displayName} : ${singleComment.content}`}
</SinlgeCommentRow>