Skip to content
Snippets Groups Projects
Commit 4b958fcb authored by chris's avatar chris
Browse files

mark position

parent 799e5984
No related branches found
No related tags found
1 merge request!127Remove if empty comment
...@@ -64,18 +64,20 @@ export default ({ comment, activeView, user }) => { ...@@ -64,18 +64,20 @@ export default ({ comment, activeView, user }) => {
current: { value }, current: { value },
} = commentInput; } = commentInput;
if (value !== '') { if (value !== '') {
saveComment(); // saveComment();
} }
// TODO Find actual from to of comment // TODO Also find fragmented marks
if (conversation.length === 0 && value === '') { if (conversation.length === 0 && value === '') {
dispatch(state.tr.removeMark(1, 5, commentMark)); const commentPosition = DocumentHelpers.findMarkPosition(activeView, comment.pos, 'comment');
dispatch(state.tr.removeMark(commentPosition.from, commentPosition.to, commentMark));
} }
}; };
const resolveComment = () => { const resolveComment = () => {
// TODO Find actual from to of comment // TODO Also find fragmented marks
dispatch(state.tr.removeMark(1, 5, commentMark)); const commentPosition = DocumentHelpers.findMarkPoistion(activeView, comment.pos, 'comment');
dispatch(state.tr.removeMark(commentPosition.from, commentPosition.to, commentMark));
}; };
const commentInputReply = () => { const commentInputReply = () => {
......
...@@ -88,10 +88,7 @@ const findAllCommentsWithSameId = state => { ...@@ -88,10 +88,7 @@ const findAllCommentsWithSameId = state => {
const allCommentsWithSameId = []; const allCommentsWithSameId = [];
commentNodes.map(node => { commentNodes.map(node => {
node.node.marks.filter(mark => { node.node.marks.filter(mark => {
if ( if (mark.type.name === 'comment' && commentOnSelection.attrs.id === mark.attrs.id) {
mark.type.name === 'comment' &&
commentOnSelection.attrs.id === mark.attrs.id
) {
allCommentsWithSameId.push(node); allCommentsWithSameId.push(node);
} }
}); });
...@@ -99,6 +96,23 @@ const findAllCommentsWithSameId = state => { ...@@ -99,6 +96,23 @@ const findAllCommentsWithSameId = state => {
return allCommentsWithSameId; return allCommentsWithSameId;
}; };
const findMarkPosition = (activeView, initialPos, markType) => {
let $pos = activeView.state.tr.doc.resolve(initialPos);
let parent = $pos.parent;
let start = parent.childAfter($pos.parentOffset);
if (!start.node) return null;
const actualMark = start.node.marks.find(mark => mark.type.name === markType);
let startIndex = $pos.index();
let startPos = $pos.start() + start.offset;
while (startIndex > 0 && actualMark.isInSet(parent.child(startIndex - 1).marks))
startPos -= parent.child(--startIndex).nodeSize;
let endIndex = $pos.indexAfter();
let endPos = startPos + start.node.nodeSize;
while (endPos < parent.childCount && actualMark.isInSet(parent.child(endIndex).marks))
endPos += parent.child(endIndex++).nodeSize;
return { from: startPos, to: endPos };
};
export const flatten = (node, descend = true) => { export const flatten = (node, descend = true) => {
if (!node) { if (!node) {
throw new Error('Invalid "node" parameter'); throw new Error('Invalid "node" parameter');
...@@ -152,4 +166,5 @@ export default { ...@@ -152,4 +166,5 @@ export default {
getSelectionMark, getSelectionMark,
findFragmentedMark, findFragmentedMark,
findAllCommentsWithSameId, findAllCommentsWithSameId,
findMarkPosition,
}; };
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