diff --git a/app/components/SimpleEditor/panes/Comments/CommentsProvider.js b/app/components/SimpleEditor/panes/Comments/CommentsProvider.js index 83cdf806df73c6b1106371beaff8c63cb0c8d1a3..9a3fbadc2826a1c75d9b89424d48d46ffae899b0 100644 --- a/app/components/SimpleEditor/panes/Comments/CommentsProvider.js +++ b/app/components/SimpleEditor/panes/Comments/CommentsProvider.js @@ -74,6 +74,7 @@ class CommentsProvider extends TocProvider { } const activeComment = this.getActiveComment() + if (!activeComment) return this.removeActiveEntry() // current selection and previous selection both belong to the same comment if (activeEntry === activeComment.id) return @@ -157,16 +158,46 @@ class CommentsProvider extends TocProvider { } getActiveComment () { - const commentAnnotations = this.getSelectionComments() - const commentState = this.getCommentState() + const comments = this.getSelectionComments() + const mode = this.getMode() + const selection = this.getSelection() + + // we have a non-collapsed selection that contains multiple comments + if (mode === 'fuse') { + // only keep the comments that contain the whole selection + let annos = _.filter(comments, annotation => { + return annotation.getSelection().contains(selection) + }) + + if (annos.length === 0) return null + if (annos.length === 1) return annos[0] + + // find comments that contain all other comments and remove them + // the focus of the active comment should be inwards + let remove = [] + _.forEach(annos, anno => { + let removeIt = true + + // TODO -- there's gotta be a better way to do this than a double loop + _.forEach(annos, innerAnno => { + if (anno === innerAnno) return - switch (commentState.mode) { - case 'fuse': - const count = commentAnnotations.length - 1 - return commentAnnotations[count] - default: - return commentAnnotations[0] + const outerSelection = anno.getSelection() + const innerSelection = innerAnno.getSelection() + if (!outerSelection.contains(innerSelection)) removeIt = false + }) + + if (removeIt) remove.push(anno) + }) + + annos = _.difference(annos, remove) + + // once you have all comments that do not contain each other wholly + // just choose the one on the left + return _.minBy(annos, 'startOffset') } + + return comments[0] } // returns an array of all comments in selection @@ -193,6 +224,11 @@ class CommentsProvider extends TocProvider { return commandStates.comment } + getMode () { + const commentState = this.getCommentState() + return commentState.mode + } + getNode (id) { const doc = this.getDocument() return doc.get(id)