Commit 610c67d8 authored by Randall Leeds's avatar Randall Leeds

Clean up the PDF cache usage

parent a6d56437
...@@ -22,30 +22,33 @@ getPage = (pageIndex) -> ...@@ -22,30 +22,33 @@ getPage = (pageIndex) ->
return PDFViewerApplication.pdfViewer.pages[pageIndex] return PDFViewerApplication.pdfViewer.pages[pageIndex]
getPageTextContent = (pageIndex) -> getPageTextContent = (pageIndex, cache) ->
return PDFViewerApplication.pdfViewer.getPageTextContent(pageIndex) if cache[pageIndex]?
.then((textContent) -> (item.str for item in textContent.items).join('')) return Promise.resolve(cache[pageIndex])
else
return PDFViewerApplication.pdfViewer.getPageTextContent(pageIndex)
.then((textContent) -> (item.str for item in textContent.items).join(''))
.then((textContent) ->
cache[pageIndex] = textContent
return textContent
)
getPageOffset = (pageIndex, cache = {}) -> getPageOffset = (pageIndex, cache = {}) ->
index = -1 index = -1
if cache[pageIndex]?
return Promise.resolve(cache[pageIndex])
next = (offset) -> next = (offset) ->
if ++index is pageIndex if ++index is pageIndex
cache[pageIndex] = offset
return Promise.resolve(offset) return Promise.resolve(offset)
return getPageTextContent(index) return getPageTextContent(index, cache)
.then((textContent) -> next(offset + textContent.length)) .then((textContent) -> next(offset + textContent.length))
return next(0) return next(0)
findPage = (offset) -> findPage = (offset, cache = {}) ->
index = 0 index = 0
total = 0 total = 0
...@@ -56,9 +59,9 @@ findPage = (offset) -> ...@@ -56,9 +59,9 @@ findPage = (offset) ->
else else
index++ index++
total += textContent.length total += textContent.length
return getPageTextContent(index).then(count) return getPageTextContent(index, cache).then(count)
return getPageTextContent(0).then(count) return getPageTextContent(0, cache).then(count)
###* ###*
...@@ -73,10 +76,7 @@ findPage = (offset) -> ...@@ -73,10 +76,7 @@ findPage = (offset) ->
# :rtype: Promise # :rtype: Promise
#### ####
exports.anchor = (selectors, options = {}) -> exports.anchor = (selectors, options = {}) ->
# Cache of anchoring data
cache = options.cache ? {} cache = options.cache ? {}
cache.pageOffset ?= {}
cache.quotePosition ?= {}
# Selectors # Selectors
position = null position = null
...@@ -122,7 +122,7 @@ exports.anchor = (selectors, options = {}) -> ...@@ -122,7 +122,7 @@ exports.anchor = (selectors, options = {}) ->
if position? if position?
promise = promise.catch -> promise = promise.catch ->
return findPage(position.start) return findPage(position.start, cache.pageText)
.then ({index, offset, textContent}) -> .then ({index, offset, textContent}) ->
page = getPage(index) page = getPage(index)
start = position.start - offset start = position.start - offset
...@@ -136,18 +136,21 @@ exports.anchor = (selectors, options = {}) -> ...@@ -136,18 +136,21 @@ exports.anchor = (selectors, options = {}) ->
promise = promise.catch -> promise = promise.catch ->
{pagesCount} = PDFViewerApplication.pdfViewer {pagesCount} = PDFViewerApplication.pdfViewer
if cache.quotePosition[quote.exact]? if position?
{page, anchor} = cache.quotePosition[quote.exact] if cache.quotePosition[quote.exact]?[position.start]?
return anchorByPosition(page, anchor) {page, anchor} = cache.quotePosition[quote.exact][position.start]
return anchorByPosition(page, anchor)
storeAndAnchor = (page, anchor) -> storeAndAnchor = (page, anchor) ->
cache.quotePosition[quote.exact] = {page, anchor} if position?
cache.quotePosition[quote.exact] ?= {}
cache.quotePosition[quote.exact][position.start] = {page, anchor}
return anchorByPosition(page, anchor) return anchorByPosition(page, anchor)
findInPages = ([pageIndex, rest...]) -> findInPages = ([pageIndex, rest...]) ->
page = getPage(pageIndex) page = getPage(pageIndex)
content = getPageTextContent(pageIndex) content = getPageTextContent(pageIndex, cache.pageText)
offset = getPageOffset(pageIndex, cache.pageOffset) offset = getPageOffset(pageIndex, cache.pageText)
Promise.all([content, offset, page]) Promise.all([content, offset, page])
.then (results) -> .then (results) ->
[content, offset, page] = results [content, offset, page] = results
...@@ -170,7 +173,7 @@ exports.anchor = (selectors, options = {}) -> ...@@ -170,7 +173,7 @@ exports.anchor = (selectors, options = {}) ->
pages = [0...pagesCount] pages = [0...pagesCount]
if position? if position?
return findPage(position.start) return findPage(position.start, cache.pageText)
.then ({index, offset, textContent}) -> .then ({index, offset, textContent}) ->
left = pages.slice(0, index) left = pages.slice(0, index)
right = pages.slice(index) right = pages.slice(index)
...@@ -188,11 +191,7 @@ exports.anchor = (selectors, options = {}) -> ...@@ -188,11 +191,7 @@ exports.anchor = (selectors, options = {}) ->
exports.describe = (range, options = {}) -> exports.describe = (range, options = {}) ->
# Cache of anchoring data
cache = options.cache ? {} cache = options.cache ? {}
cache.pageOffset ?= {}
cache.quotePosition ?= {}
range = new xpathRange.BrowserRange(range).normalize() range = new xpathRange.BrowserRange(range).normalize()
startTextLayer = getNodeTextLayer(range.start) startTextLayer = getNodeTextLayer(range.start)
...@@ -213,7 +212,7 @@ exports.describe = (range, options = {}) -> ...@@ -213,7 +212,7 @@ exports.describe = (range, options = {}) ->
start = seek(iter, range.start) start = seek(iter, range.start)
end = seek(iter, range.end) + start + range.end.textContent.length end = seek(iter, range.end) + start + range.end.textContent.length
return getPageOffset(startPageIndex, cache.pageOffset).then (pageOffset) -> return getPageOffset(startPageIndex, cache.pageText).then (pageOffset) ->
# XXX: range covers only one page # XXX: range covers only one page
start += pageOffset start += pageOffset
end += pageOffset end += pageOffset
......
...@@ -8,6 +8,9 @@ class PDF extends Annotator.Plugin ...@@ -8,6 +8,9 @@ class PDF extends Annotator.Plugin
pdfViewer: null pdfViewer: null
pluginInit: -> pluginInit: ->
@annotator.anchoringCache.pageText = {}
@annotator.anchoringCache.quotePosition = {}
@pdfViewer = PDFViewerApplication.pdfViewer @pdfViewer = PDFViewerApplication.pdfViewer
@pdfViewer.viewer.classList.add('has-transparent-text-layer') @pdfViewer.viewer.classList.add('has-transparent-text-layer')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment