From 04794fd905ff21685bb4ec3abc0c282da208dc93 Mon Sep 17 00:00:00 2001 From: Felix Niklas Date: Fri, 2 Oct 2015 18:00:59 +0200 Subject: [PATCH] highlighter: dont select when selection started outside of article --- .../app/controllers/layout_ref.coffee | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/app/controllers/layout_ref.coffee b/app/assets/javascripts/app/controllers/layout_ref.coffee index ad2bf3070..8379b7c3d 100644 --- a/app/assets/javascripts/app/controllers/layout_ref.coffee +++ b/app/assets/javascripts/app/controllers/layout_ref.coffee @@ -895,7 +895,7 @@ class TicketZoomRef extends App.ControllerContent # don't go into highlight mode # just toggle the selected if !selection.isCollapsed - @toggleHighlightAtSelection $(selection.anchorNode).closest @articles.selector + @toggleHighlightAtSelection selection, $(selection.anchorNode).closest @articles.selector else # show color @highlightIcon.css('fill', @colors[@activeColorIndex].color) @@ -919,7 +919,9 @@ class TicketZoomRef extends App.ControllerContent @setColor() onMouseUp: (e) => - @toggleHighlightAtSelection $(e.currentTarget).closest @articles.selector + selection = rangy.getSelection() + + @toggleHighlightAtSelection selection, $(e.currentTarget).closest @articles.selector # # toggle Highlight @@ -930,18 +932,27 @@ class TicketZoomRef extends App.ControllerContent # - or highlights the selection # - clears the selection - toggleHighlightAtSelection: (article) -> - selection = rangy.getSelection() + toggleHighlightAtSelection: (selection, article) -> if @highlighter.selectionOverlapsHighlight selection @highlighter.unhighlightSelection() - else - @highlighter.highlightSelection @highlightClass, - selection: selection - containerElementId: article.get(0).id + return @storeHighlights() - # remove selection - selection.removeAllRanges() + # selection.anchorNode = element in which the selection started + # selection.focusNode = element in which the selection ended + # + # check if the start node is inside of the article or the article itself + startNode = @$(selection.anchorNode) + + if !(article.is(startNode) or article.contents().is(startNode)) + return selection.removeAllRanges() + + @highlighter.highlightSelection @highlightClass, + selection: selection + containerElementId: article.get(0).id + + # remove selection + selection.removeAllRanges() @storeHighlights()