Skip to content
Snippets Groups Projects
Commit 1dd39326 authored by chris's avatar chris
Browse files

fix position when it's first computed

parent 2bc93ebb
No related branches found
No related tags found
1 merge request!128add TODO's
...@@ -34,7 +34,6 @@ export default ({ comment, activeView, user }) => { ...@@ -34,7 +34,6 @@ export default ({ comment, activeView, user }) => {
const obj = { [user.username]: value }; const obj = { [user.username]: value };
commentAnnotation.attrs.conversation.push(obj); commentAnnotation.attrs.conversation.push(obj);
const allComments = DocumentHelpers.findAllCommentsWithSameId(state); const allComments = DocumentHelpers.findAllCommentsWithSameId(state);
allComments.forEach(singleComment => { allComments.forEach(singleComment => {
dispatch( dispatch(
...@@ -67,7 +66,6 @@ export default ({ comment, activeView, user }) => { ...@@ -67,7 +66,6 @@ export default ({ comment, activeView, user }) => {
// saveComment(); // saveComment();
} }
// TODO pass correct comment pos for notes
if (conversation.length === 0 && value === '') { if (conversation.length === 0 && value === '') {
const commentPosition = DocumentHelpers.findMarkPosition(activeView, comment.pos, 'comment'); const commentPosition = DocumentHelpers.findMarkPosition(activeView, comment.pos, 'comment');
dispatch(state.tr.removeMark(commentPosition.from, commentPosition.to, commentMark)); dispatch(state.tr.removeMark(commentPosition.from, commentPosition.to, commentMark));
...@@ -75,8 +73,7 @@ export default ({ comment, activeView, user }) => { ...@@ -75,8 +73,7 @@ export default ({ comment, activeView, user }) => {
}; };
const resolveComment = () => { const resolveComment = () => {
// TODO pass correct comment pos for notes const commentPosition = DocumentHelpers.findMarkPosition(activeView, comment.pos, 'comment');
const commentPosition = DocumentHelpers.findMarkPoistion(activeView, comment.pos, 'comment');
dispatch(state.tr.removeMark(commentPosition.from, commentPosition.to, commentMark)); dispatch(state.tr.removeMark(commentPosition.from, commentPosition.to, commentMark));
}; };
......
...@@ -60,29 +60,9 @@ export default ({ comment, top, dataBox }) => { ...@@ -60,29 +60,9 @@ export default ({ comment, top, dataBox }) => {
let commentPos = comment.pos; let commentPos = comment.pos;
const viewId = comment.attrs.viewid; const viewId = comment.attrs.viewid;
if (comment.attrs.group !== 'main') {
const allInlineNodes = DocumentHelpers.findInlineNodes(
view[viewId].state.doc,
);
allInlineNodes.forEach(node => {
if (node.node.marks.length > 0) {
node.node.marks.forEach(mark => {
if (
mark.type.name === 'comment' &&
mark.attrs.id === comment.attrs.id
) {
commentPos = node.pos;
}
});
}
});
}
view[viewId].dispatch( view[viewId].dispatch(
view[viewId].state.tr.setSelection( view[viewId].state.tr.setSelection(
new TextSelection( new TextSelection(view[viewId].state.tr.doc.resolve(commentPos + 1, commentPos + 1)),
view[viewId].state.tr.doc.resolve(commentPos + 1, commentPos + 1),
),
), ),
); );
...@@ -93,19 +73,8 @@ export default ({ comment, top, dataBox }) => { ...@@ -93,19 +73,8 @@ export default ({ comment, top, dataBox }) => {
<> <>
<Transition in={animate} timeout={1000}> <Transition in={animate} timeout={1000}>
{state => ( {state => (
<CommentBoxStyled <CommentBoxStyled top={top} state={state} data-box={dataBox} active={active} onClick={setCommentActive}>
top={top} <Comment comment={comment} active={active} activeView={activeView} user={user} />
state={state}
data-box={dataBox}
active={active}
onClick={setCommentActive}
>
<Comment
comment={comment}
active={active}
activeView={activeView}
user={user}
/>
</CommentBoxStyled> </CommentBoxStyled>
)} )}
</Transition> </Transition>
......
import { Mark } from 'prosemirror-model'; import { Mark } from 'prosemirror-model';
import React, { import React, { useContext, useState, useEffect, useMemo, useCallback } from 'react';
useContext, import { each, uniqBy, sortBy, throttle } from 'lodash';
useState,
useEffect,
useMemo,
useCallback,
} from 'react';
import { each, uniqBy, sortBy } from 'lodash';
import { WaxContext } from 'wax-prosemirror-core'; import { WaxContext } from 'wax-prosemirror-core';
import { DocumentHelpers } from 'wax-prosemirror-utilities'; import { DocumentHelpers } from 'wax-prosemirror-utilities';
import BoxList from './BoxList'; import BoxList from './BoxList';
export default ({ area }) => { export default ({ area }) => {
const { const {
view,
view: { main }, view: { main },
app, app,
activeView, activeView,
...@@ -29,10 +24,11 @@ export default ({ area }) => { ...@@ -29,10 +24,11 @@ export default ({ area }) => {
let top = 0; let top = 0;
const allCommentsTop = []; const allCommentsTop = [];
each(marksNodes[area], (markNode, pos) => { const nodesMarksToIterrate = marksNodes[area] === 'main' ? sortBy(marksNodes[area], ['pos']) : marksNodes[area];
each(nodesMarksToIterrate, (markNode, pos) => {
const WaxSurface = main.dom.getBoundingClientRect(); const WaxSurface = main.dom.getBoundingClientRect();
const id = const id = markNode instanceof Mark ? markNode.attrs.id : markNode.node.attrs.id;
markNode instanceof Mark ? markNode.attrs.id : markNode.node.attrs.id;
const activeComment = commentPlugin.getState(activeView.state).comment; const activeComment = commentPlugin.getState(activeView.state).comment;
...@@ -42,19 +38,12 @@ export default ({ area }) => { ...@@ -42,19 +38,12 @@ export default ({ area }) => {
// annotation top // annotation top
if (area === 'main') { if (area === 'main') {
markNodeEl = document.querySelector(`[data-id="${id}"]`); markNodeEl = document.querySelector(`[data-id="${id}"]`);
if (markNodeEl) if (markNodeEl) annotationTop = markNodeEl.getBoundingClientRect().top - WaxSurface.top;
annotationTop =
markNodeEl.getBoundingClientRect().top - WaxSurface.top;
} else { } else {
const panelWrapper = document.getElementsByClassName('panelWrapper'); const panelWrapper = document.getElementsByClassName('panelWrapper');
const panelWrapperHeight = panelWrapper[0].getBoundingClientRect() const panelWrapperHeight = panelWrapper[0].getBoundingClientRect().height;
.height; markNodeEl = document.querySelector('#notes-container').querySelector(`[data-id="${id}"]`);
markNodeEl = document if (markNodeEl) annotationTop = markNodeEl.getBoundingClientRect().top - panelWrapperHeight - 50;
.querySelector('#notes-container')
.querySelector(`[data-id="${id}"]`);
if (markNodeEl)
annotationTop =
markNodeEl.getBoundingClientRect().top - panelWrapperHeight - 50;
} }
// get height of this markNode box // get height of this markNode box
...@@ -118,28 +107,27 @@ export default ({ area }) => { ...@@ -118,28 +107,27 @@ export default ({ area }) => {
}); });
useEffect(() => { useEffect(() => {
setMarksNodes(updateMarks(main)); setMarksNodes(updateMarks(view));
setPosition(setTops()); setPosition(setTops());
}, [JSON.stringify(updateMarks(main)), JSON.stringify(setTops())]); }, [JSON.stringify(updateMarks(view)), JSON.stringify(setTops())]);
const CommentTrackComponent = useMemo( const CommentTrackComponent = useMemo(
() => ( () => <BoxList commentsTracks={marksNodes[area] || []} area={area} view={main} position={position} />,
<BoxList
commentsTracks={marksNodes[area] || []}
area={area}
view={main}
position={position}
/>
),
[marksNodes[area] || [], position], [marksNodes[area] || [], position],
); );
return <>{CommentTrackComponent}</>; return <>{CommentTrackComponent}</>;
}; };
// TODO if allInlineNodes and allBlockNodes count don't change, do not compute again
const updateMarks = view => { const updateMarks = view => {
if (view) { if (view.main) {
const allBlockNodes = DocumentHelpers.findBlockNodes(view.state.doc); const allInlineNodes = [];
const allInlineNodes = DocumentHelpers.findInlineNodes(view.state.doc);
Object.keys(view).forEach(eachView => {
allInlineNodes.push(...DocumentHelpers.findInlineNodes(view[eachView].state.doc));
});
const allBlockNodes = DocumentHelpers.findBlockNodes(view.main.state.doc);
const finalMarks = []; const finalMarks = [];
const finalNodes = []; const finalNodes = [];
...@@ -152,7 +140,6 @@ const updateMarks = view => { ...@@ -152,7 +140,6 @@ const updateMarks = view => {
mark.type.name === 'deletion' || mark.type.name === 'deletion' ||
mark.type.name === 'format_change' mark.type.name === 'format_change'
) { ) {
// THIS POSITION REFERS TO THE MAIN VIEW
mark.pos = node.pos; mark.pos = node.pos;
finalMarks.push(mark); finalMarks.push(mark);
} }
...@@ -170,10 +157,8 @@ const updateMarks = view => { ...@@ -170,10 +157,8 @@ const updateMarks = view => {
const groupedMarkNodes = {}; const groupedMarkNodes = {};
sortBy(nodesAndMarks, ['pos']).forEach(markNode => { nodesAndMarks.forEach(markNode => {
const markNodeAttrs = markNode.attrs const markNodeAttrs = markNode.attrs ? markNode.attrs : markNode.node.attrs;
? markNode.attrs
: markNode.node.attrs;
if (!groupedMarkNodes[markNodeAttrs.group]) { if (!groupedMarkNodes[markNodeAttrs.group]) {
groupedMarkNodes[markNodeAttrs.group] = [markNode]; groupedMarkNodes[markNodeAttrs.group] = [markNode];
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment