Newer
Older
/* eslint-disable no-param-reassign */
import { v4 as uuidv4 } from 'uuid';
import { Decoration, DecorationSet } from 'prosemirror-view';
import CommentDecoration from './CommentDecoration';
import { CommentDecorationPluginKey } from './CommentDecorationPlugin';
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const randomId = () => {
return uuidv4();
};
export default class CommentState {
constructor(options) {
this.decorations = DecorationSet.empty;
this.options = options;
}
addComment(action) {
const { map } = this.options;
const { from, to, data } = action;
const id = randomId();
map.set(id, { id, from, to, data });
}
updateComment(action) {
const { map } = this.options;
const annotationToUpdate = map.get(action.id);
if (annotationToUpdate) {
annotationToUpdate.data = action.data;
}
}
deleteComment(id) {
const { map } = this.options;
map.delete(id);
}
commentsAt(position, to) {
return this.decorations.find(position, to || position).map(decoration => {
return new CommentDecoration(decoration);
});
}
allCommentsList() {
const { map } = this.options;
return Array.from(map, ([key, value]) => {
// eslint-disable-next-line prefer-object-spread
return Object.assign(Object.assign({}, value), {
id: key,
});
}).filter(value => {
return 'from' in value && 'to' in value;
});
}
createDecorations(state) {
const { map } = this.options;
const decorations = [];
const termList = Array.from(map, ([key, value]) => {
// eslint-disable-next-line prefer-object-spread
return Object.assign(Object.assign({}, value), {
id: key,
});
}).filter(value => {
return 'from' in value && 'to' in value;
});
termList.forEach(annotation => {
const { from, to } = annotation;
decorations.push(
Decoration.inline(
from,
to,
{
class: 'comment',
'data-id': annotation.id,
},
{
id: annotation.id,
data: annotation,
inclusiveEnd: true,
},
),
);
});
this.decorations = DecorationSet.create(state.doc, decorations);
}
apply(transaction, state) {
const action = transaction.getMeta(CommentDecorationPluginKey);
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
if (action && action.type) {
if (action.type === 'addComment') {
this.addComment(action);
}
if (action.type === 'updateComment') {
this.updateComment(action);
}
if (action.type === 'deleteComment') {
this.deleteComment(action.id);
}
this.createDecorations(state);
return this;
}
// manually map annotation positions
this.options.map.forEach((annotation, _) => {
if ('from' in annotation && 'to' in annotation) {
annotation.from = transaction.mapping.map(annotation.from);
annotation.to = transaction.mapping.map(annotation.to);
}
});
this.createDecorations(state);
return this;
}
}
// let res =
// annotation.to === state.selection.to &&
// state.selection.from === state.selection.to;
// console.log(res, transaction.docChanged);