Newer
Older
import React, { useState, useRef } from 'react';
import { v4 as uuidv4 } from 'uuid';
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 }) => {
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;
const saveComment = event => {
event.stopPropagation();
const { tr, doc } = state;
const obj = { [user.username]: value };
commentAnnotation.attrs.conversation.push(obj);
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(activeView, 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();
<input
type="text"
ref={commentInput}
placeholder="add a new comment"
onChange={updateCommentInputValue}
onKeyPress={handleKeyDown}
onClick={event => {
event.stopPropagation();
}}
<button type="button" onClick={event => saveComment(event)}>
<button type="button" onClick={event => resolveComment(event)}>
<SinlgeCommentRow key={uuidv4()}>{`${user.username} : ${singleComment[user.username]}`}</SinlgeCommentRow>