Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, {
Fragment,
useState,
useEffect,
useContext,
useRef
} from "react";
import styled from "styled-components";
import { WaxContext } from "wax-prosemirror-core";
export default ({ comment, activeView, user }) => {
const commentInput = useRef(null);
const [currentUser, setCurrentuser] = useState(user);
const [commentAnnotation, setCommentAnnotation] = useState(comment);
const { state, dispatch } = activeView;
const { attrs: { conversation } } = comment;
console.log("dddd", conversation);
const handleKeyDown = event => {
if (event.key === "Enter" || event.which === 13) {
saveComment();
}
};
const saveComment = () => {
const { current: { value } } = commentInput;
const { tr, doc } = state;
const commentMark = state.schema.marks.comment;
const obj = { [user.username]: value };
commentAnnotation.attrs.conversation.push(obj);
dispatch(
tr.addMark(
commentAnnotation.pos,
commentAnnotation.pos + commentAnnotation.node.nodeSize,
commentMark.create({
...((commentAnnotation && commentAnnotation.attrs) || {}),
conversation: commentAnnotation.attrs.conversation
})
)
);
};
return (
<Fragment>
<input
type="text"
ref={commentInput}
placeholder="add a new comment"
onKeyPress={handleKeyDown}
autoFocus
/>
<button onClick={saveComment}>Post</button>
<button>Cancel</button>
</Fragment>
);
};