Newer
Older
/* eslint react/prop-types: 0 */
import React, { useLayoutEffect, useContext } from 'react';
import { WaxContext } from 'wax-prosemirror-core';
import {
ySyncPluginKey,
relativePositionToAbsolutePosition,
absolutePositionToRelativePosition,
} from 'y-prosemirror';
import CommentBubble from './CommentBubble';
import { CommentDecorationPluginKey } from '../../../plugins/CommentDecorationPlugin';
const {
activeView,
activeViewId,
const { state, dispatch } = activeView;
useLayoutEffect(() => {
const WaxSurface = activeView.dom.getBoundingClientRect();
const { selection } = activeView.state;
const { from, to } = selection;
const start = activeView.coordsAtPos(from);
const end = activeView.coordsAtPos(to);
const difference = end.top - start.top;
const top = end.top - WaxSurface.top - difference / 2 - 5;
setPosition({ ...position, left, top });
// if (context.app.config.get('config.YjsService')) {
// return createYjsComments(selection);
// }
const fromPos = state.tr.mapping.map(selection.from);
const toPos = state.tr.mapping.map(selection.to);
dispatch(
state.tr.setMeta(CommentDecorationPluginKey, {
type: 'addComment',
yjsFrom: fromPos,
yjsTo: toPos,
pmFrom: fromPos,
pmTo: toPos,
conversation: [],
title: '',
group,
viewId: activeViewId,
},
}),
);
dispatch(state.tr);
};
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const createYjsComments = selection => {
const ystate = ySyncPluginKey.getState(state);
const { doc, type, binding } = ystate;
const from = absolutePositionToRelativePosition(
selection.from,
type,
binding.mapping,
);
const to = absolutePositionToRelativePosition(
selection.to,
type,
binding.mapping,
);
dispatch(
state.tr.setMeta(CommentDecorationPluginKey, {
type: 'addComment',
from: relativePositionToAbsolutePosition(
doc,
type,
from,
binding.mapping,
),
to: relativePositionToAbsolutePosition(doc, type, to, binding.mapping),
data: {
yjsFrom: selection.from,
yjsTo: selection.to,
pmFrom: selection.from,
pmTo: selection.to,
type: 'comment',
conversation: [],
title: '',
group,
viewId: activeViewId,
},
}),
);
dispatch(state.tr);
};
const isCommentAllowed = () => {
let allowed = true;
state.doc.nodesBetween(
state.selection.$from.pos,
state.selection.$to.pos,
node.type.name === 'math_inline' ||
node.type.name === 'image'
comm.data.pmFrom === state.selection.from &&
comm.data.pmTo === state.selection.to,