From b04288ed449a0759eca8f4570d4160a75ec72726 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Thu, 15 Jun 2017 14:11:08 +0100 Subject: [PATCH] avoid getting duplicate handlers for mouseup --- .../ide/pdfng/directives/pdfViewer.coffee | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index dd6e949f90..ea521a74d1 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -417,16 +417,28 @@ define [ # grey background area or the scrollbars. if e.target != element[0] and !_hasSelection() element.addClass 'pdfjs-viewer-show-text' - $(document.body).one 'mouseup', _handleSelectionMouseUp + _setMouseUpHandler() + + mouseUpHandler = null # keep track of the handler to avoid adding multiple times + + _setMouseUpHandler = () -> + if not mouseUpHandler? + mouseUpHandler = $(document.body).one 'mouseup', _handleSelectionMouseUp _handleSelectionMouseUp = () -> - window.setTimeout _removeClassIfNoSelection, 10 + mouseUpHandler = null # reset handler, has now fired + window.setTimeout () -> + removedClass = _removeClassIfNoSelection() + # if we still have a selection we need to keep the handler going + if not removedClass then _setMouseUpHandler() + , 10 _removeClassIfNoSelection = () -> if _hasSelection() - $(document.body).one 'mouseup', _handleSelectionMouseUp + return false # didn't remove the text layer else element.removeClass 'pdfjs-viewer-show-text' + return true _hasSelection = () -> selection = window.getSelection()