diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index ff4d5a4101..e6401b11ac 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -323,6 +323,8 @@ define [ clearTimeout spinnerTimer else spinner.remove(element) + # stop displaying the text layer + element.removeClass 'pdfjs-viewer-show-text' ctrl.redraw(origposition) $timeout renderVisiblePages scope.loadSuccess = true @@ -405,6 +407,53 @@ define [ scope.adjustingScroll = true element.scrollTop(currentScrollTop + delta) + element.on 'mousedown', (e) -> + # We're checking that the event target isn't the directive root element + # to make sure that the click was within a PDF page - no point in showing + # the text layer when the click is outside. + # If the user clicks a PDF page, the mousedown target will be the canvas + # element (or the text layer one). Alternatively, if the event target is + # the root element, we can assume that the user has clicked either the + # grey background area or the scrollbars. + if e.target != element[0] and !_hasSelection() + element.addClass 'pdfjs-viewer-show-text' + _setMouseUpHandler() + + mouseUpHandler = null # keep track of the handler to avoid adding multiple times + + _setMouseUpHandler = () -> + if not mouseUpHandler? + mouseUpHandler = $(document.body).one 'mouseup', _handleSelectionMouseUp + + _handleSelectionMouseUp = () -> + 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 + return true + + _removeClassIfNoSelection = () -> + if _hasSelection() + return false # didn't remove the text layer + else + element.removeClass 'pdfjs-viewer-show-text' + return true + + _hasSelection = () -> + selection = window.getSelection?() + # check the selection collapsed state in preference to + # using selection.toString() as the latter is "" when + # the selection is hidden (e.g. while viewing logs) + return selection? and _isSelectionWithinPDF(selection) and !selection.isCollapsed + + _isSelectionWithinPDF = (selection) -> + if selection.rangeCount == 0 + return false + selectionAncestorNode = selection.getRangeAt(0).commonAncestorContainer + return element.find(selectionAncestorNode).length > 0 or element.is(selectionAncestorNode) + element.on 'scroll', () -> #console.log 'scroll event', element.scrollTop(), 'adjusting?', scope.adjustingScroll #scope.scrollPosition = element.scrollTop() diff --git a/services/web/public/stylesheets/app/editor/pdf.less b/services/web/public/stylesheets/app/editor/pdf.less index 12c86a6190..0c98050242 100644 --- a/services/web/public/stylesheets/app/editor/pdf.less +++ b/services/web/public/stylesheets/app/editor/pdf.less @@ -37,6 +37,7 @@ margin: 10px auto; padding: 0 10px; box-sizing: content-box; + user-select: none; } } .progress-thin { @@ -338,3 +339,13 @@ .files-dropdown { display: inline-block; } + +.plv-text-layer { + display: none; + user-select: text; + + .pdf-page-container:hover &, + .pdfjs-viewer-show-text & { + display: block; + } +} diff --git a/services/web/public/stylesheets/app/editor/toolbar.less b/services/web/public/stylesheets/app/editor/toolbar.less index 5f84f64682..1bb5f62bdc 100644 --- a/services/web/public/stylesheets/app/editor/toolbar.less +++ b/services/web/public/stylesheets/app/editor/toolbar.less @@ -10,6 +10,7 @@ right: 0; padding: .15em .6em .2em; font-size: 60%; + pointer-events: none; // Labels were capturing button/anchor clicks. } }