From 06330e650665cc139b2068b1cdf790a670993c6c Mon Sep 17 00:00:00 2001 From: Brian Date: Fri, 16 Jan 2015 15:51:22 +0000 Subject: [PATCH 01/18] add pdfng scroll handler using requestAnimationFrame --- .../ide/pdfng/directives/pdfViewer.coffee | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 635f2ec8a3..61ff33a391 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -307,26 +307,57 @@ define [ # trigger a redraw scope.scale = angular.copy (scope.scale) + // shim layer with setTimeout fallback + requestAnimationFrame = do () -> + window.requestAnimationFrame || + window.webkitRequestAnimationFrame || + window.mozRequestAnimationFrame || + (callback) -> window.setTimeout(callback, 1000 / 60) + + rafActive = null element.on 'scroll', () -> #console.log 'scroll event', element.scrollTop(), 'adjusting?', scope.adjustingScroll + scope.scrollPosition = element.scrollTop() if scope.adjustingScroll updateContainer() scope.$apply() scope.adjustingScroll = false return - scope.scrolled = true - if scope.scrollHandlerTimeout - $timeout.cancel(scope.scrollHandlerTimeout) - scope.scrollHandlerTimeout = $timeout scrollHandler, 100 + # scope.scrolled = true + # if scope.scrollHandlerTimeout + # $timeout.cancel(scope.scrollHandlerTimeout) + # scope.scrollHandlerTimeout = $timeout scrollHandler, 100 + if not rafActive? + rafActive = requestAnimationFrame(scrollCheck) + + prev = null + scope.scrollPosition = element.scrollTop() + scrollCheck = (timestamp) -> + prev = timestamp if not prev? + dt = timestamp - prev + if (dt > 100) + scope.lastScrollPosition = scope.scrollPosition if not scope.lastScrollPosition? + dy = scope.scrollPosition - scope.lastScrollPosition + scope.lastScrollPosition = scope.scrollPosition + console.log 'handle scroll dy', dy if dy != 0 + scope.scrolled = true if dy != 0 + if scope.scrolled && dy == 0 + scrollHandler() + scope.scrolled = false + rafActive = null + return + requestAnimationFrame(scrollCheck) + + requestAnimationFrame(scrollCheck) scrollHandler = () -> - scope.scrollHandlerTimeout = null updateContainer() scope.$apply() newPosition = ctrl.getPdfPosition() if newPosition? scope.position = newPosition scope.$apply() + # scope.scrollHandlerTimeout = null scope.$watch 'pdfSrc', (newVal, oldVal) -> # console.log 'loading pdf', newVal, oldVal From adab8c676989436e9ca990905a2e28d38cc2041f Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 19 Jan 2015 14:24:52 +0000 Subject: [PATCH 02/18] delay pdf spinner by 100ms --- .../public/coffee/ide/pdfng/directives/pdfRenderer.coffee | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index 798d3f9539..e7d2b061cb 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -131,7 +131,9 @@ define [ @jobs = @jobs + 1 element.canvas.addClass('pdfng-loading') - @spinner.add(element.canvas) + spinTimer = $timeout () => + @spinner.add(element.canvas) + , 100 completeRef = @complete renderTaskRef = @renderTask @@ -142,6 +144,7 @@ define [ Raven.captureMessage?('pdfng page load timed out after ' + @PAGE_LOAD_TIMEOUT + 'ms') # console.log 'page load timed out', pagenum timedOut = true + $timeout.cancel(spinTimer) @spinner.stop(element.canvas) # @jobs = @jobs - 1 # @triggerRenderQueue(0) @@ -153,6 +156,7 @@ define [ @pageLoad[pagenum].then (pageObject) => # console.log 'in page load success', pagenum $timeout.cancel(timer) + $timeout.cancel(spinTimer) @renderTask[pagenum] = @doRender element, pagenum, pageObject @renderTask[pagenum].then () => # complete @@ -166,6 +170,7 @@ define [ .catch (error) -> # console.log 'in page load error', pagenum, 'timedOut=', timedOut $timeout.cancel(timer) + $timeout.cancel(spinTimer) # console.log 'ERROR', error doRender: (element, pagenum, page) -> From 906c9114cddd1cfdbf9d57d2bd62a5527f39cf02 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 19 Jan 2015 14:25:38 +0000 Subject: [PATCH 03/18] replace per-page pdf highlight $watch with highlight event --- services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee | 2 +- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee index c440b6fc12..5ad743c180 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee @@ -99,7 +99,7 @@ define [ highlights: highlightsElement }) - scope.$watch 'highlights', (highlights, oldVal) -> + scope.$on 'pdf:highlights', (event, highlights) -> return unless highlights? return unless highlights.length > 0 if scope.timeoutHandler diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 61ff33a391..ad1c2eef6c 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -452,6 +452,8 @@ define [ return if !highlights.length + scope.$broadcast 'pdf:highlights', areas + first = highlights[0] pageNum = scope.pages[first.page].pageNum From 2e021b64d8215f56cd9f5c4afac15c844542f8a8 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 19 Jan 2015 14:36:20 +0000 Subject: [PATCH 04/18] remove per-page $watch on default page size in pdf viewer --- .../coffee/ide/pdfng/directives/pdfPage.coffee | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee index 5ad743c180..b481b66701 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee @@ -56,8 +56,16 @@ define [ # parent with scope.pages[i].element scope.page.element = element - if (!scope.page.sized && scope.defaultPageSize) - updatePageSize scope.defaultPageSize + if !scope.page.sized + if scope.defaultPageSize? + updatePageSize scope.defaultPageSize + else + # shouldn't get here - the default page size should now + # always be set before redraw is called + handler = scope.$watch 'defaultPageSize', (defaultPageSize) -> + return unless defaultPageSize? + updatePageSize defaultPageSize + handler() if scope.page.current # console.log 'we must scroll to this page', scope.page.pageNum, 'at position', scope.page.position @@ -65,10 +73,6 @@ define [ # this is the current page, we want to scroll it into view ctrl.setPdfPosition(scope.page, scope.page.position) - scope.$watch 'defaultPageSize', (defaultPageSize) -> - return unless defaultPageSize? - updatePageSize defaultPageSize - watchHandle = scope.$watch 'containerSize', (containerSize, oldVal) -> return unless containerSize? return unless scope.page.sized From 776535f9f3fd7a5d4d95968644a318f8c39db31b Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 19 Jan 2015 16:55:44 +0000 Subject: [PATCH 05/18] move pdf page visibility calculation up into pdfViewer directive removes per-page watch for visibility --- .../ide/pdfng/directives/pdfPage.coffee | 47 +++--------- .../ide/pdfng/directives/pdfRenderer.coffee | 9 +++ .../ide/pdfng/directives/pdfViewer.coffee | 71 +++++++++---------- 3 files changed, 53 insertions(+), 74 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee index b481b66701..7a38176bf3 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee @@ -29,32 +29,16 @@ define [ canvasElement.width(w) scope.page.sized = true - isVisible = (containerSize) -> - elemTop = element.offset().top - containerSize[2] - elemBottom = elemTop + element.innerHeight() - visible = (elemTop < containerSize[1] and elemBottom > 0) - scope.page.visible = visible - scope.page.elemTop = elemTop - scope.page.elemBottom = elemBottom - return visible - - renderPage = () -> - scope.document.renderPage { - canvas: canvasElement, - text: textElement - annotations: annotationsElement - highlights: highlightsElement - }, scope.page.pageNum - - pausePage = () -> - scope.document.pause { - canvas: canvasElement, - text: textElement - }, scope.page.pageNum - # keep track of our page element, so we can access it in the - # parent with scope.pages[i].element + # parent with scope.pages[i].element, and the contained + # elements for each part scope.page.element = element + scope.page.elementChildren = { + canvas: canvasElement, + text: textElement + annotations: annotationsElement + highlights: highlightsElement + } if !scope.page.sized if scope.defaultPageSize? @@ -69,23 +53,10 @@ define [ if scope.page.current # console.log 'we must scroll to this page', scope.page.pageNum, 'at position', scope.page.position - renderPage() # this is the current page, we want to scroll it into view + # FIXME: do we need to ensure render fires before moving to this position??? ctrl.setPdfPosition(scope.page, scope.page.position) - watchHandle = scope.$watch 'containerSize', (containerSize, oldVal) -> - return unless containerSize? - return unless scope.page.sized - oldVisible = scope.page.visible - newVisible = isVisible containerSize - scope.page.visible = newVisible - if newVisible && !oldVisible - renderPage() - # TODO deregister this listener after the page is rendered - #watchHandle() - else if !newVisible && oldVisible - pausePage() - element.on 'dblclick', (e) -> offset = $(element).find('.pdf-canvas').offset() dx = e.pageX - offset.left diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index e7d2b061cb..d8979bb69a 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -107,6 +107,15 @@ define [ @jobs = @jobs - 1 @triggerRenderQueue(0) + renderPages: (pages) -> + return if @shuttingDown + @renderQueue = for page in pages + { + 'element': page.elementChildren + 'pagenum': page.pageNum + } + @triggerRenderQueue() + renderPage: (element, pagenum) -> return if @shuttingDown current = { diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index ad1c2eef6c..505e1d8020 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -245,6 +245,33 @@ define [ element.offset().top ] + getVisiblePages = () -> + top = element.offset().top + bottom = top + element.innerHeight(); + isVisible = (pageElement) -> + pageTop = pageElement.offset().top + pageBottom = pageTop + pageElement.innerHeight() + return pageTop < bottom and pageBottom > top + return (page for page in scope.pages when isVisible(page.element)) + + getExtraPages = (visiblePages) -> + extra = [] + firstVisiblePage = visiblePages[0].pageNum + firstVisiblePageIdx = firstVisiblePage - 1 + len = visiblePages.length + lastVisiblePage = visiblePages[len-1].pageNum + lastVisiblePageIdx = lastVisiblePage - 1 + # first page after + if lastVisiblePageIdx + 1 < len + extra.push scope.pages[lastVisiblePageIdx + 1] + # page before + if firstVisiblePageIdx > 0 + extra.push scope.pages[firstVisiblePageIdx - 1] + # second page after + if lastVisiblePageIdx + 2 < len + extra.push scope.pages[lastVisiblePageIdx + 2] + return visiblePages.concat extra + doRescale = (scale) -> # console.log 'doRescale', scale origposition = angular.copy scope.position @@ -307,14 +334,6 @@ define [ # trigger a redraw scope.scale = angular.copy (scope.scale) - // shim layer with setTimeout fallback - requestAnimationFrame = do () -> - window.requestAnimationFrame || - window.webkitRequestAnimationFrame || - window.mozRequestAnimationFrame || - (callback) -> window.setTimeout(callback, 1000 / 60) - - rafActive = null element.on 'scroll', () -> #console.log 'scroll event', element.scrollTop(), 'adjusting?', scope.adjustingScroll scope.scrollPosition = element.scrollTop() @@ -323,41 +342,21 @@ define [ scope.$apply() scope.adjustingScroll = false return - # scope.scrolled = true - # if scope.scrollHandlerTimeout - # $timeout.cancel(scope.scrollHandlerTimeout) - # scope.scrollHandlerTimeout = $timeout scrollHandler, 100 - if not rafActive? - rafActive = requestAnimationFrame(scrollCheck) - - prev = null - scope.scrollPosition = element.scrollTop() - scrollCheck = (timestamp) -> - prev = timestamp if not prev? - dt = timestamp - prev - if (dt > 100) - scope.lastScrollPosition = scope.scrollPosition if not scope.lastScrollPosition? - dy = scope.scrollPosition - scope.lastScrollPosition - scope.lastScrollPosition = scope.scrollPosition - console.log 'handle scroll dy', dy if dy != 0 - scope.scrolled = true if dy != 0 - if scope.scrolled && dy == 0 - scrollHandler() - scope.scrolled = false - rafActive = null - return - requestAnimationFrame(scrollCheck) - - requestAnimationFrame(scrollCheck) + scope.scrolled = true + if scope.scrollHandlerTimeout + $timeout.cancel(scope.scrollHandlerTimeout) + scope.scrollHandlerTimeout = $timeout scrollHandler, 25 scrollHandler = () -> - updateContainer() + visiblePages = getVisiblePages() + pages = getExtraPages visiblePages + scope.document.renderPages(pages) scope.$apply() newPosition = ctrl.getPdfPosition() if newPosition? scope.position = newPosition scope.$apply() - # scope.scrollHandlerTimeout = null + scope.scrollHandlerTimeout = null scope.$watch 'pdfSrc', (newVal, oldVal) -> # console.log 'loading pdf', newVal, oldVal From c9e019b4a78826afcd77d31546e4dd452d7d70e1 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 20 Jan 2015 11:06:40 +0000 Subject: [PATCH 06/18] clean up calculation of visible pdf pages and display on redraw --- .../ide/pdfng/directives/pdfViewer.coffee | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 505e1d8020..e6ec4fe47b 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -144,18 +144,18 @@ define [ # find first visible page visible = $scope.pages.some (page, i) -> [topPageIdx, topPage] = [i, page] if page.visible - if visible + if visible && topPage.element? # console.log 'found it', topPageIdx else # console.log 'CANNOT FIND TOP PAGE' return # console.log 'top page is', topPage.pageNum, topPage.elemTop, topPage.elemBottom, topPage - top = topPage.elemTop - bottom = topPage.elemBottom - viewportTop = 0 - viewportHeight = $element.height() - topVisible = (top >= viewportTop && top < viewportTop + viewportHeight) + top = topPage.element.offset().top + bottom = top + topPage.element.innerHeight() + viewportTop = $element.offset().top + viewportBottom = viewportTop + $element.height() + topVisible = (top >= viewportTop && top < viewportBottom) someContentVisible = (top < viewportTop && bottom > viewportTop) # console.log 'in PdfListView', top, topVisible, someContentVisible, viewportTop if topVisible @@ -237,13 +237,10 @@ define [ layoutReady.promise.then () -> # console.log 'layoutReady was resolved' - # TODO can we combine this with scope.parentSize, need to finalize boxes - updateContainer = () -> - scope.containerSize = [ - element.innerWidth() - element.innerHeight() - element.offset().top - ] + renderVisiblePages = () -> + visiblePages = getVisiblePages() + pages = getExtraPages visiblePages + scope.document.renderPages(pages) getVisiblePages = () -> top = element.offset().top @@ -252,7 +249,11 @@ define [ pageTop = pageElement.offset().top pageBottom = pageTop + pageElement.innerHeight() return pageTop < bottom and pageBottom > top - return (page for page in scope.pages when isVisible(page.element)) + visiblePages = [] + for page in scope.pages + page.visible = visible = isVisible(page.element) + visiblePages.push page if visible + return visiblePages getExtraPages = (visiblePages) -> extra = [] @@ -282,6 +283,7 @@ define [ ctrl.setScale(scale, h, w).then () -> spinner.remove(element) ctrl.redraw(origposition) + $timeout renderVisiblePages, 0 checkElementReady = () -> # if element is zero-sized keep checking until it is ready @@ -297,7 +299,7 @@ define [ scope.$on 'layout-ready', () -> # console.log 'GOT LAYOUT READY EVENT' # console.log 'calling refresh' - updateContainer() + # updateContainer() spinner.add(element) layoutReady.resolve 'layout is ready' scope.parentSize = [ @@ -336,21 +338,20 @@ define [ element.on 'scroll', () -> #console.log 'scroll event', element.scrollTop(), 'adjusting?', scope.adjustingScroll - scope.scrollPosition = element.scrollTop() + #scope.scrollPosition = element.scrollTop() if scope.adjustingScroll - updateContainer() + renderVisiblePages() + # updateContainer() scope.$apply() scope.adjustingScroll = false return - scope.scrolled = true + #scope.scrolled = true if scope.scrollHandlerTimeout $timeout.cancel(scope.scrollHandlerTimeout) scope.scrollHandlerTimeout = $timeout scrollHandler, 25 scrollHandler = () -> - visiblePages = getVisiblePages() - pages = getExtraPages visiblePages - scope.document.renderPages(pages) + renderVisiblePages() scope.$apply() newPosition = ctrl.getPdfPosition() if newPosition? From f18f862be26a07007a0b0576d71e043dd7790725 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 20 Jan 2015 14:28:14 +0000 Subject: [PATCH 07/18] handle different page size in pdf viewer --- .../coffee/ide/pdfng/directives/pdfPage.coffee | 1 + .../coffee/ide/pdfng/directives/pdfRenderer.coffee | 11 +++++++++-- .../coffee/ide/pdfng/directives/pdfViewer.coffee | 12 ++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee index 7a38176bf3..b929ede5c9 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee @@ -38,6 +38,7 @@ define [ text: textElement annotations: annotationsElement highlights: highlightsElement + container: element } if !scope.page.sized diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index d8979bb69a..039a29f87d 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -25,6 +25,7 @@ define [ pdfDocument.getDownloadInfo().then () => @options.loadedCallback() @errorCallback = @options.errorCallback + @pageSizeChangeCallback = @options.pageSizeChangeCallback @pdfjs.catch (exception) => # console.log 'ERROR in get document', exception @errorCallback(exception) @@ -216,8 +217,14 @@ define [ canvas.height(newHeight + 'px') canvas.width(newWidth + 'px') - element.canvas.height(newHeight) - element.canvas.width(newWidth) + oldHeight = element.canvas.height() + oldWidth = element.canvas.width() + if newHeight != oldHeight or newWidth != oldWidth + element.canvas.height(newHeight + 'px') + element.canvas.width(newWidth + 'px') + element.container.height(newHeight + 'px') + element.container.width(newWidth + 'px') + @pageSizeChangeCallback?(pagenum, newHeight - oldHeight) if pixelRatio != 1 ctx.scale(pixelRatio, pixelRatio) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index e6ec4fe47b..6583072af7 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -40,6 +40,8 @@ define [ errorCallback: (error) -> Raven.captureMessage?('pdfng error ' + error) $scope.$emit 'pdf:error', error + pageSizeChangeCallback: (pageNum, deltaH) -> + $scope.$broadcast 'pdf:page:size-change', pageNum, deltaH }) # we will have all the main information needed to start display @@ -336,6 +338,16 @@ define [ # trigger a redraw scope.scale = angular.copy (scope.scale) + scope.$on 'pdf:page:size-change', (event, pageNum, delta) -> + #console.log 'page size change event', pageNum, delta + origposition = angular.copy scope.position + #console.log 'orig position', JSON.stringify(origposition) + if pageNum - 1 < origposition.page && delta != 0 + currentScrollTop = element.scrollTop() + console.log 'adjusting scroll from', currentScrollTop, 'by', delta + scope.adjustingScroll = true + element.scrollTop(currentScrollTop + delta) + element.on 'scroll', () -> #console.log 'scroll event', element.scrollTop(), 'adjusting?', scope.adjustingScroll #scope.scrollPosition = element.scrollTop() From 96af4e0b4a13f361f41a5c4bfed3d2e573a14f73 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 20 Jan 2015 14:28:43 +0000 Subject: [PATCH 08/18] fix selection of prerender pages in pdf viewer --- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 6583072af7..ecb8c0f84f 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -265,13 +265,13 @@ define [ lastVisiblePage = visiblePages[len-1].pageNum lastVisiblePageIdx = lastVisiblePage - 1 # first page after - if lastVisiblePageIdx + 1 < len + if lastVisiblePageIdx + 1 < scope.pages.length extra.push scope.pages[lastVisiblePageIdx + 1] # page before if firstVisiblePageIdx > 0 extra.push scope.pages[firstVisiblePageIdx - 1] # second page after - if lastVisiblePageIdx + 2 < len + if lastVisiblePageIdx + 2 < scope.pages.length extra.push scope.pages[lastVisiblePageIdx + 2] return visiblePages.concat extra From 721843695b429f87944740793acd6573dc0b3793 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 20 Jan 2015 15:36:52 +0000 Subject: [PATCH 09/18] avoid using jquery in pdf scrollhandler to improve peformance --- .../coffee/ide/pdfng/directives/pdfViewer.coffee | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index ecb8c0f84f..6d2fbdd621 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -245,15 +245,15 @@ define [ scope.document.renderPages(pages) getVisiblePages = () -> - top = element.offset().top - bottom = top + element.innerHeight(); + top = element[0].offsetTop + bottom = top + element[0].clientHeight isVisible = (pageElement) -> - pageTop = pageElement.offset().top - pageBottom = pageTop + pageElement.innerHeight() + pageTop = pageElement.offsetTop + pageBottom = pageTop + pageElement.clientHeight return pageTop < bottom and pageBottom > top visiblePages = [] for page in scope.pages - page.visible = visible = isVisible(page.element) + page.visible = visible = isVisible(page.element[0]) visiblePages.push page if visible return visiblePages From 53e9086094d9d3a5c43175b8eefa2cbca8ea40cf Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 20 Jan 2015 15:37:11 +0000 Subject: [PATCH 10/18] remove useless scope.$apply in pdf scrollhandler --- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 6d2fbdd621..1fef6962af 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -364,11 +364,11 @@ define [ scrollHandler = () -> renderVisiblePages() - scope.$apply() + #scope.$apply() newPosition = ctrl.getPdfPosition() if newPosition? scope.position = newPosition - scope.$apply() + #scope.$apply() scope.scrollHandlerTimeout = null scope.$watch 'pdfSrc', (newVal, oldVal) -> From 591498a05641216d4e1e7584a49b3575030a5691 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 20 Jan 2015 16:20:12 +0000 Subject: [PATCH 11/18] fix dom position calculation in pdf viewer --- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 1fef6962af..b36d96c1bf 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -245,8 +245,8 @@ define [ scope.document.renderPages(pages) getVisiblePages = () -> - top = element[0].offsetTop - bottom = top + element[0].clientHeight + top = element[0].scrollTop; + bottom = top + element[0].clientHeight; isVisible = (pageElement) -> pageTop = pageElement.offsetTop pageBottom = pageTop + pageElement.clientHeight From 441bc63935b8d091307d1796cbd6743363cbb6e1 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 20 Jan 2015 16:20:38 +0000 Subject: [PATCH 12/18] use setTimeout instead of $timeout in pdf scrollhandler --- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index b36d96c1bf..aa90b11a50 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -359,8 +359,8 @@ define [ return #scope.scrolled = true if scope.scrollHandlerTimeout - $timeout.cancel(scope.scrollHandlerTimeout) - scope.scrollHandlerTimeout = $timeout scrollHandler, 25 + clearTimeout(scope.scrollHandlerTimeout) + scope.scrollHandlerTimeout = setTimeout scrollHandler, 25 scrollHandler = () -> renderVisiblePages() From debc3037ae8c6cf8b7b18f493557d8dbb6c30871 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 20 Jan 2015 16:22:34 +0000 Subject: [PATCH 13/18] remove debugging from pdf viewer --- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index aa90b11a50..645f033bd0 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -344,7 +344,7 @@ define [ #console.log 'orig position', JSON.stringify(origposition) if pageNum - 1 < origposition.page && delta != 0 currentScrollTop = element.scrollTop() - console.log 'adjusting scroll from', currentScrollTop, 'by', delta + #console.log 'adjusting scroll from', currentScrollTop, 'by', delta scope.adjustingScroll = true element.scrollTop(currentScrollTop + delta) From c8012dc22ca5d8bc7e0008d3d5b40374996894d0 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 20 Jan 2015 20:35:29 +0000 Subject: [PATCH 14/18] fix getVisiblePages in pdf viewer to allow optimisation --- .../coffee/ide/pdfng/directives/pdfViewer.coffee | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 645f033bd0..22795b0edc 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -247,14 +247,12 @@ define [ getVisiblePages = () -> top = element[0].scrollTop; bottom = top + element[0].clientHeight; - isVisible = (pageElement) -> + visiblePages = scope.pages.filter (page) -> + pageElement = page.element[0] pageTop = pageElement.offsetTop pageBottom = pageTop + pageElement.clientHeight - return pageTop < bottom and pageBottom > top - visiblePages = [] - for page in scope.pages - page.visible = visible = isVisible(page.element[0]) - visiblePages.push page if visible + page.visible = pageTop < bottom and pageBottom > top + return page.visible return visiblePages getExtraPages = (visiblePages) -> From ed6eedd9e23f92f1d385a244308963937206af63 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 20 Jan 2015 20:36:43 +0000 Subject: [PATCH 15/18] remove unused functions in pdfRenderer --- .../ide/pdfng/directives/pdfRenderer.coffee | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index 039a29f87d..46803fcc06 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -90,13 +90,6 @@ define [ setScale: (@scale) -> @resetState() - pause: (element, pagenum) -> - return if @complete[pagenum] - return if @shuttingDown - @renderQueue = @renderQueue.filter (q) -> - q.pagenum != pagenum - @spinner.stop(element.canvas) - triggerRenderQueue: (interval = @JOB_QUEUE_INTERVAL) -> $timeout () => @processRenderQueue() @@ -117,15 +110,6 @@ define [ } @triggerRenderQueue() - renderPage: (element, pagenum) -> - return if @shuttingDown - current = { - 'element': element - 'pagenum': pagenum - } - @renderQueue.push(current) - @triggerRenderQueue() - processRenderQueue: () -> return if @shuttingDown return if @jobs > 0 From 62d59168837a7141200dcc85cfdd75c53e000005 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 20 Jan 2015 21:06:53 +0000 Subject: [PATCH 16/18] replace $timeout by setTimeout in pdf viewer update --- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 22795b0edc..778f7f9a53 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -283,7 +283,7 @@ define [ ctrl.setScale(scale, h, w).then () -> spinner.remove(element) ctrl.redraw(origposition) - $timeout renderVisiblePages, 0 + setTimeout renderVisiblePages, 0 checkElementReady = () -> # if element is zero-sized keep checking until it is ready From f510c5d19fcf0648edac218b2bebe7db3ece9521 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 20 Jan 2015 21:33:49 +0000 Subject: [PATCH 17/18] render current page immediately --- .../public/coffee/ide/pdfng/directives/pdfPage.coffee | 3 ++- .../coffee/ide/pdfng/directives/pdfRenderer.coffee | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee index b929ede5c9..d52ef60256 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfPage.coffee @@ -55,7 +55,8 @@ define [ if scope.page.current # console.log 'we must scroll to this page', scope.page.pageNum, 'at position', scope.page.position # this is the current page, we want to scroll it into view - # FIXME: do we need to ensure render fires before moving to this position??? + # and render it immediately + scope.document.renderPage scope.page ctrl.setPdfPosition(scope.page, scope.page.position) element.on 'dblclick', (e) -> diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee index 46803fcc06..31880b83ca 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfRenderer.coffee @@ -110,6 +110,15 @@ define [ } @triggerRenderQueue() + renderPage: (page) -> + return if @shuttingDown + current = { + 'element': page.elementChildren + 'pagenum': page.pageNum + } + @renderQueue.push current + @processRenderQueue() + processRenderQueue: () -> return if @shuttingDown return if @jobs > 0 From 4ab0f00b8d1b5d32cfe76744617bf8cc33a433f0 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 21 Jan 2015 14:37:59 +0000 Subject: [PATCH 18/18] do not render offscreen pdf pages yet they cause a delay without a visible spinner --- .../web/public/coffee/ide/pdfng/directives/pdfViewer.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee index 778f7f9a53..73bd907a6f 100644 --- a/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee +++ b/services/web/public/coffee/ide/pdfng/directives/pdfViewer.coffee @@ -240,8 +240,8 @@ define [ # console.log 'layoutReady was resolved' renderVisiblePages = () -> - visiblePages = getVisiblePages() - pages = getExtraPages visiblePages + pages = getVisiblePages() + # pages = getExtraPages visiblePages scope.document.renderPages(pages) getVisiblePages = () ->