Newer
Older
/* eslint-disable no-param-reassign */
import { v4 as uuidv4 } from 'uuid';
import { Decoration, DecorationSet } from 'prosemirror-view';
import {
ySyncPluginKey,
relativePositionToAbsolutePosition,
absolutePositionToRelativePosition,
} from 'y-prosemirror';
import { CommentDecorationPluginKey } from './CommentDecorationPlugin';
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
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]) => {
}).filter(value => {
return 'from' in value && 'to' in value;
});
}
if (ystate?.binding) {
const { doc, type, binding } = ystate;
if (typeof annotation.data.yjsFrom !== 'object') {
annotation.data.yjsFrom = absolutePositionToRelativePosition(
annotation.data.pmFrom,
type,
binding.mapping,
);
}
if (typeof annotation.data.yjsTo !== 'object') {
annotation.data.yjsTo = absolutePositionToRelativePosition(
annotation.data.pmTo,
type,
binding.mapping,
);
}
const from = relativePositionToAbsolutePosition(
doc,
type,
binding.mapping,
);
const to = relativePositionToAbsolutePosition(
doc,
type,
decorations.push(
Decoration.inline(
from,
to,
{
class: 'comment',
'data-id': annotation.id,
},
{
id: annotation.id,
data: annotation,
inclusiveEnd: true,
},
),
);
});
} else {
this.allCommentsList().forEach(annotation => {
{
class: 'comment',
'data-id': annotation.id,
},
{
id: annotation.id,
data: annotation,
inclusiveEnd: true,
},
),
);
});
}
this.decorations = DecorationSet.create(state.doc, decorations);
}
updateCommentPostions(ystate) {
this.options.map.doc.transact(() => {
this.decorations.find().forEach(deco => {
const { id } = deco.spec;
const newFrom = absolutePositionToRelativePosition(
deco.from,
ystate.type,
ystate.binding.mapping,
);
const newTo = absolutePositionToRelativePosition(
deco.to,
ystate.type,
ystate.binding.mapping,
);
const annotation = this.options.map.get(id);
annotation.from = newFrom;
annotation.to = newTo;
annotation.data.pmFrom = relativePositionToAbsolutePosition(
ystate.doc,
ystate.type,
newFrom,
ystate.binding.mapping,
);
annotation.data.pmTo = relativePositionToAbsolutePosition(
ystate.doc,
ystate.type,
newTo,
ystate.binding.mapping,
);
this.options.map.set(id, annotation);
});
}, CommentDecorationPluginKey);
}
const action = transaction.getMeta(CommentDecorationPluginKey);
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);
}
if (action.type === 'createDecorations') {
this.createDecorations(state);
}
// this.createDecorations(state);
const ystate = ySyncPluginKey.getState(state);
this.decorations = this.decorations.map(
transaction.mapping,
transaction.doc,
);
if (ystate?.isChangeOrigin) {
this.updateCommentPostions(ystate);
this.createDecorations(state);
return this;
}
// Not YJS
map.forEach((annotation, _) => {
if ('from' in annotation && 'to' in annotation) {
annotation.data.pmFrom = transaction.mapping.map(
annotation.data.pmFrom,
);
annotation.data.pmTo = transaction.mapping.map(annotation.data.pmTo);
}
});